Skip to content

Commit 8c207fe

Browse files
committed
Chapter 5 Exercises.
1 parent 0f1d98d commit 8c207fe

13 files changed

Lines changed: 516 additions & 0 deletions

ch05/Comparison.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Exercise 5-ES-1
2+
// Exercise 1
3+
4+
public class Comparison
5+
{
6+
public static void main(String[] args)
7+
{
8+
String txt = "Fantastic";
9+
String lang = "Java";
10+
11+
boolean state = (txt == lang);
12+
System.out.println("String Equality Test: " + state);
13+
14+
state = (txt != lang);
15+
System.out.println("String Inequality Test: " + state);
16+
17+
System.out.println();
18+
19+
int dozen = 12;
20+
int score = 10;
21+
22+
state = (dozen > score);
23+
System.out.println("Greater Than Test: " + state);
24+
25+
state = (dozen < score);
26+
System.out.println("Less Than Test: " + state);
27+
}
28+
}

ch05/Condition.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Exercise 5-ES-1
2+
// Exercise 3
3+
4+
public class Condition
5+
{
6+
public static void main(String[] args)
7+
{
8+
int num1 = 1357;
9+
int num2 = 2468;
10+
String result;
11+
12+
result = (num1 % 2 != 0) ? "Odd" : "Even";
13+
System.out.println( num1 + " is " + result);
14+
15+
System.out.println();
16+
17+
result = (num2 % 2 != 0) ? "Odd" : "Even";
18+
System.out.println( num2 + " is " + result);
19+
}
20+
}

ch05/CrazyEdWholesaleCheese.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//Exercise 5-G.
2+
3+
import java.util.Scanner;
4+
5+
public class CrazyEdWholesaleCheese
6+
{
7+
public static void main(String[] args)
8+
{
9+
Scanner in = new Scanner(System.in);
10+
11+
System.out.print("What size cheese would you like to order? ");
12+
int cheeseSize = in.nextInt();
13+
switch(cheeseSize)
14+
{
15+
case 1 : case 2 : case 3 :
16+
System.out.println("You chose: " + cheeseSize + " inch diameter string cheese.");
17+
break;
18+
19+
default :
20+
System.out.println("Please choose 1, 2, or 3 inch diameter string cheese.");
21+
return;
22+
}
23+
24+
System.out.println("What length of cheese would you like to order? ");
25+
int cheeseLength = in.nextInt();
26+
27+
determineCostOfOrder(cheeseSize, cheeseLength);
28+
}
29+
30+
public static void determineCostOfOrder(int cheeseSize, int cheeseLength)
31+
{
32+
final int COST_OF_1_INCH_CHEESE = 2;
33+
final int COST_OF_2_INCH_CHEESE = 4;
34+
final int COST_OF_3_INCH_CHEESE = 6;
35+
36+
final int SHIPPING_COST_OF_1_INCH_CHEESE = 2;
37+
final int SHIPPING_COST_OF_2_INCH_CHEESE = 2;
38+
final int SHIPPING_COST_OF_3_INCH_CHEESE = 4;
39+
40+
final int FREE_SHIPPING_BREAKPOINT_FOR_1_INCH_CHEESE = 50;
41+
final int FREE_SHIPPING_BREAKPOINT_FOR_2_INCH_CHEESE = 75;
42+
final int FREE_SHIPPING_BREAKPOINT_FOR_3_INCH_CHEESE = 25;
43+
44+
final int HANDLING_CHARGE = 5;
45+
46+
int cheeseCost = 0;
47+
int shippingCost = 0;
48+
49+
boolean isValidOrder = true;
50+
51+
if ((cheeseSize == 1) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_1_INCH_CHEESE))
52+
{
53+
cheeseCost = (cheeseLength * COST_OF_1_INCH_CHEESE);
54+
shippingCost = 0;
55+
}
56+
else if (cheeseSize == 1)
57+
{
58+
cheeseCost = (cheeseLength * COST_OF_1_INCH_CHEESE);
59+
shippingCost = (cheeseLength * SHIPPING_COST_OF_1_INCH_CHEESE);
60+
}
61+
else if ((cheeseSize == 2) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_2_INCH_CHEESE))
62+
{
63+
cheeseCost = (cheeseLength * COST_OF_2_INCH_CHEESE);
64+
shippingCost = 0;
65+
}
66+
else if (cheeseSize == 2)
67+
{
68+
cheeseCost = (cheeseLength * COST_OF_2_INCH_CHEESE);
69+
shippingCost = (cheeseLength * SHIPPING_COST_OF_2_INCH_CHEESE);
70+
}
71+
else if ((cheeseSize == 3) && (cheeseLength > FREE_SHIPPING_BREAKPOINT_FOR_3_INCH_CHEESE))
72+
{
73+
cheeseCost = (cheeseLength * COST_OF_3_INCH_CHEESE);
74+
shippingCost = 0;
75+
}
76+
else if (cheeseSize == 3)
77+
{
78+
cheeseCost = (cheeseLength * COST_OF_3_INCH_CHEESE);
79+
shippingCost = (cheeseLength * SHIPPING_COST_OF_3_INCH_CHEESE);
80+
}
81+
else
82+
{
83+
isValidOrder = false;
84+
}
85+
86+
int totalCost = cheeseCost + shippingCost + HANDLING_CHARGE;
87+
88+
if (isValidOrder)
89+
{
90+
System.out.println("The total cost of this cheese is: $" + cheeseCost);
91+
System.out.println("The total shipping cost is: $" + shippingCost);
92+
System.out.println("The Handling Charge for this order is: $" + HANDLING_CHARGE);
93+
System.out.println("The total cost of this order is: $" + totalCost);
94+
System.out.println();
95+
}
96+
else
97+
{
98+
System.out.println("This order is too crazy!");
99+
}
100+
}
101+
}

ch05/Else.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Exercise 5-ES-1
2+
// Exercise 6
3+
4+
public class Else
5+
{
6+
public static void main(String[] args)
7+
{
8+
int hrs = 21;
9+
10+
if (hrs < 13)
11+
{
12+
System.out.println("Good morning: " + hrs);
13+
}
14+
else if (hrs < 18)
15+
{
16+
System.out.println("Good afternoon: " + hrs);
17+
}
18+
else
19+
{
20+
System.out.println("Good evening: " + hrs);
21+
}
22+
23+
}
24+
}

ch05/EmailOffer.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//Exercise 5-J.
2+
3+
public class EmailOffer
4+
{
5+
public static void main(String[] args)
6+
{
7+
determineEmailOffer("IA", 10);
8+
}
9+
10+
public static void determineEmailOffer(String state, int currentSpeed)
11+
{
12+
if ((state.equals("IA")) && (currentSpeed < 30))
13+
{
14+
System.out.println("Congratulations! You are eligible for a free HSI upgrade to 50M.");
15+
}
16+
else if ((state.equals("MO")) && (currentSpeed < 10))
17+
{
18+
System.out.println("Congratulations! You are eligible for a free HSI upgrade to 20M.");
19+
}
20+
else if ((state.equals("MO")) && (currentSpeed < 25))
21+
{
22+
System.out.println("Congratulations! You are eligible for a free HSI upgrade to 50M.");
23+
}
24+
else
25+
{
26+
System.out.println("We are sorry. No HSI Upgrade is available at this time.");
27+
}
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Exercise 5-I.
2+
3+
public class GreenCoffeeGrowersDiscount
4+
{
5+
public static void main(String[] args)
6+
{
7+
determineDiscount("Bicycle", 50);
8+
}
9+
10+
public static void determineDiscount(String modeOfTravel, int distanceOfCommute)
11+
{
12+
if (distanceOfCommute < 21)
13+
{
14+
System.out.println("Congratulations! You are eligible for a Free Coffee and a 30% Discount.");
15+
}
16+
else if ((modeOfTravel.equals("Bicycle")) && (distanceOfCommute > 20 && distanceOfCommute < 30))
17+
{
18+
System.out.println("Congratulations! You are eligible for a 30% Discount.");
19+
}
20+
else if ((modeOfTravel.equals("Bicycle")) && (distanceOfCommute > 29 && distanceOfCommute < 50))
21+
{
22+
System.out.println("Congratulations! You are eligible for a 20% Discount.");
23+
}
24+
else if ((modeOfTravel.equals("Bus")) && (distanceOfCommute > 34 && distanceOfCommute < 50))
25+
{
26+
System.out.println("Congratulations! You are eligible for a 50% Discount.");
27+
}
28+
else
29+
{
30+
System.out.println("We are sorry. You are not eligible for a discount at this time.");
31+
}
32+
}
33+
}

ch05/If.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Exercise 5-ES-1
2+
// Exercise 5
3+
4+
public class If
5+
{
6+
public static void main(String[] args)
7+
{
8+
if (5>1)
9+
{
10+
System.out.println("Five is greater than one.");
11+
}
12+
13+
System.out.println();
14+
15+
if (2<4)
16+
{
17+
System.out.println("Two is less than four.");
18+
System.out.println("Test succeeded.");
19+
}
20+
21+
System.out.println();
22+
23+
int num = 15;
24+
if (((num > 5) && (num < 10)) || (num == 12))
25+
{
26+
System.out.println("Number is 6-9 inclusive, or 12");
27+
}
28+
}
29+
}

ch05/Logic.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Exercise 5-ES-1
2+
// Exercise 2
3+
4+
public class Logic
5+
{
6+
public static void main(String[] args)
7+
{
8+
boolean yes = true;
9+
boolean no = false;
10+
11+
//Add these lines to test if both conditions are true.
12+
System.out.println("Both YesYes True: " + ( yes && yes));
13+
System.out.println("Both YesNo True: " + ( yes && no));
14+
15+
System.out.println();
16+
17+
//Add these lines to test if either condition is true.
18+
System.out.println("Either YesYes True: " + ( yes || yes));
19+
System.out.println("Either YesNo True: " + ( yes || no));
20+
System.out.println("Either NoNo True: " + ( no || no));
21+
22+
System.out.println();
23+
24+
//Add two more lines to show an original and inverse value.
25+
System.out.println("Original Yes Value: " + ( yes ));
26+
System.out.println("Inverse Yes Value: " + ( !yes ));
27+
}
28+
}

ch05/LogicMethods.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//Exercises 5-A through 5-D.
2+
3+
public class LogicMethods
4+
{
5+
public static void main(String[] args)
6+
{
7+
//Exercise 5-A.
8+
printIsLarge(4);
9+
//Exercise 5-B.
10+
printIsLargeOrSmall(47);
11+
//Exercise 5-C.
12+
printLargest(25, 9);
13+
//Exercise 5-D
14+
printLargestOdd(12, 12);
15+
}
16+
17+
public static void printIsLarge(int number)
18+
{
19+
if (number > 99)
20+
{
21+
System.out.println("The number is large.");
22+
}
23+
}
24+
25+
public static void printIsLargeOrSmall(int number)
26+
{
27+
if (number > 99)
28+
{
29+
System.out.println("The number is large.");
30+
}
31+
else if (number < 10)
32+
{
33+
System.out.println("The number is small.");
34+
}
35+
}
36+
37+
public static void printLargest(int number1, int number2)
38+
{
39+
if (number1 == number2)
40+
{
41+
System.out.println("The numbers are equal.");
42+
}
43+
else if (number1 > number2)
44+
{
45+
System.out.println("The largest number is: " + number1);
46+
}
47+
else
48+
{
49+
System.out.println("The largest number is: " + number2);
50+
}
51+
}
52+
53+
public static void printLargestOdd(int x, int y)
54+
{
55+
boolean xIsOdd = (x % 2 == 1);
56+
boolean xIsEven = (x % 2 == 0);
57+
boolean yIsOdd = (y % 2 == 1);
58+
boolean yIsEven = (y % 2 == 0);
59+
60+
if ((xIsEven) && (yIsEven))
61+
{
62+
System.out.println("Neither number is ODD.");
63+
}
64+
else if ((xIsEven) && (yIsOdd))
65+
{
66+
System.out.println("The largest ODD number is: " + y);
67+
}
68+
else if ((xIsOdd) && (yIsEven))
69+
{
70+
System.out.println("The largest ODD number is: " + x);
71+
}
72+
else if ((x == y) && (xIsOdd))
73+
{
74+
System.out.println("Two ODDs make an EVEN: " + (x + y));
75+
}
76+
else if (x > y)
77+
{
78+
System.out.println("The largest ODD number is: " + x);
79+
}
80+
else if (y > x)
81+
{
82+
System.out.println("The largest ODD number is: " + y);
83+
}
84+
}
85+
}
86+

0 commit comments

Comments
 (0)