forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogicMethods.java
More file actions
86 lines (79 loc) · 2.08 KB
/
LogicMethods.java
File metadata and controls
86 lines (79 loc) · 2.08 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Exercises 5-A through 5-D.
public class LogicMethods
{
public static void main(String[] args)
{
//Exercise 5-A.
printIsLarge(4);
//Exercise 5-B.
printIsLargeOrSmall(47);
//Exercise 5-C.
printLargest(25, 9);
//Exercise 5-D
printLargestOdd(12, 12);
}
public static void printIsLarge(int number)
{
if (number > 99)
{
System.out.println("The number is large.");
}
}
public static void printIsLargeOrSmall(int number)
{
if (number > 99)
{
System.out.println("The number is large.");
}
else if (number < 10)
{
System.out.println("The number is small.");
}
}
public static void printLargest(int number1, int number2)
{
if (number1 == number2)
{
System.out.println("The numbers are equal.");
}
else if (number1 > number2)
{
System.out.println("The largest number is: " + number1);
}
else
{
System.out.println("The largest number is: " + number2);
}
}
public static void printLargestOdd(int x, int y)
{
boolean xIsOdd = (x % 2 == 1);
boolean xIsEven = (x % 2 == 0);
boolean yIsOdd = (y % 2 == 1);
boolean yIsEven = (y % 2 == 0);
if ((xIsEven) && (yIsEven))
{
System.out.println("Neither number is ODD.");
}
else if ((xIsEven) && (yIsOdd))
{
System.out.println("The largest ODD number is: " + y);
}
else if ((xIsOdd) && (yIsEven))
{
System.out.println("The largest ODD number is: " + x);
}
else if ((x == y) && (xIsOdd))
{
System.out.println("Two ODDs make an EVEN: " + (x + y));
}
else if (x > y)
{
System.out.println("The largest ODD number is: " + x);
}
else if (y > x)
{
System.out.println("The largest ODD number is: " + y);
}
}
}