Question

In: Computer Science

1, Create a class Car. The class has four instance fields: make (String - admissible values:...

1, Create a class Car. The class has four instance fields:

  • make (String - admissible values: Chevy, Ford, Toyota, Nissan, Hyundai)
  • size (String - admissible values: compact, intermediate, fullSized),
  • weight (int - admissible values: 500 <= weight <= 4000)
  • horsePower (int - admissible values: 30 <= horsePower <= 400)

Provide

  • a default constructor that sets String values to null and int values to 0
  • a parameterized constructor that sets all four instance fields to the parameter values
  • setters and getters for all instance fields
  • a decent toString method

2. Create a class CarFactory

It provides just one method public static Car createRandomCar() which creates a random car instance with value chosen from the admissible set/range.

Solutions

Expert Solution

// PLEASE LIKE THE SOLUTION
// FEEL FREE TO DISCUSS IN COMMENT SECTION
// JAVA PROGRAM


import java.util.*;

//Car class
class Car{
   // properties
   private String make;
   private String size;
   private int weight;
   private int horsePower;

   // constructor default
   Car(){
       make = null;
       size = null;
       weight=0;
       horsePower=0;
   }

   // param constructor
   Car(String make,String size,int weight,int horsePower){
       this.make = make;
       this.size = size;
       this.weight = weight;
       this.horsePower = horsePower;
   }

   // setters
   public void setMake(String make){
       this.make = make;
   }


   public void setSize(String size){
       this.size = size;
   }

   public void setWeight(int weight){
       this.weight = weight;
   }

   public void setHorsePower(int horsePower){
       this.horsePower = horsePower;
   }

   // getters
   public String getMake(){
       return this.make;
   }


   public String setSize(){
       return this.size;
   }

   public int setWeight(){
       return this.weight;
   }

   public int setHorsePower(){
       return this.horsePower;
   }

   // to String method
   public String toString(){
       return ("Make = "+(this.make)+" Size = "+(this.size)+" Weight = "+(this.weight)+" HorsePower = "+(this.horsePower));
   }
}


// class CarFactory
class CarFactory{
   // static method
   public static Car createRandomCar(){
       // array to store permissible values
       String makeArray[] = {"Chevy","Ford","Toyota","Nissan","Hyundai"};
       String sizeArray[] = {"compact","intermediate","fullSized"};
      
       // create instance of Random class
       Random rand = new Random();

       // Generate random integers in range 0 to 4
       int makevalue = rand.nextInt(5);

       // Generate random integers in range 0 to 2
       int sizevalue = rand.nextInt(3);

       // generate random 500 to 4000
       int weight = rand.nextInt(3501)+500;

       // generate random 30 to 400
       int horsePower = rand.nextInt(401)+30;

       // call constructor
       Car car=new Car(makeArray[makevalue],sizeArray[sizevalue],weight,horsePower);

       return car;
   }

   // main method for testing
   public static void main(String arg[]){
       Car car = createRandomCar();
       System.out.println(car);
   }
}

// SAMPLE OUTPUT


Related Solutions

Create a class named “Car” which has the following fields. The fields correspond to the columns...
Create a class named “Car” which has the following fields. The fields correspond to the columns in the text file except the last one. i. Vehicle_Name : String ii. Engine_Number : String iii. Vehicle_Price : double iv. Profit : double v. Total_Price : double (Total_Price = Vehicle_Price + Vehicle_Price* Profit/100) 2. Write a Java program to read the content of the text file. Each row has the attributes of one Car Object (except Total_Price). 3. After reading the instances of...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
(Java Problem) Create a Produce class that have an instance variable of type String for the...
(Java Problem) Create a Produce class that have an instance variable of type String for the name, appropriate constructors, appropriate accessor and mutator methods, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String, the price (this is the price per box) as a double, the quantity as an integer, and invoke the appropriate constructor from the base class to...
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....
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and a double for the price. 2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object. 3. The Soda class...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the...
Create a class named CollegeCourse that includes the following data fields: dept (String) - holds the department (for example, ENG) id (int) - the course number (for example, 101) credits (double) - the credits (for example, 3) price (double) - the fee for the course (for example, $360). All of the fields are required as arguments to the constructor, except for the fee, which is calculated at $120 per credit hour. Include a display() method that displays the course data....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age....
IN JAVA PLEASE Create a class called Child with an instance data values: name and age. a. Define a constructor to accept and initialize instance data b. include setter and getter methods for instance data c. include a toString method that returns a one line description of the child
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT