Java 進 階 程 式 設 計 03/14~03/21 Name: 邱 逸 夫 NO:942864 2.30 Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print 4 2 3 3 9 Assume that the user enters the correct number of digits. What happens when you execute the program and type a number with more than five digits? What happens when you execute the program and type a number with fewer than five digits? [Hint: It is possible to do this exercise with the techniques you learned in this chapter. You will need to use both division and remainder operations to "pick off" each digit.] c:\>java num2dig Enter an number:55663 5 5 6 6 3 c:\>java num2dig Enter an number:12 Number must in 5 digit! c:\>java num2dig Enter an number:989975 Number must in 5 digit! SteveHome Workstation HW List Page 1
E:\THUWorks\java 程 式 設 計 \num2dig.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:number to digit 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 import java.util.scanner; // program uses Scanner 7 8 public class num2dig { 9 public static void main (String args[]) { 10 11 int input_value; 12 Scanner input = new Scanner( System.in ); 13 14 System.out.print("Enter an number:"); 15 input_value = input.nextint(); 16 17 if ((input_value > 99999) (input_value < 9999)) { 18 System.out.println("Number must in 5 digit!"); 19 } else { 20 //Do something... 21 22 int leader_digit = input_value/10000; 23 System.out.print(leader_digit); 24 System.out.print(" "); 25 input_value = input_value - (leader_digit * 10000); 26 27 int second_digit = input_value/1000; 28 System.out.print(second_digit); 29 System.out.print(" "); 30 input_value = input_value - (second_digit * 1000); 31 32 int third_digit = input_value/100; 33 System.out.print(third_digit); 34 System.out.print(" "); 35 input_value = input_value - (third_digit * 100); 36 37 int fouth_digit = input_value/10; 38 System.out.print(fouth_digit); 39 System.out.print(" "); 40 input_value = input_value - (fouth_digit * 10); 41 42 43 System.out.print(input_value); 44 System.out.println(); 45 } 46 47 } 48 } SteveHome Computing Workstation, Inc. Page 1
2.31 Using only the programming techniques you learned in this chapter, write an application that calculates the squares and cubes of the numbers from 0 to 10 and prints the resulting values in table format, as shown below. [Note: This program does not require any input from the user.] c:\>java shownum number square cube 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 SteveHome Workstation HW List Page 2
E:\THUWorks\java 程 式 設 計 \shownum.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name: 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 public class shownum { 7 8 public static void main (String args[]) { 9 10 System.out.println("number\tsquare\tcube"); 11 12 for (int i = 1; i <=10 ; i++) { 13 14 System.out.printf("%d\t%d\t%d\n", i, i*i, i*i*i); 15 16 } 17 18 } 19 } SteveHome Computing Workstation, Inc. Page 1
3.13 Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getinvoiceamount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test application named InvoiceTest that demonstrates class Invoice's capabilities. c:\>java InvoiceTest Enter Number:5562 Enter Description:Some voice about you. Enter Quantity:6 Enter Price:39.9 The current item info: Number:5562 Description:Some voice about you. Quantity:6 Price:39.90 The current invoice amount: 239.39999999999998 SteveHome Workstation HW List Page 3
E:\THUWorks\java 程 式 設 計 \Invoice.java 2007 年 3 月 28 日 上 午 04:16 1 // Prog.Name:Invoice 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 public class Invoice { 7 8 private String part_number; 9 private String part_description; 10 private int part_quantity; 11 private double part_price; 12 13 public void setnumber (String number) { 14 part_number = number; 15 } 16 public void setdescription (String description) { 17 part_description = description; 18 } 19 public void setquantity (int quantity) { 20 if (quantity < 0) { 21 part_quantity = 0; 22 } else { 23 part_quantity = quantity; 24 } 25 } 26 public void setprice (double price) { 27 part_price = price; 28 } 29 30 public Invoice() { 31 } 32 33 public Invoice(String number, String description, int quantity, double price) { 34 setnumber(number); 35 setdescription(description); 36 setquantity(quantity); 37 setprice(price); 38 } 39 40 public String getnumber () { 41 return part_number; 42 } 43 public String getdescription () { 44 return part_description; 45 } 46 public int getquantity () { 47 return part_quantity; 48 } 49 public double getprice () { 50 return part_price; 51 } 52 public void showinvoice() { 53 System.out.printf("Number:%s\n", getnumber()); 54 System.out.printf("Description:%s\n", getdescription()); 55 System.out.printf("Quantity:%d\n", getquantity()); SteveHome Computing Workstation, Inc. Page 1
E:\THUWorks\java 程 式 設 計 \Invoice.java 2007 年 3 月 28 日 上 午 04:16 56 System.out.printf("Price:%.2f\n", getprice()); 57 } 58 public double getinvoiceamount() { 59 return part_quantity * part_price; 60 } 61 62 } SteveHome Computing Workstation, Inc. Page 2
E:\THUWorks\java 程 式 設 計 \InvoiceTest.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:InvoiceTest (depend on Invoice) 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/21/07 6 import java.util.scanner; // program uses Scanner 7 8 public class InvoiceTest { 9 10 public static void main (String args[]) { 11 12 Scanner input = new Scanner( System.in ); 13 14 String part_number; 15 String part_description; 16 int part_quantity; 17 double part_price; 18 19 Invoice myinvoice = new Invoice(); 20 21 System.out.print("Enter Number:"); 22 // part_number = input.nextline(); 23 myinvoice.setnumber(input.nextline()); 24 25 System.out.print("Enter Description:"); 26 // part_description = input.nextline(); 27 myinvoice.setdescription(input.nextline()); 28 29 System.out.print("Enter Quantity:"); 30 // part_quantity = input.nextint(); 31 myinvoice.setquantity(input.nextint()); 32 33 System.out.print("Enter Price:"); 34 // part_price = input.nextint(); 35 myinvoice.setprice(input.nextdouble()); 36 37 System.out.println("The current item info:"); 38 myinvoice.showinvoice(); 39 40 System.out.println("The current invoice amount:"); 41 System.out.println(myInvoice.getInvoiceAmount()); 42 43 } 44 45 } SteveHome Computing Workstation, Inc. Page 1
3.14 Create a class called Employee that includes three pieces of information as instance variables a first name (type String), a last name (type String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again. E:\>java EmployeeTest Frist Employee Enter Frist Neme:Steve Enter Last Neme:Bob Enter Monthly Salary:0 Second Employee Enter Frist Neme:Steve Enter Last Neme:Joe Enter Monthly Salary:10 Third Employee Enter Frist Neme:Steve Enter Last Neme:Nick Enter Monthly Salary:19.9 This year is end. Our boss decided to give each employee's monthly salary +10%. Employee:Steve Bob Monthly Salary:0.0 The employee:steve Bob 's monthly salary NOW is 0.0 Employee:Steve Joe Monthly Salary:10.0 The employee:steve Joe 's monthly salary NOW is 11.0 Employee:Steve Nick Monthly Salary:19.9 The employee:steve Nick 's monthly salary NOW is 21.9 SteveHome Workstation HW List Page 4
E:\THUWorks\java 程 式 設 計 \Employee.java 2007 年 3 月 28 日 上 午 04:18 1 // Prog.Name:Employee 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/28/07 6 public class Employee { 7 8 private String first_name; 9 private String last_name; 10 private double monthly_salary; 11 12 public Employee () { 13 } 14 15 public Employee (String fname, String lname, double msalary) { 16 first_name = fname; 17 last_name = lname; 18 if (msalary < 0) { 19 monthly_salary = 0; 20 } else { 21 monthly_salary = msalary; 22 } 23 } 24 25 public void setfristname (String fname) { 26 first_name = fname; 27 } 28 29 public void setlastname (String lname) { 30 last_name = lname; 31 } 32 33 public void setmonthlysalary (double msalary) { 34 if (msalary < 0) { 35 monthly_salary = 0; 36 } else { 37 monthly_salary = msalary; 38 } 39 } 40 41 public String getfristname () { 42 return first_name; 43 } 44 public String getlastname () { 45 return last_name; 46 } 47 public double getmonthlysalary (){ 48 return monthly_salary; 49 } 50 51 public void showemployeeinfo() { 52 System.out.printf("Employee:%s %s\n", getfristname(), getlastname()); 53 System.out.printf("Monthly Salary:%.1f\n", getmonthlysalary()); 54 } 55 SteveHome Computing Workstation, Inc. Page 1
E:\THUWorks\java 程 式 設 計 \Employee.java 2007 年 3 月 28 日 上 午 04:18 56 } SteveHome Computing Workstation, Inc. Page 2
E:\THUWorks\java 程 式 設 計 \EmployeeTest.java 2007 年 3 月 28 日 上 午 04:18 1 import java.util.scanner; 2 3 public class EmployeeTest { 4 5 public static void main ( String args[] ){ 6 7 Scanner input = new Scanner( System.in ); 8 9 // Frist Employee 10 System.out.println("Frist Employee"); 11 Employee myfristemployee = new Employee(); 12 System.out.print("Enter Frist Neme:"); 13 myfristemployee.setfristname(input.nextline()); 14 System.out.print("Enter Last Neme:"); 15 myfristemployee.setlastname(input.nextline()); 16 System.out.print("Enter Monthly Salary:"); 17 myfristemployee.setmonthlysalary(input.nextdouble()); 18 19 // Second Employee 20 System.out.println("Second Employee"); 21 input.nextline(); //Drop next line to fix input error. 22 Employee mysecondemployee = new Employee(); 23 System.out.print("Enter Frist Neme:"); 24 mysecondemployee.setfristname(input.nextline()); 25 System.out.print("Enter Last Neme:"); 26 mysecondemployee.setlastname(input.nextline()); 27 System.out.print("Enter Monthly Salary:"); 28 mysecondemployee.setmonthlysalary(input.nextdouble()); 29 30 // Third Employee 31 System.out.println("Third Employee"); 32 input.nextline(); //Drop next line to fix input error. 33 Employee mythirdemployee = new Employee(); 34 System.out.print("Enter Frist Neme:"); 35 mythirdemployee.setfristname(input.nextline()); 36 System.out.print("Enter Last Neme:"); 37 mythirdemployee.setlastname(input.nextline()); 38 System.out.print("Enter Monthly Salary:"); 39 mythirdemployee.setmonthlysalary(input.nextdouble()); 40 41 System.out.println(); 42 System.out.println("This year is end. Our boss decided to give each employee's monthly salary +10%.\n"); 43 44 System.out.println(); 45 myfristemployee.showemployeeinfo(); //Show current Employee Info 46 myfristemployee.setmonthlysalary(myfristemployee.getmonthlysalary() * (11.0 /10.0)); //Add MonthlySalary to 110% 47 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", myfristemployee. getfristname(), myfristemployee.getlastname(), myfristemployee.getmonthlysalary()); 48 49 System.out.println(); 50 mysecondemployee.showemployeeinfo(); //Show current Employee Info 51 mysecondemployee.setmonthlysalary(mysecondemployee.getmonthlysalary() * ( 11.0/10.0)); //Add MonthlySalary to 110% 52 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", mysecondemployee. SteveHome Computing Workstation, Inc. Page 1
E:\THUWorks\java 程 式 設 計 \EmployeeTest.java 2007 年 3 月 28 日 上 午 04:18 getfristname(), mysecondemployee.getlastname(), mysecondemployee.getmonthlysalary ()); 53 54 System.out.println(); 55 mythirdemployee.showemployeeinfo(); //Show current Employee Info 56 mythirdemployee.setmonthlysalary(mythirdemployee.getmonthlysalary() * (11.0 /10.0)); //Add MonthlySalary to 110% 57 System.out.printf("The employee:%s %s 's monthly salary NOW is %.1f\n", mythirdemployee. getfristname(), mythirdemployee.getlastname(), mythirdemployee.getmonthlysalary()); 58 59 } 60 } SteveHome Computing Workstation, Inc. Page 2
3.15 Create a class called Date that includes three pieces of information as instance variables a month (type int), a day (type int) and a year (type int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displaydate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates class Date's capabilities. E:\>java DateTest DateTest for Object Date. Enter year:1 Enter month:1 Enter day:1 Incorrect year, the data will rest to 1990! You just enter the date:1/1/1990 E:\>java DateTest DateTest for Object Date. Enter year:2000 Enter month:2 Enter day:29 You just enter the date:2/29/2000 E:\>java DateTest DateTest for Object Date. Enter year:2001 Enter month:2 Enter day:29 Incorrect day(february check), the data will rest to 1! You just enter the date:2/1/2001 E:\>java DateTest DateTest for Object Date. Enter year:2006 Enter month:7 Enter day:31 You just enter the date:7/31/2006 E:\>java DateTest DateTest for Object Date. Enter year:2006 Enter month:7 Enter day:31 SteveHome Workstation HW List Page 5 You just enter the date:7/31/2006
E:\THUWorks\java 程 式 設 計 \Date.java 2007 年 3 月 28 日 上 午 04:18 1 // Prog.Name:Date 2 // StdNo.:s942864 邱 逸 夫 3 // input: none 4 // output: see code. 5 // Date: 03/28/07 6 public class Date { 7 private int month = 1; 8 private int day = 1; 9 private int year = 1990; //The Java's pre(oak) birthday. 10 private int replace_date = 0; // 0 = 28 days, 1 = 29 days for February use only. 11 private int replace_day = 1; // 0 = 30 days, 1 = 31 days 12 13 public void setyear(int the_year) { 14 if ((1989<the_year) && (65535>the_year)) { 15 year = the_year; 16 } else { 17 System.out.println("Incorrect year, the data will rest to 1990!"); 18 } 19 // 不 能 被 4 整 除 的 不 是 閏 年, 能 被 100 整 除 而 不 能 被 400 整 除 的 年 份 不 是 閏 年, 能 被 3200 整 除 的 也 不 是 閏 年 20 if ((the_year%4!= 0) ((the_year%100 == 0) && (the_year%400!= 0)) ( the_year%3200 == 0)) { 21 replace_date = 0; 22 } else { 23 replace_date = 1; 24 } 25 } 26 27 public void setmonth(int the_month) { 28 if ((0<the_month) && (13>the_month)) { 29 month = the_month; 30 } else { 31 System.out.println("Incorrect month, the data will rest to 1!"); 32 } 33 if (((the_month%2 == 1) && (the_month <= 7)) ((the_month%2 == 0) && ( the_month >= 8))) { //1 3 5 7 8 10 12 34 replace_day = 1; 35 } else { //2 4 6 8 9 11 36 replace_day = 0; 37 } 38 } 39 40 public void setday(int the_day) { 41 if (replace_day == 1) { 42 if ((0<the_day) && (32>the_day) ) { 43 day = the_day; 44 } else { 45 System.out.println("Incorrect day(even check), the data will rest to 1!"); 46 } 47 } else if (getmonth() == 2) { //28 天, 是 否 閏 年? 48 if ((0<the_day) && (30>the_day) && (replace_date == 1)) { 49 day = the_day; 50 } else if ((0<the_day) && (29>the_day) && (replace_date == 0)) { 51 day = the_day; 52 } else { 53 System.out.println("Incorrect day(february check), the data will rest to 1!"); SteveHome Computing Workstation, Inc. Page 1
E:\THUWorks\java 程 式 設 計 \Date.java 2007 年 3 月 28 日 上 午 04:18 54 } 55 } else { 56 if ((0<the_day) && (31>the_day)) { 57 day = the_day; 58 } else { 59 System.out.println("Incorrect day(odd check), the data will rest to 1!"); 60 } 61 } 62 } 63 64 public int getmonth(){ 65 return month; 66 } 67 public int getday(){ 68 return day; 69 } 70 public int getyear(){ 71 return year; 72 } 73 74 public Date (int the_month, int the_day, int the_year){ 75 setyear(the_year); 76 setmonth(the_month); 77 setday(the_day); 78 } 79 80 public String displaydate () { 81 //return year.tostring() + "/" + month.tostring()+ "/" + day.tostring(); 82 return month + "/" + day + "/" + year; 83 } 84 } SteveHome Computing Workstation, Inc. Page 2
E:\THUWorks\java 程 式 設 計 \DateTest.java 2007 年 3 月 28 日 上 午 04:17 1 // Prog.Name:DateTest 2 // StdNo.:s942864 邱 逸 夫 3 // input: year, month, day. All input value must be Type:int. 4 // output: see code. 5 // Date: 03/28/07 6 import java.util.scanner; // program uses Scanner 7 8 public class DateTest { 9 10 public static void main(string args[]) { 11 12 int month; 13 int day; 14 int year; 15 byte trymore = 'y'; 16 17 System.out.println("DateTest for Object Date."); 18 19 Scanner input = new Scanner( System.in ); 20 21 System.out.print("Enter year:"); 22 year = input.nextint(); 23 System.out.print("Enter month:"); 24 month = input.nextint(); 25 System.out.print("Enter day:"); 26 day = input.nextint(); 27 28 Date mydate = new Date(month, day, year); 29 30 System.out.println("You just enter the date:" + mydate.displaydate()); 31 32 33 34 } 35 36 } SteveHome Computing Workstation, Inc. Page 1