Lab 01 - solution PDF

Title Lab 01 - solution
Author Cường Nguyễn
Course Intermediate Object-Oriented Programming
Institution La Trobe University
Pages 5
File Size 143.4 KB
File Type PDF
Total Downloads 643
Total Views 978

Summary

Download Lab 01 - solution PDF


Description

Laboratory 01: Inheritance // Laboratory 01 Part A Question 1 public void display() { System.out.println("id: " + id + "\nname: " + name); } // Laboratory 01 Part A Question 2 in Student.java public void display() { super.display(); System.out.println("course: " + course); } in InternationalStudent.java public void display() { super.display(); System.out.println("country: " + country); } in Staff.java public void display() { super.display(); System.out.println("department: " + department); } // Laboratory 01 Part A Question 3 Need a class method with body something like:

Person[] people = new Person[8]; people[0] = ... people[1] = people[2] = etc. then iterate along the array calling the display() method on each element. We can have an array of superclass references refer to objects of subclass type. Calling display() illustrates polymorphism. ------------------------------------------------------------------------------// Laboratory 01 Part B Question 1 public class RailroadCar { private String id; private double tareWeight; public RailroadCar(String id, double tareWeight) { this.id = id; this.tareWeight = tareWeight; } public double getTareWeight()

{ return tareWeight; } public double calculateTotalWeight() { return tareWeight; } public void displayDetails() { System.out.println("id: " + id + "\ntareWeight: " + tareWeight); } } // Laboratory 01 Part B Question 2 public class PassengerCarriage extends RailroadCar { private final double PASSENGER_WEIGHT = .08; private final double LUGGAGE_WEIGHT = .025; private int numberOfPassengers; public PassengerCarriage(String id, double tareWeight, int numberOfPassengers) { super(id, tareWeight); this.numberOfPassengers = numberOfPassengers; } public double calculateTotalWeight() { return getTareWeight() + numberOfPassengers * (PASSENGER_WEIGHT + LUGGAGE_WEIGHT); } public void displayDetails() { super.displayDetails(); System.out.println("numberOfPassengers: " + numberOfPassengers); } } // Laboratory 01 Part B Question 3 public class GoodsCar extends RailroadCar { private double goodsWeight; private String goodsDescription; public GoodsCar(String id, double tareWeight, double goodsWeight, String goodsDescription) { super(id, tareWeight); this.goodsWeight = goodsWeight; this.goodsDescription = goodsDescription; } public double calculateTotalWeight() { return getTareWeight() + goodsWeight; }

public void displayDetails() { super.displayDetails(); System.out.println("goodsWeight: " + goodsWeight + "\ngoodsDescription: " + goodsDescription); } } // Laboratory 01 Part B Question 4 import java.util.*; public class Tester1 { public static void main(String[] args) { RailroadCar[] train = new RailroadCar[8]; train[0] train[1] train[2] train[3] train[4] train[5]

= = = = = =

new new new new new new

RailroadCar("Thomas", 10.5); RailroadCar("Fat Controller", 200); PassengerCarriage("Ghan", 7, 50); PassengerCarriage("Orient Express", 4, 10); GoodsCar("Wheat", 4, 4, "Western NSW"); GoodsCar("Feathers", 4, 0.5, "Downs Country");

double totalWeight = 0; for (int i = 0; i < train.length; ++i) { if (train[i] != null) { train[i].displayDetails(); System.out.println(); totalWeight += train[i].calculateTotalWeight(); } } System.out.println("Total weight: " + totalWeight + " tones!"); } } // Laboratory 01 Part B Question 5 public class RefrigeratedUnit extends GoodsCar { private double coolantWeight; public RefrigeratedUnit(String id, double tareWeight, double goodsWeight, String goodsDescription, double coolantWeight) { super(id, tareWeight, goodsWeight, goodsDescription); this.coolantWeight = coolantWeight; } public double calculateTotalWeight() { return super.calculateTotalWeight() + coolantWeight; } public void displayDetails() { super.displayDetails(); System.out.println("coolantWeight: " + coolantWeight);

} } // Laboratory 01 Part B Question 6 import java.util.*; public class Tester2 { public static void main(String[] args) { RailroadCar[] train = new RailroadCar[8]; train[0] train[1] train[2] train[3] train[4] train[5] train[6]

= = = = = = =

new new new new new new new

RailroadCar("Thomas", 10.5); RailroadCar("Fat Controller", 200); PassengerCarriage("Ghan", 7, 50); PassengerCarriage("Orient Express", 4, 10); GoodsCar("Wheat", 4, 4, "Western NSW"); GoodsCar("Feathers", 4, 0.5, "Downs Country"); RefrigeratedUnit("XXXX", 4, 6, "Top End, Australia",

1.5); train[7] = new RefrigeratedUnit("MMMM", 3, 5, "Milk", 2); double totalWeight = 0; for (int i = 0; i < train.length; ++i) { if (train[i] != null) { train[i].displayDetails(); System.out.println(); totalWeight += train[i].calculateTotalWeight(); } } System.out.println("Total weight: " + totalWeight + " tones!"); } } // Laboratory 01 Part B Question 7 private void addCar(char option) { System.out.print("Enter id (String): "); String id = keyboard.nextLine(); System.out.print("Enter tare weight(double): "); double tareWeight = keyboard.nextDouble(); keyboard.nextLine(); //clear \n switch(option) { case 'O': train[numberOfCars] = new RailroadCar(id, tareWeight); break; case 'P': System.out.print("Enter number of passengers (int): "); int numberOfPassengers = keyboard.nextInt(); keyboard.nextLine(); //clear \n train[numberOfCars] = new PassengerCarriage(id, tareWeight, numberOfPassengers); break; case 'G': case 'R': System.out.print("Enter goods weight(double): ");

double goodsWeight = keyboard.nextDouble(); keyboard.nextLine(); //clear \n System.out.print("Enter description (String): "); String description = keyboard.nextLine(); if (option == 'G') { train[numberOfCars] = new GoodsCar(id, tareWeight, goodsWeight, description); } else { System.out.print("Enter coolant weight(double): "); double coolantWeight = keyboard.nextDouble(); keyboard.nextLine(); //clear \n train[numberOfCars] = new RefrigeratedUnit(id, tareWeight, goodsWeight, description, coolantWeight); } break; default : System.out.println("Programming error"); } numberOfCars++; } private void displayRollingStock() { double totalWeight = 0; System.out.println(); for (int i = 0; i < numberOfCars; ++i) { train[i].displayDetails(); totalWeight += train[i].calculateTotalWeight(); } System.out.println(); System.out.println("Total weight: " + totalWeight + " tones!"); }...


Similar Free PDFs