Question

In: Computer Science

Create a class called Car (Car.java). It should have the following private data members: • String...

Create a class called Car (Car.java). It should have the following private data members:

• String make

• String model

• int year

Provide the following methods:

• default constructor (set make and model to an empty string, and set year to 0)

• non-default constructor Car(String make, String model, int year)

• getters and setters for the three data members

• method print() prints the Car object’s information, formatted as follows:

Make: Toyota

Model: 4Runner

Year: 2010

public class Car {

}

Solutions

Expert Solution

Java Program :

public class Car
{
    private String make, model;   // declared all required variables as private
    private int year;
    
    Car()    // default constructor to initialize all variables
    {
        make = "";
        model = "";
        year =0;
    }
    
    Car(String make, String model, int year) // Parameterized constructor to give values to variables
    {
        this.make = make;         // this keyword is used to represent the class variable
        this.model = model;
        this.year = year;
    }
    
    void setMake(String make)    // setter for make
    {
        this.make = make;
    }
    
    String getMake()           // getter for make
    {
        return make;
    }

    void setModel(String model) // setter for model
    {
        this.model = model;
    }
    
    String getModel()        // getter for model
    {
        return model;
    }

    void setYear(int year)  // setter for year
    {
        this.year = year;
    }
    
    int getYear()         // getter for year
    {
        return year;
    } 
    
    void print()        // prints all data
    {
        System.out.println("Make: "+make);
        System.out.println("Model: "+model);
        System.out.println("Year: "+year);
    }
    
}

public class Main
{
        public static void main(String[] args) 
        {
                Car c = new Car("Toyota", "4Runner", 2010);   // creates a new object of Class Car and intializes with a constructor
                c.print();            // calls the print() method of Class Car
        }
}

Screenshots / Outputs :


Related Solutions

Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
C++ Define a base class called Person. The class should have two data members to hold...
C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and !=...
Create a class, called Song. Song will have the following fields:  artist (a string) ...
Create a class, called Song. Song will have the following fields:  artist (a string)  title (a string)  duration (an integer, recorded in seconds)  collectionName (a string) All fields, except for collectionName, will be unique for each song. The collectionName will have the same value for all songs. In addition to these four fields, you will also create a set of get/set methods and a constructor. The get/set methods must be present for artist, title, and duration....
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100]...
Create a class tuck shop with following data members: String Owner String Food_Items[100] Double Price [100] Int Quantity [100]                Note: All arrays are open ended. It is not necessary that they are completely filled. All three arrays will work in synchronization with each other i-e the item at index 0 will have its price in Price array at index 0 and quantity at index 0 of Quantity array. Methods: Two Constructors (default, four-argument) set for Owner , get for...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
C++ Create a class for working with fractions. Only 2 private data members are needed: the...
C++ Create a class for working with fractions. Only 2 private data members are needed: the int numerator of the fraction, and the positive int denominator of the fraction. For example, the fraction 3/7 will have the two private data member values of 3 and 7. The following methods should be in your class: a. A default constructor that should use default arguments in case no initializers are included in the main. The fraction needs to be stored in reduced...
Assume you have created the following data definition class: public class Book {    private String title;...
Assume you have created the following data definition class: public class Book {    private String title;    private double cost;       public String getTitle() { return this.title; }    public double getCost() { return this.cost; }       public void setTitle(String title) {       this.title = title;    } // Mutator to return true/false instead of using exception handling public boolean setCost(double cost) {       if (cost >= 0 && cost < 100) {          this.cost = cost;          return true;       }       else {          return false;       }...
Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT