Class中的构造方法不能应用于给定类型

时间:2018-07-07 18:40:45

标签: java

我根据deitel的《 Java How to program》一书创建了一个程序。在第3章“有所作为”中,练习说:

  

(目标心率计算器)创建一个称为HeartRates的类。的   班级属性应包括此人的名字,姓氏和出生日期(由单独的   出生月份,日期和年份的属性)。您的班级应该有一个接收   此数据作为参数。为每个属性提供set和get方法。班上也应该   包括一种计算并返回该人的年龄(以年为单位)的方法,一种计算和   返回该人的最大心率以及一种计算并返回该人的目标的方法   心率。编写一个Java应用程序以提示输入该人的信息,实例化一个对象   类HeartRates并打印该对象的信息,包括该人的名字,   姓氏和出生日期-然后计算并打印该人的年龄(以年为单位),最大心脏   率和目标心率范围。

所以我写了这段代码:

// File: HeartRates.java

public class HeartRates {

    private String firstName;
    private String lastName;
    private int month;
    private int day;
    private int year;

    // constructor
    public HeartRates( String fName, String lName, int aMonth,
        int aDay, int aYear) {

        firstName = fName;
        lastName = lName;
        month = aMonth;
        day = aDay;
        year = aYear;
    }

    // method to set first name
    public void setFirstName( String fName ) {
        firstName = fName;
    }

    // method to get first name
    public String getFirstName() {
        return firstName;
    } 

    // method to set last name
    public void setLastName( String lName ) {
        lastName = lName;
    }

    // method to get last name
    public String getLastName() {
        return lastName;
    }

    // method to set month
    public void setMonth( int aMonth ) {
        month = aMonth;
    }

    // method to get month
    public int getMonth() {
        return month;
    }

    // method to set day
    public void setDay( int aDay ) {
        day = aDay;
    }

    // method to get day
    public int getDay() {
        return day;
    }

    // method to set year
    public void setYear( int aYear ) {
        year = aYear;
    }

    // method to get year
    public int getYear() {
        return year;
    }

    // returns person's age
    public int ageInYears() {
        return 2018 - getYear();
    }

    // returns maximum heart rate
    public int maxHeartRate() {
        return 220 - ageInYears();
    } 

    // display target heart rate
    public String targetHeartRate() {
    // targetHeartRate = range 50% - 85% of maxheartrate
        double fifty = 0.5 * maxHeartRate();
        double eightyFive = 0.85 * maxHeartRate();
        String target = (int)fifty + " BPM - " + (int)eightyFive + " BPM\n"; 
        return target;
    }

} // end class HeartRates

和另一个类对其进行测试:

// File: HeartRatesTest.java
// Testing heart rate class
import java.util.Scanner;

public class HeartRatesTest {

    public static void main(String[] args) {

        Scanner input = new Scanner( System.in );
        HeartRates profile = new HeartRates();

        String firstName;
        String lastName;
        int month;
        int day;
        int year;

        // collecting user's information
        System.out.print("Enter your first name: ");
        firstName = input.nextLine();
        profile.setFirstName( firstName );

        System.out.print("Enter your last name: ");
        lastName = input.nextLine();
        profile.setLastName( lastName );

        System.out.print("Enter your date of birth(month day year): ");
        month = input.nextInt();
        profile.setMonth( month );
        day = input.nextInt();
        profile.setDay( day );
        year = input.nextInt();
        profile.setYear( year );

        // displaying user's information
        System.out.printf( "\nFirst Name: %s\n", profile.getFirstName() );
        System.out.printf( "Last Name: %s\n", profile.getLastName() );
        System.out.printf( "Date of birth: %d\\%d\\%d\n", 
            profile.getMonth(), profile.getDay(), profile.getYear() );
        System.out.printf( "Age: %d\n", profile.ageInYears() );
        System.out.printf( "Maximum heart rate: %d BPM\n", profile.maxHeartRate() );
        System.out.printf( "Target heart rate: " + profile.targetHeartRate() );

    } // end method main
} // end class HeartRateTest

但我收到此错误消息:

 HeartRatesTest.java:10: error: constructor HeartRates in class
 HeartRates cannot be applied to given types;
                 HeartRates profile = new HeartRates();
                                      ^   required: String,String,int,int,int   
 found: no arguments   reason: actual and
 formal argument lists differ in length 1 error

但是我不知道为什么要我放String,String,int,int,int。 谁能帮我解决此错误?

1 个答案:

答案 0 :(得分:0)

public HeartRates( String fName, String lName, int aMonth, int aDay, int aYear)的构造函数HeartRates具有参数String,String,int,int,int

所以您必须在

中提供这些值
HeartRates profile = new HeartRates(); // need values as argument

类似:-

// calling constructor with arguments
HeartRates profile = new HeartRates(firstName, lastName, month, day, year);

修改后的HeartRatesTest.java

// File: HeartRatesTest.java
// Testing heart rate class
import java.util.Scanner;

public class HeartRatesTest {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String firstName;
        String lastName;
        int month;
        int day;
        int year;

        // collecting user's information
        System.out.print("Enter your first name: ");
        firstName = input.nextLine(); // no need of setFirstName()

        System.out.print("Enter your last name: ");
        lastName = input.nextLine(); // no need of setLastName()

        System.out.print("Enter your date of birth(month day year): ");
        month = input.nextInt(); // no need of setMonth()
        day = input.nextInt();   // no need of setDay()
        year = input.nextInt();  // no need of setYear()

        /* Object of HeartRates is defined after reading all values */
        HeartRates profile = new HeartRates(firstName, lastName, month, day, year); // calling constructor with arguments

        // displaying user's information
        System.out.printf("\nFirst Name: %s\n", profile.getFirstName());
        System.out.printf("Last Name: %s\n", profile.getLastName());
        System.out.printf("Date of birth: %d\\%d\\%d\n", profile.getMonth(), profile.getDay(), profile.getYear());
        System.out.printf("Age: %d\n", profile.ageInYears());
        System.out.printf("Maximum heart rate: %d BPM\n", profile.maxHeartRate());
        System.out.printf("Target heart rate: " + profile.targetHeartRate());

    } // end method main
} // end class HeartRateTest

正确使用构造函数时,不需要使用那些set-functions。每次数据插入都可以由构造函数完成。

输出:-

Enter your first name: abc
Enter your last name: def
Enter your date of birth(month day year): 12 25 1998

First Name: abc
Last Name: def
Date of birth: 12\25\1998
Age: 20
Maximum heart rate: 200 BPM
Target heart rate: 100 BPM - 170 BPM