Question

In: Computer Science

Please note that problem solving often requires you to consider additional requirements that are not specifically...

Please note that problem solving often requires you to consider additional requirements that are not specifically listed.

You are to write a Java program to represent cars.

  • The program has a Car class (Car.java) with constructors, accessors, mutators, and print methods.
  • This class should not have static data members or static methods. The main method will be in another class.
  • The data members include the car model year, make, model, retail price, and current value; for example, a 2020 Ford Mustang, retail of 28999, and current value 27500.
  • There are two constructors. One constructor should have arguments used to set the car make, model, model year, and retail price, but set the current value will to zero without using an argument. Another constructor should have arguments used to set all the data members. Name the arguments in both constructors the same as the data members.
  • There are accessors and mutators for each data member. The accessors will return the data member value. The mutators will sent the data member to a value passed in as an argument.
  • The print method will output the data members to the console. We will keep it simple by just putting the data member values on the same line with spaces between. For example:
2020 Ford Mustang 28999 27500

The program has a Driver class (Driver.java) with the main method. The main method:

  1. Instantiates an ArrayList of Cars and instantiates three Car objects (car1, car2, car3).
  2. Adds the Car objects to the ArrayList of Cars. After adding the cars to the ArrayList, all operations on the objects are done by accessing the methods through the ArrayList. In other words, do not use car1, car2, or car3 variables after the cars are added to the ArrayList.
  3. Change the current value of the third car to 10% less than its current value.
  4. Set the first car’s retail price to $30,000.
  5. Using a loop, printout the three cars using the ArrayList.

Solutions

Expert Solution

package Array;

// Defines class Car
public class Car
{
   // Instance variable to store data
   private int carModelYear;
   private String make;
   private String model;
   private double retailPrice;
   private double currentValue;
  
   // Parameterized constructor to assign all parameter except current value
   // to instance variables
   Car(int carModelYear, String make, String model, double retailPrice)
   {
       this.carModelYear = carModelYear;
       this.make = make;
       this.model = model;
       this.retailPrice = retailPrice;
       this.currentValue = 0.0;
   }// End of parameterized constructor
  
   // Overrides parameterized constructor to assign all parameter value
   // to instance variables
   Car(int carModelYear, String make, String model,
           double retailPrice, double currentValue)
   {
       this.carModelYear = carModelYear;
       this.make = make;
       this.model = model;
       this.retailPrice = retailPrice;
       this.currentValue = currentValue;
   }// End of parameterized constructor
  
   // Method to return model year
   int getCarModelYear()
   {
       return carModelYear;
   }// End of method
  
   // Method to return make
   String getMake()
   {
       return make;
   }// End of method
  
   // Method to return model
   String getModel()
   {
       return model;
   }// End of method
  
   // Method to return retail price
   double getRetailPrice()
   {
       return retailPrice;
   }// End of method
  
   // Method to return current value
   double getCurrentValue()
   {
       return currentValue;
   }// End of method
  
   // Method to set model year
   void setCarModelYear(int carModelYear)
   {
       this.carModelYear = carModelYear;
   }// End of method
  
   // Method to set make
   void setMake(String make)
   {
       this.make = make;
   }// End of method
  
   // Method to set model
   void setModel(String model)
   {
       this.model = model;
   }// End of method
  
   // Method to set retail price
   void setRetailPrice(double retailPrice)
   {
       this.retailPrice = retailPrice;
   }// End of method
  
   // Method to set current value
   void setCurrentValue(double currentValue)
   {
       this.currentValue = currentValue;
   }// End of method
  
   // Method to display car information  
   public void print()
   {
       System.out.println(carModelYear + " " + make + " " + model + " "
               + retailPrice + " " + currentValue);
   }// End of method
}// End of class Car
-------------------------------------------------------------------------------------------------------------

package Array;

import java.util.ArrayList;

// Driver class definition
public class CarDriver
{
   // main method definition
   public static void main(String ss[])
   {
       // Creates an array list to store Car class objects
       ArrayList <Car> cars = new ArrayList<Car>();
      
       // Creates 3 object of Car class using parameterized constructor
       Car car1 = new Car(2020, "Ford", "Mustang", 28999, 27500);
       Car car2 = new Car(2020, "Santro", "Fero", 89456, 32809);
       Car car3 = new Car(2020, "Maruti", "M800", 45988, 25357);
       // Adds the car objects to array list
       cars.add(car1);
       cars.add(car2);
       cars.add(car3);
      
       System.out.println("\n ************* Car Information ************* ");
       // Loops till end of the array list
       for(int c = 0; c < cars.size(); c++)
           // Calls the method to display each car object information
           cars.get(c).print();
      
       // Calculates 10% less value of third car object current value
       double valueLess = cars.get(2).getCurrentValue() -
               (cars.get(2).getCurrentValue()* 0.10);
       // Sets the third car object's current value
       cars.get(2).setCurrentValue(valueLess);
       // Sets the first car object's retail price
       cars.get(0).setRetailPrice(30000);
      
       System.out.println("\n ************* Car Information after modification ************* ");
       // Loops till end of the array list
       for(int c = 0; c < cars.size(); c++)
           // Calls the method to display each car object information
           cars.get(c).print();
   }// End of main method
}// End of driver class

Sample Output:


************* Car Information *************
2020 Ford Mustang 28999.0 27500.0
2020 Santro Fero 89456.0 32809.0
2020 Maruti M800 45988.0 25357.0

************* Car Information after modification *************
2020 Ford Mustang 30000.0 27500.0
2020 Santro Fero 89456.0 32809.0
2020 Maruti M800 45988.0 22821.3


Related Solutions

In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will...
In this assignment, you will practice solving a problem using object-oriented programming and specifically, you will use the concept of object aggregation (i.e., has-a relationship between objects). You will implement a Java application, called MovieApplication that could be used in the movie industry. You are asked to implement three classes: Movie, Distributor, and MovieDriver. Each of these classes is described below. The Movie class represents a movie and has the following attributes: name (of type String), directorsName (of type String),...
Note: Please do not attempt to solve this assignment if you cannot fulfill all the requirements,...
Note: Please do not attempt to solve this assignment if you cannot fulfill all the requirements, review the "sample" journal and create the new one using the same format!!! Write journal entry on a topic of your choice from any optional reading you may have done. You may write on any concept or insight that stands out to you that is relevant and useful to your life and work. You will write a journal entry of about one page describing...
What is Group Problem Solving? Explain the different types of problem-solving techniques. Please explain what technique...
What is Group Problem Solving? Explain the different types of problem-solving techniques. Please explain what technique you think is most effective and why?
Read the case below and respond to the requirements that follow. Please note that this is...
Read the case below and respond to the requirements that follow. Please note that this is a hypothetical case and does not represent the real issues as may pertain to the case study area. The Case of Nasha Body clinic Nana Ashorkor Kyei is the Managing Director of Nasha Body clinic, a leading “body massage butik” in Accra. She has established standards in her organisation that to the best of her knowledge must satisfy customers. Although some of her customers...
Problem Solving/Goal Setting Checkpoint Provide an example of a time when you used the problem solving...
Problem Solving/Goal Setting Checkpoint Provide an example of a time when you used the problem solving and decision making. What is the role of creativity in the problem solving process? What are three ways that you can increase your personal creativity? Respond to the following in a essay: Describe a time you encountered a problem that required you to use the problem solving and decision making steps on page 150 of the textbook. How did you solve the problem using...
Developing software programs requires a systematic approach to problem solving and involves several steps. For a...
Developing software programs requires a systematic approach to problem solving and involves several steps. For a given problem statement – assuming the problem has a software solution – list and describe in your own words the steps that enable you to bring the software solution to life.
What difficulties might you anticipate when using the rational problem-solving process?  Why? What additional difficulties might arise...
What difficulties might you anticipate when using the rational problem-solving process?  Why? What additional difficulties might arise because of personal attributes? Which of these have you experienced? Explain. What were the consequences? How can these difficulties be avoided?
For this Variation of parameter problem, consider the following method of solving the general linear equation...
For this Variation of parameter problem, consider the following method of solving the general linear equation of first order y' + p(t)y= g(t) (a) If g(t) = 0 for all t, show that the solution is y= Aexp[ - the integral of p(t)dt] , where A is a constant Please use good handwriting and show as many steps as possible. Thank you.
Instructions : • It is recommended that you use the IRAC problem solving method. • You...
Instructions : • It is recommended that you use the IRAC problem solving method. • You may use headings and subheadings to structure your answer. • Your answer must include legal references (relevant cases and/or sections of Acts). The word limit is 600 words. Question 2 Earlier in the day, when Rob arrived at the Fancy Hotel before the performance, he was surprised to find that there was a valet car parking service. Rob had not been to the Fancy...
please use R for solving the questions (e) Is multicollinearity a potential problem in this model?...
please use R for solving the questions (e) Is multicollinearity a potential problem in this model? (f) Construct a normal regression plot of residuals. Does there seem to be any problem with the normality assumption? (g) Construct and interpret a plot of the residuals versus predicted response. (h) Based on the above analysis, what is your recommended model? [Hint: Use the lm commend in R to fit a regression equation. Table B.4 y x1 x2 x3 x4 x5 x6 x7...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT