Question

public class Car {    /* four private instance variables*/        private String make;   ...

public class Car {

   /* four private instance variables*/
       private String make;
       private String model;
       private int mileage ;
       private int year;
       //
       /* four argument constructor for the instance variables.*/
       public Car(String make) {
           super();
       }
       public Car(String make, String model, int year, int mileage) {
       super();
       this.make = make;
       this.model = model;
       this.year = year;
       this.mileage = mileage;
       /* get method for make.*/
       }
       public String getMake() {
       return make;
       }
       /* set method for make. */
         
       public void setMake(String make) {
       this.make = make;
       }
       /* get method for model.*/
         
       public String getModel() {
       return model;
       }
       /* set method for make. */
         
       public void setModel(String model) {
       this.model = model;
       }
       /* get method for year. */
         
       public int getYear() {
       return year;
       }
       /* set method for year. */
       public void setYear(int year) {
       this.year = year;
       }
       /* get method for mileage. */
       public int getMileage() {
       return mileage;
       }
       /* set method for mileage. */
       public void setMileage(int mileage) {
       this.mileage = mileage;
       }
         
       /* overriding toString method to get the same format*/
       public String toString() {
       return make + "," +model + "," + year + "," + mileage ;
       }

       public int compareTo(Car o) {
  
           }
       }
// need help with this compare to method which will compare make and model and return it in a sorted way, i will use this to sort my linked list.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Car {

    /* four private instance variables*/
    private String make;
    private String model;
    private int mileage;
    private int year;

    //
    /* four argument constructor for the instance variables.*/
    public Car(String make) {
        super();
    }

    public Car(String make, String model, int year, int mileage) {
        super();
        this.make = make;
        this.model = model;
        this.year = year;
        this.mileage = mileage;
        /* get method for make.*/
    }

    public String getMake() {
        return make;
    }
    /* set method for make. */

    public void setMake(String make) {
        this.make = make;
    }
    /* get method for model.*/

    public String getModel() {
        return model;
    }
    /* set method for make. */

    public void setModel(String model) {
        this.model = model;
    }
    /* get method for year. */

    public int getYear() {
        return year;
    }

    /* set method for year. */
    public void setYear(int year) {
        this.year = year;
    }

    /* get method for mileage. */
    public int getMileage() {
        return mileage;
    }

    /* set method for mileage. */
    public void setMileage(int mileage) {
        this.mileage = mileage;
    }

    /* overriding toString method to get the same format*/
    public String toString() {
        return make + "," + model + "," + year + "," + mileage;
    }

    public int compareTo(Car o) {
        int cmp = make.compareTo(o.make);
        if (cmp == 0)
            cmp = model.compareTo(o.model);
        return cmp;
    }
}
Add a comment
Know the answer?
Add Answer to:
public class Car {    /* four private instance variables*/        private String make;   ...
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 =...

  • Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year :...

    Codes: (car.h ; car.cpp ; carDemo.cpp) /* ----------------------- Car ----------------------- - make: string - year : int ----------------------- + Car() + setMake(m: string) : void + getMake() : string + setYear(y: int) : void + getYear() : int ---------------------- */ #ifndef CAR_H #define CAR_H #include <iostream> using namespace std; class Car { private: string make; int year; public: Car(); Car(string); Car(int); Car(string, int); void setMake (string); string getMake() {return make;} void setYear (int); int getYear() {return year;} }; #endif #include...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • You are to create a class Appointment.java that will have the following: 5 Instance variables: private...

    You are to create a class Appointment.java that will have the following: 5 Instance variables: private String month private int day private int year private int hour private int minute 1 default constructor public Appointment() 1 non-default constructor that accepts arguments for all instance variables, your constructor must call the setter methods below to set the values of the instance variables public Appointment(String monthPassed, int dayPassed, int yearPassed, int hourPassed, int minutePassed) 5 setter methods (one for each instance variable)...

  • Consider the Automobile class: public class Automobile {    private String model;    private int rating;...

    Consider the Automobile class: public class Automobile {    private String model;    private int rating; // a number 1, 2, 3, 4, 5    private boolean isTruck;    public Automobile(String model, boolean isTruck) {       this.model = model;       this.rating = 0; // unrated       this.isTruck = isTruck;    }        public Automobile(String model, int rating, boolean isTruck) {       this.model = model;       this.rating = rating;       this.isTruck = isTruck;    }                public String getModel()...

  • //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;   ...

    //Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected:    string make; int year;    double mpg;    Warranty warranty;    static int numOfVehicles; public:    Vehicle();    Vehicle(string s, int y, double m, Warranty warranty);    Vehicle(Vehicle& v);    ~Vehicle();    string getMake();    int getYear();    double getGasMileage();    void setMake(string s);    void setYear(int y);    void setYear(string y);    void setGasMileage(double m);    void setGasMileage(string m);    void displayVehicle();    static int getNumVehicles();    Warranty getWarranty();    void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() {    make = "unknown";    year =...

  • public class Fish { private String species; private int size; private boolean hungry; public Fish() {...

    public class Fish { private String species; private int size; private boolean hungry; public Fish() { } public Fish(String species, int size) { this.species = species; this.size = size; } public String getSpecies() { return species; } public int getSize() { return size; } public boolean isHungry() { return hungry; } public void setHungry(boolean hungry) { this.hungry = hungry; } public String toString() { return "A "+(hungry?"hungry":"full")+" "+size+"cm "+species; } }Define a class called Lake that defines the following private...

  • public class Pet {    //Declaring instance variables    private String name;    private String species;...

    public class Pet {    //Declaring instance variables    private String name;    private String species;    private String parent;    private String birthday;    //Zero argumented constructor    public Pet() {    }    //Parameterized constructor    public Pet(String name, String species, String parent, String birthday) {        this.name = name;        this.species = species;        this.parent = parent;        this.birthday = birthday;    }    // getters and setters    public String getName() {        return name;   ...

  • GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance...

    GIVEN CODES *****Boat.java***** import java.util.HashSet; import java.util.Set; public class Boat { private String name; //private instance variable name of type String private String boatClass; //private instance variable boatClass of type String private int regNum; //private instance variable regNum of type int private Set<String> crew = new HashSet<String>(); public void setName(String name) { this.name = name; } public void setBoastClass(String boatClass) { this.boatClass = boatClass; } public void setRegNum(int regNum) { this.regNum = regNum; } public String getName() { return name;...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

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