代写java – CSE 114 Midterm

CSE 114 Midterm

代写java – 这是一个关于java的题目, 主要考察了关于java的内容,是一个比较经典的题目, 涉及了java等代写方面

d3.js代写 代写d3.js 代写d3.js

Summer 2022 Solutions

1. What will be the output for the following program? [4]
public class Problem {
public static void main(String[] args) {
for ( int i = 0; i < 5; i++) {
for ( int j = 0; j <= i; j++)
System. out .print(i * j + " ");
System. out .println();
}
}
}
Solution:
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
2. What would be the output for the following code segment: [4]
public class Problem {
public static void main(String[] args) {
boolean x = 2 * 2 – 3 > 2 && 4 – 2 > 5;
System.out.println(x);
}
}
Solution: false
3. What will be the output of the following program? [4]
public class Problem {
public static void main(String[] args) {
System. out .println("1" + 2);
System. out .println(‘2’ + 3);
System. out .println("3" + 4 + 5 * 5);
System. out .println("4" + (1 + 1) * 3);
System. out .println(‘5’ + 1 + 1);
}
}
Hint: Unicode and ASCII value of 2 is 50 and 5 is 53
Solution:
12
53
3425
46
55
4. What will be value of the variables j and sum respectively, after this code sequence is
executed? [4]
public class Problem {
public static void main(String[] args) {
int sum = 0;
int j = 17;
while (j %10 != 0) {
sum += j;
j++;
}
System. out .println("j:–>" + j + ", sum:" + sum);
}
}
Solution:
j:–>20, sum:–> 54
5. Rewrite the following program using a switch statement. [4]
public class Problem {
public static void main(String[] args) {
int x = 1, a = 3;
if (a == 1)
x += 5;
else if (a == 2)
x += 10;
else if (a == 3)
x += 16;
else if (a == 4)
x += 34;
System. out .println("x:–>" + x);
}
}
Solution:
switch(a) {
case 1:
x += 5;
break;
case 2:
x += 10;
break;
case 3:
x += 16;
break;
case 4:
x += 34;
}
6. Given a number 457 in octal number system, covert it into decimal number system
[4]
Solution:
78^0 + 58^1 + 4*8^2 = 303
7. What will be the output of the following program? [4]
public class Problem {
public static void main(String[] args) {
int x = 5;
switch (x){
case 1:
System. out .println("one");
break ;
default :
System. out .println("default");
case 2:
System. out .println("two");
case 3:
System. out .println("three");
break ;
}
}
}
Solution:
default
two
three
8. What will be the result of the following code segment: [4]
public class Problem {
public static void main(String[] args) {
int [] a;
a = {3, 4, 2, 1, 10};
for ( int i = 0; i < a.length; i++)
System. out .println(a[i]);
}
}
Solution:
Compiler error at a = {3, 4, 2, 1, 10};
9. What will be the output of the following program?
public class Problem {
public static void main(String[] args) {
System. out .println(Math. rint (2.5));
System. out .println(Math. rint (5.5));
}
}
Solution:
2.
6.
10. Given below is a program that simulates rolling of a pair of dice. We can simulate rolling one
die by choosing one of the integers 1, 2, 3, 4, 5, or 6 at random. The number we pick
represents the number on the die after it is rolled. The program reports the number showing
on each die as well as the total roll. For example: [4]
Sample run:
The first die comes up 3
The second die comes up 5
Your total roll is 8
Comment on the correctness of the program: You may say following:
Program is correct
Program is incorrect
If you think that it is incorrect then identify the problem and fix it.
[ Note: You don’t need to rewrite the whole program, just identify line# and provide
the corresponding correct statement, only if you think that the program is incorrect .]
public class Problem {
public static void main(String[] args) {
int dice1, dice2;
dice1 = ( int )(Math. random ()*7;
dice2 = ( int )(Math. random ()*7;
System. out .println("First die comes up " + dice1);
System. out .println("Second die comes up " + dice2);
System. out .println("Your total roll is " + (dice1 + dice2));
}

######## }

Solution : Incorrect logic on line:
dice1 = ( int )(Math. random ()*7;
dice2 = ( int )(Math. random ()*7;
Should be fixed by using:
dice1 = 1 + ( int )(Math. random ()*6;
dice2 = 1+ ( int )(Math. random ()*6;
11. Suppose you save $100 each month into a saving account with the annual interest rate
5%. So, the monthly interest rate is 0.05/12 = 0.00417. After the first month, the value in the
account becomes: [10]
100 * (1 + 0.00417) = 100.
After the second month, the value in the account becomes:
(100 + 100.417) * (1 + 0.00417) = 201.
After the third month, the value in the account becomes:
(100 + 201.252) * (1 + 0.00417) = 302.
and so on.
Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate
(e.g., 5) and the number of months (e.g., 6) and displays the amount in the saving account
after the given month.
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]);
}
}
}
15. Implement the following two methods:
// Return true if the sum of every two sides is greater than the third side.
public static boolean isValid( double side1, double
side2, double side3)
public static double area( double side1, double
side2, double side3)
Write a test program that reads three sides for a triangle and uses the isValid method to
test if the input is valid and uses the area method to obtain the area. The program displays
the area if the input is valid. Otherwise, it displays that the input is invalid.
The formula for computing the area of a triangle is given below:
s = (side1 + side2 + side3)/
area = sqrt(s * (s – side1) * (s – side2) * (s – side3))
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;
}

######## }

16. Write a program that prompts the user to enter a string and displays the number of vowels
and consonants in the string.
Here is a sample run:
Enter a string : Programming is Fun
The number of vowels is 5
The number of consonants is 11
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);
}
}