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:

  1. Create a program class named TestArrays This class should have a main() method that behaves as follows:
  2. Create an integer array of size 10.
  3. Using a loop, fill the elements with increments of 5, starting with 5 in the first element.
  4. Using the array elements, sum the second, fifth and seventh elements.
  5. Display the sum with a label.
  6. Change the fourth element to 86.
  7. Subtract 9 from the ninth element.
  8. Using a loop, display the elements of the array.
  9. Compile and execute the code to ensure correct results.
  10. Dont forget to include descriptive and detailed comments in your code!!!

Part II

  1. Write a toString() method for the Vehicle class that would display the attributes of the class.
  2. Create an array of 7 elements that will hold Vehicle references.
  3. Using a loop, assign the first 4 elements to Vehicle objects using the null constructor.
  4. Instantiate a Vehicle object with a base price of $37,000 and additional features of $400.
  5. Assign the object to the 5th element of the array.
  6. Assign the 6th element of the array to a Vehicle object with a base price of $24,000 and additional features of $5500.
  7. Assign the 7th element of the array to a Vehicle object with a base price of $53,000 and additional features of $9000.
  8. Calculate the total price for the 5th, 6th and 7th vehicles in the array.
  9. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!