In: Computer Science
Part I:
Part II
//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
(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