Question

In: Computer Science

Write a Java application, and an additional class to represent some real-world entity such as a...

Write a Java application, and an additional class to represent some real-world entity such as a technology item, an animal, a person, a vehicle, etc. Keep in mind that a class is a model in code of something real or imagined, which has attributes (member variables) and behaviors (member methods).

The class will:

  1. Create a total of 5 member variables for the class, selecting the appropriate data types for each field. For example, a class to represent a lamp might include color, price, height, numBulbs, batteryOperated. Each of these 5 variables need a data type.

  2. Include at least three different constructor methods, in addition to the default constructor (0 argument constructor). The constructor is a function (method) which allocates memory and initialized the member variables specified in (a.).

  3. Include getters/setters for to serve as as mutators and accessors for each variable. Name these appropriately such as setColor & getColor, setPrice & getPrice, setHeight & getHeight, setNumBulbs and getNumBulbs, and setBatteryOperated & getBatteryOperated.

  4. Create a member function showValues() to display the values of an object in a formatted manner.

  5. Create at least 2 other member functions (methods) for the class that will perform some operation on the data (i.e. calculations or additional report/display of output). For example, turnLampOn, changeBulb, etc.

The Java application class (with a main method) will:

  1. Instantiate at least three objects (using each of the constructors at least once) with your program.

Note: Set the remaining data with the mutator methods.

  1. Store the data from the individual objects into an ArrayList (or some other dynamic data structure of objects.

  2. Utilize the showValues() method and each of the two methods on each of your objects to demonstrate their use.

Required Output: Generate output samples demonstrating all of your member functions, adequately testing them and showing their functionality (i.e. inputting values, outputting values (displaying them), performing calculations, etc.).

Solutions

Expert Solution

Vehicle.java

public class Vehicle {
  
// INSTANCE VRIABLES
private String make, model, color;
private int year;
private int distanceTravelled;
  
// DEFAULT CONSTRUCTOR
public Vehicle()
{
this.make = "";
this.model = "";
this.color = "";
this.year = 0;
this.distanceTravelled = 0;
}

// OVERLOADED CONSTRUCTOR
public Vehicle(String make, String model, String color, int year)
{
this.make = make;
this.model = model;
this.color = color;
this.year = year;
this.distanceTravelled = 0;
}
  
// GETTER METHODS

public String getMake() {
return make;
}

public String getModel() {
return model;
}

public String getColor() {
return color;
}

public int getYear() {
return year;
}

public int getDistanceTravelled() {
return distanceTravelled;
}
  
// SETTER METHODS

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

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

public void setColor(String color) {
this.color = color;
}

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

public void setDistanceTravelled(int distanceTravelled) {
this.distanceTravelled = distanceTravelled;
}
  
public void moveForward(int distance)
{
if(distance < 0)
System.out.println("Please enter a positive distance for moving forward.");
else
{
System.out.println("Vehicle moved " + distance + " km forward.");
this.distanceTravelled += distance;
}
}
  
public void moveBackward(int distance)
{
if(distance > 0)
System.out.println("Please enter a negative distance for moving backward.");
else
{
System.out.println("Vehicle moved " + (distance * -1) + " km backward.");
this.distanceTravelled -= distance;
}
}
  
// METHOD TO PRINT OBJECT DETAILS
public void showValues()
{
System.out.println("Make: " + this.make + ", Model: " + this.model + ", Color: "
+ this.color + ", Year built-in: " + this.year + ", Distance travelled: " + this.distanceTravelled);
}
}

DriverApp.java (Driver class)

import java.util.ArrayList;

public class DriverApp {
  
public static void main(String[] args)
{
ArrayList<Vehicle> vehicles = new ArrayList<>();
  
// adding Vehcile instance using it's constrtuctor
vehicles.add(new Vehicle("Cadillac", "Series 61", "Red", 1957));
  
// adding Vehicle instance using Setter methods
Vehicle v1 = new Vehicle();
v1.setMake("BMW");
v1.setModel("5-Series GT");
v1.setColor("White");
v1.setYear(2016);
vehicles.add(v1);
  
// adding Vehcile instance using it's constrtuctor
vehicles.add(new Vehicle("Kia", "Bongo Frontier", "Black", 2001));
  
// display all the vehicle instance details
System.out.println("ALL VEHICLE DETAILS:");
for(Vehicle v : vehicles)
{
v.showValues();
}
  
System.out.println("\nMOVING VEHICLE1 FORWARD, AND VEHICLE2 & VEHICLE3 BACKWARD:");
vehicles.get(0).moveForward(15);
vehicles.get(1).moveBackward(-7);
vehicles.get(2).moveBackward(-22);
  
// display all the vehicle instance details
System.out.println("\nALL VEHICLE DETAILS:");
for(Vehicle v : vehicles)
{
v.showValues();
}
}
}

***************************************************************** SCREENSHOT **********************************************************


Related Solutions

how do you write a real world application paper
how do you write a real world application paper
Java Write a class called Triangle that can be used to represent a triangle. Write a...
Java Write a class called Triangle that can be used to represent a triangle. Write a class called Describe that will interface with the Triangle class The Server • A Triangle will have 3 sides. It will be able to keep track of the number of Triangle objects created. It will also hold the total of the perimeters of all the Triangle objects created. • It will allow a client to create a Triangle, passing in integer values for the...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
Search on the internet to find some real world example of the application of flexible budgets...
Search on the internet to find some real world example of the application of flexible budgets or standard costs
Suppose the interface and the class of stack already implemented, Write application program to ( java)...
Suppose the interface and the class of stack already implemented, Write application program to ( java) 1- insert 100 numbers to the stack                         2- Print the even numbers 3- Print the summation of the odd numbers
Write the following methods in java class ARM that represent state information as well as functional...
Write the following methods in java class ARM that represent state information as well as functional blocks of the ARM platform. [Go through the following 5 classes then write methods for the instructions: mov, str, ldr, add in class ARM, finally, write a print method for ARM in Main.java that can display the registers and the memory locations that have been used. (make sure to make a visualization of the print method instead of just a console dump)] --- Please...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
What's the application of ROI in real-world business? What's the application of ROI in the stock...
What's the application of ROI in real-world business? What's the application of ROI in the stock valuation?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT