(JAVA) In this problem we are trying to model a repair service that can repair C
ID: 3706535 • Letter: #
Question
(JAVA) In this problem we are trying to model a repair service that can repair Cars and Computers. Even though the Car and Computer are different products, they have common repair methods that can be abstracted in an interface. Please complete the interface and the classes by adding the required methods, as per the problem description. Partially complete code is provided as shown below.
A) Create an interface called Mechanism. This interface has two methods. The first method called reportProblems() returns an int and takes in no parameters. The other method getFixed() returns a void and takes in no parameters.
B) Create a class called Computer that implements the interface Mechanism. This class has the following instance variables and constants that depicts the properties of the computer
public static final double MIN_REQUIRED_SPEED = 2.0;
public static final double MIN_REQUIRED_CAPACITY = 1.0;
private double processorSpeed;
private double memoryCapacity;
private boolean networked;
This class has a full arg constructor.
This class has getters for each instance variable. The getter for networked should be named isNetworked .
The class should simulate a repair process and hence it should be able to do the following methods :
changeProcessor to a given new processor speed ; addMemoryCapacity to a given new memoryCapacity; be able to connect, upon which the property networked is set to true ; be able to disconnect where networked is set to false.
The class should implement the methods specified in the interface as follows. These methods will check the computer’s parameters and get the trouble fixed.
In the reportProblems method, number of troubles are tallied and reported as the return value as follows. If the processorSpeed is less than the If the memoryCapacity is less than the
In the getFixed method if the processorSpeed is less than the minimum required speed, then add the difference in speed. If the memoryCapacity is less than the minimum required capacity then add the difference in capacity. Then, connect the computer.
C) Create a class called Car that implements the interface Mechanism. This class has the following variables and constants:
This class a full arg-constructor.
This class has getters for the two instance variables.
This class implements the reportProblems method as follows: if the numOfWheels is less than RIGHT_NUM_WHEELS , then the number of troubles is equal to the difference in RIGHT_NUM_WHEELS and numOfWheels. If brand is the
This class implements the getFixed method as follows: If numOfwheels is less than theRIGHT_NUM_WHEELS, then add the required number of wheels. If the brand is the PROBLEMATIC_BRAND , then change the brand to the GOOD_BRAND.
D) Create a class called Repair that contains the main method. Ask the user if they need to service something. If the user doesn’t want to service anything, then exit the program .
If the user chooses to service then ask if it is a 1) Computer or 2) a Car. If the user chooses 1) Computer then ask for each value of the instance variables. Take these values and create a Computer object. Then call the reportProblems method and obtain the number of problems. If the num of troubles is more than zero , then call the getFixed method for this computer object.
If the user chooses to service a Car, then ask the user for the values of the car’s instance variables one by one. Take these values and create a Car object. The call the reportProblems method and obtain the number of troubles. Then call the getFixed method to get the troubles fixed.
Explanation / Answer
import java.util.*;
interface Mechanism //interface
{
public int reportProblems();
public void getFixed();
}
class Computer
{
public static final double MIN_REQUIRED_SPEED = 2.0;
public static final double MIN_REQUIRED_CAPACITY = 1.0;
private double processorSpeed;
private double memoryCapacity;
private boolean networked;
public Computer(double processorSpeed,double memoryCapacity,boolean networked)//argument constructor
{
this.processorSpeed = processorSpeed;
this.memoryCapacity = memoryCapacity;
this.networked = networked;
}
//get methods
public double getProcessorSpeed()
{
return processorSpeed;
}
public double getMemoryCapacity()
{
return memoryCapacity;
}
public boolean isNetworked()
{
return networked;
}
public void changeProcessor(double processorSpeed)
{
this.processorSpeed = processorSpeed;
}
public void addMemoryCapacity(double memoryCapacity)
{
this.memoryCapacity = memoryCapacity;
}
public void connect()
{
networked = true;
}
public void disconnect()
{
networked = false;
}
//implement interface methods
public int reportProblems()
{
int problems = 0;
if(processorSpeed < MIN_REQUIRED_SPEED)
problems++;
if(memoryCapacity < MIN_REQUIRED_CAPACITY)
problems++;
return problems;
}
public void getFixed()
{
if(processorSpeed < MIN_REQUIRED_SPEED)
processorSpeed += (MIN_REQUIRED_SPEED - processorSpeed);
if(memoryCapacity < MIN_REQUIRED_CAPACITY)
memoryCapacity += (MIN_REQUIRED_CAPACITY - memoryCapacity);
connect();
}
}
class Car
{
public static final int RIGHT_NUM_OF_WHEELS = 4;
public static final String PROBLEMATIC_BRAND = "Daewoo";
public static final String GOOD_BRAND = "BMW";
private String brand;
private int numOfWheels;
public Car(String brand,int numOfWheels)//argument constructor
{
this.brand = brand;
this.numOfWheels = numOfWheels;
}
//get methods
public String getBrand()
{
return brand;
}
public int getNumOfWheels()
{
return numOfWheels;
}
//implement interface methods
public int reportProblems()
{
int troubles = 0;
if(numOfWheels < RIGHT_NUM_OF_WHEELS)
troubles = (RIGHT_NUM_OF_WHEELS - numOfWheels);
return troubles;
}
public void getFixed()
{
if(numOfWheels < RIGHT_NUM_OF_WHEELS)
numOfWheels += (RIGHT_NUM_OF_WHEELS - numOfWheels);
if(brand == PROBLEMATIC_BRAND)
brand = GOOD_BRAND;
}
}
class Repair
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int option;
int problems = 0;
System.out.println("Do you need to service something? <y/n>");
String answer = input.next();
if(answer == "n")
System.exit(0);
System.out.println("1) Computer or 2) a Car");
option = input.nextInt();
switch(option)
{
case 1: System.out.println("Enter processor Speed : ");
double processorSpeed = input.nextDouble();
System.out.println("Enter memory capacity : ");
double memoryCapacity = input.nextDouble();
System.out.println("Is the computer networked : ");
boolean networked = input.nextBoolean();
Computer comp = new Computer(processorSpeed,memoryCapacity,networked);
problems = comp.reportProblems();
System.out.println("Number of problems : "+problems);
if(problems > 0)
comp.getFixed();
System.out.println("your computer is fixed");
System.out.println("new processor speed : "+comp.getProcessorSpeed());
System.out.println("new memory capacity : "+comp.getMemoryCapacity());
break;
case 2: System.out.println("Enter the brand of the car : ");
String brand = input.next();
System.out.println("Enter the number of wheels of the car : ");
int numOfWheels = input.nextInt();
Car car = new Car(brand,numOfWheels);
problems = car.reportProblems();
System.out.println("Number of problems : "+problems);
if(problems > 0)
car.getFixed();
System.out.println("your computer is fixed");
System.out.println("new brand of the car : "+car.getBrand());
System.out.println("new number of wheels of the car : "+ car.getNumOfWheels());
break;
default: System.out.println("Invalid option ");
break;
}
}
}
output:
Do you need to service something? <y/n>y
1) Computer or 2) a Car 1
Enter processor Speed :1.0
Enter memory capacity :0.067
Is the computer networked :false
Number of problems : 2
your computer is fixed
new processor speed : 2.0
new memory capacity : 1.0
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.