Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 00914bb

Browse files
committed
add lect 11 code
1 parent 50f9491 commit 00914bb

File tree

9 files changed

+340
-1
lines changed

9 files changed

+340
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ assessments/
88
*.log
99
*~
1010
*.swp
11-
fromslides/S11*
1211
fromslides/S12*
1312
fromslides/S13*
1413
fromslides/S14*

fromslides/S06/DataLoss.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* This class is the teacher's example of
3+
* data loss between data types
4+
* double is 64bits (IEEE 754)
5+
* float is 32bits (IEEE 754)
6+
* long is 64bits
7+
* int is 32bits
8+
*
9+
* *
10+
*
11+
* @author PMCampbell
12+
* @version today
13+
**/
14+
15+
import java.util.Scanner;
16+
17+
public class DataLoss {
18+
public static void main(String[] args) {
19+
long longVar;
20+
int intVar, i=0;
21+
double doubleVar;
22+
float floatVar;
23+
24+
longVar = 1000000001;
25+
i = 0;
26+
while (i++ < 55 ) {
27+
System.out.printf("\n long %d == float %f", longVar, (float)longVar);
28+
longVar++;
29+
}
30+
longVar = 10000000001L;
31+
i = 0;
32+
while (i++ < 55) {
33+
System.out.printf("\n long %d == float %f", longVar, (float)longVar);
34+
longVar++;
35+
}
36+
longVar = 1000000001;
37+
i = 0;
38+
while (i++ < 55) {
39+
System.out.printf("\n long %d == double %f", longVar, (double)longVar);
40+
longVar++;
41+
}
42+
double a = 12345.0;
43+
double b = 1e-16;
44+
//System.out.printf("a %f + b %f == a %f", a, b, a);
45+
//System.out.printf("a + b => %f", a + b);
46+
System.out.println("\na " + a + "\n b " +b );
47+
System.out.println("a + b => "+ ( a + b ) );
48+
System.out.println(a + b == a);
49+
// Roundoff error
50+
// floating point arithmetic is not exact
51+
// some real numbers need an infinte number of digits
52+
// ex: 1/3, natural log, PI
53+
// surprise 1/10 not exactly representable
54+
// see this example:
55+
double x1 = 0.3;
56+
double x2 = 0.1 + 0.1 + 0.1;
57+
System.out.println("x1 " + x1);
58+
System.out.println("x2 " + x2);
59+
System.out.println("x1 == x2 " + (x1 == x2));
60+
61+
double z1 = 0.5;
62+
double z2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
63+
System.out.println("z1 " + z1);
64+
System.out.println("z2 " + z2);
65+
System.out.println("z1 == z2 " + (z1 == z2));
66+
67+
68+
} // main()
69+
} // DataLoss

