Skip to content

Commit 593d6ba

Browse files
committed
Chapter 2 Exercises.
1 parent ce03ee9 commit 593d6ba

12 files changed

Lines changed: 282 additions & 0 deletions

‎ch02/Arithmetic.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//Exercise 2-ES-1.
2+
//Exercise 4.
3+
4+
public class Arithmetic
5+
{
6+
public static void main (String[] args)
7+
{
8+
int num = 100;
9+
int factor = 20;
10+
int sum = 0;
11+
12+
sum = num + factor; // 100+20
13+
System.out.println("Addition sum: " + sum);
14+
15+
sum = num - factor; // 100-20
16+
System.out.println("Subtraction sum: " + sum);
17+
18+
sum = num * factor; // 100*20
19+
System.out.println("Multiplication sum: " + sum);
20+
21+
sum = num / factor; // 100/20
22+
System.out.println("Division sum: " + sum);
23+
}
24+
}

‎ch02/Assignment.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//Exercise 2-ES-1.
2+
//Exercise 5.
3+
4+
public class Assignment
5+
{
6+
public static void main (String[] args)
7+
{
8+
String txt = "Fantastic";
9+
String lang = "Java";
10+
txt += lang; //Assign concatenated String.
11+
System.out.println("Add & Assign Strings: " + txt);
12+
13+
int sum = 10;
14+
int num = 20;
15+
sum += num; //Assign result (10+20=30)
16+
System.out.println("Add & Assign Integers: " + sum);
17+
18+
int factor = 5;
19+
sum *= factor; //Assign result (30*5=150)
20+
System.out.println("Multiplication sum: " + sum);
21+
22+
sum /= factor; //Assign result (150/5=30)
23+
System.out.println("Division sum: " + sum);
24+
}
25+
}

‎ch02/Constants.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//Exercise 2-ES-1.
2+
//Exercise 3.
3+
4+
public class Constants
5+
{
6+
public static void main (String[] args)
7+
{
8+
//Constant score values.
9+
final int TOUCHDOWN = 6;
10+
final int CONVERSION = 1;
11+
final int FIELDGOAL = 3;
12+
13+
//Calculate points scored.
14+
int td;
15+
int pat;
16+
int fg;
17+
int total;
18+
19+
td = 4 * TOUCHDOWN;
20+
pat = 3 * CONVERSION;
21+
fg = 2 * FIELDGOAL;
22+
total = (td + pat + fg);
23+
24+
//Output calculated total.
25+
System.out.println("Score: " + total);
26+
}
27+
}

‎ch02/DataTypes.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//Exercise 2-ES-1.
2+
//Exercise 2.
3+
4+
public class DataTypes
5+
{
6+
public static void main (String[] args)
7+
{
8+
char letter = 'M';
9+
String title = "Java in Easy Steps";
10+
int number = 365;
11+
float decimal = 98.6f;
12+
boolean result = true;
13+
14+
System.out.println("Initial is " + letter);
15+
System.out.println("Book is " + title);
16+
System.out.println("Days are " + number);
17+
System.out.println("Temperature is " + decimal);
18+
System.out.println("Answer is " + result);
19+
20+
}
21+
}

‎ch02/Date.java‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Exercise 2-2.
2+
3+
public class Date
4+
{
5+
public static void main (String[] args)
6+
{
7+
String day = "Monday";
8+
int date = 16;
9+
String month = "April";
10+
int year = 2018;
11+
12+
System.out.println(day);
13+
System.out.println(date);
14+
System.out.println(month);
15+
System.out.println(year);
16+
17+
System.out.println();
18+
19+
System.out.println("American Format:");
20+
System.out.println(day + ", " + month + " " + date + ", " + year);
21+
22+
System.out.println();
23+
24+
System.out.println("European Format:");
25+
System.out.println(day + " " + date + " " + month + " " + year);
26+
27+
}
28+
}

‎ch02/DoubleByZero.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Exercise 2-C.
2+
3+
public class DoubleByZero
4+
{
5+
public static void main (String[] args)
6+
{
7+
double var1 = 42.0;
8+
double var2 = 0.0;
9+
double result = (var1 / var2);
10+
11+
System.out.println("The result is: " + result);
12+
}
13+
}
14+
//The result is: Infinity

‎ch02/FirstVariable.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//Exercise 2-ES-1.
2+
//Exercise 1.
3+
4+
public class FirstVariable
5+
{
6+
public static void main (String[] args)
7+
{
8+
String message = "Initial value.";
9+
System.out.println(message);
10+
11+
message = "Modified value.";
12+
System.out.println(message);
13+
}
14+
}

‎ch02/IntByZero.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Exercise 2-B.
2+
3+
public class IntByZero
4+
{
5+
public static void main (String[] args)
6+
{
7+
int var1 = 42;
8+
// int var2 = 0;
9+
int var2 = 2;
10+
int result = (var1 / var2);
11+
12+
System.out.println("The result is: " + result);
13+
}
14+
}
15+
// Exception in thread "main" java.lang.ArithmeticException: / by zero
16+
// at IntByZero.main(IntByZero.java:7)

‎ch02/IntExtremes.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Exercise 2-A.
2+
3+
public class IntExtremes
4+
{
5+
public static void main (String[] args)
6+
{
7+
int positiveInt = 2147483647;
8+
System.out.println(positiveInt);
9+
positiveInt = positiveInt + 1;
10+
System.out.println(positiveInt);
11+
12+
System.out.println();
13+
14+
int negativeInt = -2147483648;
15+
System.out.println(negativeInt);
16+
negativeInt = negativeInt - 1;
17+
System.out.println(negativeInt);
18+
}
19+
}

‎ch02/LoveJava.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//Exercise 2-E.
2+
3+
public class LoveJava
4+
{
5+
public static void main (String[] args)
6+
{
7+
System.out.println("I love Java!");
8+
9+
String word1 = "I";
10+
String word2 = "love";
11+
String word3 = "Java!";
12+
13+
System.out.println(word1 + " " + word2 + " " + word3);
14+
15+
}
16+
}

0 commit comments

Comments
 (0)