SteveHome Workstation
HW List Page 1
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!
E:\THUWorks\java程式設計\num2dig.java 2007年3月28日 上午 04:17
1
// Prog.Name:number to digit2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/21/076
import
java
.
util
.
Scanner
;
// program uses Scanner7
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
.
(
"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
.
(
leader_digit
);
24
System
.
out
.
(
" "
);
25
input_value
=
input_value
- (
leader_digit
*
10000);
26
27
int
second_digit
=
input_value
/1000;
28
System
.
out
.
(
second_digit
);
29
System
.
out
.
(
" "
);
30
input_value
=
input_value
- (
second_digit
*
1000);
31
32
int
third_digit
=
input_value
/100;
33
System
.
out
.
(
third_digit
);
34
System
.
out
.
(
" "
);
35
input_value
=
input_value
- (
third_digit
*
100);
36
37
int
fouth_digit
=
input_value
/10;
38
System
.
out
.
(
fouth_digit
);
39
System
.
out
.
(
" "
);
40
input_value
=
input_value
- (
fouth_digit
*
10);
41
42
43
System
.
out
.
(
input_value
);
44
System
.
out
.
println
();
45
}
46
47
}
SteveHome Workstation
HW List Page 2
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
E:\THUWorks\java程式設計\shownum.java 2007年3月28日 上午 04:17
1
// Prog.Name:2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/21/076
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
}
SteveHome Workstation
HW List Page 3
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
E:\THUWorks\java程式設計\Invoice.java 2007年3月28日上午 04:16
1
// Prog.Name:Invoice2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/21/076
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
());
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
}
E:\THUWorks\java程式設計\InvoiceTest.java 2007年3月28日上午 04:17
1
// Prog.Name:InvoiceTest (depend on Invoice)2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/21/076
import
java
.
util
.
Scanner
;
// program uses Scanner7
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
.
(
"Enter Number:"
);
22
// part_number = input.nextLine();23
myInvoice
.
setNumber
(
input
.
nextLine
());
24
25
System
.
out
.
(
"Enter Description:"
);
26
// part_description = input.nextLine();27
myInvoice
.
setDescription
(
input
.
nextLine
());
28
29
System
.
out
.
(
"Enter Quantity:"
);
30
// part_quantity = input.nextInt();31
myInvoice
.
setQuantity
(
input
.
nextInt
());
32
33
System
.
out
.
(
"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
SteveHome Workstation
HW List Page 4
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
E:\THUWorks\java程式設計\Employee.java 2007年3月28日上午 04:18
1
// Prog.Name:Employee2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/28/076
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
}
E:\THUWorks\java程式設計\Employee.java 2007年3月28日上午 04:18
56
}
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 Employee10
System
.
out
.
println
(
"Frist Employee"
);
11
Employee myFristEmployee
=
new
Employee
();
12
System
.
out
.
(
"Enter Frist Neme:"
);
13
myFristEmployee
.
setFristName
(
input
.
nextLine
());
14
System
.
out
.
(
"Enter Last Neme:"
);
15
myFristEmployee
.
setLastName
(
input
.
nextLine
());
16
System
.
out
.
(
"Enter Monthly Salary:"
);
17
myFristEmployee
.
setMonthlySalary
(
input
.
nextDouble
());
18
19
// Second Employee20
System
.
out
.
println
(
"Second Employee"
);
21
input
.
nextLine
();
//Drop next line to fix input error.22
Employee mySecondEmployee
=
new
Employee
();
23
System
.
out
.
(
"Enter Frist Neme:"
);
24
mySecondEmployee
.
setFristName
(
input
.
nextLine
());
25
System
.
out
.
(
"Enter Last Neme:"
);
26
mySecondEmployee
.
setLastName
(
input
.
nextLine
());
27
System
.
out
.
(
"Enter Monthly Salary:"
);
28
mySecondEmployee
.
setMonthlySalary
(
input
.
nextDouble
());
29
30
// Third Employee31
System
.
out
.
println
(
"Third Employee"
);
32
input
.
nextLine
();
//Drop next line to fix input error.33
Employee myThirdEmployee
=
new
Employee
();
34
System
.
out
.
(
"Enter Frist Neme:"
);
35
myThirdEmployee
.
setFristName
(
input
.
nextLine
());
36
System
.
out
.
(
"Enter Last Neme:"
);
37
myThirdEmployee
.
setLastName
(
input
.
nextLine
());
38
System
.
out
.
(
"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 Info46
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 Info51
mySecondEmployee
.
setMonthlySalary
(
mySecondEmployee
.
getMonthlySalary
() * (
11.0/10.0));
//Add MonthlySalary to 110%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 Info56
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 Workstation
HW List Page 5
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
E:\THUWorks\java程式設計\Date.java 2007年3月28日上午 04:18
1
// Prog.Name:Date2
// StdNo.:s942864 邱逸夫3
// input: none4
// output: see code.5
// Date: 03/28/076
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 days12
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 1234
replace_day
=
1;
35
}
else
{
//2 4 6 8 9 1136
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!"
);
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
}
E:\THUWorks\java程式設計\DateTest.java 2007年3月28日上午 04:17
1
// Prog.Name:DateTest2
// StdNo.:s942864 邱逸夫3
// input: year , month, day. All input value must be Type:int.4
// output: see code.5
// Date: 03/28/076
import
java
.
util
.
Scanner
;
// program uses Scanner7
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
.
(
"Enter year:"
);
22
year
=
input
.
nextInt
();
23
System
.
out
.
(
"Enter month:"
);
24
month
=
input
.
nextInt
();
25
System
.
out
.
(
"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
}