Sie sind auf Seite 1von 7

CS181P Java Programming 1 Take Home Exam (Lec and Lab) Due Date: December 12, 2012 5PM

Answer all the problems. Provide necessary UML (Class Diagrams) for each problems. Problem 1. THE SOLDIER Write a Gun class for Soldier.java. The Gun class is a class for guns that a soldier possesses. When creating a gun, you indicate the name of that gun as well as its bullet capacity (e.g., a fully-loaded pistol has 6 bullets, and a machine gun has 30). This capacity is important because this is what the gun will contain after the load() method is called. Upon object creation, the gun has zero bullets (or rounds) in it. There are two ways to fire a gun: either you specify the number of rounds that you fire or you don't. If you don't, it is assumed that only one round will be fired. Arrange it so that firing will not work (no shots are fired) if there aren't enough rounds. The Gun class also needs to keep track of the total shots fired. Begin by writing a stubbed class for Gun (add empty methods in the Gun class just to make Soldier.java compile); this is worth 50 points of your hands-on grade. Then, implement the methods in Gun.java. Do not revise Soldier.java. The Soldier.java (Test Driver program)
/** * Soldier holds several gun objects * * @author Julius S. Cansino * */ public class Soldier { Gun pistol; Gun shotGun; Gun machineGun; public Soldier() { pistol = new Gun( "pistol", 6 ); shotGun = new Gun( "shotgun", 2 ); machineGun = new Gun( "machinegun", 30 ); } private Gun getGun( String name ) { if ( name.equalsIgnoreCase( "pistol" ) ) { return pistol; }

else if ( name.equalsIgnoreCase( "shotgun" ) ) { return shotGun; } else if ( name.equalsIgnoreCase( "machinegun" ) ) { return machineGun; } else { return null; } } public boolean load( String name ) { Gun g = getGun( name ); if ( g == null ) { return false; } else { g.load(); return true; } } public boolean fire( String name ) { Gun g = getGun( name ); if ( g == null ) { return false; } else { g.fire(); return true; } } public boolean fireShots( String name, int numShots ) { Gun g = getGun( name ); if ( g == null ) { return false; } else { g.fireShots( numShots ); return true; } } public void showAllGuns()

System.out.println( pistol ); System.out.println( shotGun ); System.out.println( machineGun );

} public void execute() { this.showAllGuns(); pistol.load(); shotGun.load(); machineGun.load(); pistol.fire(); machineGun.fireShots( 10 ); shotGun.fire(); shotGun.fire(); shotGun.fire(); // will not fire a round pistol.fire(); machineGun.fireShots( 10 ); shotGun.load(); machineGun.load(); // should cause number of rounds left to be 30, not 40 pistol.fireShots( 2 ); machineGun.fire(); System.out.println( "---" ); this.showAllGuns(); } }

When you invoke the execute() method in Soldier, this should appear like:
pistol- rounds left: 0, total shots fired: 0 shotgun- rounds left: 0, total shots fired: 0 machinegun- rounds left: 0, total shots fired: 0 --pistol- rounds left: 2, total shots fired: 4 shotgun- rounds left: 2, total shots fired: 2 machinegun- rounds left: 29, total shots fired: 21

Submit the zip of your Java Project and the output screen snapshot saved as a DOCUMENT (.docx) as FPREX_1.zip to your MOODLE account.

Problem 2: Toll Lane Plaza Write a TollLane class (TollLane.java) for the given program below.
public class TollPlaza { public static void main( { TollLane first; TollLane second; String args[] )

first = new TollLane(); second = new TollLane(); first.letCarPass( 10.00 ); first.letCarPass( 9.00 ); second.letCarPass( 10.00 ); first.letCarPass( 15.00 ); System.out.print( "TollLane 1:" ); System.out.print( first.getCash()+" pesos," ); System.out.print( first.getCars()+" cars," ); System.out.println( first.getViolators()+" violators" ); System.out.print( "TollLane 2:" ); System.out.print( second.getCash()+" pesos," ); System.out.print( second.getCars()+" cars," ); System.out.println( second.getViolators()+" violators" );

} }

A TollLane object represents a typical toll lane in an expressway. Assume that toll lanes are automated instead of manned. Cars would pass by a toll lane and deposit money in a slot. A driver may end up placing more or less than the expected amount in the slot. Assume that the toll is 10.00 pesos. If a driver deposits less than this amount, the driver is considered a violator. If more money than the toll is deposited, no change is given. At some point, toll operators would like to find out how much cash has been collected, how many cars have passed by a toll lane as well as the number of violators. For the sample program, this should be the output/result:
TollLane 1:34.0 pesos,3 cars,1 violators TollLane 2:10.0 pesos,1 cars,0 violators

You should be able to infer which methods need to be present in the TollLane class from the TollPlaza.java provided. Nevertheless, the methods are:

letCarPass: method that causes a car to go through; the argument to this method is a double value representing the amount deposited by the driver getCash: returns the amount the toll lane has collected so far getCars: returns the number of cars that passed through the toll lane (including the violators) getViolators: returns the number of violators that passed through the toll lane

Hint: you need three attributes for the TollLane class: cash collected, number of cars, and number of violators (all start with the value 0). Submit the zip of your Java Project and the output screen snapshot saved as a DOCUMENT (.docx) as FPREX_2.zip to your MOODLE account. Problem 3. The MobilePhone class Create a MobilePhone class that stores load left (as a double) in pesos, total minutes called (as an int), and rate (as a double) in pesos per minute. Load and minutes consumed start at 0, rate starts at 6.50 pesos/minute. Support the following methods:

void load( double pesos ) void call( int minutes ) double getLoadLeft() int getTotalMinutesCalled()

optional:

void changeRate( double newCallRate ) void sendTextMessage(): subtract 1 peso from load int getNumTextMessages(): you will need an extra field to support this

Write this and test the class by creating objects and running methods on that object. Submit the zip of your Java Project and the output screen snapshot saved as a DOCUMENT (.docx) as FPREX_3.zip to your MOODLE account.

Problem 4. The Elevator Simulator Program


Write an Elevator class (Elevator.java) for the following ElevatorSimulation.java. An Elevator object represents a typical elevator in a building. An elevator can load and unload passengers, and move up and down the building. Assume that the building that houses these elevators have 5 floors, numbered 1 through 5. The number of passengers allowed in an elevator is dependent on the elevator's total weight limit and the passengers' average weight, which are fixed at 2000 1000 lbs and 123.45 123.5 lbs, respectively. At any point, you would like to be able to find out the current floor the elevator is on, the number of passengers in the elevator, and the elevator's total passenger weight. The up and down methods of the elevator have an int argument representing the number of floors the elevator will move. The load and unload methods also have an int parameter representing the number of passengers entering or leaving the elevator. The elevator must prevent the entry of passengers that cause the total weight limit to be exceeded.
/** * ElevatorSimulation.java -Tester program for the Elevator class * * @author Julius S. Cansino * @version 05 December 2012 */ public class ElevatorSimulation { public static void main( String args[] ) { Elevator e1 = new Elevator(); Elevator e2 = new Elevator(); e1.load( 3 ); e1.up( 4 ); e1.unload( 1); e1.down( 2 ); e2.up( 1 ); e2.load( 1 ); System.out.println( e1.getPassengerCount() ); System.out.println( ); System.out.println( e1.getCurrentFloor() ); System.out.println( e2.getPassengerCount() ); System.out.println( ); System.out.println( e2.getCurrentFloor() ); System.out.println( e2.load( 10 ); e2.load( 10 ); e2.up( 10 ); e1.down( 100 ); System.out.println( e1.getPassengerCount() ); System.out.println( ); System.out.println( e1.getCurrentFloor() );

"Number of persons in elevator 1: " + "Weight of persons in elevator 1: " + e1.getTotalWeight() "Elevator 1 is on floor number : " +

"Number of persons in elevator 2: " + "Weight of persons in elevator 2: " + e2.getTotalWeight() "Elevator 2 is on floor number "---" ); : " +

"Number of persons in elevator 1: " + "Weight of persons in elevator 1: " + e1.getTotalWeight() "Elevator 1 is on floor number : " +

System.out.println( "Number of persons in elevator 2: " + e2.getPassengerCount() ); System.out.println( "Weight of persons in elevator 2: " + e2.getTotalWeight() ); System.out.println( "Elevator 2 is on floor number : " + e2.getCurrentFloor() ); } }

For the sample program, this should output/result.


SAMPLE OUTPUT: Number of persons in elevator Weight of persons in elevator Elevator 1 is on floor number Number of persons in elevator Weight of persons in elevator Elevator 2 is on floor number --Number of persons in elevator Weight of persons in elevator Elevator 1 is on floor number Number of persons in elevator Weight of persons in elevator Elevator 2 is on floor number 1: 1: : 2: 2: : 1: 1: : 2: 2: : 2 247.0 3 1 123.5 2 2 247.0 1 8 988.0 5

You should be able to infer which methods need to be present in the Elevator class from the ElevatorSimulation program. Nevertheless, the methods are:

up: move up x floors, where x is the int parameter. If x is too large that it causes the elevator to go past the 5th floor, the elevator should end up on the 5th floor. down: move down x floors, where x is the int parameter. If x is too large that it causes the elevator to go past the 1st floor, the elevator should end up on the 1st floor. load: let in an additional p persons in the elevator. If p is such that it will cause the elevator to exceed its limit, then let in only enough persons that the elevator can handle. unload: let out p persons from the elevator. It is ridiculous for p to be more than the actual number of persons in the elevator, but if this is the case, everyone in the elevator leaves. getPassengerCount: return the number of passengers currently in the elevator. getTotalWeight: return the total weight of passengers currently in the elevator. (assume an average weight of 123.45). getCurrentFloor: return the number of the floor that the elevator is on.

You do not have to worry about negative-valued parameters. Submit the zip of your Java Project and the output screen snapshot saved as a DOCUMENT (.docx) as FPREX_4.zip to your MOODLE account.

Das könnte Ihnen auch gefallen