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 {

}

Complete the following unit test for the class Car described above. Make sure to test each public constructor and each public method, print the expected and actual results.

// Start of the unit test of class Car

public class CarTester {

public static void main() {

// Your source codes are placed here

return;

}

}

Solutions

Expert Solution

import java.util.*;
import java.lang.*;

class Car{
    
    //Declare a private variables.
    private String make;
    private String model;
    private int year;

    //Default constructor.
    public Car(){
        this.make = "";
        this.model = "";
        this.year = 0;
    }
    //Constructor with user-defined values.
    public Car(String make  , String model , int year) {
        this.make = make;
        this.model = model;
        this.year = year;
    }

    //Getter and Setter method for all of them.
    public void setMake(String make) {
        this.make = make;
    }

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

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

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public int getYear() {
        return year;
    }

    
    //Print function for print object in appropriate order.
    public void print(){
        System.out.println("Make: " + make);
        System.out.println("Model: " + model);
        System.out.println("Year: " + year);
    }
}
class CarTester {
    public static void main(String[] args) {
        
        //Testing code.
        Car test1 = new Car();
        test1.setMake("toyota");
        test1.setModel("4tuner");
        test1.setYear(2010);
        System.out.println(test1.getMake());
        System.out.println(test1.getModel());
        System.out.println(test1.getYear());

        Car test2 = new Car("Audi" , "a4" , 2012);
        test2.print();

    }
}

OUTPUT :-


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