Question

In: Computer Science

Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...

Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods for all instance variables • A method called printDetails that prints the Vehicle details e.g. “The vehicle details are:” followed by all instance variables. The output must be formatted for readability In the main method write the code to: • Create 2 vehicles, one using the default constructor, the other using the constructor that initialises all the instance variables for the vehicle. • Demonstrate the use of one accessor method, and one mutator method for one of the vehicles you created. • Print the vehicle’s details using the printDetails method for one of the vehicles you created. Part 2 – Inheritance, collections and polymorphism Create a new Java project called usernamePart2 in NetBeans. Select the option to create a main method. Create a new class called Car. Create a second class called Vehicle. Copy the code from the Vehicle class you created in Part 1 into the new Vehicle class. Modify the new Car class so that it extends Vehicle (Vehicle is the superclass, Car is the subclass). In the Car class write the code for: • The instance variables that store the transmission type, and body type • Accessor and mutator methods for the 2 instance variables • A default constructor, and a second constructor that initialises all the instance variables in the Car and the Vehicle classes using the super keyword • The Vehicle class has a method called printDetails that prints the Vehicle details. Override the printDetails method in the Car class and print all of the car’s details. The printDetails method in the Car class must use the super keyword to call the printDetails method in the Vehicle class. • The Car class must also demonstrate the use of overloaded methods In the main method write the code to: • Declare an ArrayList with a type parameter of Car 3 • Add at least 2 Cars to the ArrayList • Use an Iterator (java.util.Iterator) to loop through the cars in the ArrayList and print out some of their details. Please note that use of any other kind of loop will not receive any marks. • Check if the ArrayList contains a particular car • Get a car from the ArrayList • Remove a car from the ArrayList • Print the size of the ArrayList • Clear the ArrayList Please note that collections are covered in Topic 3 and 4. You may need to wait until we have covered these topics to implement your ArrayList. Polymorphism In the class that contains the main method, create a second method that takes a Vehicle as a parameter and write the code to print out the vehicle’s fuel type using one of the vehicle’s accessor methods. Then create an object of type Car and an object of type Vehicle in the main method and use the method you have just created to demonstrate polymorphism. Part 3 – Abstract classes Create a new Java project called usernamePart3 in NetBeans. Select the option to create a main method. Create a new class called Car. Create a second new class called Vehicle. Rewrite your code from Part2 so that: • The Vehicle class is abstract • Car extends Vehicle • The Vehicle class contains at least one abstract method All methods, constructors and instance variables that were in the Car and Vehicle classes in part 2 must be included in part 3. You need to rewrite the Car and Vehicle classes from part 2 so that Vehicle is abstract and there are multiple ways to create a Car. Check your code works by creating a new Car in your main method. Part 4 – Interfaces Create a new Java project called usernamePart4 in NetBeans. Select the option to create a main method. Create a new class called Car. Create a new interface called Vehicle. Rewrite your code from Part 2 so that: • Vehicle is an interface 4 • Car implements Vehicle • The Vehicle interface contains at least one abstract methods All methods, constructors and instance variables that were in the Car and Vehicle classes in part 2 must be included in part 4. You need to rewrite the Car and Vehicle classes from part 2 so that Vehicle is an interface and there are multiple ways to create a Car. In the main method of your project write the code to: • Declare a Stack with a type parameter of Car • Add at least 3 cars to the Stack • Demonstrate the use of: o peek() o pop() o empty() Part 5 – UML Create a word document called usernamePart5. Draw 2 UML diagrams to show the inheritance relationship for the Vehicle and Car in part 3 and part 4. Make sure that each diagram has a heading and the header of the word document contains your name and student ID.

Solutions

Expert Solution

Part1

Program:-

//Create a class Vehicle
class Vehicle{
   //Instance variables
   private String make;
   private String model;
   private String colour;
   private String fuelType;
   //Member functions
   //Default constructor
   public Vehicle() {
       make="";
       model="";
       colour="";
       fuelType="";
   }
   //Parameterized constructor
   public Vehicle(String make,String model,String colour,String fuelType) {
       this.make=make;
       this.model=model;
       this.colour=colour;
       this.fuelType=fuelType;
   }
   //Getters and Setters
   public String getMake() {
       return make;
   }
   public void setMake(String make) {
       this.make = make;
   }
   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model = model;
   }
   public String getColour() {
       return colour;
   }
   public void setColour(String colour) {
       this.colour = colour;
   }
   public String getFuelType() {
       return fuelType;
   }
   public void setFuelType(String fuelType) {
       this.fuelType = fuelType;
   }
   //Display details
   public void printDetails() {
       System.out.println("The vehicle details are:\nMake: "+make+"\nModel: "+model+"\nColour: "+colour+"\nFuel type: "+fuelType);
   }
}
public class rghanbarPart1 {

   public static void main(String[] args) {
       //Create objects of vehicles
       Vehicle vehicle1=new Vehicle();
       Vehicle vehicle2=new Vehicle("Ford","Endevour","Deep blue","Petrol");
       //Setter check
       vehicle1.setColour("Blue");
       //Getter check
       System.out.println("Colour of the vehicle1 = "+vehicle1.getColour());
        //Print check
       vehicle2.printDetails();
   }

}

Output:-

Colour of the vehicle1 = Blue
The vehicle details are:
Make: Ford
Model: Endevour
Colour: Deep blue
Fuel type: Petrol

--------------------------------------------------------------------------------------------------

Part2

Program:-

import java.util.ArrayList;
//Create super class Vehicle
class Vehicle{
   //Instance variables
   private String make;
   private String model;
   private String colour;
   private String fuelType;
   //Member functions
   //Default constructor
   public Vehicle() {
       make="";
       model="";
       colour="";
       fuelType="";
   }
   //Parameterized constructor
   public Vehicle(String make,String model,String colour,String fuelType) {
       this.make=make;
       this.model=model;
       this.colour=colour;
       this.fuelType=fuelType;
   }
   //Getters and Setters
   public String getMake() {
       return make;
   }
   public void setMake(String make) {
       this.make = make;
   }
   public String getModel() {
       return model;
   }
   public void setModel(String model) {
       this.model = model;
   }
   public String getColour() {
       return colour;
   }
   public void setColour(String colour) {
       this.colour = colour;
   }
   public String getFuelType() {
       return fuelType;
   }
   public void setFuelType(String fuelType) {
       this.fuelType = fuelType;
   }
   //Display details
   public void printDetails() {
       System.out.println("The vehicle details are:\nMake: "+make+"\nModel: "+model+"\nColour: "+colour+"\nFuel type: "+fuelType);
   }
}
//Create sub class Car
class Car extends Vehicle{
   //Instance variables
   private String transmissionType;
   private String bodyType;
   //Default constructor
   public Car() {
       super();
       transmissionType="";
       bodyType="";
   }
   //Parameterized constructor
   public Car(String make,String model,String colour,String fuelType,String transmissionType,String bodyType) {
       super(make,model,colour,fuelType);
       this.transmissionType=transmissionType;
       this.bodyType=bodyType;
   }
   //Getters and setters
   public String getTransmissionType() {
       return transmissionType;
   }
   public void setTransmissionType(String transmissionType) {
       this.transmissionType = transmissionType;
   }
   public String getBodyType() {
       return bodyType;
   }
   public void setBodyType(String bodyType) {
       this.bodyType = bodyType;
   }
   //Display details
   public void printDetails() {
       super.printDetails();
       System.out.println("Transmission type: "+transmissionType+"\nBodyType: "+bodyType+"\n");
   }
}
public class usernamePart2 {

   public static void main(String[] args) {
       //Declare an arraylist of type car
       ArrayList<Car>cars=new ArrayList<Car>();
       //Add 2 cars
       cars.add(new Car("Ford","Endevour","Deep blue","Petrol","Semi automatic","Metalic Body"));
       cars.add(new Car("Maruti","Swift","White","Diesel","Automatic","Three Box Design"));
       //Create an iterator
       java.util.Iterator<Car> iter =cars.iterator();
       while (iter.hasNext()) {
                iter.next().printDetails();
          }
       //Search for a car using model
       iter =cars.iterator();
       boolean flag=false;
       while (iter.hasNext()) {
              Car testCar=iter.next();
          
               if(testCar.getModel().equals("Swift")) {
                   System.out.println("Car found in the list!!!");
                   flag=true;
                   break;
               }
          }
       if(flag==false) {
           System.out.println("Car not found in the list!!!");
       }
       flag=false;
       //Get a car from list using model
       iter =cars.iterator();
       System.out.println("\nGet a car:-");
       while (iter.hasNext()) {
               Car car=iter.next();
               if(car.getMake().equalsIgnoreCase("Ford")) {
                   car.printDetails();
                   flag=true;
                   break;
               }
          }
       if(flag==false) {
           System.out.println("Car not found in the list!!!");
       }
       //Remove a car from list using model
       flag=false;
       iter =cars.iterator();
       while (iter.hasNext()) {
               Car car=iter.next();
               if(car.getModel().equalsIgnoreCase("Endevour")) {
                   cars.remove(car);
                   flag=true;
                   break;
               }
          }
       if(flag==false) {
           System.out.println("Car not found in the list!!!");
       }
       //Display after removal
       iter =cars.iterator();
           System.out.println("Display after removal");
               while (iter.hasNext()) {
                    iter.next().printDetails();
              }
   }

}

Output

The vehicle details are:
Make: Ford
Model: Endevour
Colour: Deep blue
Fuel type: Petrol
Transmission type: Semi automatic
BodyType: Metalic Body

The vehicle details are:
Make: Maruti
Model: Swift
Colour: White
Fuel type: Diesel
Transmission type: Automatic
BodyType: Three Box Design

Car found in the list!!!

Get a car:-
The vehicle details are:
Make: Ford
Model: Endevour
Colour: Deep blue
Fuel type: Petrol
Transmission type: Semi automatic
BodyType: Metalic Body

Display after removal
The vehicle details are:
Make: Maruti
Model: Swift
Colour: White
Fuel type: Diesel
Transmission type: Automatic
BodyType: Three Box Design

---------------------------------------------------------------------

Part 3

Program

import java.util.ArrayList;
abstract class Vehicle{
   //Instance variables
       private String make;
       private String model;
       private String colour;
       private String fuelType;
       //Member functions
       //Default constructor
       public Vehicle() {
           make="";
           model="";
           colour="";
           fuelType="";
       }
       //Parameterized constructor
       public Vehicle(String make,String model,String colour,String fuelType) {
           this.make=make;
           this.model=model;
           this.colour=colour;
           this.fuelType=fuelType;
       }
       //Getters and Setters
       public String getMake() {
           return make;
       }
       public void setMake(String make) {
           this.make = make;
       }
       public String getModel() {
           return model;
       }
       public void setModel(String model) {
           this.model = model;
       }
       public String getColour() {
           return colour;
       }
       public void setColour(String colour) {
           this.colour = colour;
       }
       public String getFuelType() {
           return fuelType;
       }
       public void setFuelType(String fuelType) {
           this.fuelType = fuelType;
       }
       //Display details
       public abstract void printDetails();
}
class Car extends Vehicle{
   //Instance variables
       private String transmissionType;
       private String bodyType;
       //Default constructor
       public Car() {
           super();
           transmissionType="";
           bodyType="";
       }
       //Parameterized constructor
       public Car(String make,String model,String colour,String fuelType,String transmissionType,String bodyType) {
           super(make,model,colour,fuelType);
           this.transmissionType=transmissionType;
           this.bodyType=bodyType;
       }
       //Getters and setters
       public String getTransmissionType() {
           return transmissionType;
       }
       public void setTransmissionType(String transmissionType) {
           this.transmissionType = transmissionType;
       }
       public String getBodyType() {
           return bodyType;
       }
       public void setBodyType(String bodyType) {
           this.bodyType = bodyType;
       }
       //Display details
       public void printDetails() {
           System.out.println("The Car details are:\nMake: "+super.getMake()+"\nModel: "+super.getModel()+"\nColour: "+super.getColour()+"\nFuel type: "+super.getFuelType());
           System.out.println("Transmission type: "+transmissionType+"\nBodyType: "+bodyType+"\n");
       }
}
public class UsernamePart3 {

   public static void main(String[] args) {
       //Declare an arraylist of type vehicle
       ArrayList<Vehicle>cars=new ArrayList<Vehicle>();
       //Add 2 cars
       cars.add(new Car("Ford","Endevour","Deep blue","Petrol","Semi automatic","Metalic Body"));
       cars.add(new Car("Maruti","Swift","White","Diesel","Automatic","Three Box Design"));
       //Create an iterator
       java.util.Iterator<Vehicle> iter =cars.iterator();
       while (iter.hasNext()) {
              iter.next().printDetails();
        }
   }

}

Output

The Car details are:
Make: Ford
Model: Endevour
Colour: Deep blue
Fuel type: Petrol
Transmission type: Semi automatic
BodyType: Metalic Body

The Car details are:
Make: Maruti
Model: Swift
Colour: White
Fuel type: Diesel
Transmission type: Automatic
BodyType: Three Box Design


------------------------------------------------

Part4

import java.util.Stack;

//Create an interface Vehicle
interface Vehicle{
   void printDetails();
}
class Car implements Vehicle{
       //Instance variables
        private String make;
        private String model;
        private String colour;
        private String fuelType;
       private String transmissionType;
       private String bodyType;
       //Default constructor
       public Car() {
           make="";
           model="";
           colour="";
           fuelType="";
           transmissionType="";
           bodyType="";
       }
       //Parameterized constructor
       public Car(String make,String model,String colour,String fuelType,String transmissionType,String bodyType) {
           this.make=make;
           this.model=model;
           this.colour=colour;
           this.fuelType=fuelType;
           this.transmissionType=transmissionType;
           this.bodyType=bodyType;
       }
       //Getters and setters
       public String getTransmissionType() {
           return transmissionType;
       }
       public void setTransmissionType(String transmissionType) {
           this.transmissionType = transmissionType;
       }
       public String getBodyType() {
           return bodyType;
       }
       public String getMake() {
           return make;
       }
       public void setMake(String make) {
           this.make = make;
       }
       public String getModel() {
           return model;
       }
       public void setModel(String model) {
           this.model = model;
       }
       public String getColour() {
           return colour;
       }
       public void setColour(String colour) {
           this.colour = colour;
       }
       public String getFuelType() {
           return fuelType;
       }
       public void setFuelType(String fuelType) {
           this.fuelType = fuelType;
       }
       public void setBodyType(String bodyType) {
           this.bodyType = bodyType;
       }
       //Display details
       public void printDetails() {
           System.out.println("The Car details are:\nMake: "+make+"\nModel: "+model+"\nColour: "+colour+"\nFuel type: "+fuelType);
           System.out.println("Transmission type: "+transmissionType+"\nBodyType: "+bodyType+"\n");
       }
}
public class usernamePart4 {

   public static void main(String[] args) {
       //Create a car stack
       Stack<Car>cars=new Stack<Car>();
       //Push check
       cars.push(new Car("Ford","Endevour","Deep blue","Petrol","Semi automatic","Metalic Body"));
       cars.push(new Car("Maruti","Swift","White","Diesel","Automatic","Three Box Design"));
       //Peek check
       System.out.println("Peek check");
       cars.peek().printDetails();
       //Pop check
       System.out.println("\nPopcheck");
       cars.pop();
       cars.peek().printDetails();
       //Empty check
       cars.pop();
       if(cars.isEmpty()) {
           System.out.println("\nNo cars in the stack!!");
       }
       else {
           System.out.println("\nCars present in the stack!!");
       }

   }

}

Output

Peek check
The Car details are:
Make: Maruti
Model: Swift
Colour: White
Fuel type: Diesel
Transmission type: Automatic
BodyType: Three Box Design


Popcheck
The Car details are:
Make: Ford
Model: Endevour
Colour: Deep blue
Fuel type: Petrol
Transmission type: Semi automatic
BodyType: Metalic Body


No cars in the stack!!

------------------------------------------------------------------

Screenshot

---------------------------------------------------------

Note:-

Due to time limit i added 4 parts and skip 5th part


Related Solutions

Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data...
Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data types to store the following information: The names of the bride and groom The total number of guests at the wedding The square footage of the location (must be accurate to 0.1 square feet). The names of each song in the DJ's playlist. You should use an ArrayList of Strings to store this, and the user should be able to enter as many song...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student gradebook using a two-dimensional array. It should allow the user to enter the number of students and the number of assignments they wish to enter grades for. This input should be used in defining the two-dimensional array for your program. For example, if I say I want to enter grades for 4 students and 5 assignments, your program should define a 4 X...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and...
Objectives: 1. Create classes to model objects Instructions: 1. Create a new folder named Lab6 and save all your files for this lab into this new folder. 2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs 3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide. Problems [30 marks] Problem 1: The Account class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT