Question

Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the...

Use an accessor and mutator as follows:

  • Remove the addFuel method from class Car
  • Replace the call to addFuel in the main program with a call to the accessor and mutator for the fuel amount to achieve the same effect (add 5 gallons of fuel to the existing fuel in the Toyota Camry).
  • Test your program again to make sure it works and produces the same output.

Part 2: Add functions to remove duplicate code

* In the main program, there should be 2 places where you are calling printInfo()all 3 cars in row. Define and call a function named printAllCarsInfo() in the main program class to call printInfo() for all 3 cars, and replace the 2 uses with function calls.

* Similarly, in the main program there should be 2 places where you are driving all 3 cars for a certain number of miles. Define and use a function to replace these with function calls. Hint: Your function will need to take a parameter.

Test your program again to make sure it still produces the same output.

Main program:

import java.util.Scanner;

class MainProgram
{
static Car honda, toyota, ford;
public static void main(String[] args)
{
System.out.println("Program 12");

Scanner in=new Scanner(System.in);

honda=new Car("Honda Civic",3000);
toyota=new Car("Toyota Camry",3400);
ford=new Car(" Ford F-150",5000);

honda.drive(10);
toyota.addFuel(5);
toyota.drive(10);
ford.drive(10);

honda.printInfo();
toyota.printInfo();
ford.printInfo();

System.out.println("How many miles is your roadtrip?");

int miles=in.nextInt();

honda.drive(miles);
toyota.drive(miles);
ford.drive(miles);
honda.printInfo();
toyota.printInfo();
ford.printInfo();
}
}

  


Class Car:

public class Car
{
  
private String model;
private double mpg;
private double milesDriven;
private double fuelGallons;
  
Car (String carModel, double weight)
{


  
model = carModel;
milesDriven = 7;
fuelGallons = 15;
if (weight>4000)
mpg = 20;
else
mpg = 30;


}
void addFuel(int noOfGallons)
{
fuelGallons += noOfGallons;
}
double milesLeft()
{
  
return mpg*fuelGallons;
}
void drive(int noOfMiles)
{
  
if (noOfMiles>milesLeft())
{
milesDriven+=milesLeft();
fuelGallons-=milesLeft()/mpg;
}
else
{
milesDriven+=noOfMiles;
  
fuelGallons-=noOfMiles/mpg;
}
  

  
}
void printInfo()
{

if(fuelGallons<=0)
{
  
System.out.println("The " + model + "has driven "+milesDriven+"miles and");
System.out.println("is out of gas");
}
else if(fuelGallons>=0)
{
System.out.println("The"+" "+ model +"has driven"+" "+milesDriven+"miles and");
System.out.println(" "+model+" "+"has"+" "+String.format("%.1f",fuelGallons)+" gallons left");
}
}
public String getModel()
{
return model;
}
public double getMpg()
{
return mpg;
}
public double getMilesDriven()
{
return milesDriven;
}
public double getFuelGallons()
{
return fuelGallons;
}
}

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

thanks for the question, here is the updated code, changes are marked and commented. Let me know for any changes or modifications.

===============================================================================================

import java.util.Scanner;



public class MainProgram {



    static Car honda,toyota,ford;



    public static void main(String[] args) {



        System.out.println("Program 12");

        Scanner in = new Scanner(System.in);



        honda =new Car("Honda Civic",3000);

        toyota =new Car("Toyota Camry",3400);

        ford=new Car("Ford F-150",5000);



        honda.drive(10);

        double fuel = toyota.getFuelGallons(); // updated

        toyota.setFuelGallons(fuel+5); // updated

        toyota.drive(10);

        ford.drive(10);



        printAllCarsInfo(honda,toyota,ford); // updated



        System.out.println("How many miles is your roadtrip?");

        int miles = in.nextInt();

        honda.drive(miles);

        toyota.drive(miles);

        ford.drive(miles);



        printAllCarsInfo(honda,toyota,ford); // updated



    }



    // new method added

    public static void printAllCarsInfo(Car car1, Car car2, Car car3){

        car1.printInfo();

        car2.printInfo();

        car3.printInfo();

    }

}
public class Car {



    private String model;

    private double mpg;

    private double milesDriven;

    private double fuelGallons;



    Car(String carModel, double weight) {

        model=carModel;

        milesDriven=7;

        fuelGallons=15;

        if(weight>4000){

            mpg=20;

        }else{

            mpg=30;

        }

    }


    // updated code
    public void setFuelGallons(double fuelGallons) {

        this.fuelGallons = fuelGallons;

    }



    double milesLeft(){

        return mpg*fuelGallons;

    }



    void drive(int noOfMiles){

        if(noOfMiles>milesLeft()){

            fuelGallons-=milesLeft()/mpg;

        }else{

            milesDriven+=noOfMiles;

            fuelGallons-=noOfMiles/mpg;

        }

    }



    void printInfo(){

        if(fuelGallons>=0){

            System.out.println("The "+model+" has driven "+milesDriven+" miles and ");

            System.out.println(" "+model+" has "+String.format("%.1f",fuelGallons)+" gallons left");

        }

    }



    public String getModel() {

        return model;

    }



    public double getMpg() {

        return mpg;

    }



    public double getMilesDriven() {

        return milesDriven;

    }



    public double getFuelGallons() {

        return fuelGallons;

    }

}
Add a comment
Know the answer?
Add Answer to:
Use an accessor and mutator as follows: Remove the addFuel method from class Car Replace the...
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
  • Below are the Car class and Dealership class that I created In the previous lab import...

    Below are the Car class and Dealership class that I created In the previous lab import java.util.ArrayList; class Car { private String make; private String model; private int year; private double transmission; private int seats; private int maxSpeed; private int wheels; private String type; public Car() { } public Car(String make, String model, int year, double transmission, int seats, int maxSpeed, int wheels, String type) { this.make = make; this.model = model; this.year = year; this.transmission = transmission; this.seats =...

  • please help me add on this java code to run public class CarHwMain public static void...

    please help me add on this java code to run public class CarHwMain public static void main(String args 1/ Construct two new cars and one used car for the simulation Cari carl = new Car W"My New Mazda", 24.5, 16.0); Car car2 = new Cart My New Ford" 20.5, 15.0) Cari car) - new Cari ("My Used Caddie", 15.5, 16.5, 5.5, 1/ ADD CODE to completely fill the tanks in the new cars and off the used cars ton //...

  • Sports car or convertible? The following table presents the fuel efficiency, in miles per gallon, for...

    Sports car or convertible? The following table presents the fuel efficiency, in miles per gallon, for a sample of convertibles and a sample of sports cars. Convertible Model MPG Sports Model MPG BMW 328i Mitsubishi Lancer Evolution Toyota Camry Solara Ford Mustang V6 Volkswagen Eos MINI Cooper Saab 9-3 Volkswagen GTI Honda Civic Si BMW 135i 23 Mazda Mazdaspeed Subaru Impreza WRX STI 24 Send date to Excel Part 1 of 2 Part 2 of 2 (b) Find the sample...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • What is output? public abstract class People { protected string name; protected int age; public abstract...

    What is output? public abstract class People { protected string name; protected int age; public abstract void PrintInfo(); public void PrintInformation() { System.out.println("In Base Class People"); public class Teacher extends People { private int experience; public void PrintInfo() { System.out.println("In Child Class Teacher"); public class Principal extends Teacher { public void PrintInformation() { System.out.println("In Child Class Principal"); public static void main(String args[]) { Principal tim; tim = new Principal(); tim.PrintInfo(); In Base Class People Error: Compiler error In Child Class...

  • Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for...

    Java Programming --- complete the given classes DeLand Space Car Dealer needs a new program for their inventory management system. The program needs the following classes: A main class (SpaceCarDealer.java) that includes the main method and GUI. Two instantiable classes: CarDealer.java o Variables to store the dealer name, and the carsOnLot array in Car type. o A constructor to initialize the name variable with the given dealer name. o An accessor method to return the name of the dealer. o...

  • Use BlueJ to write a program that reads a sequence of data for several car objects...

    Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info for any number of cars. (You should not assume that it will always be seven lines in the input file). Use notepad to create input file "inData.txt". File should be stored in the same folder where all files from BlueJ for this program...

  • *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J...

    *** FOR A BEGINNER LEVEL JAVA CLASS, PLEASE KEEP CODE SIMPLE, AND WE USE BLUE J IN CLASS (IF IT DOESNT MATTER PLEASE COMMENT TELLING WHICH PROGRAM YOU USED TO WRITE THE CODE, PREFERRED IS BLUE J!!)*** ArrayList of Objects and Input File Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an ArrayList<Car> list . Program should work for input file containing info...

  • How to write a program that create a car class with: instance variables name and mpg...

    How to write a program that create a car class with: instance variables name and mpg (make public) No need for a main() method In a separate file, create a CompareCars class with a main()method create 2 instances of car using new set the name and mpg for each instance variables are public, so use the name an mpg to access the instance variables compare the fuel efficiency of your 2 cars and print out which is more efficient to...

  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

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