Create a class named Vehicle . The class has the following five member variables
ID: 3821919 • Letter: C
Question
Create a class named Vehicle. The class has the following five member variables:
• Vehicle Name
• Vehicle number
• Sale Tax
• Unit price
• Total price
Include set (mutator) and get (accessor) functions for each field except the total price field. The set function prompt the user for values for each field. This class also needs a function named computePrice() to compute the total price (quantity times unit price + salesTax) and a function to display the field values. [Note: Sale tax is 10%, sale tax = Unit price * saleTax)
Create a subclass named VehicleOrder that overrides computePrice() function by adding a shipping and handling charge of $12.00.
Write an application named UseVehicle that instantiates an object of each of these classes.
Prompt the user for data for the Vehicle object, and display the results, then prompt the user for data for the VehicleOrder object, and display the results.
Explanation / Answer
import java.util.Scanner;
public class Vehicle {
String vehicleName;
String vehicleNumber;
int saleTax;
int unitPrice;
float totalPrice;
/*Accesors*/
public String getvehicleName()
{
return vehicleName;
}
public String getvehicleNumber()
{
return vehicleNumber;
}
public int getsaleTax()
{
return saleTax;
}
public int getunitPrice()
{
return unitPrice;
}
/*Mutators*/
public void setvehicleName()
{
System.out.println("Enter Vehicle Name");
Scanner scanner = new Scanner(System.in);
String vName = scanner.nextLine();
this.vehicleName = vName;
}
public void setVehicleNumber(String vehicleNumber)
{
System.out.println("Enter Vehicle Number");
Scanner scanner = new Scanner(System.in);
String vNumber = scanner.nextLine();
this.vehicleNumber = vNumber;
}
public void setSaleTax(String saleTax)
{
System.out.println("Enter sales tax");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
this.saleTax=i;
}
public void setUnitPrice(String unitPrice)
{
System.out.println("Enter Unit Price");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
this.unitPrice=i;
}
public void computePrice(int unitQuantity)
{
totalPrice = (unitQuantity*unitPrice)+((saleTax/100) * unitPrice);
}
public void funPrint()
{
System.out.println("Vehicle Name:"+ vehicleName);
System.out.println("Vehicle Number:" + vehicleNumber);
System.out.println("Sales Tax:" + saleTax);
System.out.println("Unit Price:" + unitPrice);
System.out.println("Total Price:"+ totalPrice);
}
}
/* Sub class Vehicle Order */
class VehicleOrder extends Vehicle
{
public void computePrice(int unitQuantity)
{
totalPrice = (unitQuantity*unitPrice)+((saleTax/100) * unitPrice) + 12;
}
}
/* Another Class which will use the Vehicle and Vehicle Order */
/*This class has object of only Vehicle Class you can create object of Vehicle Order too and fetch value from accessor method of Vehicle Class */
public class UseVehicle {
Vehicle obj = new Vehicle();
obj.setVehicleName();
obj.setVehicleNumber();
obj.setSaleTax();
obj.setUnitPrice();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.