4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method.
private int cost;
public static int TEST_PRICE = 10;
public static int REPAIR_PRICE = 50;
Have a default constructor that will set cost to 0.
This class three methods : needRepair, howMuch and repair. These methods simulate the functions of a mechanic.
The methods are defined as follows. Complete the method body, as directed by the comments. We will model the Mechanic to be able to work on any Mechanism , which can be anything ( a Car or a Computer)
public boolean needRepair(Mechanism m) {
// include TEST_PRICE into the cost;
/* call the reportProblem() method for mechanism m. If this
Return is greater than 0, then return true, else return
false *
}
public void repair(Mechanism m) {
//call the reportProblem method for mechanism m
/* add to the cost, the value of REPAIR_PRICE * number of
Problems returned by the reportProblem() method */
// call the getFixed() method for mechanism m;
}
public int howMuch() {
// set a variable charge = cost;
//set cost = 0;
//return charge;
}
Then ask if the user wants to repair a computer. If yes, then ask for values of the computer’s parameters and create a new computer object called comp1. Then call the needRepair method for smartGuy and pass comp1 as a parameter.If the return value of this needRepair method is true , call the repair method for smartGuy. Then call the howMuch method for smartGuy.
Add the total values returned by the two howMuch methods and print it on the screen.
ans
Mechanic.java class
public class Mechanic
{
private int cost;
public static int TEST_PRICE=10;
public static int REPAIR_PRICE=50;
public Mechanic()
{
cost=0;
}
public boolean needRepair(Mechanism m)
{
cost+=TEST_PRICE;
return m.reportProblems()>0;
}
public void repair(Mechanism m)
{
int numberOfProblems=m.reportProblems();
cost+=REPAIR_PRICE*numberOfProblems;
m.getFixed();
}
public int howMuch()
{
int charge=cost;
cost=0;
return charge;
}
}
TestMechanic.java
import java.util.Scanner;
public class TestMechanic {
public static void main(String[] args)
{
String userAns;
Mechanic smartGuy=new Mechanic();
Scanner sc=new Scanner(System.in);
System.out.print("If you want to repair your car? (y/n) :");
userAns=sc.next();
if(userAns.equalsIgnoreCase("N"))
System.exit(0);
System.out.print("\nEnter the number of wheels your car have:
");
int numWheels=sc.nextInt();
System.out.print("Enter the brand of the car : ");
String brand=sc.next();
Car car1=new Car(brand, numWheels);
if(smartGuy.needRepair(car1))
{
smartGuy.repair(car1);
System.out.println("The mechanic cost is
:$"+smartGuy.howMuch());
}
}
}
output
i hope it helps..
If you have any doubts please comment and please don't dislike.
PLEASE GIVE ME A LIKE. ITS VERY IMPORTANT FOR ME
Get Answers For Free
Most questions answered within 1 hours.