Skip to content
Advertisement

Eclipse says The constructor Email() is undefined

I am tring to expand on a tutorial I was following Java Project Step by step Build An Email Administration Application (https://www.youtube.com/watch?v=U3Ibvu0htNs&t=386s). I am trying to use getters and setters to allow the encapsulation to do its job, and eventually allow a user to enter their own name instead of having a preset name in the variable. I know the code is messy I have been playing with this code longer then I would like to admit. XD

package encap;

public class Main {

    public static void main(String[] args) {
        Email s = new Email();
        
        s.setName("Billy");
        
        System.out.println(s.getName());
        
        
        //Email em1 = new Email("John", "Smith");
        //System.out.println (em1.ShowInfo());
    }

}
package encap;

import java.util.Scanner;

public class Email {
    private String firstName;
    private String lastName;
    private String password;
    private String department;
    private String email;
    private int mailboxCapacity = 500;
    private String alternateEmail;
    private int passwordDefaultLength = 10;
    private String companySuffix = "company.com";
    
    //Constructor to receive first and lastName
    public Email() {
        this.firstName = firstName;
        this.lastName = lastName;
        
        //call a method asking for the department return the department
        this.department = setDepartment();
        
        // Call a method that returns a random password
        this.password = randomPassword(passwordDefaultLength);
        System.out.println("Your password is: " + password);
        
        //combine elements to generate email
        email = firstName.toLowerCase() + "." + lastName.toLowerCase() + "@" + department.toLowerCase() + "." + companySuffix.toLowerCase();
    }

    // Ask for the department
    private String setDepartment() {
        System.out.print("New worker: " + firstName + " " + lastName + "nDEPARTMENT CODES:n1 for Salesn2 for Developmentn3 for Accounting n0 for nonenEnter department code: ");
        Scanner in = new Scanner(System.in);
        int depChoice = in.nextInt();
        if(depChoice == 1) {return "Sales";}
        else if (depChoice == 2) {return "Development";}
        else if (depChoice == 3) {return "Accounting";}
        else {return ""; }
        }
    
    //Generate random password
    private String randomPassword(int length) {
        String passwordSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
        char[] password = new char[length];
        for(int i=0; i<length; i++) {
            int rand = (int) (Math.random() * passwordSet.length());
            password[i] = passwordSet.charAt(rand);
        }
        return new String(password);
    }
    
    //Employee First and Last name getter and setter

///////trying this

    
    public void setName(String newName) {
        firstName = newName;
    }
    
    public String getName() {
        return firstName;
    }

    
    
    // setters & getters mailbox capacity
    public void setMailboxCapacity(int capacity) {
        this.mailboxCapacity = capacity;
    }
        public int getMailboxCapacity() {
        return mailboxCapacity;
    }
    //setter and getter  alternate email
    

    public String getAlternateEmail() {
        return alternateEmail;
    }
    public void setAlternateEmail(String altEmail) {
        this.alternateEmail = altEmail;
    }
    
    
    public String getPassword() {
        return password;
    }
    
    //change the password
    public void ChangePassword(String password) {
        this.password = password;
    }
    
    public String ShowInfo() {
        return "Display Name: " + firstName + " " + lastName +
            "nCompany Email: " + email +
            "nMailbox Capacity: " + mailboxCapacity + "mb.";
        
    }

}

Advertisement

Answer

a) The line “Email s = new Email();” should work fine. Just ensure whether you had saved the program before running it.But you’ll get a null pointer exception at

email = firstName.toLowerCase() + “.” + lastName.toLowerCase() + “@” + department.toLowerCase() + “.” + companySuffix.toLowerCase();

b) The error you mentioned might occur only when you un-comment the line “Email em1 = new Email(“John”, “Smith”);” and execute the program as there are no parameterized constructor in the class Email. At that time you might get the error as “The constructor Email(String, String) is undefined”.

But the current constructor instantiation should work fine.Just ensure(a)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement