Question 3 . Check whether there is any error in the program below, if yes, plea
ID: 3553509 • Letter: Q
Question
Question 3. Check whether there is any error in the program below, if yes, please fix it in each line. If no, simply write "correct". (5 points)
class TestConversion {
public static voidmain(String[] args) {
int i1 = 12; // line 1
int i2 = 5.6; // line 2
double d1 = (i1 - i2) * 1.0; // line 3
float f1 = (i1 + i2) * 1.0; // line 4
short s1 = 1; // line 5
byte b1 = 2; // line 6
short s2 = s1 + b1; // line 7
float f2 = 1.2; // line 8
float f3 = 2.2f; // line 9
f3 = s1 + i1; // line 10
}
}
Answer:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Question 4: Please print out the value of a, b,c, i, j, k. (6 points)
int a = 10;
int b = 5;
int i = a++;
int j = --b;
Answer:
a = b =
i = j =
Please create a constructor to set the initial value of maxSpeed for Car class given below.
class Car {
String licensePlate;
double speed;
double maxSpeed;
//car is moving at the speed of "speed"
void run() {
System.out.println("Car " + licensePlate + "is moving at " + speed + "miles per hour.");
}
}
Answer:
Question 5: Assume you are given a method int minInThree(int a, int b, int c) which returns the minimum value among three integers. Please write a method to return the minimum value among five integers . (6 points)
The method is defined as:
public int minInFive(int a, int b, int c, int d, int e) {
}
Question 6: (12 points)
Write a method to count the number of question mark "?" in the string. For example, "@bc? ab!cd?da" has 2 "?".
public int numberOfQuestionMark(String str) {
}
Question7: (12 points)
Assume the tax rate in IL is shown as below
Income (salary)
Tax rate (rate)
Salary >= 90,000
rate = 40%
70,000 <= Salary < 90,000
rate = 30%
40,000 <= Salary < 70,000
rate = 15%
20,000 <= Salary < 40,000
rate = 10%
Salary < 20,000
rate = 5%
Please write a method to determine the tax to be paid based on the salary.
public double calculateTax(double salary) {
}
Income (salary)
Tax rate (rate)
Salary >= 90,000
rate = 40%
70,000 <= Salary < 90,000
rate = 30%
40,000 <= Salary < 70,000
rate = 15%
20,000 <= Salary < 40,000
rate = 10%
Salary < 20,000
rate = 5%
Explanation / Answer
Question 3:
1. correct
2. double i2 = 5.6; // line 2
3. correct
4. double f1 = (i1 + i2) * 1.0; // line 4
5. correct
6. correct
7. int s2 = s1 + b1; // line 7
8. double f2 = 1.2; // line 8
9. correct
10. correct
Question 4:
a = 11 b = 4
i = 10 j = 4
Constructor for Car class:
Car(double m){
maxSpeed = m;
}
Question 5:
public int minInFive(int a, int b, int c, int d, int e) {
int i = a;
if(i > b) i = b;
if(i > c) i = c;
if(i > d) i = d;
if(i > e) i = e;
return i;
}
Question 6:
public int numberOfQuestionMark(String str) {
int i, count = 0;
for(i = 0; i < str.length(); i++){
if(str.charAt(i) == '?') count++;
}
return count;
}
Question 7:
public double calculateTax(double salary) {
if(salary >= 90000) return 0.4 * salary;
else if(salary >= 70000 && salary < 90000) return 0.3 * salary;
else if(salary >= 40000 && salary < 70000) return 0.15 * salary;
else if(salary >= 20000 && salary < 40000) return 0.1 * salary;
else return 0.05 * salary;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.