MUST BE IN JAVA Please create a constructor to set the initial value of maxSpeed
ID: 3553502 • Letter: M
Question
MUST BE IN JAVA
Please create a constructor to set the initial value of maxSpeed for Car class given below. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
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 8: 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 9: (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) {
}
Question10: (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
Constructor for Car class:
Car(double m){
maxSpeed = m;
}
Question 8:
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 9:
public int numberOfQuestionMark(String str) {
int i, count = 0;
for(i = 0; i < str.length(); i++){
if(str.charAt(i) == '?') count++;
}
return count;
}
Question 10:
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.