Skip to content

Commit fcc5d71

Browse files
authored
Add files via upload
0 parents  commit fcc5d71

File tree

85 files changed

+3761
-0
lines changed

Some content is hidden

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

85 files changed

+3761
-0
lines changed

Chapter02/ArrayDemo.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class ArrayDemo
2+
{
3+
public static void main(String[] args)
4+
{ // declare an int array with 5 elements
5+
int testScores[] = new int [5];
6+
7+
// populate the elements
8+
testScores[0] = 75;
9+
testScores[1] = 80;
10+
testScores[2] = 70;
11+
testScores[3] = 85;
12+
testScores[4] = 90;
13+
14+
// display the element contents
15+
System.out.println("test score 1 = " + testScores[0]);
16+
System.out.println("test score 2 = " + testScores[1]);
17+
System.out.println("test score 3 = " + testScores[2]);
18+
System.out.println("test score 4 = " + testScores[3]);
19+
System.out.println("test score 5 = " + testScores[4]);
20+
21+
// compute average using length attribute
22+
double average = 0;
23+
for(int i = 0; i < testScores.length; i++)
24+
{average += testScores[i];}
25+
average = average / 5;
26+
System.out.println("average score is " + average);
27+
}
28+
}
29+
30+
31+
32+
33+

Chapter02/ComputationDemo.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
public class ComputationDemo
2+
{
3+
public static void main(String[] args)
4+
{
5+
// illustrate arithmetic operators
6+
int a = 11;
7+
int b = 2;
8+
int c = 4;
9+
System.out.println("a = " + a);
10+
System.out.println("b = " + b);
11+
System.out.println("c = " + c);
12+
System.out.println("a + b = " + (a + b));
13+
System.out.println("a - b = " + (a - b));
14+
System.out.println("a * b = " + (a * b));
15+
System.out.println("integer division a / b = " + (a / b));
16+
// cast a & b to double before divide
17+
System.out.println("double division a / b = " + (double) a / (double) b);
18+
System.out.println("a % b = " + (a % b));
19+
20+
// illustrate Math class methods
21+
System.out.println("c to b power = " + Math.pow(c,b));
22+
System.out.println("square root of c = " + Math.sqrt(c));
23+
}
24+
}
25+

Chapter02/HelloWorldWideWeb.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class HelloWorldWideWeb
2+
{
3+
// this class has one method named main
4+
public static void main(String[] args)
5+
{
6+
System.out.println("Hello World Wide Web");
7+
}
8+
}

Chapter02/IfDemo.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
public class IfDemo
2+
{
3+
public static void main(String[] args)
4+
{
5+
int i = 1;
6+
// simple if
7+
if (i < 10)
8+
{
9+
System.out.println("i < 10");
10+
}
11+
if (i > 0)
12+
System.out.println("i > 0");
13+
14+
// if using a block of code
15+
if (i == 1)
16+
{
17+
System.out.println("i equals 1");
18+
System.out.println("Use a code block to");
19+
System.out.println("execute more than 1 statement");
20+
}
21+
22+
// if - else
23+
if (i == 2)
24+
System.out.println("i equals 2");
25+
else
26+
{ // a block contains multiple statements
27+
System.out.println("i does not equal 2");
28+
System.out.println("We can execute more");
29+
System.out.println("than one statement in");
30+
System.out.println("an if");
31+
}
32+
33+
// compound logical expressions
34+
if (i == 1 || i == 2)
35+
System.out.println("i equals 1 or 2");
36+
if (i > 0 && i < 3 ) //
37+
System.out.println("i is > 0 and < 3");
38+
39+
// nested if can replace compound logical expressions
40+
if (i > 0)
41+
if (i < 3)
42+
System.out.println("i is > 0 and < 3");
43+
44+
// conditional operator
45+
int j = (i == 1)? 5: 6;
46+
System.out.println("i equals " + i);
47+
}
48+
}
49+

Chapter02/LoopDemo.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
public class LoopDemo
2+
{
3+
public static void main(String[] args)
4+
{
5+
// while loop
6+
int i = 1; // declare & initialize loop counter variable
7+
while(i <= 3) // loop while i is < = 3
8+
{
9+
System.out.println("while loop: i = " + i);
10+
i = i + 1; // increment loop counter
11+
}
12+
// do loop
13+
int j = 1;
14+
do
15+
{
16+
System.out.println("do loop: j = " + j);
17+
j++; // use the increment operator
18+
} while(j <= 3); // don't forget the semicolon here
19+
20+
// pre-test & post-test compared
21+
j = 1;
22+
while(j > 3) // expression is initially false
23+
{
24+
System.out.println("while loop executed");
25+
}
26+
do
27+
{
28+
System.out.println("do loop executed");
29+
}while(j > 3); // expression is initially false
30+
31+
32+
// for loop
33+
for(int k = 1; k <= 3; k++)
34+
{
35+
System.out.println("for loop: k = " + k);
36+
}
37+
38+
// nested loop
39+
for(int m = 1; m <= 2 ; m++) // outer loop begins
40+
{
41+
for(int n = 1; n <= 3 ; n++) // inner loop begins
42+
{System.out.println("nested loop: m = " + m + ", n = " + n);}
43+
}
44+
}
45+
}
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+

Chapter02/TwoDimArrayDemo.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class TwoDimArrayDemo
2+
{
3+
public static void main(String[] args)
4+
{
5+
// declare an int array with 5 rows & 2 columns
6+
int testScoreTable[][] = new int [5][2];
7+
8+
// populate the elements in column 1
9+
testScoreTable[0][0] = 75;
10+
testScoreTable[1][0] = 80;
11+
testScoreTable[2][0] = 70;
12+
testScoreTable[3][0] = 85;
13+
testScoreTable[4][0] = 90;
14+
// populate the elements in column 2
15+
testScoreTable[0][1] = 80;
16+
testScoreTable[1][1] = 90;
17+
testScoreTable[2][1] = 60;
18+
testScoreTable[3][1] = 95;
19+
testScoreTable[4][1] = 100;
20+
21+
// compute the average test score using nested for loop
22+
double average;
23+
// outer loop goes across the columns
24+
for(int col = 0; col < 2; col++)
25+
{
26+
average = 0;
27+
// inner loop goes down the rows
28+
for(int row = 0; row < 5; row++)
29+
{average += testScoreTable[row][col];}
30+
// compute the column average
31+
average = average / 5;
32+
System.out.println("test " + (col + 1) + " average is " + average);
33+
} // end of outer loop
34+
}
35+
}
36+
37+
38+
39+
40+

Chapter02/VariableDemo.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class VariableDemo
2+
{
3+
public static void main(String[] args)
4+
{
5+
// declare variables & initialize them
6+
char c = 'A';
7+
int i = 1;
8+
double d = 2.5;
9+
boolean b = true;
10+
final double SALES_TAX_RATE = 7.5;
11+
String s = new String("Hello Again");
12+
13+
// display variable contents
14+
System.out.println("c = " + c);
15+
System.out.println("i = " + i);
16+
System.out.println("c = " + d);
17+
System.out.println("b = " + b);
18+
System.out.println("SALES_TAX_RATE = " + SALES_TAX_RATE);
19+
System.out.println("s = " + s);
20+
}
21+
}
22+

Chapter03/AppletDemo1.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import java.awt.Graphics;
2+
import java.applet.Applet;
3+
4+
public class AppletDemo1 extends Applet
5+
{
6+
public void paint(Graphics g)
7+
{
8+
// display the string data
9+
g.drawString("Hello World", 20, 30);
10+
}
11+
}

Chapter03/AppletDemo2.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<APPLET
2+
CODE = "AppletDemo2.class"
3+
WIDTH="300"
4+
HEIGHT="300">
5+
</APPLET>

Chapter03/DateDemo.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.*;
2+
import java.text.*;
3+
4+
public class DateDemo
5+
{
6+
public static void main(String[] args)
7+
{
8+
// create a Calendar instance
9+
Calendar aCalendar = Calendar.getInstance();
10+
11+
// get today's date
12+
Date today = aCalendar.getTime();
13+
14+
// illustrate arithmetic - add 1 to MONTH and YEAR
15+
aCalendar.add(Calendar.MONTH, 1);
16+
aCalendar.add(Calendar.YEAR, 1);
17+
Date aYearAndMonthLater = aCalendar.getTime();
18+
19+
// create a specific date
20+
aCalendar.set(1998, Calendar.DECEMBER, 15);
21+
Date eleanorsBirthday = aCalendar.getTime();
22+
23+
// get DateFormat instances in each format
24+
DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
25+
DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
26+
DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
27+
28+
// display dates using various formats
29+
System.out.println("long format: today is " + longFormat.format(today));
30+
System.out.println("medium format: today is " + mediumFormat.format(today));
31+
System.out.println("short format: today is " + shortFormat.format(today));
32+
System.out.println("A year and month later is " + longFormat.format(aYearAndMonthLater));
33+
System.out.println("Eleanor's birthday is " + mediumFormat.format(eleanorsBirthday));
34+
35+
// illustrate date comparison
36+
if (aYearAndMonthLater.after(today))
37+
System.out.println("aYearAndMonthLater is after today");
38+
if (today.before(aYearAndMonthLater))
39+
System.out.println("today is before aYearAndMonthLater");
40+
}
41+
}
42+

0 commit comments

Comments
 (0)