I am fairly new to Java. I am getting an error "The constructor Family(String, String, int) is undefined". Im not sure what this means. Need a bit of help here please.
EDIT: I was missing the extra 3 parameters and also was missing quotes around 31.
Main.java
public class Main {
public static void main(String[] args){
Family person = new Family("CHRIS", "PEREZ", 31);
String person1 = person.getPerson();
System.out.println(person1);
}
}
Family.java
public class Family {
String firstName;
String lastName;
int age;
int phoneNumber;
String dob;
String married;
public Family(String firstName, String lastName, int age, int phoneNumber,
String dob, String married) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.phoneNumber = phoneNumber;
this.dob = dob;
this.married = married;
public String getPerson() {
return ("Hi my name is"+this.firstName+" "+ this.lastName+"."+"I am "+this.age+" years old.");
}
}
This is because your Family class only has a six-argument constructor requiring all of the six fields to be provided. Your call:
Family person = new Family("CHRIS", "PEREZ", 31);
only provides three of the six required. You could override the constructor, for instance:
public Family(String firstName, String lastName, int age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
...
}
but you should do something with the rest of the fields that you didn't provide here that you provide in the other constructor.
You're calling the Family class constructor like this:
Family person = new Family("CHRIS", "PEREZ", 31);
But the only constructor in the class is defined like this:
public Family(String firstName, String lastName, int age, int phoneNumber,
String dob, String married) {
See that it has more parameters than you're passing in: phoneNumber, dob, married. In Java you have to give values to all parameters:
Family person = new Family("CHRIS", "PEREZ", 31, 123456, "5/Apr/1975", "who's asking");
Or, you need to define a new constructor that needs only firstName, lastName and age.
Related
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", "me#daysofourlives.com");
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","me#daysofourlives.com");
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:
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","me#daysofourlives.com");
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.
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.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the constructor on the FresherCandidate class which inherits from the Candidate class. I want to get the field from the Candidate class but I'm stuck.
This is constructor in the Candidate class:
public Candidate(String IDCandidate, String Firstname, String Lastname, String Birthdate, String Address, String Phone, String Email, int CandidateType) {
this.IDCandidate = IDCandidate;
this.Firstname = Firstname;
this.Lastname = Lastname;
this.Birthdate = Birthdate;
this.Address = Address;
this.Phone = Phone;
this.Email = Email;
this.CandidateType = CandidateType;
}
and the constructor in the FresherCandidate class:
public FresherCandidate(String GraduationDate, String GradudationRankm, String Education, String IDCandidate, String Firstname, String Lastname, String Birthdate, String Address, String Phone, String Email, int CandidateType) {
super(IDCandidate, Firstname, Lastname, Birthdate, Address, Phone, Email, CandidateType);
this.GraduationDate = GraduationDate;
this.GradudationRankm = GradudationRankm;
this.Education = Education;
}
How can I put these code:
this.IDCandidate = IDCandidate;
this.Firstname = Firstname;
this.Lastname = Lastname;
this.Birthdate = Birthdate;
this.Address = Address;
this.Phone = Phone;
this.Email = Email;
this.CandidateType = CandidateType;
into the constructor in the FresherCandidate:
public FresherCandidate(String GraduationDate, String GradudationRankm, String Education, String IDCandidate, String Firstname, String Lastname, String Birthdate, String Address, String Phone, String Email, int CandidateType) {
super(IDCandidate, Firstname, Lastname, Birthdate, Address, Phone, Email, CandidateType);
this.GraduationDate = GraduationDate;
this.GradudationRankm = GradudationRankm;
this.Education = Education;
--> here
}
It sounds like your "shadowing" when you extend a class you don't want to declare the variables again.
class A{
String name;
A(String name){
this.name = name;
}
}
class B extends A{
String name;
String number;
B(String name, String number){
super(name);
this.number = number;
}
public String toString(){
return name + ", " + number + ", " + super.name;
}
}
Since I declared a variable name in B, B essentially has two names. The A constructor assigns one of them.
System.out.println( new B("bob", "two") );
null, two, bob
The solution is to remove the declaration of name in the B class. Or in your case, remove the duplicate variable names in your extended class eg IDCandidate should only be declared in Candidate.
How do you create a java object that uses a variable from another class, or calls the entire constructor?
For example, accountNumber, firstName, lastName, phone are all variables passed in address is comprised of street, city, state, and zip, and has already been created:
Address address = new Address(street, city, state, zip);
Data is comprised of only megabytes, and has already been created:
Data data = new Data(megabytes);
This is what I have for the customer object:
Customer customer = new Customer(accountNumber, firstName, lastName, address, phone, data);
This is supposed to be an "overloaded constructor", but I don't understand what that means.
This is the constructor I have so far:
public Customer(String accountNumber, String firstName, String lastName, Address address, int phone, Data megabytes)
{
this.accountNumber = accountNumber;
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
this.megabytes= megabytes;
}
I get the error:
The constructor Customer(String, String, String, Address, int, Data) is undefined
At a glance everything seems fine. I hoped you saved the file before compiling.
Since you mentioned that you didn't understand what an overloaded constructor is, I'll try my best to explain that.
An overloaded constructor has the same constructor name but it differs from other constructors in the following ways -
It has different number of formal arguments
The order of type of formal parameters of the constructor are different
Here is an example -
public class Customer {
private String firstName;
private String lastName;
private int phoneNumber;
public Customer() {
// default constructor
}
public Customer(String firstName) {
this.firstName = firstName;
}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public Customer(String firstName, String lastName, int phoneNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
}
public Customer(int phoneNumber, String firstName, String lastName) {
this.phoneNumber = phoneNumber;
this.firstName = firstName;
this.lastName = lastName;
}
// This is not an overloaded constructor as there is already a constructor of type
// Customer(String, String)
// public Customer(String lastName, String firstName) {
// this.lastName = lastName;
// this.firstName = firstName;
// }
}
I'm doing a project with overloaded constructors in a class and I'm a little stuck, below is what I'm supposed to be doing with the overloaded constructors:
"One that allows first, middle, and last names to be passed as Strings with an int for age
One that accepts a Name object reference, and an age as an int
Make a new Name inside Person, copying the references for the parts of the name."
I'm not quite sure what to do with my code, here is what I got:
public class Person {
int age;
Name aPersonHasAName;
Name newPerson = new Name();
public Person(String firstName, String middleName, String lastName, int age) {
newPerson.firstName = firstName;
newPerson.middleName = middleName;
newPerson.lastName = lastName;
}
public Person(Name aPersonHasAName, int age) {
}
public void details() {
System.out.println(aPersonHasAName + " age: " + age);
}
}
I'm just lost as to what I'm supposed to be typing. I believe I've done the first overloaded constructor, but I am new to this.
So what should I be doing to make this work with overloaded constructors?
I think having the code from the other two classes might help.
Here is PersonTester:
public class PersonTester {
public static void main(String[] args) {
Person person1 = new Person("a1", "b1", "c1", 11);
Person person2 = new Person(new Name("a2", "b2", "c2"), 22);
Person person3 = new Person(new Name("a3", "c3"), 33);
Person person4 = new Person(new Name("a4"), 44);
Person person5 = new Person(new Name(), 55);
System.out.println(person1.details());
System.out.println(person2.details());
System.out.println(person3.details());
System.out.println(person4.details());
System.out.println(person5.details());
}
}
Then here is the Name class:
public class Name {
String firstName;
String middleName;
String lastName;
public Name(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public Name(String firstName, String lastName) {
this(firstName, "", lastName);
}
public Name(String firstName) {
this(firstName, "", "");
}
public Name() {
this("", "", "");
}
public String getLastName() {
return lastName;
}
public String getMiddleName() {
return middleName;
}
public String getFirstName() {
return firstName;
}
public String getFullName(String nameString) {
StringBuilder build = new StringBuilder();
build.append(nameString);
build.deleteCharAt(nameString.length() - 1);
build.insert(0, build.hashCode());
return build.toString();
}
}
The problem I am having now is the error message in PersonTester which is: The method println(boolean) in the type PrintStream is not applicable for the arguments (void)
I just need to know what in which class needs to be fixed to make it work.
I am very new to Java and object oriented programming.
So far so good. But eventually you'll reach a point where you duplicate a fair bit of code.
The constructor
public Person(String firstName, String middleName, String lastName, int age) {
is the most comprehensive one in the sense that it takes in all the possible data.
With the other constructors, say one that takes a last name and an age, you can use delegating constructors:
public Person(String lastName, int age) {
this(null, null, lastName, age); /*calls the other constructor*/
}
If you can't make such an assumption then you'll need to split up the name string by hand.
Updating your code:
public Person(String firstName, String middleName, String lastName, int age) {
newPerson.firstName = firstName;
newPerson.middleName =
newPerson.lastName = lastName;
this.age = age; //<---- was missing in your code
}
And your second contructor may look like this:
public Person(Name aPersonHasAName, int age) {
this.newPerson = aPersonHasAName;
this.age = age;
}
These contructors are implemented as you needed.
Notice that you already done your overloading, if you got multiple constructors with not the same titles you contructors overloading
public class Person {
int age;
Name aPersonHasAName;
public Person(String firstName, String middleName, String lastName, int age) {
aPersonHasAName = new Name();
aPersonHasAName.firstName = firstName;
aPersonHasAName.middleName = middleName;
aPersonHasAName.lastName = lastName;
this.age = age;
}
public Person(Name aPersonHasAName, int age) {
this.aPersonHasAName = aPersonHasAName;
this.age = age;
}
public void details() {
System.out.println(aPersonHasAName + " age: " + age);
}
}
I guess it also depends on if Name has a constructor for firstName, middleName, lastName
I'm very new to the java programming language and I would really like some help understanding what the following code is doing. I have a pretty decent understanding of what is going on within the Main class. My problem is what part "this._" plays within the code. How exactly are the names getting transferred? This is not homework just self study. The exercise can be found here:http://www.learnjavaonline.org/Functions Also, suggested reading would be great! Thanks!
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void printFullName(){
System.out.println(this.firstName+" "+this.lastname);
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[] {
new Student("Morgan", "Freeman"),
new Student("Brad", "Pitt"),
new Student("Kevin", "Spacey"),
};
for (Student s : students) {
s.printFullName();
}
}
}
this references to the object your is working in.
so in your sample
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public void printFullName(){
System.out.println(this.firstName+" "+this.lastname);
}
}
this.firstName is the private String firstName; value in your object/class
and firstName is the method parameter.
the this is required in this example as it otherwise would be firstName = firstName and that would assign the value of your parameter to itself.
The reason this is used is because the variables firstName and lastName are shadowed by the constructor parameters. See the differences with this:
class Student {
private String firstName;
private String lastName;
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
Compared to without this:
class Student {
private String myFirstName;
private String myLastName;
public Student(String firstName, String lastName) {
myFirstName = firstName;
myLastName = lastName;
}
You use this to reference variables in the current object.
See that variables with "this" are in constructor. This means THE OBJECT, so in the lines:
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
you assing variable to your object. Remember that these variables are in constructor !