0%

Java HW08 Answer

8.9

main code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package HW08;

import java.security.SecureRandom;
import java.util.Scanner;

public class hw8_9 {

private final static SecureRandom randomNumbers = new SecureRandom();

public static void main(String args[]){
//user to enter number
Scanner input = new Scanner(System.in);
//input the number of random numbers
//user enter a number
System.out.print("enter a number : ");
int n = input.nextInt();

System.out.println("random numbers : ");
for(int i = 0 ; i < n ; i++){
System.out.println(10 + randomNumbers.nextInt(91));
}
}



}


8.11

class code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package HW082;

public class Complex {
private double realPart;
private double imaginaryPart;
//no-argument constructor
public Complex() {
this(0,0);
}
//constructor of Complex
public Complex(double realPart,double imaginaryPart) {
this.realPart = realPart;
this.imaginaryPart = imaginaryPart;
}
//addition
public void add(Complex left) {
this.realPart += left.realPart;
this.imaginaryPart += left.imaginaryPart;
}
//subtraction
public void subtract(Complex left) {
this.realPart -= left.realPart;
this.imaginaryPart -= left.imaginaryPart;
}
//print out
public void print_out() {
System.out.printf("(%f, %f)%n",realPart,imaginaryPart);
}

}

main code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package HW082;

public class hw8_11 {
public static void main(String args[]) {
//no-argument and argument
Complex complex1 = new Complex();
Complex complex2 = new Complex(1,10);

//print out complex1 and complex2
System.out.println("complex1 & complex2 :");
complex1.print_out();
complex2.print_out();
//addition complex1 and complex2
complex1.add(complex2);
//print out complex1 and complex2
System.out.println("after addition complex1 & complex2 :");
complex1.print_out();
complex2.print_out();
//argument
Complex complex3 = new Complex(2,8);
Complex complex4 = new Complex(14,2);
//print out complex3 and complex4
System.out.println("complex3 & complex4 :");
complex3.print_out();
complex4.print_out();
//subtract complex3 and complex4
complex3.subtract(complex4);
//print out complex3 and complex4
System.out.println("after subtraction complex3 & complex4 :");
complex3.print_out();
complex4.print_out();
}
}