update,
This commit is contained in:
24
_resources/it114105/itp3914/Lab08/Ex5a.java
Normal file
24
_resources/it114105/itp3914/Lab08/Ex5a.java
Normal file
@@ -0,0 +1,24 @@
|
||||
public class Ex5a {
|
||||
public static void main(String [ ] args) {
|
||||
int times = 0;
|
||||
int count=1;
|
||||
System.out.print(2); // 2 is the first prime number
|
||||
int num=2;
|
||||
while (count < 20) {
|
||||
num++; // try next number
|
||||
boolean isPrime=true;
|
||||
for (int i=2; i<num; i++) {
|
||||
if (num%i == 0) // divisible by i
|
||||
isPrime = false; // not a prime
|
||||
times++;
|
||||
}
|
||||
if (isPrime) {
|
||||
count++; // one more prime is found
|
||||
System.out.print(", " + num);
|
||||
}
|
||||
}
|
||||
System.out.println("\nDone");
|
||||
System.out.println("Total calculation = " + times);
|
||||
}
|
||||
|
||||
}
|
26
_resources/it114105/itp3914/Lab08/Ex5b.java
Normal file
26
_resources/it114105/itp3914/Lab08/Ex5b.java
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
public class Ex5b {
|
||||
public static void main(String [ ] args) {
|
||||
int times = 0;
|
||||
int count=1;
|
||||
System.out.print(2); // 2 is the first prime number
|
||||
int num=2;
|
||||
while (count < 20) {
|
||||
num++; // try next number
|
||||
boolean isPrime=true;
|
||||
for (int i=2; i<num; i++) {
|
||||
times++;
|
||||
if (num%i == 0 ){ // divisible by i
|
||||
isPrime = false; // not a prime
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isPrime) {
|
||||
count++; // one more prime is found
|
||||
System.out.print(", " + num);
|
||||
}
|
||||
}
|
||||
System.out.println("\nDone");
|
||||
System.out.println("Total calculation = " + times);
|
||||
}
|
||||
}
|
15
_resources/it114105/itp3914/Lab08/Ex6a.java
Normal file
15
_resources/it114105/itp3914/Lab08/Ex6a.java
Normal file
@@ -0,0 +1,15 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ex6a {
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Number? ");
|
||||
int num = input.nextInt();
|
||||
for(int i = 1; i <= num; i++){
|
||||
for(int j = 1; j <= i; j++){
|
||||
System.out.print(j);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
16
_resources/it114105/itp3914/Lab08/Ex6b.java
Normal file
16
_resources/it114105/itp3914/Lab08/Ex6b.java
Normal file
@@ -0,0 +1,16 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ex6b {
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Number? ");
|
||||
int num = input.nextInt();
|
||||
for(int i = 1; i <= num; i++){
|
||||
for( int j = 0; j < num-i; j++)
|
||||
System.out.print(" ");
|
||||
for (int k = 1; k <=i; k++)
|
||||
System.out.print(k);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
32
_resources/it114105/itp3914/Lab08/Ex7.java
Normal file
32
_resources/it114105/itp3914/Lab08/Ex7.java
Normal file
@@ -0,0 +1,32 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ex7 {
|
||||
|
||||
final static String[] option = {"Paper", "Scissor", "Stone"};
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
while(true){
|
||||
System.out.print("0:Paper 1:Scissor 2:Stone 3:Exit? ");
|
||||
int userInput = input.nextInt();
|
||||
if(userInput == 3)
|
||||
break;
|
||||
int cmpInput = (int) (Math.random()*3);
|
||||
System.out.printf("You choose %s vs CPU choose %s\n", option[userInput], option[cmpInput]);
|
||||
if(userInput == cmpInput)
|
||||
System.out.println("It's Draw");
|
||||
else {
|
||||
if(userInput == 0 && cmpInput == 2 ||
|
||||
userInput == 1 && cmpInput == 0 ||
|
||||
userInput == 2 && cmpInput == 1){
|
||||
System.out.println("You Win");
|
||||
}
|
||||
else {
|
||||
System.out.println("You Lose");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
System.out.println("Bye Bye!");
|
||||
|
||||
}
|
||||
}
|
38
_resources/it114105/itp3914/Lab08/Ex8.java
Normal file
38
_resources/it114105/itp3914/Lab08/Ex8.java
Normal file
@@ -0,0 +1,38 @@
|
||||
import java.util.Arrays;
|
||||
import java.util.DoubleSummaryStatistics;
|
||||
import java.util.Scanner;
|
||||
import java.util.stream.DoubleStream;
|
||||
|
||||
class Ex8{
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
double[] values = new double[10];
|
||||
int index = 0;
|
||||
while(index < 10){
|
||||
double value = input.nextDouble();
|
||||
if(value == -1)
|
||||
break;
|
||||
if(value > 0)
|
||||
values[index++] = value;
|
||||
}
|
||||
|
||||
double sum, mean, max, min, mu, sd;
|
||||
sum = mean = mu = sd = 0.0;
|
||||
min = max = values[0];
|
||||
for(int i = 0; i < index; i++){
|
||||
sum += values[i];
|
||||
if(max < values[i])
|
||||
max = values[i];
|
||||
if(min > values[i])
|
||||
min = values[i];
|
||||
}
|
||||
mean = mu = sum/index;
|
||||
double tmpsum = 0.0;
|
||||
for(int i = 0; i < index; i++)
|
||||
tmpsum += Math.pow((values[i]-mu) , 2);
|
||||
|
||||
sd = Math.sqrt(tmpsum/index);
|
||||
System.out.printf("sum=%.2f, mean=%.2f, maximum=%.2f, minimum=%.2f, and standard deviation=%.2f\n", sum, mean, max, min, sd);
|
||||
|
||||
}
|
||||
}
|
28
_resources/it114105/itp3914/Lab08/Ex9.java
Normal file
28
_resources/it114105/itp3914/Lab08/Ex9.java
Normal file
@@ -0,0 +1,28 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ex9 {
|
||||
public static void main(String[] args) {
|
||||
Scanner input = new Scanner(System.in);
|
||||
System.out.print("Enter x (in radians): ");
|
||||
double result = 0.0;
|
||||
double x = result = input.nextDouble();
|
||||
System.out.println("Term 1 = " + x);
|
||||
for(int i = 3; i <= 27; i+=2){
|
||||
double factorial = 1;
|
||||
for(int j = 2; j <= i; j++)
|
||||
factorial *= j;
|
||||
|
||||
double value = Math.pow(x, i)/factorial;
|
||||
int term = i/2;
|
||||
if((term %2) == 0){
|
||||
result += value;
|
||||
System.out.println("Term " + (term+1) + " = " + value);
|
||||
}else{
|
||||
result -= value;
|
||||
System.out.println("Term " + (term+1) + " = -" + value);
|
||||
}
|
||||
}
|
||||
System.out.println("Sin(" + x + ")=" + result);
|
||||
}
|
||||
|
||||
}
|
14
_resources/it114105/itp3914/Lab08/Q6cExtra.java
Normal file
14
_resources/it114105/itp3914/Lab08/Q6cExtra.java
Normal file
@@ -0,0 +1,14 @@
|
||||
public class Q6cExtra {
|
||||
public static void main(String[] args) {
|
||||
for(int i = 0; i < 5; i++){
|
||||
|
||||
for(int s = 0; s < i; s++)
|
||||
System.out.print(" ");
|
||||
|
||||
|
||||
for(int j = i+1; j <= 5; j++)
|
||||
System.out.print(" "+j);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
9
_resources/it114105/itp3914/Lab08/Q6dExtra.java
Normal file
9
_resources/it114105/itp3914/Lab08/Q6dExtra.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Q6dExtra {
|
||||
public static void main(String[] args) {
|
||||
for(int i = 0; i < 5; i++){
|
||||
for (int j = 5-i; j > 0; j--)
|
||||
System.out.print(j + " ");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
9
_resources/it114105/itp3914/Lab08/Q6eExtra.java
Normal file
9
_resources/it114105/itp3914/Lab08/Q6eExtra.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Q6eExtra {
|
||||
public static void main(String[] args) {
|
||||
for(int i = 0 ; i < 5; i++){
|
||||
for(int j = 5; j > 0 + 4 - i; j--)
|
||||
System.out.print(j + " ");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
11
_resources/it114105/itp3914/Lab08/Q6fExtra.java
Normal file
11
_resources/it114105/itp3914/Lab08/Q6fExtra.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Q6fExtra {
|
||||
public static void main(String[] args) {
|
||||
for(int i = 0; i < 5; i++){
|
||||
for(int s = 0; s < 4-i; s++)
|
||||
System.out.print(" ");
|
||||
for(int j = 5; j > 4-i; j--)
|
||||
System.out.print(" " + j);
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
11
_resources/it114105/itp3914/Lab08/Q6gExtra.java
Normal file
11
_resources/it114105/itp3914/Lab08/Q6gExtra.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Q6gExtra {
|
||||
public static void main(String[] args) {
|
||||
for(int i = 0; i < 5; i++){
|
||||
for (int s = 0; s < i; s++)
|
||||
System.out.print(" ");
|
||||
for( int j = 5-i; j > 0; j--)
|
||||
System.out.print(j + " ");
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
}
|
283
_resources/it114105/itp3914/Lab08/README.md.original
Normal file
283
_resources/it114105/itp3914/Lab08/README.md.original
Normal file
@@ -0,0 +1,283 @@
|
||||
# Lab 8 Repetition Part 2 (Nested Loops)
|
||||
## Topic 3: Basic Program Structures
|
||||
|
||||
### Intended Learning Outcomes
|
||||
|
||||
Upon completion of this lab, you should be able to:
|
||||
- Trace loops to determine their actions, such as the number of iterations.
|
||||
- Use loops to process elements in an array.
|
||||
- Use nested loops to generate two-dimensional tables.
|
||||
|
||||
## Exercise 1
|
||||
What will be the value of x after executing the program fragments below?
|
||||
|
||||
(a)
|
||||
```java
|
||||
int x, a=0;
|
||||
for (x=9; x>=1; x--) {
|
||||
a++;
|
||||
if (a == 4)
|
||||
break;
|
||||
}
|
||||
// x is 6
|
||||
```
|
||||
(b)
|
||||
```java
|
||||
int a=9, x=0;
|
||||
while (a > 0) {
|
||||
a--;
|
||||
x+=2;
|
||||
if (a < 3)
|
||||
continue;
|
||||
x+=3;
|
||||
}
|
||||
|
||||
// x is 36
|
||||
```
|
||||
|
||||
## Exercise 2
|
||||
Will the programs below terminate? If so, give the output.
|
||||
(a)
|
||||
```java
|
||||
int balance = 100;
|
||||
while ( true ) {
|
||||
if ( balance < 12 )
|
||||
break;
|
||||
balance = balance – 12;
|
||||
}
|
||||
System.out.println( "Balance is " + balance );
|
||||
|
||||
// Output:
|
||||
Balance is 4
|
||||
```
|
||||
(b)
|
||||
```java
|
||||
int balance = 100;
|
||||
while ( true ) {
|
||||
if ( balance < 12 )
|
||||
continue;
|
||||
balance = balance – 12;
|
||||
}
|
||||
System.out.println( "Balance is " + balance );
|
||||
|
||||
// loop
|
||||
```
|
||||
|
||||
## Exercise 3
|
||||
What will be the number of times the statement marked with a comment executes?
|
||||
|
||||
(a)
|
||||
```java
|
||||
for (i=0; i<5; i++)
|
||||
for (j=0; j<7; j++)
|
||||
System.out.println("loop"); // statement in question
|
||||
|
||||
// 5*7 = 35 times
|
||||
```
|
||||
(b)
|
||||
```java
|
||||
for (i=0; i<5; i++)
|
||||
for (j=0; j<7; j++)
|
||||
for (k=0; k<6; k++)
|
||||
System.out.println("another loop");
|
||||
System.out.println("loop"); // statement in question
|
||||
|
||||
// 1 times, due to for loop not {}. Therefore, the loop will only handle next line only.
|
||||
```
|
||||
|
||||
## Exercise 4
|
||||
Determine the output of the program below.
|
||||
|
||||
(a)
|
||||
```java
|
||||
public class Test {
|
||||
public static void main ( String[] args ) {
|
||||
for ( int i = 1; i < 5; i++ ) {
|
||||
int j = 0;
|
||||
while ( j < i ) {
|
||||
System.out.print( j + " " );
|
||||
j++;
|
||||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
0 0 1 0 1 2 0 1 2 3
|
||||
```
|
||||
|
||||
(b)
|
||||
```java
|
||||
public static void main( String[] args ) {
|
||||
String s1;
|
||||
for ( int row=1; row<=5; row++ ) {
|
||||
s1 = "";
|
||||
for ( int column=1; column<=5; column++ ) {
|
||||
s1 += ( row == column ) ? "*" : " ";
|
||||
}
|
||||
System.out.println( s1 );
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
*
|
||||
```
|
||||
|
||||
(c)
|
||||
```java
|
||||
for (int i=0; i<2; i++) {
|
||||
System.out.println("outer " + i);
|
||||
for (int j=0; j<3; j++) {
|
||||
System.out.println("Inner " + i + " " + j);
|
||||
}
|
||||
for (int k=2; k>0; k--) {
|
||||
System.out.println("Inner " + i + " " + k);
|
||||
}
|
||||
}
|
||||
|
||||
// Output:
|
||||
outer 0
|
||||
Inner 0 0
|
||||
Inner 0 1
|
||||
Inner 0 2
|
||||
Inner 0 2
|
||||
Inner 0 1
|
||||
outer 1
|
||||
Inner 1 0
|
||||
Inner 1 1
|
||||
Inner 1 2
|
||||
Inner 1 2
|
||||
Inner 1 1
|
||||
```
|
||||
|
||||
## Exercise 5
|
||||
In lecture notes 3.2 (Repetition Structures – Part 2), the Java program FindPrime is used to illustrate nested-loops.
|
||||
|
||||
```java
|
||||
public class FindPrime {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int count = 1;
|
||||
|
||||
System.out.print(2); // 2 is the first prime number
|
||||
int num = 2;
|
||||
|
||||
while (count < 20) {
|
||||
num++; // try next number
|
||||
boolean isPrime = true;
|
||||
for (int i = 2; i < num; i++) {
|
||||
if (num % i == 0) // divisible by i
|
||||
isPrime = false; // not a prime
|
||||
}
|
||||
if (isPrime) {
|
||||
count++; // one more prime is found
|
||||
System.out.print(", " + num);
|
||||
}
|
||||
}
|
||||
System.out.println("\nDone");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
(a) Add some program statements to count the number of times the inner-loop has been executed, and verify that it agrees with the value 2415 given in the lecture notes.
|
||||
```
|
||||
c:\> java FindPrime
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
|
||||
Done
|
||||
Total calculation = 2415
|
||||
```
|
||||
|
||||
(b) Modify the program so that the number of times the inner-loop is executed can be reduced without sacrificing the correctness of the program. Report the number of times the inner-loop has been executed in your improved program.
|
||||
```
|
||||
c:\> java FindPrime2
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71
|
||||
Done
|
||||
Total calculation = 677
|
||||
```
|
||||
|
||||
## Exercise 6
|
||||
Write a program that asks the user to input an integer. The program then prints a pattern with the number of lines controlled by the user input. You must use a nested-for loop.
|
||||
|
||||
(a)
|
||||
```
|
||||
c:\exercises> java Pattern
|
||||
Number? 4
|
||||
1
|
||||
12
|
||||
123
|
||||
1234
|
||||
```
|
||||
|
||||
(b)
|
||||
```
|
||||
c:\exercises> java Pattern2
|
||||
Number? 5
|
||||
1
|
||||
12
|
||||
123
|
||||
1234
|
||||
12345
|
||||
```
|
||||
|
||||
## Exercise 7
|
||||
Generate a Multiplication Table:
|
||||
Write a Java program to fill values to a two-dimensional array, mTable[10][10], for a multiplication table. Print the multiplication table as follows.
|
||||
|
||||
```
|
||||
0 1 2 3 4 5 6 7 8 9
|
||||
+------------------------------
|
||||
0| 0 0 0 0 0 0 0 0 0 0
|
||||
1| 0 1 2 3 4 5 6 7 8 9
|
||||
2| 0 2 4 6 8 10 12 14 16 18
|
||||
3| 0 3 6 9 12 15 18 21 24 27
|
||||
4| 0 4 8 12 16 20 24 28 32 36
|
||||
5| 0 5 10 15 20 25 30 35 40 45
|
||||
6| 0 6 12 18 24 30 36 42 48 54
|
||||
7| 0 7 14 21 28 35 42 49 56 63
|
||||
8| 0 8 16 24 32 40 48 56 64 72
|
||||
9| 0 9 18 27 36 45 54 63 72 81
|
||||
```
|
||||
|
||||
## Exercise 8
|
||||
Write a Java program to prompt a user to input some positive real numbers and store them in an array. The user can enter no more than 10 numbers. The program should stop prompting input when the user has entered the 10th number or input a negative value, e.g. -1. Then, the program starts to calculate the following statistics.
|
||||
1. Sum = $\sum_{i=1}^{n}x_i$
|
||||
2. Mean, $\overline{x} = \frac{Sum}{n}$
|
||||
3. Maximum
|
||||
4. Minimum
|
||||
5. Standard Deviation, $\sigma = \sqrt{\frac{\sum_{i=1}^{n}(x_i-\overline{x})^2}{n-1}}$ (for $n>1$ only)
|
||||
|
||||
Test your program with the following five numbers, 1.23, 2.05, 4.0, 0.01, 0.12. Their sum=7.41, mean=1.48, maximum=4.0, minimum=0.01, and standard deviation=1.64.
|
||||
|
||||
## Exercise 9
|
||||
Approximate sin x by a sum of series:
|
||||
Write a Java program to calculate the following series ($x$ is in radians, $360° = 2\pi$ radians):
|
||||
|
||||
```math
|
||||
\sin(x)=x-\frac{x^3}{3!}+\frac{x^5}{5!}-\frac{x^7}{7!}+\frac{x^9}{9!}-\frac{x^{11}}{11!}...+\frac{x^{25}}{25!}-\frac{x^{27}}{27!'}, -3.1416 < x < 3.1416
|
||||
```
|
||||
|
||||
**Sample Output**
|
||||
```
|
||||
C:\>java SinX
|
||||
Enter x (in radians): _2_
|
||||
Term 1 = 2.0
|
||||
Term 2 = -1.3333333333333333
|
||||
Term 3 = 0.26666666666666666
|
||||
Term 4 = -0.025396825396825393
|
||||
Term 5 = 0.0014109347442680773
|
||||
Term 6 = -5.130671797338463E-5
|
||||
Term 7 = 1.3155568711124264E-6
|
||||
Term 8 = -2.5058226116427168E-8
|
||||
Term 9 = 3.6850332524157597E-10
|
||||
Term 10 = -4.309980412182175E-12
|
||||
Term 11 = 4.1047432496973094E-14
|
||||
Term 12 = -3.2448563238713907E-16
|
||||
Term 13 = 2.163237549247594E-18
|
||||
Term 14 = -1.2326139881752673E-20
|
||||
Sin(2.0)=0.9092974268256817
|
||||
```
|
Reference in New Issue
Block a user