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...
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....
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...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
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...
Using the attached files, TreeAssignment.java and IntTree.java Add to the methods in IntTree so that the...
Using the attached files, TreeAssignment.java and IntTree.java Add to the methods in IntTree so that the sumNodes methods returns the sum of all the integers in the tree. countLeftNodes should count up all the left children in the tree. Provided in IntTree.java is an example to help you called countEvenBranches which counts all the branches (nodes with children) with even values, but not leaves (nodes with no children) with even values. Both methods return an integer. The tree generated is...
JAVA please Arrays are a very powerful data structure with which you must become very familiar....
JAVA please Arrays are a very powerful data structure with which you must become very familiar. Arrays hold more than one object. The objects must be of the same type. If the array is an integer array then all the objects in the array must be integers. The object in the array is associated with an integer index which can be used to locate the object. The first object of the array has index 0. There are many problems where...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run...
Casting class objects 1.2 Compile and execute the code listed below as it is written. Run it a second time after uncommenting the line obj.speak();. public class AnimalRunner {    public static void main(String[] args)    {       Dog d1 = new Dog("Fred");       d1.speak();       Object obj = new Dog("Connie");       // obj.speak();    } } The uncommented line causes a compile error because obj is an Object reference variable and class Object doesn’t contain a speak() method. This...
Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1-...
Create an interface named Turner, with a single method called turn(). Then create 4 classes: 1- Leaf: that implements turn(), which changes the color of the Leaf object and returns true. If for any reason it is unable to change color, it should return false (you can come up with a reason for failure). The new color can be determined at random. 2- Document: that implements turn(), which changes the page on the document to the next page and returns...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Compare the American Counselors Association (ACA) code of ethics regarding continuing education to the National Association...
    asked 5 minutes ago
  • Python Jupyter Notebook An acronym is a word formed by taking the first letters of the...
    asked 37 minutes ago
  • Question posed after chapter 4.5 of differential equations class, book by p. blanchard, 4th edition :...
    asked 40 minutes ago
  • Below are some crude cause-specific mortality rates from the two studies. Crude mortality rate per 100,000...
    asked 1 hour ago
  • A computer chess game and a human chess champion are evenly matched. They play twelve games....
    asked 1 hour ago
  • Identify and describe instances of the following: Cognitive Dissonance Denial Overgeneralization Conformity Also, how about the...
    asked 1 hour ago
  • Your task is to identify 2 advertisements. One advertisement encourages a consumer to use a non-compensatory...
    asked 1 hour ago
  • For the following exercises, consider this scenario: For each year t, the population of a forest...
    asked 1 hour ago
  • Question 5 A 15-minute-rainfall of 20-year Average Recurrence Interval (ARI) was fallen over a 5ha catchment...
    asked 1 hour ago
  • Explain what is Band structure and and why energy bands are formed in a solid state?...
    asked 1 hour ago
  • Test the hypothesis using the p-value approach Upper H 0H0​: pequals=0.3 versus Upper H 1H1​: pgreater...
    asked 1 hour ago
  • As head of state agency, you are concerned that your employees have fallen into the old...
    asked 2 hours ago