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

Create a project: FinalEXam Create a package: finalexam Write an automobile clas

ID: 3579652 • Letter: C

Question

Create a project: FinalEXam

Create a package: finalexam

Write an automobile class, Automobile, that contains an odometer (integer) and a tripmeter (integer) as a minimum. You may need other variables as well.

Include a default constructor that inititalizes all instance variables.

Include getters and setters for each instance variable.

Overload the constructor to accept a tripmeter value.

Include an initializeTrip method that resets the trip meter.

Include a startTrip method that gets the odometer reading and starts the tripmeter.

Include a stopTrip method that sends how many miles were traveled on a trip.

You must be able to start and stop the trip during the session.

Save the odometer value in a file so when the next trip is started you begin with the last saved value.

Read the odometer value each time the program starts.

The file name must be odometerReading.txt and contain 1100 (for 1100 miles).initially.

The second file name must be tripmeter.txt and contain 0 (for 0 miles) initially

Overload the toString method to return the original odometer reading, the trip meter reading and the final odometer reading.

Main will be provided below and may not be changed. (Main) that uses and verifies that all methods of the Automobile class work correctly.

Explanation / Answer

import java.util.*;

import java.io.*;

class Automobile

{

int odometer, tripmeter;

Automobile()

{

odometer=0;

tripmeter=0;

}

Automobile(int o, int t)

{

odometer=o;

tripmeter=t;

}

void setOdometer(int o)

{

odometer=o;

}

void setTripmeter(int t)

{

tripmeter=t;

}

int getOdometer()

{

return odometer;

}

int getTripmeter()

{

return tripmeter;

}

void initializeTrip()

{

tripmeter=0;

}

int startTrip()

{

return odometer;

}

void stopTrip(int t)

{

odometer+=t;

tripmeter=t;

}

public String toString()

{

return "Odometer reading: "+odometer+" Tripmeter Reading: "+tripmeter;

}

public static void main(String s[])

{

try

{

Scanner sc= new Scanner(new File("odometerReading.txt"));

int o= sc.nextInt();

sc.close();

sc= new Scanner(new File("tripmeter.txt"));

int t= sc.nextInt();  

Automobile auto = new Automobile();

if(o>0) auto.setOdometer(o);

if(t>=0) auto.setTripmeter(t);

System.out.println(auto);

auto.initializeTrip();

auto.startTrip();

auto.stopTrip(150);

auto.startTrip();

auto.stopTrip(140);

auto.startTrip();

auto.stopTrip(130);

auto.startTrip();

auto.stopTrip(125);

System.out.println(auto);

///////////////Writing details to files/////////////

Writer wr= new FileWriter("odometerReading.txt");

wr.write(String.valueOf(auto.getOdometer())) ;

wr.flush();

wr.close();

wr= new FileWriter("tripmeter.txt");

wr.write(String.valueOf(auto.getTripmeter())) ;

wr.flush();

wr.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote