stapler class help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaLoser
    New Member
    • May 2008
    • 1

    stapler class help

    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.prin tln("New Stapler with 100 Staples");
    }

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

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

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

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

    public void staple(){

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

    }

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

    }
    else if (numStaples >numStaples){

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

    }

    }
    }
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by javaLoser
    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.prin tln("New Stapler with 100 Staples");
    }

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

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

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

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

    public void staple(){

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

    }

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

    }
    else if (numStaples >numStaples){

    System.out.prin tln("Stapled "+ staple +" times");
    numStaples=0;
    System.out.prin tln(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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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:

      [code=java]
      public class Stapler {
      public static final int MAXSTAPLES= 100;
      private int staples= MAXSTAPLES; // init at construction time
      //
      // not really needed constructor:
      public Stapler() { } // do nothing
      ...
      }
      [/code]

      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:

      [code=java]
      public boolean staple() {
      if (staples > 0) {
      staples--;
      return true;
      }
      return false;
      }
      [/code]

      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:

      [code=java]
      public int staple(int n) { // staple n staples
      while (n > 0 && staple()) { // protect against nonsense values
      staple(); // staple once
      n--;
      }
      return n; // return the number of staples to do
      }
      [/code]

      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:

      [code=java]
      public int refill(int n) { // try to add n staples to the stapler
      if (n > 0) { // protect against nonsense values
      int newStaples= Math.min(staple s+n, MAXSTAPLES);
      n-= newStaples-staples; // those didn't fit
      staples= newStaples; // refill the stapler
      }
      return n; // return number of staples that couldn't be added
      }
      [/code]

      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

      Comment

      Working...