クラスについては、あまりにも長い間駐車している車に駐車券を発行する警察官をシミュレートするプログラムを作ろうとしています。私は、ParkedCar、ParkingMeter、ParkingTicket、PoliceOfficerの4つのクラスを作成すると仮定しています。私は2つの車の配列を作り、2つのParkingMeterの配列を作り、1つの車を違反にし、もう1つを非違反にしなければならないので、主な方法にちょうど立ちません。これどうやってするの?クラスメソッドを配列内の別のクラスメソッドと連携させるにはどうすればよいですか?
ParkedCarクラス
// a class of type ParkedCar
public class ParkedCar {
// the class fields
private String make;
private String model;
private String color;
private String licenseNumber;
private int minutesParked;
// the class constructor
public ParkedCar(String mk, String mdel, String col, String lic, int minParked) {
// assign the constrictor fields to the class fields
make = mk;
model = mdel;
color = col;
licenseNumber = lic;
minutesParked = minParked;
}
// the copy constructor
public ParkedCar copy(ParkedCar car2) {
ParkedCar copyObject = new ParkedCar(make, model, color, licenseNumber, minutesParked);
return copyObject;
}
// getter method for make of a car
public String getMake() {
return make;
}
// setter method for make of a car
public void setMake(String make) {
this.make = make;
}
// getter method for model of a car
public String getModel() {
return model;
}
// setter method for model of a car
public void setModel(String model) {
this.model = model;
}
// getter method for color of a car
public String getColor() {
return color;
}
// setter method for a color of a car
public void setColor(String color) {
this.color = color;
}
// getter method for a car licence number
public String getLicenseNumber() {
return licenseNumber;
}
// setter method for a car licence number
public void setLicenseNumber(String licenseNumber) {
this.licenseNumber = licenseNumber;
}
// getter method for minutes parked
public int getMinutesParked() {
return minutesParked;
}
// setter method for minutes parked
public void setMinutesParked(int minutesParked) {
this.minutesParked = minutesParked;
}
}
ParkingMeterクラス
// a class of type ParkingMeter
public class ParkingMeter {
// the class fields
private int minutesPurchased;
// the class constructor
public ParkingMeter(int numMinPurchased) {
// assign the constrictor fields to the class fields
this.minutesPurchased = numMinPurchased;
}
// getter method for minutes purchased
public int getMinutesPurchased() {
return minutesPurchased;
}
// setter method for minutes purchased
public void setMinutesPurchased(int minutesPurchased) {
this.minutesPurchased = minutesPurchased;
}
}
PoliceOfficerクラス
// a class of type PoliceOfficer
public class PoliceOfficer {
// the class fields
private String name;
private String badgeNumber;
// the class constructor
public PoliceOfficer(String officeName, String badgeNumber) {
// assign the constrictor fields to the class fields
name = officeName;
this.badgeNumber = badgeNumber;
}
// the copy constructor
public PoliceOfficer copy(PoliceOfficer officer) {
PoliceOfficer copyObject = new PoliceOfficer(name, badgeNumber);
return copyObject;
}
// the method patrol looks at the number of minutes a car has been parked and the
// number of minutes purchased
public ParkingTicket patrol(ParkedCar car, ParkingMeter meter) {
ParkingTicket ticket = null;
// Calculate the total number of minutes parked over minutes
// purchased
int illegalMinutes = car.getMinutesParked()
- meter.getMinutesPurchased();
// if illegalMinutes, give ticket
if (illegalMinutes > 0) {
// yes, it is illegally parked.
ticket = new ParkingTicket(car, this, illegalMinutes);
}
return ticket;
}
// a getter method to get name of officer
public String getName() {
return name;
}
// a setter method to set name of officer
public void setName(String name) {
this.name = name;
}
// a getter method to get officer badge number
public String getBadgeNumber() {
return badgeNumber;
}
// a setter method to set officer badge number
public void setBadgeNumber(String badgeNumber) {
this.badgeNumber = badgeNumber;
}
}
ParkingTicketクラス
// a class of type ParkingTicket
public class ParkingTicket {
// the class fields
private ParkedCar car;
private PoliceOfficer officer;
private double fine;
private int minutes;
public final double BASE_FINE = 25.0;
public final double HOURLY_FINE = 10.0;
// the class constructor
public ParkingTicket(ParkedCar aCar, PoliceOfficer anOfficer, int meterMins) {
// assign the constrictor fields to the class fields
car = aCar;
officer = anOfficer;
minutes = meterMins;
}
// a copy constructor
public ParkingTicket copy(ParkingTicket ticket) {
ParkingTicket copyObject = new ParkingTicket(car, officer, minutes);
return copyObject;
}
// The method calculateFine calculates the amount of a parking fine
public void calculateFine() {
double hours = minutes/60.0;
int hoursAsInt = (int) hours;
if ((hours - hoursAsInt) > 0) {
hoursAsInt++;
}
// Assign the base fine.
fine = BASE_FINE;
// Add the additional hourly fines.
fine += (hoursAsInt * HOURLY_FINE);
}
// getter method to get a car
public ParkedCar getCar() {
return car;
}
// setter method to set a car
public void setCar(ParkedCar car) {
this.car = car;
}
// getter method to get officer
public PoliceOfficer getOfficer() {
return officer;
}
// setter method to set officer
public void setOfficer(PoliceOfficer officer) {
this.officer = officer;
}
// getter method to get fine
public double getFine() {
return fine;
}
// setter method to set fine
public void setFine(double fine) {
this.fine = fine;
}
// getter method to get minutes
public int getMinutes() {
return minutes;
}
// setter method to set minutes
public void setMinutes(int minutes) {
this.minutes = minutes;
}
public String toString() {
return "ParkingTicket [car=" + car + ", officer=" + officer
+ ", fine=" + fine + ", minutes=" + minutes
+ ", BASE_FINE=" + BASE_FINE + ", HOURLY_FINE="
+ HOURLY_FINE + "]";
}
}
あなたが持っている0
メインメソッド
// a class of type PatrolSimulation
public class PatrolSimulation {
// the main method
public static void main(String[] args) {
// an array of 2 Car objects, with various minutesParked values
ParkedCar[] car = new ParkedCar[2];
car[0] = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 100);
car[1] = new ParkedCar("Volkswagen", "1972", "Red", "147RHZM", 30);
// an array of 2 ParkingMeter objects, with minutes so that
// the first Car object is in violation, while the second is not
ParkingMeter[] meter = new ParkingMeter[2];
meter[0] = new ParkingMeter(30);
meter[1] = new ParkingMeter(40);
// an array of 2 ParkingTicket objects
ParkingTicket[] ticket = new ParkingTicket[2];
// a PoliceOfficer object
PoliceOfficer officer = new PoliceOfficer("Sargent Cody", "007");
}
}
メインメソッド擬似コード
// Create an array of 2 Car objects, with various minutesParked values
// Create an array of 2 ParkingMeter objects, with minutes so that
// the first Car object is in violation, while the second is not
// Create an array of 2 ParkingTicket objects
// Create a PoliceOfficer object. Give the officer a name and badge
// number
// Have the officer patrol each of the Car and ParkingMeter object
// combinations (index i for the array of Car objects should be
// matched with index i for the array of ParkingMeter objects, which
// should be matched with index i of the array of ParkingTicket
// objects)
// After the PoliceOfficer has patrolled the cars and parking
// meters, walk over the array of ParkingTickets and invoke the
// toString method if a ticket has been issued, otherwise indicate
// that a ticket has not been issued
私はあなたが何を抱いているのかよく分かりません。それは "警察官を巡視させてください..."ですか? – 4castle
私は2つの車オブジェクトの配列を作成しなければならなかったメインメソッドに執着しており、2つのParkingMeterオブジェクトの配列を作成する必要があります。私は、配列内の車のオブジェクトの2つを取って、1つの車のオブジェクトを違法に駐車させ、他の車のオブジェクトを合法的に駐車することができるのだろうかと思っていますか? –