DOC PREVIEW
UW-Madison COMPSCI 302 - Cars and Parking Lots Exercise

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS302 Cars and Parking Lots1.) Write a code fragment that creates a ParkingLot object named, lot60, with 121 stalls. Add a C...2.) Write a code fragment using a loop that adds 22 cars to lot60. The cars have the following li...3.) Write a class method named, getLotCount, that is passed a ParkingLot object and returns the i...4.) Write a class method named, findCar, that is passed a ParkingLot object and an integer licens...CS 302 Spring 2001: Lectures 1 & 2  2000,2001 James D. SkrentnyDiscussion 8SOLUTIONCS302 Cars and Parking LotsThe goal of this exercise is to work with list iterators and control statements.Consider the following classes and methods:• classCarCar (int license) // constructs a car with the specified licenseint getLicense() // returns the license plate of the car• class CarListIterator//no constructor is to be used for this classboolean hasMoreCars() // returns true if there are more cars in this listCar nextCar() // returns the next car in this list• class ParkingLotParkingLot (int stalls) // constructs a parking lot with the// specified number of stallsboolean addCar (Car car) // adds car to the parking lotint getStalls () // returns the parking lot’s number of stallsCarListIterator getCarListIterator()// returns a CarListIterator for the parking lot1.) Write a code fragment that creates a ParkingLot object named, lot60, with 121 stalls. Add aCar object with the license plate number 11. ParkingLot lot60 = new ParkingLot( 121 ); lot60.addCard(new Car( 11 ));2.) Write a code fragment using a loop that adds 22 cars to lot60. The cars have the followinglicense plate numbers:the first car has the license plate 101,the second car has the plate 102,...,the last car that has the plate 122. ParkingLot lot60 = new ParkingLot( 121 );for ( int i = 1; i <= 22 ; i++ ) { Car car = new Car( 100 + i ); lot.addCar( car );}3.) Write a class method named, getLotCount, that is passed a ParkingLot object and returnsthe integer number of cars that are parked in the lot. If the lot is full it should display the mes-sage “Full Lot” in the console window (use System.out.println).CS 302 Spring 2001: Lectures 1 & 2  2000,2001 James D. SkrentnyDiscussion 8SOLUTION public static int getLotCount(ParkingLot p) { int count = 0; CarListIterator cli = p.getCarListIterator(); while(cli.hasMoreCars()) { cli.nextCar(); count++; } if (count == p.getStalls())System.out.println(“Full Lot”); return count; }4.) Write a class method named, findCar, that is passed a ParkingLot object and an integerlicense number. The method returns true if and only if there is a car in the lot with the specifiedlicense plate number.public static boolean findCar(ParkingLot p, int licenseNumber) { CarList cl = p.getCarsList(); while (cl.hasMoreCars() ) { Car c = cl.nextCar(); if ( c.getLicense() == licenseNumber ) return true; } return false;


View Full Document

UW-Madison COMPSCI 302 - Cars and Parking Lots Exercise

Download Cars and Parking Lots Exercise
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Cars and Parking Lots Exercise and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Cars and Parking Lots Exercise 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?