Lab 6 OOP
Lab 6 OOP
Exercise 1
• Define a class, called Car, that contains a method to calculate the fuel
consumption of a car, in liters per 100 kilometers
• Class variables
• private double startKm; //Starting odometer reading
• private double endKm; //Ending odometer reading
• private double liters; //Liters of fuel used between the readings
• Constructor
• public Car(double startOdo, double endOdo, double litersUsed)
• Creates a new instance of a Car object with the starting and ending
odometer readings and the number of liters of fuel consumed
Exercise 1
• Method
• public double computeConsumption()
• Calculates and returns the fuel consumption of a car, in liters per 100
kilometers
• In another class, called CarTest, that contains the main method,
create 2 instances for Car objects, compute and display for each car
the fuel consumption
Exercise 2
• Consider a fleet consisting of two cars: a town car and a sports utility
vehicle
• The state of a Fleet object is held in its two instance variables
• These variables refer to Car objects
• Class Fleet
• A class that holds two Car objects
• private Car town;
• private Car suv;
Exercise 2
• Constructor
• public Fleet(double Car1StartOdo, double Car1EndOdo, double Car1Liters,
double Car2StartOdo, double Car2EndOdo, double Car2Liters)
• Creates a new instance of a Fleet object with the starting and ending
odometer readings and the number of liters of fuel consumed for
each car
• The constructor for Fleet class is given (in its parameters) all the
information it needs to build two cars. It uses the Car constructor for
this (twice)
Exercise 2
• Method
• public double averageConsumption()
• Calculates and returns the average fuel consumption of the fleet
• There are two cars in the fleet, so the average consumption of the
fleet is the sum of the fuel consumptions for each car, divided by two
• In another class, called FleetTest, that contains the main method,
create an instance for a fleet object, compute and display the average
fuel consumption for the fleet
• In this exercise, you also need the code for the Car class
Exercise 3
• Define a class, called Account, for managing accounts in a bank
• The constructor creates a new checking account and initializes it with
its account number, the account holder’s name, and the starting
balance
• Class variables
• private String accountNumber;
• private String accountHolder;
• private double balance;
Exercise 3
• Constructor
• public Account(String accNumber, String accHolder, double bal)
• Methods
• public double getBalance()
• public void processDeposit(double amount)
• The method processDeposit adds an amount to the current balance. The
current balance can be negative or positive
• public void processCheck(double amount)
• The method processCheck subtracts the amount of the check from the
current balance. Overdrafts are allowed, so the balance can become negative.
However, if the balance is less than 10000 before the check is processed, 15 is
charged for each check
Exercise 3
• In another class, called AccountTest, that contains the main method,
create two objects, account1 and account2 of type Account
• Display the balance of each account
• Deposit 2000 in account1 and process a check of 1500 in the
account2
• Display the new balance for each account