Skip to content

Commit c9b1805

Browse files
committed
Adding code for chapters 1-12
1 parent f4da6da commit c9b1805

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1588
-0
lines changed

ch01/Goodbye.java

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Example program that demonstrates print vs println.
3+
*/
4+
public class Goodbye {
5+
6+
/**
7+
* Prints a greeting.
8+
*/
9+
public static void main(String[] args) {
10+
System.out.print("Goodbye, "); // note the space
11+
System.out.println("cruel world");
12+
}
13+
}

ch01/Hello.java

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public class Hello {
2+
3+
public static void main(String[] args) {
4+
// generate some simple output
5+
System.out.println("Hello, World!");
6+
}
7+
}

ch02/Variables.java

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Examples from Chapter 2.
3+
*/
4+
public class Variables {
5+
6+
public static void main(String[] args) {
7+
8+
String message;
9+
10+
int x;
11+
12+
String firstName;
13+
String lastName;
14+
int hour, minute;
15+
16+
message = "Hello!"; // give message the value "Hello!"
17+
hour = 10; // assign the value 10 to hour
18+
minute = 59; // set minute to 59
19+
20+
message = "123"; // legal
21+
// message = 123; not legal
22+
23+
String message2 = "Hello!";
24+
int hour2 = 10;
25+
int minute2 = 59;
26+
27+
int a = 5;
28+
int b = a; // a and b are now equal
29+
a = 3; // a and b are no longer equal
30+
31+
String firstLine = "Hello, again!";
32+
System.out.println(firstLine);
33+
34+
System.out.print("The value of firstLine is ");
35+
System.out.println(firstLine);
36+
37+
System.out.print("The current time is ");
38+
System.out.print(hour);
39+
System.out.print(":");
40+
System.out.print(minute);
41+
System.out.println(".");
42+
43+
System.out.print("Number of minutes since midnight: ");
44+
System.out.println(hour * 60 + minute);
45+
46+
47+
System.out.print("Fraction of the hour that has passed: ");
48+
System.out.println(minute / 60);
49+
50+
System.out.print("Percent of the hour that has passed: ");
51+
System.out.println(minute * 100 / 60);
52+
53+
double pi;
54+
pi = 3.14159;
55+
56+
double minute3 = 59.0;
57+
System.out.print("Fraction of the hour that has passed: ");
58+
System.out.println(minute3 / 60.0);
59+
60+
double y = 1.0 / 3.0; // correct
61+
62+
System.out.println(0.1 * 10);
63+
System.out.println(0.1 + 0.1 + 0.1 + 0.1 + 0.1
64+
+ 0.1 + 0.1 + 0.1 + 0.1 + 0.1);
65+
66+
double balance = 123.45; // potential rounding error
67+
int balance2 = 12345; // total number of cents
68+
69+
System.out.println(1 + 2 + "Hello");
70+
// the output is 3Hello
71+
72+
System.out.println("Hello" + 1 + 2);
73+
// the output is Hello12
74+
75+
System.out.println(17 * 3);
76+
System.out.println(hour * 60 + minute);
77+
78+
int percentage;
79+
percentage = (minute * 100) / 60;
80+
81+
hour = minute + 1; // correct
82+
// minute + 1 = hour; syntax error
83+
}
84+
}

ch03/Convert.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Converts centimeters to feet and inches.
5+
*/
6+
public class Convert {
7+
8+
public static void main(String[] args) {
9+
double cm;
10+
int feet, inches, remainder;
11+
final double centPerInch = 2.54;
12+
Scanner in = new Scanner(System.in);
13+
14+
// prompt the user and get the value
15+
System.out.print("Exactly how many cm? ");
16+
cm = in.nextDouble();
17+
18+
// convert and output the result
19+
inches = (int) (cm / centPerInch);
20+
feet = inches / 12;
21+
remainder = inches % 12;
22+
System.out.printf("%.2f cm = %d ft, %d in\n",
23+
cm, feet, remainder);
24+
}
25+
}

ch03/Echo.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Scanner;
2+
3+
public class Echo {
4+
5+
public static void main(String[] args) {
6+
String line;
7+
Scanner in = new Scanner(System.in);
8+
9+
System.out.print("Type something: ");
10+
line = in.nextLine();
11+
System.out.println("You said: " + line);
12+
13+
System.out.print("Type something else: ");
14+
line = in.nextLine();
15+
System.out.println("You also said: " + line);
16+
}
17+
}

ch03/GuessStarter.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Random;
2+
3+
/**
4+
* Starter code for the "guess my number" exercise.
5+
*/
6+
public class GuessStarter {
7+
8+
public static void main(String[] args) {
9+
// pick a random number
10+
Random random = new Random();
11+
int number = random.nextInt(100) + 1;
12+
System.out.println(number);
13+
}
14+
}

ch03/Input.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Examples from Chapter 3.
3+
*/
4+
public class Input {
5+
6+
public static void main(String[] args) {
7+
System.out.println(System.out);
8+
9+
System.out.print(4.0 / 3.0);
10+
System.out.printf("Four thirds = %.3f", 4.0 / 3.0);
11+
12+
double pi = 3.14159;
13+
double x = (int) pi * 20.0;
14+
}
15+
}

ch03/ScannerBug.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Scanner;
2+
3+
/**
4+
* Demonstrates a common problem using Scanner.
5+
*/
6+
public class ScannerBug {
7+
8+
public static void main(String[] args) {
9+
String name;
10+
int age;
11+
Scanner in = new Scanner(System.in);
12+
13+
System.out.print("What is your name? ");
14+
name = in.nextLine();
15+
System.out.print("What is your age? ");
16+
age = in.nextInt();
17+
System.out.printf("Hello %s, age %d\n", name, age);
18+
19+
System.out.print("What is your age? ");
20+
age = in.nextInt();
21+
System.out.print("What is your name? ");
22+
name = in.nextLine();
23+
System.out.printf("Hello %s, age %d\n", name, age);
24+
}
25+
}

ch04/Methods.java

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Examples from Chapter 4.
3+
*/
4+
public class Methods {
5+
6+
public static void main(String[] args) {
7+
double root = Math.sqrt(17.0);
8+
double angle = 1.5;
9+
double height = Math.sin(angle);
10+
11+
double degrees = 90;
12+
double angle2 = degrees / 180.0 * Math.PI;
13+
double radians = Math.toRadians(180.0);
14+
double degrees2 = Math.toDegrees(Math.PI);
15+
long x = Math.round(Math.PI * 20.0);
16+
double x2 = Math.cos(angle + Math.PI / 2);
17+
double x3 = Math.exp(Math.log(10.0));
18+
double x4 = Math.pow(2.0, 10.0);
19+
}
20+
}

ch04/NewLine.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class NewLine {
2+
3+
public static void newLine() {
4+
System.out.println();
5+
}
6+
7+
public static void threeLine() {
8+
newLine();
9+
newLine();
10+
newLine();
11+
}
12+
13+
public static void main(String[] args) {
14+
System.out.println("First line.");
15+
threeLine();
16+
System.out.println("Second line.");
17+
}
18+
}

ch04/PrintTime.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class PrintTime {
2+
3+
public static void printTime(int hour, int minute) {
4+
System.out.print(hour);
5+
System.out.print(":");
6+
System.out.println(minute);
7+
}
8+
9+
public static void main(String[] args) {
10+
int hour = 11;
11+
int minute = 59;
12+
printTime(hour, minute);
13+
}
14+
}

ch04/PrintTwice.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class PrintTwice {
2+
3+
public static void printTwice(String s) {
4+
System.out.println(s);
5+
System.out.println(s);
6+
}
7+
8+
public static void main(String[] args) {
9+
printTwice("Don't make me say this twice!");
10+
}
11+
}

0 commit comments

Comments
 (0)