# 代写java – CSE 114 Midterm

### CSE 114 Midterm

#### Summer 2022 Solutions

##### Comment on the correctness of the program: You may say following:

######## }

###### dice1 = 1 + ( int )(Math. random ()*6;
``````dice2 = 1+ ( int )(Math. random ()*6;
``````
###### Solution:
``````import java.util.Scanner;
public class Problem {
public static void main(String[] args) {
double amount;
double annualInterest;
int numMonths;
Scanner stdin = new Scanner(System. in );
System. out .println("Enter the amount:");
amount = stdin.nextDouble();
System. out .println("Enter the annual interest:");
annualInterest = stdin.nextDouble();
System. out .println("Enter the number of months:");
numMonths = stdin.nextInt();
``````
``````double monthlyInterest = annualInterest/(100*12.0);
double finalAmount = 0;
for ( int i = 0; i < numMonths; i++) {
finalAmount = (finalAmount + amount)*(1 + monthlyInterest);
}
System. out .println("final saving account amount:" + finalAmount);
stdin.close();
}
}
``````

####### 12. Twin primes are a pair of prime numbers that differ by 2. For example, 3 and 5 are twin primes, 5 and

####### 7 are twin primes, and 11 and 13 are twin primes. Write a program to find all twin primes less than

####### 1,000. Display the output as follows:

####### (3, 5)

####### (5, 7)

####### …

###### Solution:

public class Problem { public static void main(String[] args) { for ( int i = 2; i<1000; i++){ if ( isPrime (i) && isPrime (i + 2)) System. out .println("(" + i + ", " + (i + 2) + ")"); } } public static boolean isPrime( int x){ boolean result = true ; for ( int i = 2; i <= x / 2; i++){ if (x % i == 0){ result = false ; break ; } } return result; } }

####### 13. Write a program that prompts the user to enter two characters and displays the major and status

####### represented in the characters. The first character indicates the major and the second is a number

####### character 1, 2, 3, or 4, which indicates whether a student is a freshman, sophomore, junior, or senior.

####### Suppose that the following characters are used to denote the majors: [10]

####### M: Mathematics

####### C: Computer Science

####### I: Information Technology

####### Here are sample runs:

####### Enter two characters: M

####### Mathematic Freshman

####### Enter two characters: C

####### Computer Science Junior

####### Enter two characters: T

####### Invalid input.

###### Solution:
``````import java.util.Scanner;
public class Problem {
public static void main(String[] args) {
char major, status;
Scanner stdin = new Scanner(System. in );
System. out .println("Enter two characters");
major = stdin.next().charAt(0);
major = Character. toUpperCase (major);
status = stdin.next().charAt(0);
switch (major){
case 'M':
System. out .print("Mathematics ");
if (status == '1')
System. out .print("Freshman");
else if (status == '2')
System. out .print("Sophomore");
else if (status == '3')
System. out .print("Junior");
else if (status == '4')
System. out .print("Sunior");
else
System. out .print("Invalid status");
break ;
case 'C':
System. out .print("Computer Science ");
if (status == '1')
System. out .print("Freshman");
else if (status == '2')
System. out .print("Sophomore");
else if (status == '3')
System. out .print("Junior");
else if (status == '4')
System. out .print("Sunior");
else
System. out .print("Invalid status");
break ;
case 'I':
System. out .print("Information Technology ");
if (status == '1')
System. out .print("Freshman");
else if (status == '2')
System. out .print("Sophomore");
else if (status == '3')
``````
``````System. out .print("Junior");
else if (status == '4')
System. out .print("Sunior");
else
System. out .print("Invalid status");
break ;
default :
System. out .println("Invalid input.");
}
``````

######## }

######## }

####### 14. Write a program that generates 100 random integers between 0 and 9 and displays the count for each

####### number. (Hint: Use an array of 10 integers, say counts, to store the counts for the number of 0s, 1s,..,

####### 9s.) [10]

###### Solution:
``````public class Problem {
public static void main(String[] args) {
int [] counts = new int [10];
int randInt;
for ( int i = 0; i < 100; i++){
randInt = ( int )(10*Math. random ());
counts[randInt]++;
}
for ( int i = 0; i < counts.length; i++){
System. out .println("Number of " + i + "s" + " : " +
counts[i]);
}
}
}
``````
##### test if the input is valid and uses the area method to obtain the area. The program displays
###### Solution:
``````import java.util.Scanner;
public class Problem {
public static void main(String[] args) {
double side1, side2, side3;
Scanner stdin = new Scanner(System. in );
System. out .println("Enter three sides of a triangle: ");
side1 = stdin.nextDouble();
side2 = stdin.nextDouble();
side3 = stdin.nextDouble();
if ( isValid (side1, side2, side3)){
System. out .println("area of the triangle: " + area (side1,
side2, side3));
} else {
System. out .println("Invalid sides !");
}
}
public static boolean isValid( double s1, double s2, double s3){
boolean result = false ;
boolean cond1, cond2, cond3;
if (s1 + s2 > s3)
cond1 = true ;
else
cond1 = false ;
if (s1 + s3 > s2)
cond2 = true ;
else
cond2 = false ;
if (s2 + s3 > s1)
con D3 = true ;
else
cond3 = false ;
if (cond1 && cond2 && cond3)
result = true ;
return result;
}
public static double area( double s1, double s2, double s3){
double a = 0;
double s = (s1 + s2 + s3)/2;
a = Math. sqrt (s*(s - s1)*(s - s2)*(s - s3));
return a;
}
``````

######## }

###### Solution:
``````import java.util.Scanner;
public class Problem {
public static void main(String[] args) {
Scanner stdin = new Scanner(System. in );
System. out .println("Enter a string : ");
String s = stdin.nextLine();
s = s.toLowerCase();
int vowelCount, consoCount;
vowelCount = consoCount = 0;
for ( int i = 0; i < s.length(); i++){
if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z'){
if (s.charAt(i) == 'a' || s.charAt(i) == 'e'
|| s.charAt(i) == 'i' || s.charAt(i) == 'o'
|| s.charAt(i) == 'u')
vowelCount++;
else
consoCount++;
``````

######## }

######## }

``````System. out .println("The number of vowels is " + vowelCount);
System. out .println("The number of consonants is " + consoCount);
}
}
``````