Question: Part I: Create a program class named TestArrays This class should have a main() method that behaves as follows: Create an integer array of size
Part I:
- Create a program class named TestArrays This class should have a main() method that behaves as follows:
- Create an integer array of size 10.
- Using a loop, fill the elements with increments of 5, starting with 5 in the first element.
- Using the array elements, sum the second, fifth and seventh elements.
- Display the sum with a label.
- Change the fourth element to 86.
- Subtract 9 from the ninth element.
- Using a loop, display the elements of the array.
- Compile and execute the code to ensure correct results.
- Dont forget to include descriptive and detailed comments in your code!!!
Part II
- Write a toString() method for the Vehicle class that would display the attributes of the class.
- Create an array of 7 elements that will hold Vehicle references.
- Using a loop, assign the first 4 elements to Vehicle objects using the null constructor.
- Instantiate a Vehicle object with a base price of $37,000 and additional features of $400.
- Assign the object to the 5th element of the array.
- Assign the 6th element of the array to a Vehicle object with a base price of $24,000 and additional features of $5500.
- Assign the 7th element of the array to a Vehicle object with a base price of $53,000 and additional features of $9000.
- Calculate the total price for the 5th, 6th and 7th vehicles in the array.
- Using an array, display the attributes of the objects in the array calling the toString().
DEFAULT CODE
public class Vehicle
{
// total cost
private int basePrice;
private int additionalFeatures;
private int totalPrice;
public Vehicle()
{
// set default values for vehicle base cost and features cost
basePrice = 25000;
additionalFeatures = 3000;
}
// (int bPrice) - stores the vehicle initial base price
public Vehicle (int bPrice)
{
// set base price to the parameter value
basePrice = bPrice;
// set default features price to 0
additionalFeatures = 0;
}
public Vehicle (int bPrice, int aFeatures)
{
// set the base price and the features cost to the parameter values
basePrice = bPrice;
additionalFeatures = aFeatures;
}
public int getBasePrice()
{
// return the current vehicle base price
return basePrice;
}
public int getAdditionalFeatures()
{
// return the current vehicle's features cost
return additionalFeatures;
}
public int getTotalPrice()
{
// return the current vehicle's total price
return totalPrice;
}
// setBasePrice
// This method updates the vehicle's base price to the passed value
// Parameters:
// (int bPrice) - stores the new value of the base price
// (no return value)
public void setBasePrice(int bPrice)
{
// update the vehicle base price cost
basePrice = bPrice;
}
// setAdditionalFeatures
// This method updates the vehicle's features cost to the passed value
// Parameters:
// (int bPrice) - stores the new value of the features cost
// (no return value)
public void setAdditionalFeatures(int aFeatures)
{
// update the vehicle features cost
additionalFeatures = aFeatures;
}
public void calcTotalPrice()
{
// calculate the total price by adding the base cost and the features cost
totalPrice = basePrice + additionalFeatures;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
