In: Computer Science
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.
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