public static void main( String arg

advertisement
Java 進階程式設計 03/28~04/04
Name:邱逸夫
4.16
NO:942864
What does the following program print?
public class Mystery
{
public static void main( String args[] )
{
int y;
int x = 1;
int total = 0;
while ( x <= 10 )
{
y = x * x;
System.out.println( y );
total += y;
++x;
} // end while
System.out.printf( "Total is %d\n", total );
} // end main
} // end class Mystery
C:\ >jrun -a -r
Compling file "Mystery.java"...
1
4
9
16
25
36
49
64
81
100
Total is 385
SteveHome Workstation, Inc.
頁 1/5
E:\THUWorks\java程式設計\hw2\Mystery.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2007年4月11日 下午 07:55
public class Mystery
{
public static void main( String args[] )
{
int y;
int x = 1;
int total = 0;
while ( x <= 10 )
{
y = x * x;
System.out.println( y );
total += y;
++x;
} // end while
System.out.printf( "Total is %d\n", total );
}
}
// end main
// end class Mystery
SteveHome Computing Workstation, Inc.
Page 1
4.19
A large company pays its salespeople on a commission basis. The salespeople receive $200 per
week plus 9% of their gross sales for that week. For example, a salesperson who sells $5,000
worth of merchandise in a week receives $200 plus 9% of $5,000, or a total of $650. You have
been supplied with a list of the items sold by each salesperson. The values of these items are as
follows:
Item Value
1
239.99
2
129.75
3
99.95
4
350.89
Develop a Java application that inputs one salesperson's items sold for last week and calculates
and displays that salesperson's earnings. There is no limit to the number of items that can be
sold by a salesperson.
C:\ >java CompanyPays
The salespeople's basic earnings is $200.00, merchandise plus is 9.00 %.
Item
Value
1
239.99
2
129.75
3
99.95
4
350.89
Select sales item(Enter 0 to leave):4
Current merchandise is: $350.89
Item
1
2
3
4
Value
239.99
129.75
99.95
350.89
Select sales item(Enter 0 to leave):2
Current merchandise is: $480.64
Item
1
2
3
4
Value
239.99
129.75
99.95
350.89
Select sales item(Enter 0 to leave):0
Current merchandise is: $480.64
The salespeople's total basis you need to pay is : $243.26
C:\ >
SteveHome Workstation, Inc.
頁 2/5
E:\THUWorks\java程式設計\hw2\CompanyPays\CommissionBasis.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class CommissionBasis {
private double BasicEarnings;
private double MerchandisePlus;
private double CurrentMerchandise;
2007年4月11日 下午 07:54
//基本工資
//出售回饋
//目前的總金額
public void setMerchandisePlus(double inMPlus) {
MerchandisePlus = inMPlus;
}
public void setBasicEarnings (double inBEarnings) {
BasicEarnings = inBEarnings;
}
public CommissionBasis(double inBEarnings, double inMPlus) {
setBasicEarnings(inBEarnings);
setMerchandisePlus(inMPlus);
}
public void setCurrentMerchandise (double inCMerchandise) {
CurrentMerchandise = inCMerchandise;
}
public double getCurrentMerchandise () {
return CurrentMerchandise;
}
public double TotalBasis (){
double result = CurrentMerchandise * MerchandisePlus + BasicEarnings;
return result;
}
}
SteveHome Computing Workstation, Inc.
Page 1
E:\THUWorks\java程式設計\hw2\CompanyPays\CompanyPays.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2007年4月11日 下午 07:53
// Prog.Name:CompanyPays (depend on CommissionBasis)
// StdNo.:s942864
邱逸夫
// input: none
// output: see code.
// Date: 04/04/07
import java.util.Scanner;
// program uses Scanner
public class CompanyPays {
public static void main (String args[]) {
double BasicEarning = 200.0;
double MerchandisePlus = 0.09;
int section = 5;
Scanner input = new Scanner( System.in );
System.out.printf("The salespeople's basic earnings is $%.2f, merchandise plus is %.2f %%.\n",
BasicEarning, MerchandisePlus * 100.0 );
CommissionBasis saler = new CommissionBasis( BasicEarning,
MerchandisePlus );
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//實做上來說,本段程式碼必須遵循設計模式,經過良好資料庫規劃後建立物
//建材能夠滿足需求分離以及兼顧未來擴充性。但因題目僅要求特定內容實做
//故直接將選項直接寫入程式中。
do {
System.out.println("Item\tValue ");
System.out.println("1\t239.99 ");
System.out.println("2\t129.75 ");
System.out.println("3\t99.95 ");
System.out.println("4\t350.89 ");
System.out.print("\nSelect sales item(Enter 0 to leave):");
section = input.nextInt();
switch(section){
case 1:
saler.setCurrentMerchandise(saler.getCurrentMerchandise() +
239.99);
36
37
38
39
break;
case 2:
saler.setCurrentMerchandise(saler.getCurrentMerchandise() +
129.75);
40
41
42
43
break;
case 3:
saler.setCurrentMerchandise(saler.getCurrentMerchandise() +
99.95);
44
45
46
47
break;
case 4:
saler.setCurrentMerchandise(saler.getCurrentMerchandise() +
350.89);
48
49
SteveHome Computing Workstation, Inc.
break;
Page 1
E:\THUWorks\java程式設計\hw2\CompanyPays\CompanyPays.java
50
51
52
53
54
55
2007年4月11日 下午 07:53
case 0:
default:
break;
}
System.out.printf("Current merchandise is: $%.2f \n\n", saler.
getCurrentMerchandise());
//getCurrentMerchandise
56
57
58
59
60
} while (section != 0);
// Calculis total basis.
System.out.printf("The salespeople's total basis you need to pay is : $%.2f", saler.TotalBasis
());
61
62
63
}
}
SteveHome Computing Workstation, Inc.
Page 2
4.26
What does the following program print?
public class Mystery3
{
public static void main( String args[] )
{
int row = 10;
int column;
while ( row >= 1 )
{
column = 1;
while ( column <= 10 )
{
System.out.print( row % 2 == 1 ? "<" : ">" );
++column;
} // end while
--row;
System.out.println();
} // end while
} // end main
} // end class Mystery3
C:\ >jrun -a -r
Compling file "Mystery3.java"...
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
SteveHome Workstation, Inc.
頁 3/5
E:\THUWorks\java程式設計\hw2\Mystery3.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2007年4月11日 下午 07:55
public class Mystery3
{
public static void main( String args[] )
{
int row = 10;
int column;
while ( row >= 1 )
{
column = 1;
while ( column <= 10 )
{
System.out.print( row % 2 == 1 ? "<" : ">" );
++column;
} // end while
--row;
System.out.println();
} // end while
} // end main
} // end class Mystery3
SteveHome Computing Workstation, Inc.
Page 1
4.31
Write an application that inputs an integer containing only 0s and 1s (i.e., a binary integer) and
prints its decimal equivalent. [Hint: Use the remainder and division operators to pick off the
binary number's digits one at a time, from right to left. In the decimal number system, the
rightmost digit has a positional value of 1 and the next digit to the left has a positional value of
10, then 100, then 1000, and so on. The decimal number 234 can be interpreted as 4 * 1 + 3 * 10
+ 2 * 100. In the binary number system, the rightmost digit has a positional value of 1, the next
digit to the left has a positional value of 2, then 4, then 8, and so on. The decimal equivalent of
binary 1101 is 1 * 1 + 0 * 2 + 1 * 4 + 1 * 8, or 1 + 0 + 4 + 8 or, 13.]
C:\ > java bin2dec
Enter binary number:10110100
You just input number :10110100
The Decimal Number is :180
C:\ > java bin2dec
Enter binary number:11011111
You just input number :11011111
The Decimal Number is :223
SteveHome Workstation, Inc.
頁 4/5
E:\THUWorks\java程式設計\hw2\bin2dec\BinUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2007年4月11日 下午 07:53
class BinUtil{
private int BinaryNumber;
private int DecimalNumber;
public BinUtil(int BinaryNumber){
this.setBinaryNumber(BinaryNumber);
this.DecimalNumber = 0 ;
}
public int getBinaryNumber(){
return this.BinaryNumber;
}
public int getDecimalNumber(){
return this.DecimalNumber;
}
public void setDecimalNumber(int DecimalNumber){
this.DecimalNumber = DecimalNumber;
}
public void setBinaryNumber(int BinaryNumber) {
this.BinaryNumber = BinaryNumber;
}
//getBinatyNumberLength: 回傳二進位數長度
private int getBinatyNumberLength(){
int inBNumber = BinaryNumber;
int length = 0;
do {
inBNumber = inBNumber / 10;
length++;
} while(inBNumber != 0);
return length;
}
//DivideNumber: 跟據傳入引數輸出對應除數
private int LevelNumber(int expNum, int baseNum){
int RtnNum = 1;
for (int exp = 1; exp <= expNum ; exp++){
RtnNum = RtnNum * baseNum;
}
//System.out.println("LevelNumber: expNum is" + expNum + ", baseNum is " + baseNum + ", Reault is" + RtnNum);
return RtnNum;
}
public void Convert(){
int BNLength = getBinatyNumberLength();
int inBNumber = BinaryNumber;
int DevidedNumber = 0;
int myDecimalNumber = 0;
/*
//二進位字串長度
//二進位數
//已分離的二進位數
//待加總的十進位數
for (int devNum = 1; devNum <= BNLength; devNum++ ){
SteveHome Computing Workstation, Inc.
Page 1
E:\THUWorks\java程式設計\hw2\bin2dec\BinUtil.java
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2007年4月11日 下午 07:53
DevidedNumber = inBNumber % LevelNumber(BNLength-devNum,10);
inBNumber = inBNumber / LevelNumber(BNLength-devNum,10);
myDecimalNumber = myDecimalNumber + DevidedNumber * LevelNumber(devNum-1,2);
System.out.println("inBNumber is " + inBNumber + ", DevidedNumber is "+ DevidedNumber);
}
*/
for (int devNum = 1; devNum <= BNLength; devNum++ ){
DevidedNumber = inBNumber % 10;
inBNumber = inBNumber / 10;
myDecimalNumber = myDecimalNumber + DevidedNumber * LevelNumber(
devNum-1,2);
//System.out.println("inBNumber is " + inBNumber + ", DevidedNumber is "+ DevidedNumber);
}
DecimalNumber = myDecimalNumber;
}
}
SteveHome Computing Workstation, Inc.
Page 2
E:\THUWorks\java程式設計\hw2\bin2dec\bin2dec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2007年4月11日 下午 07:53
// Prog.Name:bin2dec
// StdNo.:s942864
邱逸夫
// input: none
// output: see code.
// Date: 04/04/07
import java.util.Scanner;
// program uses Scanner
public class bin2dec {
public static void main(String args[]){
int BinInput;
int DecNum;
Scanner input = new Scanner( System.in );
System.out.print("Enter binary number:");
BinInput = input.nextInt();
BinUtil BinNum = new BinUtil (BinInput);
System.out.println("You just input number :" + BinNum.getBinaryNumber());
BinNum.Convert();
System.out.println("The Decimal Number is :" + BinNum.getDecimalNumber());
}
}
SteveHome Computing Workstation, Inc.
Page 1
4.38
The factorial of a nonnegative integer n is written as n! (pronounced "n factorial") and is defined
as follows:
n! = n ∙ n − 1 ∙ n − 2 ∙ … ∙ 1 (for values of n greater than or equal to 1)
and
n! = 1(For n = 0)
For example, 5! = 5 . 4 . 3 . 2 . 1, which is 120.
a. Write an application that reads a nonnegative integer and computes and prints its
factorial.
b. Write an application that estimates the value of the mathematical constant e by using the
formula
𝑒 =1+
1 1 1
+ + +⋯
1! 2! 3!
c. Write an application that computes the value of e x by using the formula
𝑒𝑥 = 1 +
𝑥 𝑥2 𝑥3
+ + +⋯
1! 2! 3!
D:\>java FactorialTest
Enter base number:1
Enter expr. number for e^x:2
Your result for n! is :1
Your result for e is :2.0000000000
Your result for e^x is:5.0000000000
D:\>java FactorialTest
Enter base number:16
Enter expr. number for e^x:12
Your result for n! is :2004189184
Your result for e is :2.7182818306
Your result for e^x is:201160.6895100300
SteveHome Workstation, Inc.
頁 5/5
E:\THUWorks\java程式設計\hw2\Factorial\Factorial.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
2007年4月11日 下午 07:54
// Prog.Name:Factorial
// StdNo.:s942864
邱逸夫
// input: none
// output: see code.
// Date: 04/11/07
public class Factorial{
private int BaseNumber;
private int ExprNumber;
private int CopiedBaseNumber;
public Factorial(int BaseNumber){
this.ExprNumber = 1;
this.setBaseNumber(BaseNumber);
}
public void setBaseNumber(int BaseNumber){
this.BaseNumber = BaseNumber;
this.CopiedBaseNumber = BaseNumber;
}
public int getBaseNumber(){
return this.BaseNumber;
}
public int CalcFactorial(){
do {
this.ExprNumber = this.CopiedBaseNumber * this.ExprNumber;
this.CopiedBaseNumber--;
} while (CopiedBaseNumber > 0);
this.CopiedBaseNumber = BaseNumber;
return this.ExprNumber;
}
}
SteveHome Computing Workstation, Inc.
Page 1
E:\THUWorks\java程式設計\hw2\Factorial\FactorialTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
2007年4月11日 下午 07:54
// Prog.Name:FactorialTest
// StdNo.:s942864
邱逸夫
// input: none
// output: see code.
// Date: 04/11/07
import java.util.Scanner;
public class FactorialTest{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
int BaseNum = 0;
int ExprBase = 0;
double ENum = 1.0;
double ExprENum = 1.0;
System.out.print("Enter base number:");
BaseNum = input.nextInt();
System.out.print("Enter expr. number for e^x:");
ExprBase = input.nextInt();
Factorial FaNum = new Factorial(BaseNum);
System.out.println("Your result for n! is:" + FaNum.CalcFactorial());
//Write an application that estimates the value of the mathematical constant e
for(int i = 1 ; i <= BaseNum; i++){
//System.out.println(myFactorial(i));
ENum = ENum + 1/myFactorial(i);
}
System.out.println("Your result for e is:" + ENum);
for(int i = 1 ; i <= BaseNum; i++){
//System.out.println(myExprNum(ExprBase,i));
ExprENum = ExprENum + myExprNum(ExprBase,i)/myFactorial(i);
}
System.out.println("Your result for e^x is:" + ExprENum);
}
private static int myExprNum(int baseNum, int ExprNum){
int rtnNum = baseNum;
for (int i = 1; i<= ExprNum; i++){
rtnNum = rtnNum * baseNum;
}
return rtnNum;
}
private static int myFactorial(int BaseNumber) {
int ExprNumber = 1;
SteveHome Computing Workstation, Inc.
Page 1
E:\THUWorks\java程式設計\hw2\Factorial\FactorialTest.java
56
57
58
59
60
61
62
63
64
65
2007年4月11日 下午 07:54
int CopiedBaseNumber = BaseNumber;
do {
ExprNumber = CopiedBaseNumber * ExprNumber;
CopiedBaseNumber--;
} while (CopiedBaseNumber > 0);
CopiedBaseNumber = BaseNumber;
return ExprNumber;
}
}
SteveHome Computing Workstation, Inc.
Page 2
Download