fromslides/S06/output.txt

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
long 1000000001 == float 1000000000.000000
3+
long 1000000002 == float 1000000000.000000
4+
long 1000000003 == float 1000000000.000000
5+
long 1000000004 == float 1000000000.000000
6+
long 1000000005 == float 1000000000.000000
7+
long 10000000001 == float 10000000000.000000
8+
long 10000000002 == float 10000000000.000000
9+
long 10000000003 == float 10000000000.000000
10+
long 10000000004 == float 10000000000.000000
11+
long 10000000005 == float 10000000000.000000
12+
long 10000000006 == float 10000000000.000000
13+
long 10000000007 == float 10000000000.000000
14+
long 10000000008 == float 10000000000.000000
15+
long 10000000009 == float 10000000000.000000
16+
long 10000000010 == float 10000000000.000000
17+
long 10000000011 == float 10000000000.000000
18+
long 10000000012 == float 10000000000.000000
19+
long 10000000013 == float 10000000000.000000
20+
long 10000000014 == float 10000000000.000000
21+
long 10000000015 == float 10000000000.000000
22+
long 10000000016 == float 10000000000.000000
23+
long 10000000017 == float 10000000000.000000
24+
long 10000000018 == float 10000000000.000000
25+
long 10000000019 == float 10000000000.000000
26+
long 10000000020 == float 10000000000.000000
27+
long 10000000021 == float 10000000000.000000
28+
long 10000000022 == float 10000000000.000000
29+
long 10000000023 == float 10000000000.000000
30+
long 10000000024 == float 10000000000.000000
31+
long 10000000025 == float 10000000000.000000
32+
long 10000000026 == float 10000000000.000000
33+
long 10000000027 == float 10000000000.000000
34+
long 10000000028 == float 10000000000.000000
35+
long 10000000029 == float 10000000000.000000
36+
long 10000000030 == float 10000000000.000000
37+
long 10000000031 == float 10000000000.000000
38+
long 10000000032 == float 10000000000.000000
39+
long 10000000033 == float 10000000000.000000
40+
long 10000000034 == float 10000000000.000000
41+
long 10000000035 == float 10000000000.000000
42+
long 10000000036 == float 10000000000.000000
43+
long 10000000037 == float 10000000000.000000
44+
long 10000000038 == float 10000000000.000000
45+
long 10000000039 == float 10000000000.000000
46+
long 10000000040 == float 10000000000.000000
47+
long 10000000041 == float 10000000000.000000
48+
long 10000000042 == float 10000000000.000000
49+
long 10000000043 == float 10000000000.000000
50+
long 10000000044 == float 10000000000.000000
51+
long 10000000045 == float 10000000000.000000
52+
long 10000000046 == float 10000000000.000000
53+
long 10000000047 == float 10000000000.000000
54+
long 10000000048 == float 10000000000.000000
55+
long 10000000049 == float 10000000000.000000
56+
long 10000000050 == float 10000000000.000000
57+
long 10000000051 == float 10000000000.000000
58+
long 10000000052 == float 10000000000.000000
59+
long 10000000053 == float 10000000000.000000
60+
long 10000000054 == float 10000000000.000000
61+
long 10000000055 == float 10000000000.000000
62+
long 1000000001 == double 1000000001.000000
63+
long 1000000002 == double 1000000002.000000
64+
long 1000000003 == double 1000000003.000000
65+
long 1000000004 == double 1000000004.000000
66+
long 1000000005 == double 1000000005.000000
67+
long 1000000006 == double 1000000006.000000
68+
long 1000000007 == double 1000000007.000000
69+
long 1000000008 == double 1000000008.000000
70+
long 1000000009 == double 1000000009.000000
71+
long 1000000010 == double 1000000010.000000
72+
a 12345.0
73+
b 1.0E-16
74+
a + b => 12345.0
75+
true
76+
x1 0.3
77+
x2 0.30000000000000004
78+
x1 == x2 false
79+
z1 0.5
80+
z2 0.5
81+
z1 == z2 true
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package fromslides.S11;
2+
import java.util.Scanner;
3+
public class DSlope {
4+
public static void main(String[] args) {
5+
6+
int lim = 0;
7+
// major loop number of rows
8+
for (int i = 0; i <= 3; i++) {
9+
// minor loop number of colums
10+
for (int j = 0; j <= lim; j++) {
11+
12+
System.out.print("D");
13+
14+
}
15+
System.out.println("");
16+
lim++;
17+
18+
}
19+
20+
} //main()
21+
} //
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package fromslides.S11;
2+
import java.util.Scanner;
3+
public class DoWhile {
4+
public static void main(String[] args) {
5+
Scanner keyboard = new Scanner(System.in);
6+
7+
int num; // not initialized
8+
do
9+
{
10+
// do while >= 1 iteration(s)
11+
// while >= 0 iterations
12+
System.out.print("Enter an even number: ");
13+
num = keyboard.nextInt();
14+
}
15+
while (num %2 == 1);
16+
17+
} //main()
18+
} //
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package fromslides.S11;
2+
import java.util.Scanner;
3+
public class For {
4+
public static void main(String[] args) {
5+
Scanner keyboard = new Scanner(System.in);
6+
7+
// table of square of numbers 1 to 10
8+
System.out.println("Number Number Squared");
9+
System.out.println("-----------------------");
10+
for (int i = 1; i <= 10; i++) {
11+
System.out.println(i + "\t\t" + Math.pow(i, 2));
12+
}
13+
//equivalent while loop
14+
int i = 1;
15+
while (i <= 10) {
16+
System.out.println(i + "\t\t" + Math.pow(i, 2));
17+
i++;
18+
}
19+
// compound int
20+
double bal = 1000; // initial balance
21+
double rate = 0.08; // interest rate
22+
System.out.println("0 \t" + Math.round(100 * bal) / 100.);
23+
for (int year = 1; year <= 10; year++) {
24+
bal = bal + rate * bal;
25+
//System.out.println(year + " " + Math.round(100 * bal) / 100.);
26+
System.out.printf("%d \t%.2f\n", year, Math.round(100 * bal) / 100.);
27+
}
28+
29+
30+
} //main()
31+
} //
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.Scanner;
2+
/**
3+
* This class illustrates exercises from the slide deck for the course
4+
* nested while loops
5+
* nested for loops
6+
*
7+
* 360-420-DW Intro to Java
8+
* @author PMCampbell
9+
* @version today
10+
**/
11+
public class NestedLoops {
12+
public static void main(String[] args) {
13+
14+
Scanner keyboard = new Scanner(System.in);
15+
// 24 clock 00:00 to23:59 w minutes
16+
int minutes, hours = 0;
17+
while (hours <= 23) {
18+
minutes = 0;
19+
while (minutes <= 59) {
20+
//System.out.println(hours + ":" + minutes);
21+
System.out.printf("%02d:%02d\n", hours, minutes);
22+
minutes++;
23+
}
24+
hours++;
25+
}
26+
// 10x10 table
27+
final int SIZE = 10;
28+
int row = 1, col = 1;
29+
while (row <= SIZE) {
30+
col = 1;
31+
while (col <= SIZE) {
32+
System.out.print(" * ");
33+
col++;
34+
}
35+
System.out.println();
36+
row++;
37+
}
38+
// triangle
39+
for (int rows = 1; rows <= 5; rows++) {
40+
for (int cols = 1; cols <= rows; cols++) {
41+
System.out.print("*");
42+
}
43+
System.out.println();
44+
}
45+
} //main()
46+
} //
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Scanner;
2+
/**
3+
* This class illustrates exercises from the slide deck for the course
4+
* Using looping & method calls calcualte PI
5+
*
6+
* 360-420-DW Intro to Java
7+
* @author PMCampbell
8+
* @version today
9+
**/
10+
11+
public class Pi {
12+
public static void main(String[] args) {
13+
double pi = calcPi(0.0000001);
14+
System.out.printf("Estimate of PI is: %.10f\n", pi);
15+
}
16+
/**
17+
* Estimating PI using the Leibniz formula
18+
*
19+
* @param accuracy at which to stop estimating
20+
* @return PI double
21+
**/
22+
23+
public static double calcPi(double accuracy) {
24+
// declare and initialize estimates
25+
double current = 0;
26+
double next = 4.0 / 1;
27+
int n = 1;
28+
29+
while (Math.abs(next - current) >= accuracy) {
30+
current = next;
31+
next = current + 4. * Math.pow(-1, n) / (2 * n + 1);
32+
n++;
33+
}
34+
return current;
35+
} // main()
36+
} // Pi
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Scanner;
2+
/**
3+
* This class illustrates exercises from the slide deck for the course
4+
* determine if a number is prime
5+
*
6+
* 360-420-DW Intro to Java
7+
* @author PMCampbell
8+
* @version today
9+
**/
10+
public class Prime {
11+
public static void main(String[] args) {
12+
Scanner kb = new Scanner(System.in);
13+
boolean isPrime = true;
14+
int count =2, num = 0;
15+
System.out.println("Enter numbers to test for prime, <= 0 to exit");
16+
System.out.print("enter an integer: ");
17+
num = kb.nextInt();
18+
19+
while (num >= 1 ) {
20+
count = 2;
21+
isPrime = true;
22+
while (count <= Math.sqrt(num) && isPrime) {
23+
if (num % count==0) {
24+
isPrime = false;
25+
}
26+
count++;
27+
}
28+
if (isPrime) {
29+
System.out.println(num + " is prime");
30+
} else {
31+
System.out.println(num + " is NOT prime");
32+
}
33+
System.out.print("enter an integer: ");
34+
num = kb.nextInt();
35+
}
36+
37+
} //main()
38+
} // Prime

0 commit comments

Comments
 (0)