Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

\'m in java one and need help with this code. please follow the directions given

ID: 3690203 • Letter: #

Question

'm in java one and need help with this code. please follow the directions given and test your program to make sure it works as it is required. I will apreciate your help. thank you.please use the given code as it is just fill the missing information to complete the program. thank you

here is the code   

cp ~lkoperski/1404_Lab/Exercises/Exercise13.java .

Programming Concepts

1. Classes

2. Objects

Exercise Location

For this exercise, you will copy a program from my directory into yours. It doesn't matter what directory this exercise is located in. You will need to copy this command and paste it into Loki:

cp ~lkoperski/1404_Lab/Exercises/Exercise13.java .

Exercise Description

You will be implementing a Car object. The Exercise13.java file is the driver file that creates an instance of the Car object, and your job is to create a file called Car.java, given the following UML diagram.

- make : String
- model : String
- fuel : Integer
- miles : Integer
- lightsOn : Boolean

+ << Constructor >> Car ( ma : String, mo : String, f : Integer, mi : Integer, l : Boolean )

+ drive ( mi : Integer )
+ getMake ( ) : String
+ getModel ( ) : String
+ getFuel ( ) : Integer
+ getMiles ( ) : Integer
+ getLightsOn ( ) : Boolean + setMake ( ma : String )

+ setModel ( mo : String )
+ setMiles ( mi : String )
+ setFuel ( f : Integer )
+ setLightsOn ( l : Boolean )

To compile the program, you can issue the command javac Exercise13.java, which will compile both the Exercise13.java file and the Car.java file.

Note: The l in setLightsOn() is the lowercase letter l, and not the number 1, or the uppercase letter I.

Note: You will not use all of the methods created, but just keep in mind that getters and setters generally exist for every class variable defined.

Description for the constructor

1. You simply just need to assign the class variables to the variables that were passed as parameters.

Description for the getters and setters

1. For each getter, you just need to return the value. For example, for getFuel(), you just need to return fuel.

2. For each setter, you just need to assign the class variable to the variable that was passed as the parameter. For example, for setFuel ( int f ), you just need to do fuel = f.

Description for the drive() method

1. The implementation of the getters and setters is straightforward. For the drive() method, it takes in the number of miles driven as an argument.

2. The number of miles that was passed to the method needs to be added to the class variable miles.

3. This car will get 30 miles per gallon, meaning, for every 30 miles the car has driven, you need to subtract one gallon of fuel from the class variable fuel.

a. You can use a loop and the modulus operator to determine how many times fuel needs to be decremented.

b. For example, if the number of miles driven is 90, then fuel should decrement by 3. If the number of miles driven is 30, then fuel should decrement by 1. If the number of miles driven is 100, then fuel should decrement by 3. If the number of miles driven is 200, then fuel should decrement by 6.

c. Don't worry about accounting for remainders. Obviously, 200 isn't divisible by 30, but don't take away 2/3rds of a gallon of fuel from the distance of 180 to 200. 30 can go into 200 at least 6 times, but not more, so you should decrement fuel only by 6. We're only dealing with integers.

User input is underlined

user@loki:~$ java Exercise12

Just created a new car!

Make: Ford

Model: T
Fuel: 10
Miles: 0
Lights On: false

About to drive 200 miles.

Make: Ford

Model: T
Fuel: 3
Miles: 200

Lights On: false

It's dark outside! About to turn the lights on.

Make: Ford

Model: T
Fuel: 3
Miles: 200

Lights On: true

About to refuel.

Make: Ford

Model: T
Fuel: 10

Miles: 200

Lights On: true

car

- make : String
- model : String
- fuel : Integer
- miles : Integer
- lightsOn : Boolean

+ << Constructor >> Car ( ma : String, mo : String, f : Integer, mi : Integer, l : Boolean )

+ drive ( mi : Integer )
+ getMake ( ) : String
+ getModel ( ) : String
+ getFuel ( ) : Integer
+ getMiles ( ) : Integer
+ getLightsOn ( ) : Boolean + setMake ( ma : String )

+ setModel ( mo : String )
+ setMiles ( mi : String )
+ setFuel ( f : Integer )
+ setLightsOn ( l : Boolean )

Explanation / Answer

Exercise13.java


public class Exercise13 {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Car c = new Car("Ford", "T", 10, 0, false);
       System.out.println("Just created a new car!");
       for(int fuel=0;fuel < c.getFuel(); fuel++){
       System.out.println("Make : "+c.getMake());
       System.out.println("Model : "+c.getModel());
       System.out.println("Fuel : "+c.getFuel());
       System.out.println("Miles : "+c.getMiles());
       System.out.println("Lights On : "+c.isLightsOn());
       if(fuel == 0){
       c.drive(200);
       System.out.println("About to drive "+c.getMiles()+" miles");
       }
       else if(fuel == 1){
       System.out.println("It's dark outside! About to turn the lights on.");
       c.setLightsOn(true);
       }
       else if(fuel == 2){
       System.out.println("About to refuel.");
      
       c.drive(200);
       c.setFuel(10);
       }
       else{
           break;
       }
      
       }
      
   }

}

Car.java


public class Car {
   private String make;
   private String model;
   private int fuel;
   private int miles;
   private boolean lightsOn;
   public Car(String ma, String mo, int f, int mi, boolean l) {
       this.make = ma;
       this.model = mo;
       this.fuel = f;
       this.miles = mi;
       this.lightsOn = l;
   }
   public String getMake() {
       return make;
   }
   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model = model;
   }
   public int getFuel() {
       return fuel;
   }
   public void setFuel(int fuel) {
       this.fuel = fuel;
   }
   public int getMiles() {
       return miles;
   }
   public void setMiles(int miles) {
       this.miles = miles;
   }
   public boolean isLightsOn() {
       return lightsOn;
   }
   public void setLightsOn(boolean lightsOn) {
       this.lightsOn = lightsOn;
   }
   public void drive(int mi){
       this.miles = mi;
       this.fuel = fuel - (miles/30) - 1;
   }
}

Output:

Just created a new car!
Make : Ford
Model : T
Fuel : 10
Miles : 0
Lights On : false
About to drive 200 miles
Make : Ford
Model : T
Fuel : 3
Miles : 200
Lights On : false
It's dark outside! About to turn the lights on.
Make : Ford
Model : T
Fuel : 3
Miles : 200
Lights On : true
About to refuel.
Make : Ford
Model : T
Fuel : 10
Miles : 200
Lights On : true