[Solved] Constructor in Class cannot be applied to gives types [closed]


The constructor public HeartRates( String fName, String lName, int aMonth, int aDay, int aYear) of class HeartRates has the arguments String,String,int,int,int.

So you have to provide those values in

HeartRates profile = new HeartRates(); // need values as argument

Some thing like:-

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

Modified 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

when you use constructor properly you don’t need to use those set-functions. Every data insertions can be done by your constructor.

Output :-

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

solved Constructor in Class cannot be applied to gives types [closed]