Home > Net >  Disaster management Class shows "cannot be applied" to User u2 error
Disaster management Class shows "cannot be applied" to User u2 error

Time:10-08

I'm working on this lab where I'm creating a hypothetical disaster management system for people to put information about disasters with Java. Within the code, there are two classes with the same name User. The tutorial for the lab says that line 75 with user u2 is supposed to use an Emergency Contact class by using the following code:

  User u2 = new User("Marlena", "Evans","6088861222", "[email protected]");

However, with this, the console brings up this error: 'User(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' in 'User' cannot be applied to '(java.lang.String, java.lang.String, boolean, int, java.lang.String, java.lang.String)'

so I changed it to:

 User u2 = new User("Marlena", "Evans", false, 30, "6088861222","[email protected]");

which removed the error.

I need a fresh set of eyes on this. Can someone look at this and tell me why the tutorial's code isn't working?

Here is what the code is supposed to look like:

enter image description here

and here is my code:

//import java.sql.Timestamp (package already active)

public class User {
    private String firstName;
    private String lastName;
    private boolean gender; //true - male; false - female
    private int age;
    private BloodType blood;
    private String telephone;
    private String email;
    private User emergencyContact;
    public  void setContact(User u){
        this.emergencyContact = u;
    }

    public User (
            String firstName,
            String lastName,
            boolean gender,
            int age,
            String blood,
            String telephone,
            String email
    ) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.age = age;
        this.blood = BloodType.fromString(blood);
        this.telephone = telephone;
        this.email = email;
    }

    public String getFirstName(){
        return this.firstName;
    }

    public String getLastName(){
        return this.lastName;
    }

    public boolean getGender(){
        return this.gender;
    }

    public int getAge() {
        return this.age;
    }

    public String getTelephone(){
        return this.telephone;
    }

    public String getEmail() {
        return this.email;
    }

    public BloodType getBlood() {
        return this.blood;
    }

    public User getEmergencyContact() {
        return this.emergencyContact;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO

         //Here is an example of using the new user constructor
        User u1 = new User("John","Black",true,25, "6085551234","jb@daysof"  
                "ourlives.com");
        //This example uses the Emergency Contact Constructor to create a new emergency contact
        User u2 = new User("Marlena", "Evans", false, 30, "6088861222","[email protected]");

        u1.setContact(u2); //This means Marlena is the Emergency Contact for John

        System.out.println("User: "   u1.firstName   " has an emergency contact of: "   u1.emergencyContact.getFirstName());
    }

    public User (
            String firstName,
            String lastName,
            boolean gender,
            int age,
            String telephone,
            String email
    ){
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.age = age;
        this.email = email;
    }
}

Thank you and as always, let me know if you need more context to help me.

CodePudding user response:

You are missing an additional constructor with the reduced number of arguments that constructor call in line 75 is using.

Add this to your User class:

  public User(String firstName, String lastName, String telephone, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.telephone = telephone;
        this.email = email;
    }

Also, in the main class, you are accessing firstName and emergencyContact fields, but these are private fields, so either set them to public or add a setter method.

  •  Tags:  
  • java
  • Related