Question

Java: Create a class called Vehicle with the following features: a. It has three private data...

Java: Create a class called Vehicle

with the following features:

a.

It has three private data members (instance variables): one is the

manufacturer’s name (String manufacturer), the second is the number of

cylinders in the engine (int cylinder), and the third is the owner (Person

owner). The class Person is described above.

b.

It has three constructors, a no-argument constructor, a constructor with three

parameters, and a copy constructor. The no-argument constructor will set the

manufacturer to “None”, cylinder to 0, and owner’s name to an empty string

“”. The three parameter constructor will set the manufacturer to the value of

the first parameter, cylinder to the value of the second parameter, and owner to a

copy of the third parameter. The copy constructor has a parameter of type

Vehicle, and it creates a new object which is an exact copy of its argument

object.

c.

It has three accessor methods (getManufacturer, getCylinder, getOwner) and

three mutator methods (setManufacturer, setCylinder, setOwner).

d.

It has a toString method that returns a string containing the manufacturer’s

name, number of cylinders, and the owner’s name. (For example,“manufacturer=Ford, cylinder= 4, owner’s Name =Bob”)

e.

It has an equals method that returns true if two independent Vehicles are

identical.

Thank you for any help

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA CODE:

class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return this.name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
class Vehicle {
    private String manufacturer;
    private int cylinder;
    private Person owner;
    // No-argument constructor
    public Vehicle() {
        this.manufacturer = "None";
        this.cylinder = 0;
        this.owner = new Person("");
    }
    // Constructor
    public Vehicle(String manufacturer, int cylinder, Person owner) {
        this.manufacturer = manufacturer;
        this.cylinder = cylinder;
        this.owner = new Person(owner.getName());
    }
    // Copy constructor
    public Vehicle(Vehicle v) {
        this.manufacturer = v.manufacturer;
        this.cylinder = v.cylinder;
        this.owner = new Person(v.owner.getName());
    }
    // Accessor methods
    public String getManufacturer() {
        return this.manufacturer;
    }
    public int getCylinder() {
        return this.cylinder;
    }
    public Person getOwner() {
        return this.owner;
    }
    // Mutator methods
    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }
    public void setCylinder(int cylinder) {
        this.cylinder = cylinder;
    }
    public void setOwner(Person owner) {
        this.owner = new Person(owner.getName());
    }
    public String toString() {
        return "manufacturer = " + this.manufacturer + ", cylinder = " + this.cylinder + ", owner\'s Name = "
                + this.owner.getName();
    }
    public boolean equals(Vehicle v) {
        return this.manufacturer.equals(v.manufacturer) && this.cylinder == v.cylinder
                && this.owner.getName().equals(v.owner.getName());
    }
}
class Main {
    public static void main(String[] args) {
        Vehicle v1 = new Vehicle("Audi", 8, new Person("John"));
        Vehicle v2 = new Vehicle(v1);
        v1.setCylinder(9);
        System.out.println(v1);
        System.out.println(v2);
        System.out.println(v1.equals(v2));
    }
}

OUTPUT:

FOR ANY HELP JUST DROP A COMMENT

Add a comment
Know the answer?
Add Answer to:
Java: Create a class called Vehicle with the following features: a. It has three private data...
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
  • Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's...

    Required in Java. Thank you. 1. Create a base class called Vehicle that has the manufacturer's name (type String), number of cylinders in the engine (type int), and owner (type Person given in Listing 8.1 in the textbook and in LMS). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double, since it may contain a fractional part) and towing capacity in tons (type double). Give your classes...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • JAVA /** * This class stores information about an instructor. */ public class Instructor { private...

    JAVA /** * This class stores information about an instructor. */ public class Instructor { private String lastName, // Last name firstName, // First name officeNumber; // Office number /** * This constructor accepts arguments for the * last name, first name, and office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** * The set method sets each field. */ public void set(String lname, String fname, String...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler...

    JAVA PROGRAMING - TESTING CLASSES AND INHERITANCE Create two classes - a vehicle class and gasguzzler class. Neither classes have I/O. Use a test class as well. Vehicle class has methods and properties that all vehicles have. This class has two properties - speed (rate of travel in miles per hour) and weight (in pounds) Vehicle Class Public Methods: Vehicle objects can be constructed 2 different ways: by specifying the weight and the speed, or by specifying the weight only...

  • In java How to get started; Create a class called Card. The card has three fields;...

    In java How to get started; Create a class called Card. The card has three fields; a value , a suit and a face. The card class has a constructor that takes three values for the three fields. Create a no-args constructor. The Card class has three get methods to return the values of each of the fields. The Card class has a toString( ) method. The Card class has a compareTo( ) method that uses the value of the...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • In Java* Please implement a class called "MyPet". It is designed as shown in the following...

    In Java* Please implement a class called "MyPet". It is designed as shown in the following class diagram. Four private instance variables: name (of the type String), color (of the type String), gender (of the type char) and weight(of the type double). Three overloaded constructors: a default constructor with no argument a constructor which takes a string argument for name, and a constructor with take two strings, a char and a double for name, color, gender and weight respectively. public...

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