英文:
Java calling a void method in a different class to a main class?
问题
以下是翻译好的部分:
// 原始的方法
public ParkingTicket issueParkingTicket(ParkedCar car, ParkingMeter meter) {
if (isParkingTimeExpired(car, meter) == true) {
ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(), calculateFine(car, meter));
ticket.displayDetails();
return ticket;
} else {
return null;
}
}
// 修改后的方法
public void issueParkingTicket(ParkedCar car, ParkingMeter meter) {
if (isParkingTimeExpired(car, meter) == true) {
ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(), calculateFine(car, meter));
ticketList.add(ticket);
ticket.displayDetails();
}
}
// 主驱动类
public class Driver {
public static void main(String[] args) {
ParkedCar myCar1 = new ParkedCar("Fred", "toyota", 2013, "Z1234", 25);
myCar1.displayDetails();
ParkingMeter myMeter1 = new ParkingMeter("SYDNEY", true, 0.15, 70);
myMeter1.displayDetails();
PoliceOfficer officer1 = new PoliceOfficer("John doe", "DMX1234");
officer1.issueParkingTicket(myCar1, myMeter1); // 不再有返回值
// 这是你遇到错误的部分:ParkingTicket ticket = officer1.issueParkingTicket(myCar1, myMeter1);
}
}
请注意,我已按照你的要求仅提供代码部分的翻译,不包括其他内容。如果你还有进一步的问题或需要其他帮助,请随时提问。
英文:
I have a method to issue a parkingTicket in my officer class,
public ParkingTicket issueParkingTicket(ParkedCar car,ParkingMeter meter){
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
ticket.displayDetails();
return ticket;
} else
{ return null;
}
}
I was asked to modify it in a way to have it not return anything so i made it void, so I did it this way
public void issueParkingTicket(ParkedCar car,ParkingMeter meter){
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
ticketList.add(ticket);
ticket.displayDetails();
}
Now in my main driver class, I have to created an officer object but since I had to make the method void to not return anything I am getting an error saying void cannot be converted to ParkingTicket
, if i take away myCar1,myMeter1
from the parentheses, I get an error telling me there's arguments required ParkedCar , ParkingMeter. How can I make it so I don't get an error and the officer object created can issue a parking ticket?
public class Driver
{
main method
public static void main(String [] args){
ParkedCar myCar1 = new ParkedCar("Fred","toyota",2013,"Z1234",25);
myCar1.displayDetails();
ParkingMeter myMeter1 = new ParkingMeter("SYDNEY",true,0.15,70);
myMeter1.displayDetails();
PoliceOfficer officer1 = new PoliceOfficer("John doe","DMX1234");
ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1);
This is the source of my error ParkingTicket ticket = officer1.issueParkingTicket(myCar1,myMeter1);
答案1
得分: 2
你只需在主函数中删除以下部分:
ParkingTicket ticket =
由于在函数中已创建了停车券,因此在调用函数时无需创建新的停车券。
英文:
You can just remove the part that says:
ParkingTicket ticket =
in your main method.
Since the ticket is created in the function there is no need to create a new ticket when you call the function.
答案2
得分: 1
在ParkedCar中添加一个数组或列表,用于跟踪该车辆的所有罚单。
public void issueParkingTicket(ParkedCar car, ParkingMeter meter)
{
if(isParkingTimeExpired(car, meter)){
ParkingTicket ticket = new ParkingTicket(officerName, officerBadgeNumber, car.getLicensePlateNumber(), calculateFine(car, meter));
car.tickets.add(ticket);
}
}
然后在您的主类中,您可以访问任何车辆对象的所有罚单。
public static void main(String[] args)
{
ParkedCar car = new ParkedCar(/*params*/);
Officer officer1 = new Officer(/*params*/);
officer1.issueTicket(/*params*/);
// 在ParkedCar类中创建一个获取罚单的方法
car.getTickets();
}
英文:
Add an array or List to ParkedCar that keeps track of all the tickets for that vehicle.
public void issueParkingTicket(ParkedCar car, ParikingMeter meter)
{
if(isParkingTimeExpired(car,meter) == true){
ParkingTicket ticket = new ParkingTicket(officerName,officerBadgeNumber,car.getLicensePlateNumber(),calculateFine(car,meter));
car.tickets.add(theTicket);
}
then in your main class you can access every ticket for any car object.
public static void main(String [] args)
{
ParkedCar car = new ParkedCar(/*params*/);
Officer officer1 = new Officer(/*params*/);
officer1.issueTicket(/*params*/);
//make a getter for the tickets in the ParkedCar class
car.getTickets();
}
答案3
得分: 0
你可以传入一张票据,并在issueParkingTicket方法内部操作该票据。
ParkingTicket ticket = new ParkingTicket();
officer1.issueParkingTicket(myCar1, myMeter1, ticket);
ticket.displayDetails();
英文:
You can pass in a ticket, and manipulate the ticket inside the issueParkingTicket method.
ParkingTicket ticket = new ParkingTicket();
officer1.issueParkingTicket(myCar1,myMeter1,ticket);
ticket.displayDetails();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论