如何用Java中的用户输入填充构造函数? [英] How to populate a constructor with user input in Java?

查看:36
本文介绍了如何用Java中的用户输入填充构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一项任务上碰壁,一直在整理网站以寻求任何有用的信息(出现空白)。我需要先创建一个类,然后在该类中创建一个构造函数,然后再创建一个子类以扩展超类。然后,我需要使用一种主要方法来演示这两种情况的新文件。从概念上来说没问题。

I've hit a wall on an assignment and have been combing the site for anything helpful (came up empty). I'm required to create a class, a constructor within that class, and then a subclass to extend the superclass. Then, I'm required to create a new file with a main method to demonstrate both cases. No problem, conceptually.

我的问题是:如何使用构造函数初始化对象,但需要用户输入?

My question is this: how do I initialize an object using the constructor, but with user input?

现在我得到的错误是:类CarRental中的构造方法CarRental无法应用于给定类型;需要
:字符串,整数,字符串,整数
:没有参数
的原因:实际参数和形式参数列表的长度不同。

Right now the error I'm getting is: "constructor CarRental in class CarRental cannot be applied to given types; required: String,int,String,int found: no arguments reason: actual and formal argument lists differ in length"

请不要对错误告诉您问题所在发表任何评论。不,它不会告诉我什么。我是这个应用中的小宝贝,需要一些帮助。

Please no snide remarks on "the error tells you what the problem is." No, it doesn't tell me what it is. I'm a wee babe in this stuff and need a little hand-holding.

我将在下面粘贴我的3个班级。因为我真是个新手,它们可能会让您痛苦不已(而且,我的课程是8周的简短课程,实际上没有时间专门用于伪代码,因此我对逻辑本身也有额外的挑战)。

I'll paste my 3 classes below. They will probably make you writhe in agony, since I'm such a newb (also, my class is an abbreviated 8-week course where virtually no time was dedicated to pseudocode, so I have the extra challenge of conceiving the logic itself).

我不是在寻找任何人为我做作业,我只是在UseCarRental.java文件中寻找帮助。这是我的代码。

I am not looking for anyone to do my homework for me, I am just looking for a helping hand in the UseCarRental.java file. Here's my code..

public class CarRental {
protected String renterName;
protected int zipCode;
protected String carSize;
protected double dailyRate;
protected int rentalDays;
protected double totalCost;
final double ECONOMY = 29.99;
final double MIDSIZE = 38.99;
final double FULLSIZE = 43.50;

public CarRental(String renterName, int zipCode, String carSize, int rentalDays){

totalCost = dailyRate * rentalDays;
}
public String getRenterName(){
return renterName;
}
public void setRenterName(String renter){
renterName = renter;
}
public int getZipCode(){
return zipCode;
}
public void setZipCode(int zip){
zipCode = zip;
}
public String getCarSize(){
return carSize;
}
public void setCarSize(String size){
carSize = size;
}
public double getDailyRate(){
return dailyRate;
}
public void setDailyRate(double rate){
switch (getCarSize()) {
        case "e":
            rate = ECONOMY;
            break;
        case "m":
            rate = MIDSIZE;
            break;
        case "f":
            rate = FULLSIZE;
            break;
    }
}
public int getRentalDays(){
return rentalDays;
}
public void setRentalDays(int days){
rentalDays = days;
}
public double getTotalCost(){
return totalCost;
}
public void setTotalCost(double cost){
totalCost = cost;
}

public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());

}

}

子类LuxuryCarRental ....

the subclass LuxuryCarRental....

public class LuxuryCarRental extends CarRental {

final double chauffeur = 200.00;
final double dailyRate = 79.99;
protected String chauffeurStatus;

public LuxuryCarRental(String renterName, int zipCode, String carSize, int rentalDays) {
    super(renterName, zipCode, carSize, rentalDays);
}

public String getChauffeurStatus(){
return chauffeurStatus;
}
public void setChauffeurStatus(String driver){
chauffeurStatus = driver;
}
public double getChauffeurFee(){
return chauffeur;
}
public void setTotalLuxuryCost(){
if (chauffeurStatus=="y")
    setTotalCost((dailyRate * getRentalDays()) + (chauffeur * getRentalDays()));
else
    setTotalCost(dailyRate * getRentalDays());
}

@Override
public void displayRental(){
System.out.println("==============================================");
System.out.println("Renter Name: " + getRenterName());
System.out.println("Renter Zip Code: " + getZipCode());
System.out.println("Car size: " + getCarSize());
System.out.println("Optional Chauffeur fee: $" + getChauffeurFee());
System.out.println("Daily rental cost: $" + getDailyRate());
System.out.println("Number of days: " + getRentalDays());
System.out.println("Total cost: $" + getTotalCost());

}
}

方法:

import java.util.Scanner;
public class UseRentalCar {

public static void main(String[] args){
    Scanner keyboard = new Scanner(System.in);
    CarRental rentalCar = new CarRental();

    System.out.println("==========================");
    System.out.println("RENTAL CAR SELECTION");
    System.out.println("==========================");
    System.out.println("Enter your name: ");
    rentalCar.setRenterName(keyboard.next());
    System.out.println("Enter your zip code: ");
    rentalCar.setZipCode(keyboard.nextInt());
    System.out.println("Enter the car size ("e=Economy, m=Midsize, f=Fullsize: ");
    rentalCar.setCarSize(keyboard.next());
    System.out.println("Enter the number of days: ");
    rentalCar.setRentalDays(keyboard.nextInt());

    rentalCar.displayRental();



}
}

(略因为没关系,主要是尝试使对象实例化起作用)

(omitted some cause it doesn't matter, mainly trying to get the object instantiation working)

感谢您的帮助!

推荐答案

在主方法中创建局部变量,例如String和int变量,然后在用用户输入填充这些变量之后,使用它们来调用构造函数。

Create local variables in your main method, say String and int variables, and then after these variables have been filled with user input, use them to call a constructor.

我将发布一个一般示例,由于这是家庭作业,因此最好向您展示概念,然后让使用该概念来创建代码:

I will post a general example, since this is homework, it is better to show you the concept and then let you use the concept to create the code:

public class Foo {
  private String name;
  private int value;

  public Foo(String name, int value) {
    this.name = name;
    this.value = value;
  }
}

其他地方

import java.util.Scanner;

public class Bar {

  public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter name: ");
    String name = keyboard.nextLine();  // local variable
    System.out.print("Please enter value: " );
    int number = keyboard.nextint();  // another local variable
    keyboard.nextLine();  // to handle the end of line characters

    // use local variables in constructor call
    Foo foo = new Foo(name, number);    
}

这篇关于如何用Java中的用户输入填充构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