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 JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
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...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
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...
This class contains the main method. In this class you should: Ask the user for the...
This class contains the main method. In this class you should: Ask the user for the name, job tilte, and salary. Instantiate a Employee object with those parameters. Use the accessor methods to check the value of all three instance variables and print them. Ask the user to enter a percentage to raise the salary by. Use the raise method to increase the salary. Use the salary accessor check the new value of salary and print it.
Write a complete java program that Define a class named sample containing: A method named division...
Write a complete java program that Define a class named sample containing: A method named division that receives two integers x and y and returns the division of the two numbers. It must handle any possible exceptions. A method named printArray that receives an array of double arr and an integer index and prints the element in the positon index. It must handle any possible exceptions. main method that recievs input from user and repeat if wrong input. Then it...
Create a Java program. The class name for the program should be 'EncryptText'. In the main...
Create a Java program. The class name for the program should be 'EncryptText'. In the main method you should perform the following: You should read a string from the keyboard using the Scanner class object. You should then encrypt the text by reading each character from the string and adding 1 to the character resulting in a shift of the letter entered. You should output the string entered and the resulting encrypted string. Pseudo flowchart for additional code to be...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT