Question

1. Assume you have a Car class that declares two private instance variables, make and model....

1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your declarations so that get- methods are not needed to read the values of these attributes from outside the class. b. Assuming you are in a separate driver class that has already used a constructor to create and fully initialize an object called car that has constant make and model attributes, write a Java statement that prints these two attributes on one line with one space between them. 3. Assume a Car class already has a two-parameter constructor that initializes make and model attributes. Write Java code that implements an additional constructor that has three parameters ─ two String parameters to initialize the same make and model attributes initialized by the two-parameter constructor, plus another int parameter to initialize a miles variable. Your code should utilize the two-parameter constructor to implement those operations which the two-parameter constructor is already able to perform. 4. Suppose a Car class defines an equals method with this header: public boolean equals(Car car) This equals method returns true if and only if the calling object and the parameter object have the same make and model. Otherwise, it returns false.

my subject is Computer science java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Thanks for the question.

Here is the completed code for this problem. 

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. 

If you are satisfied with the solution, please rate the answer. 

Thanks!
===========================================================================

public class Car {

    //1. Assume you have a Car class that declares two private
    // instance variables, make and model.

    //a. Write Java code that declares constant make and model
    // attributes that cannot be changed after
    // they are initialized by a constructor.

    // we made the variables public as they are constant
    // so once the values are set even they are public we cannot
    // directly change the values of these two attributes as they are now final
    public final String model;
    public final String make;

    // 3.plus another int parameter to initialize a miles variable.
    private int miles;

    //Write Java code that implements a two-parameter constructor
    // that instantiates a Car object and initializes both of its
    // instance variables.

    public Car(String model, String make) {
        this.model = model;
        this.make = make;
    }


    public Car(String model, String make, int miles) {
        //Your code should utilize the two-parameter constructor
        // to implement those operations which the two-parameter
        // constructor is already able to perform.
        this(model, make);
        this.miles = miles;
    }

    // class defines an equals method
    public boolean equals(Car car) {
        //This equals method returns true if and only if the calling
        // object and the parameter object have the same make and model.
        return make.equals(car.make) &&
                model.equals(car.model);
    }
}

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

public class CarDriver {

    public static void main(String[] args) {

        //initialize an object called car that has constant make and model attributes
        Car aCar = new Car("UX-123", "Ferrari");
        //prints these two attributes on one line with one space between them.
        System.out.println(aCar.make + " " + aCar.model);
    }
}

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

Add a comment
Know the answer?
Add Answer to:
1. Assume you have a Car class that declares two private instance variables, make and model....
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
  • Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand...

    Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks) 2.        Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks) 3.        Create a get and set methods for instance variable model variable only. (2 Marks) 4.        Create PrintInfo() method which prints all three instance variables. (2 Marks) 5.        Create a subclass GasCar with instance variable TankSize (double).. (2...

  • Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also...

    Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also make a constructor to initialize these data variables. Make a getPrice() method to return price. Make a Child class of Car, called Truck. It has a data variable, weight. Make a constructor that takes all the data variables including weight and those of Car class. Also override getPrice() method, which return 20% discount price if the weight is more than 20,000 else it returns...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • 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...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • Write a class named Book containing: Two instance variables named title and author of type String....

    Write a class named Book containing: Two instance variables named title and author of type String. A constructor that accepts two String parameters. The value of the first is used to initialize the value of title and the value of the second is used to initialize author. A method named toString that accepts no parameters. toString returns a String consisting of the value of title, followed by a newline character, followed by the value of author.

  • Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class...

    Write a JAVA program that declares a class Pet  With three variables Name, Age, weight .The class has  different constructors the first without parameters ,the  second with three parameters and the third with two parameters. In the main method  create three objects using thee different constructors to initialize the Variables.  then print out all the information a bout all objects .

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

  • Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate...

    Task 01: (a)Write a class Fruit with: private instance variables name, and pricePerKilogram Appropriate constructor Appropriate accessor methods Appropriate mutator methods toString method equals method (b) Write a FruitDriver class that: Initializes an array of Fruit objects, fruitArray, with 10 objects with names: banana, apple, mango, orange,pineapple, pear, grapes, tangerine, watermelon, sweetmelon and appropriate prices per kilogram. Uses an appropriate loop to display all objects with pricePerKilogram > 5.00 Saudi Riyals, if any. Calls a linearSearch method:                        public static...

  • Create a Ruby class Cylinder. Each cylinder is characterized by the instance variables radius and height. For this class...

    Create a Ruby class Cylinder. Each cylinder is characterized by the instance variables radius and height. For this class the initializer/constructor and the method volume that returns the volume of the cylinder. For this class the initializer/constructor and the method area that returns the area of cylinder. Create a class Can that inherits properties from the class Cylinder and adds new instance variablescontents(e.g. beer, Pepsi, Coke) and price(real number). Initialize an array of cans and write Ruby code necessary to...

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