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

For all cars sold we will be using a Linked List to deal with these. You should

ID: 3567542 • Letter: F

Question

For all cars sold we will be using a Linked List to deal with these. You should be able to place a new item in the linked list and remove an item and display all the items in the linked list. The car object should hold the name and make of the car as well as list price, price sold for, commision made, color of the car and any extras that were added to the vehicle as well as new or used, mileage, and year of the vehicle.

I have this for now but it wont work...

class Node(){

   String name;

   String make;           // you can add more specifications to it //

   String colour;

   int ListPrice;

   int PriceSold;

   int Commission;

   Node next;

   public Node(int x, int y, int z){

       ListPrice = x;

       PriceSold = y;

       Commission = z;

       next = null;

   }

}

Node head;

public void add(Node new){           // for adding a car to it //

   new.next = head;

   head = new;

}

public Node remove(){               // for removing a car from it //

   Node temp = new Node();

   temp = head.next;

   head.next = null;

   head= temp;

}

public void display(Node head){           // for display//

   while(head!=null){

       System.out.println(head.ListPrice);           // you can display any specification of the car by calling head.specification //

       head = head.next;

   }

}

Explanation / Answer

public class CarBN

{

private String make;
private String model;
private int year;
private double odometer;
private double gallons;
private double mpg;

//constants
private final int SPACER_WIDTH = 40;
private final char SPACER_CHAR = '-';
private final double TOL = 0.0001;

//Default constructor
public CarBN()

{
make = "CarBN Make";
model = "CarBN Model";
year = 1950;
odometer = 1.0;
gallons = 1.0;

mpg = calculateMpg();
}

//Full Constructor
public CarBN(String make, String model, int year, double odometer, double gallons) {
this();
  
this.make = make;
this.model = model;
setYear(year);
setOdometer(odometer);
setGallons(gallons);
  
mpg = calculateMpg();
}

//alternate constructor
public CarBN(String make, String model, int year) {
this();
  
this.make = make;
this.model = model;
setYear(year);
  
mpg = calculateMpg();
}


// accessors, mutators
  
// make
public String getMake() {
return this.make;
}

public void setMake(String make) {
this.make = make;
}

// model
public String getModel() {
return this.model;
}

public void setModel(String model) {
this.model = model;
}

//year
public int getYear() {
return this.year;
}

public void setYear(int year) {
this.year = year;
}

//odometer
public double getOdometer() {
return this.odometer;
}

public void setOdometer(double odometer) {
this.odometer = odometer;
mpg = calculateMpg();
}

//gallons
public double getGallons() {
return this.gallons;
}

public void setGallons(double gallons) {
this.gallons = gallons;
mpg = calculateMpg();
}


//MPG
public double getMpg() {
return this.mpg;
}


// display methods
  
// returns a String representation
public String toString() {
return make + ", " +
model + ", " +
year + ", " +
odometer + ", " +
gallons + ", " +
mpg;

}


//print fields
public void print() {
System.out.println("make: " + make);
System.out.println("model: " + model);
System.out.println("year: " + year);
System.out.println("odom: " + odometer);
System.out.println("gals: " + gallons);
System.out.println("mpg: " + mpg);
}

//print fields with header
public void print(String msg) {
  
spacer();
System.out.println(msg);
spacer();
print();
spacer();
System.out.println();
}
  
// prints a spacer line of a certain character
private void spacer(int width, char ch) {
for (int i=0; i<width; i++) {
System.out.print(ch);
}
  
System.out.println();
}
  
// prints a default spacer line
private void spacer() {
spacer(SPACER_WIDTH, SPACER_CHAR);
}


//take a trip
public void takeTrip(double miles) {
odometer += miles;
  
mpg = calculateMpg();
}

public void takeTrip(int avgSpeed, double hrs) {
odometer += (avgSpeed * hrs);
  
mpg = calculateMpg();
}


//add gas to car
public void addGas(double galsAdded) {
gallons += galsAdded;
  
mpg = calculateMpg();
}

//calculate the MPG
private double calculateMpg() {
double mpg;
if (gallons == 0) {
gallons = 1;
System.out.println("Error, gallons cannot equal 0, defaulting to 1");
}
  
mpg = odometer/gallons;
  
return mpg;
}


//check if two objects equal each other
public boolean equals(Object obj) {
boolean status;
  
if (obj instanceof CarBN) {
  
//Cast to car object
CarBN c = (CarBN) obj;
  
// check field by field
status = ( this.make.equals(c.getMake()) ) &&
( this.model.equals(c.getModel()) ) &&
( this.year == c.getYear() ) &&
( Math.abs(this.odometer - c.getOdometer()) <= TOL ) &&
( Math.abs(this.gallons - c.getGallons()) <= TOL ) &&
( Math.abs(this.mpg - c.getMpg()) <= TOL );

if (status) {
return true;
}
else {
return false;
}
  
}
  
else {
//obj is not car
return false;
}

}

//Tester for Class
public static void main(String [] args) {
CarBN car1 = new CarBN();
CarBN car2 = new CarBN("Ford", "F-150", 2010, 45000.23, 20.2);
CarBN car3 = new CarBN("Acura", "RSX", 2006);
  
//Display objects
System.out.println(car1);
car1.print("Default Constructor");
System.out.println(car2);
car2.print("Full Constructor");
System.out.println(car3);
car3.print("Alternate Constructor");
  
  
//Test mutators
car1.print("CarBN1 Before:");
car1.setMake("Porsche");
car1.setModel("CarBNrerra");
car1.setYear(2014);
car1.setOdometer(1234.56);
car1.setGallons(15.5);
car1.print("CarBN1 After:");
  
//Test accessors
System.out.println("Individual Fields:");
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getYear());
System.out.println(car1.getOdometer());
System.out.println(car1.getGallons());
System.out.println(car1.getMpg());
System.out.println();
  
//Test utility-methods
System.out.println("Add 5 gals to car1:");
car1.addGas(5);
System.out.println(car1.getGallons());
System.out.println();
  
System.out.println("Take a trip for 200 miles:");
car1.takeTrip(200);
System.out.println(car1.getOdometer());
System.out.println();
  
System.out.println("Take a trip at 30 mph for 2.5 hours");
car1.takeTrip(30, 2.5);
System.out.println(car1.getOdometer());
  
System.out.println(car1.equals(car1));
System.out.println(car1.equals(car2));
System.out.println(car1.equals(car3));
System.out.println(car3.equals(car3));
  
}
}

import java.text.DecimalFormat; // for display formats

public class CarBN {

// instance variables

private String make;
private String model;
private int year;
private double odometer;
private double gallons;
private double mpg;
  
//constants
private final int SPACER_WIDTH = 40;
private final char SPACER_CHAR = '-';
private final double TOL = 0.0001;
private final int MIN_YEAR = 1920;
private final int MAX_YEAR = 2150;
  
// format constants
private final DecimalFormat GAL_FMT = new DecimalFormat("###,##0.000");
private final DecimalFormat ODM_FMT = new DecimalFormat("###,##0.0");
private final DecimalFormat MPG_FMT = new DecimalFormat("###,##0.00");

  
// constructors
  
//Default constructor
public CarBN() {
make = "Car Make";
model = "Car Model";
year = 1950;
odometer = 1.0;
gallons = 1.0;

mpg = calculateMpg();
}

//Full Constructor
public CarBN(String make, String model, int year, double odometer, double gallons) {
this();
  
this.make = make;
this.model = model;
setYear(year);
setOdometer(odometer);
setGallons(gallons);
  
mpg = calculateMpg();
}

//alternate constructor
public CarBN(String make, String model, int year) {
this();
  
this.make = make;
this.model = model;
setYear(year);
  
mpg = calculateMpg();
}

  
// accessors, mutators


public String getMake() {
return this.make;
}

public void setMake(String make) {
this.make = make;
}
  
// model
public String getModel()

{
return this.model;
}

public void setModel(String model)

{
this.model = model;
}

//year
public int getYear() {
return this.year;
}

public void setYear(int year) {
if (year >= MIN_YEAR && year <= MAX_YEAR) {
this.year = year;
}
else {
System.out.println("Invalid Year, please set correct year [" + MIN_YEAR + "-" + MAX_YEAR + "]");
}
}

//odometer
public double getOdometer() {
return this.odometer;
}

public void setOdometer(double odometer) {
if (odometer >= 0) {
this.odometer = odometer;
mpg = calculateMpg();
}
else {
System.out.println("Invalid Odometer. Odometer must be >= 0");
}
}

//gallons
public double getGallons() {
return this.gallons;
}

public void setGallons(double gallons) {
if (gallons > 0) {
this.gallons = gallons;
mpg = calculateMpg();
}
else {
System.out.println("Invalid Gallons. Gallons must be > 0");
}
}

  
//MPG
public double getMpg() {
return this.mpg;
}

  

// returns a String representation
public String toString() {
return make + ", " +
model + ", " +
year + ", " +
ODM_FMT.format(odometer) + ", " +
GAL_FMT.format(gallons) + ", " +
MPG_FMT.format(mpg);

}

  
//print fields
public void print() {
System.out.println("make: " + make);
System.out.println("model: " + model);
System.out.println("year: " + year);
System.out.println("odom: " + ODM_FMT.format(odometer));
System.out.println("gals: " + GAL_FMT.format(gallons));
System.out.println("mpg: " + MPG_FMT.format(mpg));
}

//print fields with header
public void print(String msg) {
  
spacer();
System.out.println(msg);
spacer();
print();
spacer();
System.out.println();
}

// prints a spacer line of a certain character
private void spacer(int width, char ch) {
for (int i=0; i<width; i++) {
System.out.print(ch);
}
  
System.out.println();
}

// prints a default spacer line
private void spacer() {
spacer(SPACER_WIDTH, SPACER_CHAR);
}

  
//take a trip
public void takeTrip(double miles) {
odometer += miles;
  
mpg = calculateMpg();
}

public void takeTrip(int avgSpeed, double hrs) {
odometer += (avgSpeed * hrs);
  
mpg = calculateMpg();
}

  
//add gas to car
public void addGas(double galsAdded) {
gallons += galsAdded;
  
mpg = calculateMpg();
}
  
//calculate the MPG
private double calculateMpg() {
double mpg;
if (gallons == 0) { //catch division by 0
gallons = 1;
System.out.println("Error, gallons cannot equal 0, defaulting to 1");
}
  
mpg = odometer/gallons;
  
return mpg;
}


//check if two objects equal each other
public boolean equals(Object obj) {
boolean status;
  
if (obj instanceof CarBN) {
  
//Cast to car object
CarBN c = (CarBN) obj;
  
// check field by field
status = ( this.make.equals(c.getMake()) ) &&
( this.model.equals(c.getModel()) ) &&
( this.year == c.getYear() ) &&
( Math.abs(this.odometer - c.getOdometer()) <= TOL ) &&
( Math.abs(this.gallons - c.getGallons()) <= TOL ) &&
( Math.abs(this.mpg - c.getMpg()) <= TOL );
  
if (status) {
return true;
}
else {
return false;
}
  
}
  
else {
//obj is not car
return false;
}

}

//Tester for Class
public static void main(String [] args) {
CarBN car1 = new CarBN();
CarBN car2 = new CarBN("Ford", "F-150", 2010, 45000.23, 20.2);
CarBN car3 = new CarBN("Acura", "RSX", 2006);
  
//Display objects
System.out.println(car1);
car1.print("Default Constructor");
System.out.println(car2);
car2.print("Full Constructor");
System.out.println(car3);
car3.print("Alternate Constructor");
  
  
//Test mutators
car1.print("Car1 Before:");
car1.setMake("Porsche");
car1.setModel("Carrerra");
car1.setYear(2014);
car1.setOdometer(1234.56);
car1.setGallons(15.5);
car1.print("Car1 After:");
  
//Test accessors
System.out.println("Individual Fields:");
System.out.println(car1.getMake());
System.out.println(car1.getModel());
System.out.println(car1.getYear());
System.out.println(car1.getOdometer());
System.out.println(car1.getGallons());
System.out.println(car1.getMpg());
System.out.println();
  
//Test utility-methods
car1.print("Utility Classes Test: Car1 Before:");
  
System.out.println("Add 5 gals to car1:");
car1.addGas(5);
System.out.println(car1.getGallons());
System.out.println();
  
System.out.println("Take a trip for 200 miles:");
car1.takeTrip(200);
System.out.println(car1.getOdometer());
System.out.println();
  
System.out.println("Take a trip at 30 mph for 2.5 hours");
car1.takeTrip(30, 2.5);
System.out.println(car1.getOdometer());
  
car1.print("Utility Classes Test: Car1 After:");
  
//Test equals
System.out.println("car1 = car1? " + car1.equals(car1));
System.out.println("car1 = car2? " + car1.equals(car2));
System.out.println("car1 = car3? " + car1.equals(car3));
System.out.println("car3 = car3? " + car3.equals(car3));
  
}
}

////////

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