• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Methods/Constructors Help

 
Evan Keenlyside
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I've got this assignment, but I'm totally lost. I'm not very good at java and some of this stuff is pretty esoteric. Maybe if someone could explain in simpler terms how these things work, because the text book is awful.

Okay, so the first part of the assignment goes like this:

Write a class that represents a jar of pennies. public class PennyJar must have the following methods:

public PennyJar()
// a constructor for the class. It initializes the
// number of pennies in the jar to be 0.

public PennyJar(int n)
// another constructor for the class. It initializes
// the number of pennies in the jar to be the value
// of the parameter.


How do constructors work? Initialization? What parameter?

If someone could lead me in the right direction that would be great.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to Java. The language may seem esoteric at first, but there's a good solid reason for doing things the way they're done. Well, usually, that is.

Quickie Class/Constructor Overview:

A class is a blueprint of a collection of states and actions. An object is a thing created from the blueprint. The states are variables, and the actions are methods.

Here's a simple class:



That's a very simple class. Now, in your program, if you create a MyClass using

In the first line, we created a new object called myClass (note the capitalization) with the constructor that takes a single int value and initializes its number with the passed value, 10. Then we create a new int variable and assign the value returned from the getTheNumber method. Last, we print it.

Does that help? Can you see how to apply this to your assignment?
 
Lou Bassett
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are designing a class, you are designing what it can know and what it can do. What it knows are called instance variables. Good practice is to declared them as private because you only want the class to know what the values are.



A constructor is a method with the same name as the class (ie. MyClass) that is invoked when you create an object of type "MyClass". Sort of an initialization routine. This would happen when you issued the following:



From here you only want access to name through a public method, and not directly to the object. If the constructor took a parameter then you could initialize the instance variable to what ever you passed in.

[ November 12, 2004: Message edited by: Lou Bassett ]
[ November 12, 2004: Message edited by: Lou Bassett ]
 
Lou Bassett
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as textbooks are concerned, try the "Head First" series by Bert Bates and Kathy Sierra.
 
Evan Keenlyside
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I got the methods figured out, but now I have to do this wierdo stuff with it. For my assignment I have to use these methods:

public class PennyJar
{
public int pennies;

public PennyJar()
{
pennies = 0;
}

public PennyJar(int n)
{
pennies = n;
}

public void takePenny()
{
pennies = pennies--;
}


public void addPenny()
{
pennies = pennies++;
}



public int total()
{
return pennies;

}
}



I somehow "test" these methods using this "driver", whatever that is:

public class PennyJarTester
{
public static void main(String[] args)
{
// create an instance of class PennyJar
// use the default constructor
PennyJar p1 = new PennyJar();

p1.addPenny();
p1.addPenny();
p1.addPenny();
p1.takePenny();

System.out.print("there are now " + p1.total());
System.out.println(" pennies in jar p1.");
System.out.println("(there should be 2)");

// now create another PennyJar object,
// and use the other constructor
PennyJar p2 = new PennyJar(55);

p2.addPenny();
p2.addPenny();
p2.addPenny();
p2.takePenny();

System.out.print("there are now " + p2.total());
System.out.println(" pennies in jar p2.");
System.out.println("(there should be 57)");

}
}


This does not work properly, however. The output of the program is this:

there are now 0 pennies in jar p1
<there should be 2>
there are now 55 pennies in jar p2
<there should be 57>


Are my methods wrong? What's going on here?
 
Sebastiaan Kortleven
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pennies = pennies++; should be just pennies++; (same with --)

pennies = pennies++; will not change the value of pennies
 
Evan Keenlyside
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, thanks.

Okay, now how do I overload the take and add methods so they will accept an integer parameter?

What exactly is overloading?
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloading a method means you have the same method name, but different arguments(parameters) passed to it, and optionally a different return value. That way the JVM knows which method to use just by looking at the arg list, even if there is more than one method with the same name in the class. In your case, the add method will just add one penny at a time, so let's say you want to tell the method you want to add 5 or 6 pennies to the jar. All we need to do is create a new add method like so:



So when you call the addPenny method on your class, if the signture of the call has an int argument, the JVM knows the correct one to call!

HTH
E
 
Evan Keenlyside
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. I got the overloading part done, but the last part of the assignment is still confusing me.

"Now write methods addNickel() and addDime() that are overloaded to either take no parameter, in which case they add one nickel/dime to the jar, or take one integer which specifies the number of nickels or dimes to add. You must use calls to the addPenny() method to implement these methods. Modify the driver to test these four new methods."

How do I implement the methods with a call to the addPenny method?
[ November 19, 2004: Message edited by: Evan Keenlyside ]
 
Evan Keenlyside
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody?

?
 
Barry Higgins
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you right to add a nickel you would have to call the addPenny function 5 times (there is five cents in a nickel right?)
This is a pretty odd way of doing it instead of saying
public void addNickel() {
pennies+=5;
}

you will have to say
public void addNickel() {
for (int i=0; i<5; i++)
addPenny();
}

I'll leave you to implement the other functions!
Barry
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember the addPenny() method that takes a parameter? Can you use this to implement (write code for) addNickel() or addDime()?

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic