235k views
3 votes
Write a class named parkingmeter containing two instance variables:

a) time_purchased and payment_method
b) time_left and time_limit
c) rate and payment_status
d) time_purchased and rate

1 Answer

5 votes

Final answer:

The student is tasked with writing a class called ParkingMeter, using the two instance variables 'time_purchased' and 'rate' to track the time bought and the cost of parking time. d) time_purchased and rate is correct answer.

Step-by-step explanation:

The question asks to write a class named ParkingMeter containing two instance variables. The options listed include various pairings of potential instance variables like 'time_purchased', 'payment_method', 'time_left', 'time_limit', 'rate' and 'payment_status'.

Given the context, the most appropriate instance variables would probably be 'time_purchased' and 'rate', as they most directly relate to the function of a parking meter, which typically tracks the amount of parking time bought and the cost rate of that time.

Example Class Definition:

class ParkingMeter {
private double time_purchased;
private double rate;

public ParkingMeter(double timePurchased, double rate) {
this.time_purchased = timePurchased;
this.rate = rate;
}

// Method to add purchased time
public void addTime(double amount) {
this.time_purchased += amount;
}

// Get the current rate
public double getRate() {
return rate;
}

// Get the current time purchased
public double getTimePurchased() {
return time_purchased;
}
}

Here, the ParkingMeter class has been created with the aforementioned instance variables and includes methods to add purchased time and retrieve the current 'rate' and 'time_purchased'.

User Naga K
by
8.1k points