Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

stapler class help

Question posted by: javaLoser (Newbie) on May 15th, 2008 01:01 AM
ok so here is the scoop i need to design a stapler class the addStaples method takes one argument for the number of staples but the stapler should never have more than 100 staples. the two staple methods one takes 0 arguments and the other takes an argument for how many times we staple a warning message should be printed when there are no staples left. the stapler starts with 100 staples when i run the program it gives me negative staples and adds more than 100 staples i need help im going crazy with this thing

here is my code

public final class Stapler {

public String color;

private int numStaples;




Stapler(){
color="Blue";
numStaples = 100;
System.out.println("New Stapler with 100 Staples");
}

public void addStaples(int add){
System.out.println("Trying to add " + add +" staples");
if(numStaples >=100){

System.out.println("Can't add anymore staples!");
numStaples = add;
}
else if (numStaples ==0 ){
System.out.println("Added " + numStaples + " staples.");
numStaples = numStaples + add;
}

else{
System.out.println("Added " + add +" staples.");

System.out.println(numStaples + " Staples left");
numStaples = add;
}
}

public void staple(){

System.out.println("Trying to staple once.");
if (numStaples <=0){
System.out.println("No staples left.");
}
else{
System.out.println("Stapled once!");
numStaples = numStaples-1;
System.out.println(numStaples + " staples left." );
}

}

public void staple(int staple){
System.out.println("Trying to staple " + staple + " times" );
if (numStaples <=100){
System.out.println("Stapled " + numStaples +" times");
numStaples=numStaples-staple;
System.out.println("0 Staples left.");

}
else if (numStaples >numStaples){

System.out.println("Stapled "+ staple +" times");
numStaples=0;
System.out.println(numStaples + " Staples left.");

}

}
}
sukatoa's Avatar
sukatoa
Needs Regular Fix
467 Posts
May 15th, 2008
01:35 AM
#2

Re: stapler class help
Quote:
ok so here is the scoop i need to design a stapler class the addStaples method takes one argument for the number of staples but the stapler should never have more than 100 staples. the two staple methods one takes 0 arguments and the other takes an argument for how many times we staple a warning message should be printed when there are no staples left. the stapler starts with 100 staples when i run the program it gives me negative staples and adds more than 100 staples i need help im going crazy with this thing

here is my code

public final class Stapler {

public String color;

private int numStaples;




Stapler(){
color="Blue";
numStaples = 100;
System.out.println("New Stapler with 100 Staples");
}

public void addStaples(int add){
System.out.println("Trying to add " + add +" staples");
if(numStaples >=100){

System.out.println("Can't add anymore staples!");
numStaples = add;
}
else if (numStaples ==0 ){
System.out.println("Added " + numStaples + " staples.");
numStaples = numStaples + add;
}

else{
System.out.println("Added " + add +" staples.");

System.out.println(numStaples + " Staples left");
numStaples = add;
}
}

public void staple(){

System.out.println("Trying to staple once.");
if (numStaples <=0){
System.out.println("No staples left.");
}
else{
System.out.println("Stapled once!");
numStaples = numStaples-1;
System.out.println(numStaples + " staples left." );
}

}

public void staple(int staple){
System.out.println("Trying to staple " + staple + " times" );
if (numStaples <=100){
System.out.println("Stapled " + numStaples +" times");
numStaples=numStaples-staple;
System.out.println("0 Staples left.");

}
else if (numStaples >numStaples){

System.out.println("Stapled "+ staple +" times");
numStaples=0;
System.out.println(numStaples + " Staples left.");

}

}
}


Just a reminder,

Please inclose your codes with a codetags....
Highlight your code then press the button(#) above the reply window

or add [/CODE] at the end and [CODE=JAVA] at the top of your code.

and don't double post your thread....

About your program, i don't know how your main class did the handling of that Sampler class, can you show the main class?(that controls the program) that contains a main method...

regards,
sukatoa

Reply
JosAH's Avatar
JosAH
Chief Editor
7,769 Posts
May 15th, 2008
08:00 AM
#3

Re: stapler class help
A stapler staples; it doesn't talk but more important is that your logic is flakey
here and there. A new stapler contains 100 staples. If it contains, say 40 staples
I can add 60 more staples; more simply don't fit. It is quite easy to translate these
notions to Java code:

Expand|Select|Wrap|Line Numbers
  1. public class Stapler {
  2.    public static final int MAXSTAPLES= 100;
  3.    private int staples= MAXSTAPLES; // init at construction time
  4.    //
  5.    // not really needed constructor:
  6.    public Stapler() { } // do nothing
  7.    ...
  8. }


This is the first skeleton of the Stapler class: it contains 100 staples. Now add
a 'staple' method; it can staple one staple if it contains staples; it returns true
if it succeeded and false if it was empty:

Expand|Select|Wrap|Line Numbers
  1. public boolean staple() {
  2.    if (staples > 0) {
  3.       staples--;
  4.       return true;
  5.    }
  6.    return false;
  7. }


Note that it doesn't say anything, i.e. it just staples and returns whether or not
it had succeeded. An overloaded method that staples n times could look like this:

Expand|Select|Wrap|Line Numbers
  1. public int staple(int n) { // staple n staples 
  2.    while (n > 0 && staple()) { // protect against nonsense values
  3.       staple(); // staple once
  4.       n--;
  5.    }
  6.    return n; // return the number of staples to do
  7. }


Refilling the stapler takes a bit of 101 math: when it has x <= 100 staples left
you can add 100-x more staples to it. Suppose we can't remove staples from
the stapler:

Expand|Select|Wrap|Line Numbers
  1. public int refill(int n) { // try to add n staples to the stapler
  2.    if (n > 0) { // protect against nonsense values
  3.       int newStaples= Math.min(staples+n, MAXSTAPLES);
  4.       n-= newStaples-staples; // those didn't fit
  5.       staples= newStaples; // refill the stapler
  6.    }
  7.    return n; // return number of staples that couldn't be added
  8. }


You can poop up this little class a bit more by adding a getter method. You can
extend this class by a TalkingStapler that indeed tells what it's doing (that'd be
a bit of a silly stapler if you'd ask me, but feel free to add whatever you want).

kind regards,

Jos

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,871 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Java Forum Contributors