Question

7. Programming Problem: In this problem we are trying to model a repair service that can repair Cars and Computers. Even thouIf the memsxSapacitw is less than the minimum required capacity then add the difference in capacity. Then, connect the comput8. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the prThen ask if the user wants to repair a computer. If yes, then ask for values of the computers parameters and create a new co

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Que 7)

Ans

Mechanism.java interface


public interface Mechanism
{
int reportProblems();
void getFixed();
}

Computer.java class


public class Computer implements Mechanism
{
public static final double MIN_REQUIRED_SPEED=2.0;
public static final double MIN_REQUIRED_CAPACITY=1.0;
private double processorSpeed;
private double memoryCapcity;
private boolean networked;

public Computer(double processorSpeed, double memoryCapcity, boolean networked) {
this.processorSpeed = processorSpeed;
this.memoryCapcity = memoryCapcity;
this.networked = networked;
}

public double getProcessorSpeed() {
return processorSpeed;
}

public double getMemoryCapcity() {
return memoryCapcity;
}

public boolean isNetworked() {
return networked;
}
  
public void changeProcessor(double newSpeed)
{
this.processorSpeed=newSpeed;
}
public void addMemoryCapacity(double newMemory)
{
this.memoryCapcity=newMemory;
}
public void connect(boolean mode)
{
this.networked=mode;
}  
@Override
public int reportProblems() {
int problemsCount=0;
if(processorSpeed<MIN_REQUIRED_SPEED)
problemsCount++;
if(memoryCapcity<MIN_REQUIRED_CAPACITY)
problemsCount++;
if(!isNetworked())
problemsCount++;
return problemsCount;
}

@Override
public void getFixed()
{
if(processorSpeed<MIN_REQUIRED_SPEED)
{
changeProcessor(processorSpeed+(MIN_REQUIRED_CAPACITY-MIN_REQUIRED_SPEED));
}
if(memoryCapcity<MIN_REQUIRED_CAPACITY)
{
addMemoryCapacity(memoryCapcity+(MIN_REQUIRED_CAPACITY-memoryCapcity));
}
if(!isNetworked())
connect(true);
}
}

Car.java


public class Car implements Mechanism
{
public static final int RIGHT_NUM_WHEELS=4;
public static final String PROBLEMATIC_BRAND = "Daewoo";
public static final String GOOD_BRAND="BW";
  
private String brand;
private int numOfWheels;

public Car(String brand, int numOfWheels) {
this.brand = brand;
this.numOfWheels = numOfWheels;
}

public String getBrand() {
return brand;
}

public int getNumOfWheels() {
return numOfWheels;
}

@Override
public int reportProblems() {
int troubles=0;
if(numOfWheels<RIGHT_NUM_WHEELS)
troubles=RIGHT_NUM_WHEELS-numOfWheels;
if(brand==PROBLEMATIC_BRAND)
troubles++;
return troubles;
}
  

@Override
public void getFixed()
{
if(numOfWheels<RIGHT_NUM_WHEELS)
{
numOfWheels+=RIGHT_NUM_WHEELS-numOfWheels;
}
if(brand==PROBLEMATIC_BRAND)
brand=GOOD_BRAND;
}
  
}

Repair.java

import java.util.Scanner;
public class Repair
{
public static void main(String[] args)
{
String userAns;
Scanner sc=new Scanner(System.in);
System.out.print("If you need to service somethind? (y/n) :");
userAns=sc.next();
if(userAns.equalsIgnoreCase("N"))
System.exit(0);
System.out.println("\n\n1) Computer\n2) Car");
System.out.print("\nYour choice: ");
userAns=sc.next();
if(userAns.equals("1"))
{
boolean network=false;
System.out.print("\nyEnter the the Processor's Speed: ");
double speed=sc.nextDouble();
System.out.print("Enter memory capacity: ");
double capacity=sc.nextDouble();
System.out.print("If you computer is networked ?(y/n):");
String net=sc.next();
if(net.equalsIgnoreCase("Y"))
network=true;
Computer cp1=new Computer(speed, capacity, network);
if(cp1.reportProblems()>0)
{
System.out.println("\nYour computer has "+cp1.reportProblems()+" problmes and its fixed.");
cp1.getFixed();
}
else
System.out.println("\nYour computer has no problems");
}
if(userAns.equals("2"))
{
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 c1=new Car(brand, numWheels);
if(c1.reportProblems()>0)
{
System.out.println("\nYour car has "+c1.reportProblems()+" problems and its fixed.");
c1.getFixed();
}
else
{
System.out.println("\nYour car has no problems");
}
}
}
}

outputs

Mechanism Interface- NetBeans IDE 8.0.1 File Edit View Navigate Source Refactor Ru Debug Profile Team Iools Window Help SearcMechanism Interface- NetBeans IDE 8.0.1 File Edit View Navigate Source Refactor Ru Debug Profile Team Iools Window Help Searc

que 8

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

Mechanism Interface - NetBeans IDE 8.0.1 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window Help SeaIf you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
7. Programming Problem: In this problem we are trying to model a repair service that can...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Programming assignment for Java: Do not add any other instance variables to any class, but you...

    Programming assignment for Java: Do not add any other instance variables to any class, but you can create local variables in a method to accomplish tasks. Do not create any methods other than the ones listed below. Step 1 Develop the following class: Class Name: College Degree Access Modifier: public Instance variables Name: major Access modifier: private Data type: String Name: numberOfCourses Access modifier: private Data type: int Name: courseNameArray Access modifier: private Data type: String Name: courseCreditArray Access modifier:...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to...

    Status Topic Interfaces Description Video Scene Problem Consider a video scene in which we want to display several different types (classes) of objects. Let's say, we want to display three objects of type Deer, two objects of type Tree and one object of type Hill. Each of them contains a method called display. We would like to store their object references in a single array and then call their method display one by one in a loop. However, in Java,...

  • Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two...

    Lab 7 Add three instance variables to your class RightTriangle from lab 5. The first two are of type int and are called xLoc and yLoc and the third is of type int called ID. Also add a static variable of type double called scaleFactor. This should be initialized to a default value of 1. Update the constructor to set the three new instance variables and add appropriate get and set methods for the four new variables. All set methods...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

  • Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions...

    Needs Help In Java Programming language Exceptional Cars Purpose To review interfaces and exception usage. Directions Your task is to write a class called Car. Your class should have the following fields and methods: private int position private boolean headlightsOn public Car() - a constructor which initializes position to 0 and headlightsOn to false public Car(int position) - a constructor which initializes position to the passed in value and headlightsOn to false, and it should throw a NegativeNumberException with a...

  • (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming...

    (Java with Netbeans) Programming. Please show modified programming code for already given Main class, and programming for code for the new GameChanger class. // the code for Main class for step number 6 has already been given, please show modified progrraming for Main class and GameCHanger class, thank you. package .games; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String args[]) { System.out.println("Welcome to the Number Guessing Game"); System.out.println(); Scanner sc = new Scanner(System.in); // Get upper...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT