Question

In: Computer Science

Concepts: Classes, Objects, Inheritance, Method overriding (toString), polymorphic methods, Array of objects, loops 1. (Classes and...

Concepts: Classes, Objects, Inheritance, Method overriding (toString), polymorphic methods, Array of objects, loops

1. (Classes and Objects) Hand-write a complete Java class that can be used to create a Car object as described below.

a. A Vehicle has-a: i. Registration number ii. Owner name iii. Price iv. Year manufactured

b. Add all instance variables

c. The class must have getters and setters for all instance variables

d. The class must have two constructors, a no-args and a constructor that receives input parameters for each instance variable.

2. (Inheritance) Hand-write two Java classes, Car and Truck. They are both subclasses of Vehicle.

a. A car has an additional instance variable, number of doors.

b. A truck has an additional instance variable, number of axles

c. Write a constructor that requires input for the instance variables of each class (including registration number and owner name).

d. Write getters and setters for all instance variables for both classes.

3. (Array of objects) The array of Vehicles below, write the static method, findMin which returns the lowest price of any Vehicle in the Array Vehicle [] myVehicles = new Vehicle[10]; public static double findMin(Vehicle [] theVehicles) { }

4.Write a class, TestVehicle, which does the following: 1. Creates one Car object from information entered by the user. 2. Creates one Truck object from information entered by the user. 3. Creates an Array of Vehicles that can hold 10 objects. 4. It creates up to 10 Vehicles objects from information entered by the user 5. Prints the information about each object in the format shown below using the toString methods of the classes

*****PLEASE USE THE CODE BELOW I HAVE SO FAR TO COMPLETE THE PROGRAM****

****************************************************************************************

public class Vehicle {
   //Instance Variables
       private int registrationNumber;
       private String ownerName;
       private double price;
       private int yearManufactured;
   //Constructors
       public Vehicle (){
           this.registrationNumber=0;
           this.ownerName= " ";
           this.price=0;
           this.yearManufactured=0;
          
       }
       public Vehicle (int newRegisteration, String newName, double newPrice, int newMnf) {
           this.registrationNumber = newRegisteration;
           this.ownerName = newName;
           this.price = newPrice;
           this.yearManufactured = newMnf;
       }
   //Get methods
       public int getRegistration () {
           return this.registrationNumber;
           }

           public String getName () {
           return this.ownerName;
           }

           public double getPrice () {
           return this.price;
           }

           public int getYearManufactured () {
           return this.yearManufactured;
           }
           //Mutators or Setters
           public void setRegistration (int newRegistration) {
               this.registrationNumber=newRegistration;
           }

           public void setName (String newName) {
               this.ownerName=newName;
           }
           public void setPrice (double newPrice) {
               this.price=newPrice;
           }

           public void setYearManufactured (int newYearManufactured) {
               this.yearManufactured=newYearManufactured;
           }
       public String toString () {
           return " Vehicle: \nRegistration Number: " +registrationNumber
                   + " Owner Name: "+ownerName+ " Price: "+price+ " Year Manufactured: " +yearManufactured;
       }
      
   }//end of class

**********************************************************************************************

public class Car extends Vehicle {
//Instance Variables
   private int numOfDoors;
   //Constructors
  
public Car() {
   super();
}


}//end of class

Solutions

Expert Solution

CODE IN JAVA:

Vechicle.java file:

public class Vehicle {
   // Instance Variables
   private int registrationNumber;
   private String ownerName;
   private double price;
   private int yearManufactured;

   // Constructors
   public Vehicle() {
       this.registrationNumber = 0;
       this.ownerName = " ";
       this.price = 0;
       this.yearManufactured = 0;

   }

   public Vehicle(int newRegisteration, String newName, double newPrice, int newMnf) {
       this.registrationNumber = newRegisteration;
       this.ownerName = newName;
       this.price = newPrice;
       this.yearManufactured = newMnf;
   }

   // Get methods
   public int getRegistration() {
       return this.registrationNumber;
   }

   public String getName() {
       return this.ownerName;
   }

   public double getPrice() {
       return this.price;
   }

   public int getYearManufactured() {
       return this.yearManufactured;
   }

   // Mutators or Setters
   public void setRegistration(int newRegistration) {
       this.registrationNumber = newRegistration;
   }

   public void setName(String newName) {
       this.ownerName = newName;
   }

   public void setPrice(double newPrice) {
       this.price = newPrice;
   }

   public void setYearManufactured(int newYearManufactured) {
       this.yearManufactured = newYearManufactured;
   }

   public String toString() {
       return " Vehicle: \nRegistration Number: " + registrationNumber + " Owner Name: " + ownerName + " Price: " + price + " Year Manufactured: " + yearManufactured;
   }
   public static double findMin(Vehicle[] theVehicles) {
       int n = theVehicles.length;
       double low = theVehicles[0].getPrice();
       for(int i=1;i<n;i++) {
           if(theVehicles[i].getPrice()<low)
               low = theVehicles[i].getPrice();
       }
       return low;
   }

}

Car.java file:


public class Car extends Vehicle {
//Instance Variables
   private int numOfDoors;
   // Constructors

   public Car(int newRegisteration, String newName, double newPrice, int newMnf,int numOfDoors) {
       super(newRegisteration,newName,newPrice,newMnf);
       this.numOfDoors = numOfDoors;
   }

   public String toString() {
       return " Car: \nRegistration Number: " + this.getRegistration() + " Owner Name: " + this.getName() + " Price: " + getPrice() + " Year Manufactured: " + getYearManufactured()+" numOfDoors=" + numOfDoors ;
   }

   public int getNumOfDoors() {
       return numOfDoors;
   }

   public void setNumOfDoors(int numOfDoors) {
       this.numOfDoors = numOfDoors;
   }
  

}

Truck.java file:


public class Truck extends Vehicle{
   private int numOfAxles;
   Truck(int newRegisteration, String newName, double newPrice, int newMnf,int numOfAxles){
       super(newRegisteration,newName,newPrice,newMnf);
       this.numOfAxles = numOfAxles;
   }
   public int getNumOfAxles() {
       return numOfAxles;
   }

   public void setNumOfAxles(int numOfAxles) {
       this.numOfAxles = numOfAxles;
   }
   public String toString() {
       return " Truck: \nRegistration Number: " + this.getRegistration() + " Owner Name: " + this.getName() + " Price: " + getPrice() + " Year Manufactured: " + getYearManufactured()+" numberOfAxles=" + numOfAxles ;
   }
}
TestVehicle.java file:

import java.util.Scanner;

public class TestVehicle {

   public static void main(String[] args) {
       int registrationNumber;
       String ownerName;
       double price;
       int yearManufactured;
       int numOfDoors;
       int numOfAxles;
       Scanner sc = new Scanner(System.in);
       System.out.print("Enter Registratin number:");
       registrationNumber = sc.nextInt();
       System.out.print("Enter Owner name :");
       ownerName = sc.nextLine();
       ownerName = sc.nextLine();
       System.out.print("Enter price:");
       price = sc.nextDouble();
       System.out.print("Enter year manfactured:");
       yearManufactured = sc.nextInt();
       System.out.print("Enter number of doors:");
       numOfDoors = sc.nextInt();
       Car c = new Car(registrationNumber, ownerName, price, yearManufactured, numOfDoors);
       System.out.print("Enter Registratin number:");
       registrationNumber = sc.nextInt();
       System.out.print("Enter Owner name :");
       ownerName = sc.nextLine();
       ownerName = sc.nextLine();
       System.out.print("Enter price:");
       price = sc.nextDouble();
       System.out.print("Enter year manfactured:");
       yearManufactured = sc.nextInt();
       System.out.print("Enter number of axles:");
       numOfAxles = sc.nextInt();
       Truck t = new Truck(registrationNumber, ownerName, price, yearManufactured, numOfAxles);
       System.out.println(c);
       System.out.println();
       System.out.println(t);
       System.out.println();
       Vehicle v[] = new Vehicle[10];
       for (int i = 0; i < 10; i++) {
           System.out.println("Enter vehicle information:");
           System.out.print("Enter Registratin number:");
           registrationNumber = sc.nextInt();
           System.out.print("Enter Owner name :");
           ownerName = sc.nextLine();
           ownerName = sc.nextLine();
           System.out.print("Enter price:");
           price = sc.nextDouble();
           System.out.print("Enter year manfactured:");
           yearManufactured = sc.nextInt();
           Vehicle vh = new Vehicle(registrationNumber, ownerName, price, yearManufactured);
           v[i] = vh;
       }
       System.out.println("Below is all 10 vehicles information:");
       for (int i = 0; i < 10; i++)
           System.out.println(v[i]);

   }

}

OUTPUT:


Related Solutions

Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should...
Lesson is on Inheritance and overriding the ToString ( ) method in Java: The program should consist of 3 classes and will calculate an hourly paycheck.    - The superclass will contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method.    - The subclass will calculate a paycheck for an hourly employee and will inherit from the superclass.   ...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide...
Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods. -Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract. -Extend Customer to two subclasses: FlightCustomer, and RetailCustomer -FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write...
IN JAVA Inheritance Using super in the constructor Using super in the method Method overriding Write an application with five classes Vehicle, Car, Bus, Truck, and Tester class with main method in it. The following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassenges, isConvertable, numberSeats, maxWeightLoad, and numberAxels. Characteristics that are applicable to all vehicles should be instance variables in the Vehicle class. The others should be in the class(s) where they belong. Class vehicle should have...
/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the...
/* Work to do: 1) add toString method to BankAccount and SavingsAccount classes 2) Override the withdraw() in SavingsAccount so that it will not withdraw more money than is currently in the account. 3) Provide constructors for SavingsAccount 4) Add this feature to SavingsAccount: If you withdraw more than 3 times you are charged $10 fee and the fee is immediately withdrawn from your account.once a fee is deducted you get another 3 free withdrawals. 5) Implement the Comparable Interface...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create...
JAVA: *USING INHERITANCE *USING SUPER IN TH CONSTRUCTOR *USING SUPER IN THE METHOD *METHOD OVERRIDING Create an application with 5 classes: 1.) Vehicle 2.) Car 3.) Bus 4.) Truck 5.) Tester Following characteristics should be used: make, weight, height, length, maxSpeed, numberDoors, maxPassengers, isCpnvertable, numberSeats, maxWeightLoad, numberAxels **Class Vehicle: should have a constructor that initializes all of it's data **Classes Car, Bus, Truck: will have constructors that resuse their parents constructors and provide additional code for initalizing their specific data...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt...
Java GPA calculator. Apply GUI, methods, exception handling, classes, objects, inheritance etc. The program should prompt user for input. When done it should give an option to compute another GPA or exit.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create...
Please do this in C++ Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT