Question

In: Computer Science

Part I: Have a program class named TestArrays This class should have a main() method that...

Part I:

  1. Have a program class named TestArrays This class should have a main() method that behaves as follows:
  2. Have 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. Don't forget to include descriptive and detailed comments in your code!!!

Part II

  1. have a toString() method for the Vehicle class that would display the attributes of the class.
  2. have 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().

//import statements

import java.util.Scanner;

public class Vehicle

{

//Attributes of class Vehicle

private double basePrice;

private double otherFeatures;

private double totalPrice;

//constructors

//no argument constructors

public Vehicle(){

  basePrice = 25000;

  otherFeatures = 3000;

  totalPrice = basePrice + otherFeatures;

}

//one argument constructor

public Vehicle(double basePrice){

  basePrice = basePrice;

otherFeatures = 0;

  totalPrice = basePrice + otherFeaturese;

}

//two argument constructor

  public Vehicle(double basePrice,double otherFeatures){

   basePrice = basePrice;

   otherFeatures = otherFeatures;

totalPrice = basePrice + otherFeaturese;

  }

  //getters

  public double getBestPrice(){

   return basePrice;

  }

  public double getOtherFeaturese(){

   return otherFeatures;

  }

  public double getTotalPrice(){

   return totalPrice;

  }

  //setters

  public void setBasePrice(double basePrice){

   basePrice = basePrice;

  }

  public void setotherFeatures(double additionalPrice){

  otherFeatures = otherFeatures;

  }

  public void calcTotalPrice(){

    totalPrice = basePrice + otherFeatures;

  }

}//End of class Vehicle

class testVehicle{

public static void main(String[] args){

//creating instances of class Vehicle

Vehicle Ford = new Vehicle();

Vehicle Toyota = new Vehicle(36000);

Vehicle Porsche = new Vehicle(64000,7000);

//changing base price of Ford to 28000

Ford.setBasePrice(28000);

//changing additional price of Toyota to 4500

Toyota.setOtherFeatures(4500);

//changing base price of Porsche to 72000

Porsche.setBasePrice(72000);

//Calculating total price for each vehicles.

Ford.calcTotalPrice();

Toyota.calcTotalPrice();

Porsche.calcTotalPrice();

//Calculating total price of 3 vehicles.

double totalCost = Ford.getTotalPrice()+Toyota.getTotalPrice()+Porsche.getTotalPrice();

  //displaying prices of vehicles

  System.out.println("Vehicle: Ford, Base Price: $ "+Ford.getBestPrice()

           +", otherFeatures: $ "+Ford.getOtherFeatures()

           +", Total cost: $ "+Ford.getTotalPrice());

  System.out.println("Vehicle: Toyota, Base Price: $ "+Toyota.getBestPrice()

           +", otherFeatures Cost: $ "+Toyota.getotherFeatures()

           +", Total cost: $ "+Toyota.getTotalPrice());

  System.out.println("Vehicle: Porsche, Base Price: $ "+Porsche.getBestPrice()

           +", otherFeatures: $ "+Porsche.getOtherFeatures()

           +", Total cost: $ "+Porsche.getTotalPrice());

  //displaying total cost of all 3 vehicles

  System.out.println("Total cost of all 3 Vehicles: "+totalCost);

}//End of main() function

}//End of class testVehicle

Solutions

Expert Solution

(Part i) The java code is as follows:-

import java.util.*;
public class TestArrays
{
   public static void main(String[] args)
   {
   int []arr=new int[10]; //Have an integer array of size 10
   int sum=0;
   for(int i=0;i<10;i++)//Using a loop, fill the elements with increments of 5, starting with 5 in the first element.
   arr[i]=i*5+5;
   sum+=arr[1]+arr[4]+arr[6];//Using the array elements, sum the second, fifth and seventh elements.
   System.out.println("Sum of second, fifth and seventh element is : "+sum);//Display the sum with a label.
   arr[3]=86;//Change the fourth element to 86.
   arr[8]-=9;//Subtract 9 from the ninth element.
   for(int i=0;i<10;i++)//Using a loop, display the elements of the array.
   System.out.print(arr[i]+" ");
   }
}

The output of the above code is as follows:-

Sum of second, fifth and seventh element is : 70
5 10 15 86 25 30 35 40 36 50

The screenshot of the above code is as follows:-

(Part ii) The java code is as follows:-

import java.util.Scanner;
class Vehicle
{
//Attributes of class Vehicle
private double basePrice;
private double otherFeatures;
private double totalPrice;
//constructors
//no argument constructors
public Vehicle()
{
basePrice = 25000;
otherFeatures = 3000;
totalPrice = basePrice + otherFeatures;
}
//one argument constructor
public Vehicle(double basePrice)
{
basePrice = basePrice;
otherFeatures = 0;
totalPrice = basePrice + otherFeatures;
}
//two argument constructor
public Vehicle(double basePrice,double otherFeatures)
{
this.basePrice = basePrice;
this.otherFeatures = otherFeatures;
this.totalPrice = basePrice + otherFeatures;
}
//getters
public double getBestPrice()
{
return this.basePrice;
}
public double getOtherFeatures()
{
return this.otherFeatures;
}
public double getTotalPrice()
{
return this.totalPrice;
}
//setters
public void setBasePrice(double basePrice)
{
this.basePrice = basePrice;
}
public void setOtherFeatures(double additionalPrice)
{
this.otherFeatures = otherFeatures;
}
public void calcTotalPrice()
{
totalPrice = basePrice + otherFeatures;
}
@Override
public String toString()
{
return "Base Price: $ "+this.getBestPrice()
+", otherFeatures: $ "+this.getOtherFeatures()
+", Total cost: $ "+this.getTotalPrice();
}
}//End of class Vehicle
class testVehicle
{
public static void main(String[] args)
{
//creating instances of class Vehicle
double totalCost=0;
Vehicle []obj=new Vehicle[7];
for(int i=0;i<7;i++)
{
if(i<4)
obj[i]=new Vehicle();
else if(i==4)
obj[i]=new Vehicle(37000,400);
else if(i==5)
obj[i]=new Vehicle(24000,5500);
else
obj[i]=new Vehicle(53000,9000);
}
obj[4].calcTotalPrice();
obj[5].calcTotalPrice();
obj[6].calcTotalPrice();
for(int i=0;i<7;i++)
{
System.out.println("Vehicle "+(i+1)+": "+obj[i].toString());
totalCost+=obj[i].getTotalPrice();
}
//displaying total cost of all 3 vehicles
System.out.println("Total cost of all Vehicles: "+totalCost);
}//End of main() function
}//End of class testVehicle

The output of the above code is as follows:-

Vehicle 1: Base Price: $ 25000.0, otherFeatures: $ 3000.0, Total cost: $ 28000.0
Vehicle 2: Base Price: $ 25000.0, otherFeatures: $ 3000.0, Total cost: $ 28000.0
Vehicle 3: Base Price: $ 25000.0, otherFeatures: $ 3000.0, Total cost: $ 28000.0
Vehicle 4: Base Price: $ 25000.0, otherFeatures: $ 3000.0, Total cost: $ 28000.0
Vehicle 5: Base Price: $ 37000.0, otherFeatures: $ 400.0, Total cost: $ 37400.0
Vehicle 6: Base Price: $ 24000.0, otherFeatures: $ 5500.0, Total cost: $ 29500.0
Vehicle 7: Base Price: $ 53000.0, otherFeatures: $ 9000.0, Total cost: $ 62000.0
Total cost of all Vehicles: 240900.0


Related Solutions

Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method)....
Write a program named MyHometown_Icon.java. The program will be an application (i.e have a main method). It will be in the default package. It will import edu.wiu.StdDraw. Its main method will make calls to methods in StdDraw to draw something iconic about your hometown. Multiple colors should be used.Multiple primitive types will be used and the drawing will consists of more than 20 primitive shapes.
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Write a java program using the information given Design a class named Pet, which should have...
Write a java program using the information given Design a class named Pet, which should have the following fields (i.e. instance variables):  name - The name field holds the name of a pet (a String type)  type - The type field holds the type of animal that a pet is (a String type). Example values are “Dog”, “Cat”, and “Bird”.  age - The age field holds the pet’s age (an int type) Include accessor methods (i.e. get...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main...
Assignment 1: Build a simple class called DBTester.java. This class should have only one method, main method. In the main method you should read in all of the rows of data from the Instructors Table in the Registration Database, then print out this data. Follow the steps outlined in class. Remember to use try-catch blocks for all database access code. The example in the book does not use Try-catch blocks, but you should
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named...
Using C# Create a class named Inches To Feet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method displays inches in feet and inches. For example, 67 inches is 5 feet 7 inches.
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to...
Create a class using C# named InchesToFeet. Its Main()method holds an integer variable named inches to which you will assign a value. Create a method to which you pass inches. The method uses 2 ref parameters: feet, inches left of type int and a parameter that is not ref of type int to which you pass inchesinches. For example, 67 inches is 5 feet 7 inches.
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT