can anyone help me? newbie- code errors!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • itgetsharder
    New Member
    • Oct 2007
    • 9

    can anyone help me? newbie- code errors!

    can anyone help me? code errors!
    --------------------------------------------------------------------------------

    im creating a code for a printer. the question i am trying to answer is :

    MyPrinter needs some printing methods. The first, signature:
    public boolean printOne(String text) {}
    should take a String as a parameter, and output it on a single line to the terminal window.
    It should also increment (add 1 to) the total number of copies made, and decrement (subtract 1 from) the number of sheets of paper available.
    It should return the boolean value true(this is actually to avoid confusion in the next assignment)
    the second, signature:
    public void print5(String text) {}
    should take a String as a parameter and outputs it on five successive single lines to the terminal window.
    It should also increase the total number of copies made by 5, and reduce the number of sheets available by 5.
    It should not return anything.


    this is my coding. i keep getting unreachable statement and im a newbie so i dont really understand what ive done wrong.


    [CODE=java]public class MyPrinter
    {
    // instance variables - replace the example below with your own
    private int prints;
    private int sheets;
    private String printerName;
    private boolean printOne;
    private boolean print5;
    private String name;
    private int startPaper;
    private int end;


    /**
    * Constructor for objects of class MyPrinter
    */
    public MyPrinter()
    {
    // initialise instance variables
    prints = 0;
    sheets = 500;
    printerName = "Epson";
    }

    public MyPrinter(Strin g name, int startPaper, int end)
    {
    // initialise instance variables
    prints = 0;
    sheets = startPaper;
    printerName = name;
    }

    /**
    * An example of a method - replace this comment with your own
    *
    * @param y a sample parameter for a method
    * @return the sum of x and y
    */





    public void testMyPrinter()
    {
    // put your code here
    System.out.prin tln("ABCDEFGHIJ KLMNOPQRSTUVWXY Z");
    System.out.prin tln("0123456789 ");
    }


    public void Test()
    {
    MyPrinter p = new MyPrinter();
    p.testMyPrinter ();
    System.out.prin tln("Printer Name: " + p.getName() );
    System.out.prin tln("Sheets: " + p.getPaper() );
    System.out.prin tln("copies: " + p.getCopies() );
    p = new MyPrinter("HP", 200, 0); p.testMyPrinter ();
    System.out.prin tln("Printer Name: " + p.getName() );
    System.out.prin tln("Sheets: " + p.getPaper() );
    System.out.prin tln("copies: " + p.getCopies() );
    p.printOne("Tes t printOne"); // ignore return value
    System.out.prin tln("Sheets: " + p.getPaper() );
    System.out.prin tln("copies: " + p.getCopies() );
    System.out.prin tln("Sheets: " + p.getPaper() );
    System.out.prin tln("copies: " + p.getCopies() );


    }


    public int getCopies()
    {
    return prints;
    }

    public int getPaper()
    {
    return sheets;
    }

    public String getName()
    {
    return printerName;
    }

    public boolean printOne(String text)
    {
    System.out.prin tln("printOne") ;
    return startPaper == sheets - 1;
    return end == prints + 1;


    }




    public void print5(String text)
    {
    System.out.prin tln("print5");
    System.out.prin tln("print5");
    System.out.prin tln("print5");
    System.out.prin tln("print5");
    System.out.prin tln("print5");
    startPaper = sheets - 5;
    end = prints + 5;

    }



    }[/CODE]


    the output is supposed to look like this below:

    The output when a test object is created should be
    ABCDEFGHIJKLMNO PQRSTUVWXYZ
    0123456789
    Printer Name: Epson
    Sheets: 500
    copies: 0
    ABCDEFGHIJKLMNO PQRSTUVWXYZ
    0123456789
    Printer Name: HP
    Sheets: 200
    copies: 0
    Test printOne
    Sheets: 199
    copies: 1
    Test print5
    Test print5
    Test print5
    Test print5
    Test print5
    Sheets: 194
    copies: 6



    any help would be much appreciated as ive been at this all day now.

    thanks x
    Last edited by Ganon11; Oct 30 '07, 06:52 PM. Reason: Please use the [CODE] tags provided.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    1.) Use code tags when posting code
    2.) You can return only one value from a method. If a return statement is encountered in a method, any code that you put after that return statement is deemed unreachable because the method is exited when the return was found.

    Comment

    • itgetsharder
      New Member
      • Oct 2007
      • 9

      #3
      sorry to post again today, but im trying to figure out why my code isnt doing what i want.
      Im trying to get the code to add 1 from sheets printed when a print has occured and minus 1 from prints available.

      This is the code i have been using:


      [CODE=java]public boolean printOne(String text)
      {
      System.out.prin tln("Test printOne");
      return sheets == sheets - 1;
      }
      public boolean PrintOne(String text)
      {
      return prints == prints + 1;
      }[/CODE]


      any help would be appreciated

      thanks x

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by itgetsharder
        sorry to post again today, but im trying to figure out why my code isnt doing what i want.
        Im trying to get the code to add 1 from sheets printed when a print has occured and minus 1 from prints available.

        This is the code i have been using:


        Code: ( text )
        public boolean printOne(String text)
        {
        System.out.prin tln("Test printOne");
        return sheets == sheets - 1;
        }
        public boolean PrintOne(String text)
        {
        return prints == prints + 1;
        }


        any help would be appreciated

        thanks x
        So your method really doesn't need to return a boolean does it? And does not need the String text argument either, does it?
        You can update both the sheets and prints variables all in one method too. So you just need one method, right?

        Make these changes and post back if you still have problems.

        Comment

        • itgetsharder
          New Member
          • Oct 2007
          • 9

          #5
          i've been told i have to use boolean (not sure why). if i have to use boolean, how do i make the code do what i need it to?

          thanks x

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by itgetsharder
            i've been told i have to use boolean (not sure why). if i have to use boolean, how do i make the code do what i need it to?

            thanks x
            Who told you to use a boolean?
            I don't know how to make it work using a boolean. Do you want to do something else in the printOne method besides updating the sheets and prints variables?

            Comment

            • itgetsharder
              New Member
              • Oct 2007
              • 9

              #7
              Originally posted by r035198x
              Who told you to use a boolean?
              I don't know how to make it work using a boolean. Do you want to do something else in the printOne method besides updating the sheets and prints variables?

              nope i dont need to. ive been set an assignment and i have to use boolean in this part of the method. i cant get it to work though. x

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by itgetsharder
                nope i dont need to. ive been set an assignment and i have to use boolean in this part of the method. i cant get it to work though. x
                You want to decrement sheets and increment prints. And you have been asked to use a boolean to do that?

                [CODE=java]public boolean printOne () {
                sheets--;
                prints++;
                return true;
                }[/CODE]
                As you can see the use of the boolean there is pathetic. It doesn't do anything at all. Do you have the complete specs of the problem that was given to you?
                Last edited by r035198x; Oct 31 '07, 03:02 PM. Reason: corrected some mess in there

                Comment

                • itgetsharder
                  New Member
                  • Oct 2007
                  • 9

                  #9
                  Originally posted by r035198x
                  You want to decrement sheets and increment prints. And you have been asked to use a boolean to do that?

                  [CODE=java]public boolean printOne () {
                  sheets--;
                  prints++;
                  return true;
                  }[/CODE]
                  As you can see the use of the boolean there is pathetic. It doesn't do anything at all. Do you have the complete specs of the problem that was given to you?
                  yes i do.
                  thanks so much. been trying to figure that out for days haha. you're a saint. x

                  Comment

                  • itgetsharder
                    New Member
                    • Oct 2007
                    • 9

                    #10
                    sorry to have to ask for help again. but i dont really understand what im trying to do.
                    I have to :
                    create a new method to make multiple copies:
                    public int printMany(Strin g text, int number)
                    This should try to make as many copies of the string as are passed to it as number
                    (so printMany("test ",7) should produce 7 copies).
                    It should use printOne() to actually produce the copy.


                    and this is my code for printOne at the moment:
                    Code:
                     public boolean printOne(String text) 
                        {
                             if(sheets >= 0)
                             {
                                               
                                    System.out.println("Test printOne");
                                    sheets--;
                                    prints++;
                                    
                                    return true;
                              
                            }
                               
                                    
                                    else {
                                System.out.println("You have no sheets left, your balance is " + sheets);
                                return false;   
                        }
                    }


                    The suggestions for how to do this is as follows:
                    declare a local boolean
                    assume true
                    while (there are copies to be made AND boolean is true)
                    boolean = return value of printOne
                    if boolean is true
                    decrement number to be made
                    endif
                    end while

                    return number of copies still to be made



                    i just dont know what this means. any help to make this clearer or some direction would be great. thanks guys x

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by itgetsharder
                      sorry to have to ask for help again. but i dont really understand what im trying to do.
                      I have to :
                      create a new method to make multiple copies:
                      public int printMany(Strin g text, int number)
                      This should try to make as many copies of the string as are passed to it as number
                      (so printMany("test ",7) should produce 7 copies).
                      It should use printOne() to actually produce the copy.


                      and this is my code for printOne at the moment:
                      Code:
                       public boolean printOne(String text) 
                          {
                               if(sheets >= 0)
                               {
                                                 
                                      System.out.println("Test printOne");
                                      sheets--;
                                      prints++;
                                      
                                      return true;
                                
                              }
                                 
                                      
                                      else {
                                  System.out.println("You have no sheets left, your balance is " + sheets);
                                  return false;   
                          }
                      }


                      The suggestions for how to do this is as follows:
                      declare a local boolean
                      assume true
                      while (there are copies to be made AND boolean is true)
                      boolean = return value of printOne
                      if boolean is true
                      decrement number to be made
                      endif
                      end while

                      return number of copies still to be made



                      i just dont know what this means. any help to make this clearer or some direction would be great. thanks guys x
                      You do know how to program, do you? This entire thing can be implemented
                      something like this:

                      [code=java]
                      int printMany(Strin g text, int nofCopies) {
                      for (; nofCopies > 0; nofCopies--)
                      if (!printOne(text )) // something failed
                      break;
                      return nofCopies;
                      }
                      [/code]

                      The above method returns the number of copies still to be made. If it returns zero
                      everything is printed without errors.

                      kind regards,

                      Jos

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by itgetsharder
                        sorry to have to ask for help again. but i dont really understand what im trying to do.
                        I have to :
                        create a new method to make multiple copies:
                        public int printMany(Strin g text, int number)
                        This should try to make as many copies of the string as are passed to it as number
                        (so printMany("test ",7) should produce 7 copies).
                        It should use printOne() to actually produce the copy.


                        and this is my code for printOne at the moment:
                        Code:
                         public boolean printOne(String text) 
                            {
                                 if(sheets >= 0)
                                 {
                                                   
                                        System.out.println("Test printOne");
                                        sheets--;
                                        prints++;
                                        
                                        return true;
                                  
                                }
                                   
                                        
                                        else {
                                    System.out.println("You have no sheets left, your balance is " + sheets);
                                    return false;   
                            }
                        }


                        The suggestions for how to do this is as follows:
                        declare a local boolean
                        assume true
                        while (there are copies to be made AND boolean is true)
                        boolean = return value of printOne
                        if boolean is true
                        decrement number to be made
                        endif
                        end while

                        return number of copies still to be made



                        i just dont know what this means. any help to make this clearer or some direction would be great. thanks guys x
                        Just follow the suggestions given to you.
                        First write this part
                        Code:
                        declare a local boolean
                        assume true
                        while (there are copies to be made AND boolean is true)
                        and let's see if you can get it right.

                        Comment

                        Working...