Skip to content

Commit 0f1d98d

Browse files
committed
Exercises for Chapter 4.
1 parent f210b48 commit 0f1d98d

File tree

7 files changed

+249
-0
lines changed

7 files changed

+249
-0
lines changed

ch04/BegMethodSignature.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Exercise 4-G.
2+
3+
public class BegMethodSignature
4+
{
5+
public static void main(String[] args)
6+
{
7+
printSum(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
8+
printSum(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
9+
}
10+
11+
private static void printSum(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
12+
{
13+
int sum = a + b + c + d + e + f + g + h + i + j;
14+
System.out.println("The values of a through j are: " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + ", " + g + ", " + h + ", " + i + ", " + j);
15+
System.out.println("This sum of a through j is: " + sum);
16+
System.out.println();
17+
}
18+
}

ch04/DemoMath.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Exercise 4-E.
2+
3+
public class DemoMath
4+
{
5+
public static void main(String[] args)
6+
{
7+
int a = -4;
8+
int b = 5;
9+
10+
System.out.println("The value of a is: " + a);
11+
System.out.println("The absolute value of a is: " + Math.abs(a));
12+
System.out.println();
13+
14+
System.out.println("The value of a is: " + a);
15+
System.out.println("The value of b is: " + b);
16+
System.out.println("The max value of a and b is: " + Math.max(a, b));
17+
System.out.println();
18+
19+
double c = 6.0;
20+
System.out.println("The value of c is: " + c);
21+
System.out.println("The value of c to the power of c is: " + Math.pow(c, c));
22+
System.out.println();
23+
24+
System.out.println("The valued of PI is: " + Math.PI);
25+
}
26+
}

ch04/Employee.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//Exercise 4-H.
2+
3+
import java.util.Scanner;
4+
import java.util.Random;
5+
6+
public class Employee
7+
{
8+
public static void main(String[] args)
9+
{
10+
int birthYear = 1995;
11+
boolean isUnionMember = false;
12+
String firstName = "Robin";
13+
String middleName = "Ann";
14+
String lastName = "Daniel";
15+
int employeeNumber;
16+
17+
printHeader();
18+
19+
Scanner in = new Scanner(System.in);
20+
System.out.println("Please enter your 5 digit employee number: ");
21+
employeeNumber = in.nextInt();
22+
23+
printFullName(lastName, firstName, middleName);
24+
printUnionStatus(isUnionMember);
25+
printAge(birthYear);
26+
printEvenOrOdd(employeeNumber);
27+
printGenerateSecretPassword(employeeNumber);
28+
}
29+
30+
private static void printHeader()
31+
{
32+
String header = "Welcome to the WallabyTech Employee Application\n";
33+
header += "-----------------------------------------------\n";
34+
System.out.print(header);
35+
}
36+
37+
private static void printFullName(String ln, String fn, String mn)
38+
{
39+
System.out.println(ln + ", " + fn + " " + mn);
40+
}
41+
42+
private static void printUnionStatus(boolean s)
43+
{
44+
System.out.println("Your union status is: " + s);
45+
}
46+
47+
private static void printAge(int a)
48+
{
49+
final int CURRENT_YEAR = 2018;
50+
int age = CURRENT_YEAR - a;
51+
System.out.println("Your age is: " + age);
52+
}
53+
54+
private static void printEvenOrOdd(int n)
55+
{
56+
int typeOfNumber = (n % 2);
57+
System.out.println("Employee Number is even (0) or odd (1): " + typeOfNumber);
58+
}
59+
60+
private static void printGenerateSecretPassword(int n)
61+
{
62+
Random random = new Random();
63+
int number = random.nextInt(10) + 1;
64+
int password = ((n + number) * 5);
65+
// System.out.println(number); //Did this to check the computation.
66+
System.out.println("Employee's random secret ps is: " + password);
67+
}
68+
}

ch04/Exercise4_1.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//Exercise 4-1.
2+
3+
public class Exercise4_1
4+
{
5+
public static void zoop()
6+
{
7+
baffle();
8+
System.out.print("You wugga ");
9+
baffle();
10+
}
11+
12+
public static void main(String[] args)
13+
{
14+
System.out.print("No, I ");
15+
zoop();
16+
System.out.print("I ");
17+
baffle();
18+
}
19+
20+
public static void baffle()
21+
{
22+
System.out.print("wug");
23+
ping();
24+
}
25+
26+
public static void ping()
27+
{
28+
System.out.println(".");
29+
// baffle(); <= Causes an endless loop.
30+
}
31+
}
32+
//No, I wug. (new line)
33+
//You wugga wug. (new line)
34+
//I wug.
35+
36+
//invoke baffle() at the end of ping()
37+
//No, I wug.
38+
//wug. Endless loop of "wug."

ch04/MathUtil.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Exercise 4-F.
2+
3+
public class MathUtil
4+
{
5+
public static void main(String[] args)
6+
{
7+
printDifference(-4, 5);
8+
printAbsValue(-45);
9+
printDifference(1000, 4000000);
10+
}
11+
12+
private static void printDifference(int a, int b)
13+
{
14+
int difference = a - b;
15+
System.out.println("The values of a and b are: " + a + " and " + b);
16+
System.out.println("This difference between a and b is: " + difference);
17+
System.out.println();
18+
printAbsValue(difference);
19+
}
20+
21+
private static void printAbsValue(int c)
22+
{
23+
int absoluteValue = Math.abs(c);
24+
System.out.println("The value of c is: " + c);
25+
System.out.println("The absolute value of c is: " + absoluteValue);
26+
System.out.println();
27+
}
28+
}

ch04/PrintDates.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//Exercise 4-3.
2+
3+
public class PrintDates
4+
{
5+
public static void main(String[] args)
6+
{
7+
String day = "Wednesday";
8+
int date = 18;
9+
String month = "April";
10+
int year = 2018;
11+
12+
printAmerican(day, date, month, year);
13+
System.out.println();
14+
printEuropean(day, date, month, year);
15+
}
16+
17+
public static void printAmerican(String day, int date, String month, int year)
18+
{
19+
System.out.println("American format:");
20+
System.out.print(day);
21+
System.out.print(", ");
22+
System.out.print(month);
23+
System.out.print(" ");
24+
System.out.print(date);
25+
System.out.print(", ");
26+
System.out.println(year);
27+
}
28+
29+
public static void printEuropean(String day, int date, String month, int year)
30+
{
31+
System.out.println("European format:");
32+
System.out.print(day);
33+
System.out.print(" ");
34+
System.out.print(date);
35+
System.out.print(" ");
36+
System.out.print(month);
37+
System.out.print(" ");
38+
System.out.println(year);
39+
}
40+
}

ch04/SimpleMethods.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Exercise 4-A.
2+
3+
public class SimpleMethods
4+
{
5+
public static void main(String[] args)
6+
{
7+
printCount(5);
8+
printSum(4, 6);
9+
printSum(7, 2);
10+
printBoolean(true);
11+
printBoolean(false);
12+
}
13+
14+
private static void printCount(int count)
15+
{
16+
System.out.println("This count is: " + count);
17+
}
18+
19+
private static void printSum(int x, int y)
20+
{
21+
int z = x + y;
22+
System.out.println(x + " + " + y + " = " + z);
23+
}
24+
25+
private static void printBoolean(boolean isRich)
26+
{
27+
System.out.println("I am rich: " + isRich);
28+
}
29+
}
30+
31+

0 commit comments

Comments
 (0)