Question

4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...

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.

  1. Create a class called Mechanic with following instance variable and constants:
        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;
        }

  1. Write a class called TestMechanic that will create a new Mechanic object called smartGuy. Then ask the user if they want to repair a Car. If the user wants to repair a car then ask for values of the car’s parameters and create a new car object car1. Then, call the needRepair method for smartGuy and pass car1 as the parameter. If the return value of this needRepair method is true, call the repair method for smartGuy. Then call the howMuch method for smartGuy.

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will...
main The main method instantiates an ArrayList object, using the Car class. So, the ArrayList will be filled with Car objects. It will call the displayMenu method to display the menu. Use a sentinel-controlled loop to read the user’s choice from stdin, call the appropriate method based on their choice, and redisplay the menu by calling displayMenu. Be sure to use the most appropriate statement for this type of repetition. displayMenu Parameters:             none Return value:          none Be sure to use...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields,...
in jGRASP INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with...
PLEASE USING C# TO SOLVE THIS PROBLEM. THANK YOU SO MUCH! a. Create a project with a Program class and write the following two methods (headers provided) as described below: - A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
C# programming Assume we have a student final exam score lookup table like this: John 40...
C# programming Assume we have a student final exam score lookup table like this: John 40 Henry 0 Mary 59 Hillary 39 Pikachu 100 Write a class that helps check if student passes or fails the course. If a student's score is greater than or equal to a parameter passScore, we will consider it a pass. If a student's name is NOT found in the table, we assume they have missed the exam and thus consider failed. The method should...