12 Replies - 719 Views - Last Post: 09 October 2009 - 10:08 AM Rate Topic: -----

#1 jenngivitis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-October 09

Help with overloading constructors

Post icon  Posted 05 October 2009 - 03:44 PM

I'm not asking for anyone to do my homework, i just need some help getting started with the default and overloaded constructors. If anyone could help me it would be greatly appreciated.


The office Registrar of Birth & and Death, records the name of children at birth. In a child could have anywhere from 2 two parts to the name (first and last), to four parts to the name. Here are some examples:
Mary Jane
James Berrisford William
Maryln Agnes Lucrecia Henry.
In addition, the individual may change his/her name later on in life.

Design a class called Registration to:
• Contain four overloaded constructors, one which must be the default construct. The default constructor must initialize the middle names to blank (empty string) – just incase a person has no middle name.
•Provide two overloaded mutator methods, one to change the last name only, and the other to change the entire name.
•Provide accessor method to return each part of the name.
•Provide a method to return the number of individuals that were registered.

To keep variables names consistent, you are encouraged to use parametric names the same as field names.

Here is a sample of a test class, you may use it and register more children:

class TestRegistration
{
public static void main(String arg[])
{
System.out.println("The number of children registerd : " +Registration.getCount());
Registration r1 = new Registration("James", "Berrisford", "William");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount());
r1.changeLast("Bellami");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount());

// You may add more people
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Help with overloading constructors

#2 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: Help with overloading constructors

Posted 05 October 2009 - 03:53 PM

View Postjenngivitis, on 5 Oct, 2009 - 02:44 PM, said:

I'm not asking for anyone to do my homework, i just need some help getting started with the default and overloaded constructors. If anyone could help me it would be greatly appreciated.


The office Registrar of Birth & and Death, records the name of children at birth. In a child could have anywhere from 2 two parts to the name (first and last), to four parts to the name. Here are some examples:
Mary Jane
James Berrisford William
Maryln Agnes Lucrecia Henry.
In addition, the individual may change his/her name later on in life.

Design a class called Registration to:
• Contain four overloaded constructors, one which must be the default construct. The default constructor must initialize the middle names to blank (empty string) – just incase a person has no middle name.
•Provide two overloaded mutator methods, one to change the last name only, and the other to change the entire name.
•Provide accessor method to return each part of the name.
•Provide a method to return the number of individuals that were registered.

To keep variables names consistent, you are encouraged to use parametric names the same as field names.

Here is a sample of a test class, you may use it and register more children:

class TestRegistration
{
public static void main(String arg[])
{
System.out.println("The number of children registerd : " +Registration.getCount());
Registration r1 = new Registration("James", "Berrisford", "William");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount());
r1.changeLast("Bellami");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount());

// You may add more people
}
}



When you post code, please use the code tags...
Can you post what code you have so far?
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Help with overloading constructors

Posted 05 October 2009 - 05:27 PM

:code:
and post your constructorS for Registration
Was This Post Helpful? 1
  • +
  • -

#4 jenngivitis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-October 09

Re: Help with overloading constructors

Posted 08 October 2009 - 10:30 AM

[quote name='pbl' date='5 Oct, 2009 - 04:27 PM' post='789697']

public class Registration
{
private String firstName;
private String lastName;
private String middleName;
private String secondMiddleName;
private String changeLastName;
private String changeEntireName;
private int noOfChildren;
 
public Registration(String first, String last)
{
firstName = first;
lastName = last;
middleName = "";
secondMiddleName = ""
}

 public Registration(String first, String middle, String last)
{
this(first, last)
middleName = middle;
 }
 
public Registration(String first, String middle, String second, String last)
{
this(first, middle, last)
secondMiddleName = second
}




thats what I have so far for the class
Was This Post Helpful? 0
  • +
  • -

#5 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Help with overloading constructors

Posted 08 October 2009 - 10:49 AM

[quote name='jenngivitis' date='8 Oct, 2009 - 09:30 AM' post='792804']

View Postpbl, on 5 Oct, 2009 - 04:27 PM, said:

public class Registration
{
private String firstName;
private String lastName;
private String middleName;
private String secondMiddleName;
private String changeLastName;
private String changeEntireName;
private int noOfChildren;
 
public Registration(String first, String last)
{
firstName = first;
lastName = last;
middleName = "";
secondMiddleName = ""
}

 public Registration(String first, String middle, String last)
{
this(first, last)
middleName = middle;
 }
 
public Registration(String first, String middle, String second, String last)
{
this(first, middle, last)
secondMiddleName = second
}




thats what I have so far for the class


As I can see from your code, you've already successfully overloaded the constructor...
So what's the current problem now? Are you receiving any problems from the code you posted?

This post has been edited by AntonWebsters: 08 October 2009 - 10:49 AM

Was This Post Helpful? 1

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Help with overloading constructors

Posted 08 October 2009 - 10:59 AM

why dont you add ";" at the end of this()?
your constructors are fine.
in order to change the last name or any part of the name later in your code, just add set methods.
like:
public void setLastName(String lastName){
this.lastName = lastName;
}


and so on.

to access your private fields, use get methods.
public String getFirstName(){
return this.firstName;
}



to check how many people where registered, i would suggest a static variable count.
add count's value by one in the constructor.
thus, every time you create an instance of that class - a new person, count will raise by one.

//Global variable:
private static int count = 0;

//inside the constructor:
public Registration(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
this.middleName = "";
this.secondMiddleName = "";
counter++;
}
//to get how much persons were registered
public int getCounter(){
	return this.counter;
}  

hope i helped!;) 


This post has been edited by japanir: 08 October 2009 - 11:00 AM

Was This Post Helpful? 0
  • +
  • -

#7 jenngivitis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-October 09

Re: Help with overloading constructors

Posted 08 October 2009 - 12:10 PM

[quote name='japanir' date='8 Oct, 2009 - 09:59 AM' post='792830']

like this??

public class Registration
{
private String firstName;
private String lastName;
private String middleName;
private String secondMiddleName;
private String changeLastName;
private String changeEntireName;
private int noOfChildren;
private int count = 0;
 
public Registration(String first, String last)
{
this.firstName = first;
this.lastName = last;
this.middleName = "";
this.secondMiddleName = "";
count++;

}
 
public Registration(String first, String middle, String last)
{
this(first, last);
middleName = middle;
}
 
public Registration(String first, String middle, String second, String last)
{
this(first, middle, last);
secondMiddleName = second;
}

public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

public int getCount()
	{
		return this.count;
	}

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

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

public String getMiddle()
	{
		return this.middleName;
	}

public String getSecondMiddle()
	{
		return this.secondMiddleName;
	}

 }




i'm still getting errors with this test class

class TestRegistration
{
public static void main(String arg[])
{
System.out.println("The number of children registerd : " +Registration.getCount()); 
Registration r1 = new Registration("James", "Berrisford", "William");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount()); 
r1.changeLast("Bellami");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount()); 
}
}



Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with overloading constructors

Posted 08 October 2009 - 12:25 PM

making count static will make more sense, since all instances of the class will have the same count
private static int count = 0;



you getCount() method should be static as well

public static int getCount()
{
		return this.count;
}


Was This Post Helpful? 0
  • +
  • -

#9 jenngivitis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-October 09

Re: Help with overloading constructors

Posted 08 October 2009 - 12:44 PM

ok....I made those changes and now I get an error message saying "non-static variable this cannot be referenced from a static context"

what do I do about that?
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with overloading constructors

Posted 08 October 2009 - 12:48 PM

post the code you have so far
Was This Post Helpful? 0
  • +
  • -

#11 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Help with overloading constructors

Posted 08 October 2009 - 09:41 PM

View Postjenngivitis, on 8 Oct, 2009 - 11:44 AM, said:

ok....I made those changes and now I get an error message saying "non-static variable this cannot be referenced from a static context"

what do I do about that?


In your main method, did you make an instance of class Registration like this : Registration r1 = new Registration(), and then calling the method by using r1.getCount()? Or did you called the method statically like this : Registration.getCount()?
Was This Post Helpful? 0
  • +
  • -

#12 jenngivitis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 05-October 09

Re: Help with overloading constructors

Posted 09 October 2009 - 08:07 AM

View PostAntonWebsters, on 8 Oct, 2009 - 08:41 PM, said:

View Postjenngivitis, on 8 Oct, 2009 - 11:44 AM, said:

ok....I made those changes and now I get an error message saying "non-static variable this cannot be referenced from a static context"

what do I do about that?


In your main method, did you make an instance of class Registration like this : Registration r1 = new Registration(), and then calling the method by using r1.getCount()? Or did you called the method statically like this : Registration.getCount()?


this is what i have so far
public class Registration
{
private String firstName;
private String lastName;
private String middleName;
private String secondMiddleName;
private String changeLastName;
private String changeEntireName;
private int noOfChildren;
private static int count = 1;
 
public Registration(String first, String last)
{
this.firstName = first;
this.lastName = last;
this.middleName = "";
this.secondMiddleName = "";
count++;

}
 
public Registration(String first, String middle, String last)
{
this(first, last);
middleName = middle;
}
 
public Registration(String first, String middle, String second, String last)
{
this(first, middle, last);
secondMiddleName = second;
}

public void setLastName(String lastName)
	{
		this.lastName = lastName;
	}

public static int getCount()
	{
		return count;
	}

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

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

public String getMiddle()
	{
		return this.middleName;
	}

public String getSecondMiddle()
	{
		return this.secondMiddleName;
	}

	void changeLast(String lastName) 
	{
	   
	}

 

}



and this

class TestRegistration
{
public static void main(String arg[])
{
System.out.println("The number of children registerd : " +Registration.getCount()); 
Registration r1 = new Registration("James", "Berrisford", "William");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount()); 
r1.changeLast("Bellami");
System.out.println(r1.getFirst() + ", " + r1.getLast() + " is registerd");
System.out.println("The number of children registerd: " +Registration.getCount()); 
}
}


Was This Post Helpful? 0
  • +
  • -

#13 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Help with overloading constructors

Posted 09 October 2009 - 10:08 AM

static variable or method are referred to a class. not to an instance.
using the keyword "this" referred to "this instance".
so thats why you cant use this inside a static method.
your code is ok.
just initiate counter to 0 and not to one.
also, add the static methodd getCounter() as mostyfriedman suggested, just dont use "this" inside.

hope it makes sense.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1