472,135 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,135 software developers and data experts.

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.


Expand|Select|Wrap|Line Numbers
  1. public class MyPrinter
  2. {
  3. // instance variables - replace the example below with your own
  4. private int prints;
  5. private int sheets;
  6. private String printerName;
  7. private boolean printOne;
  8. private boolean print5;
  9. private String name;
  10. private int startPaper;
  11. private int end;
  12.  
  13.  
  14. /**
  15. * Constructor for objects of class MyPrinter
  16. */
  17. public MyPrinter()
  18. {
  19. // initialise instance variables
  20. prints = 0;
  21. sheets = 500;
  22. printerName = "Epson";
  23. }
  24.  
  25. public MyPrinter(String name, int startPaper, int end)
  26. {
  27. // initialise instance variables
  28. prints = 0;
  29. sheets = startPaper;
  30. printerName = name;
  31. }
  32.  
  33. /**
  34. * An example of a method - replace this comment with your own
  35. * @param y a sample parameter for a method
  36. * @return the sum of x and y 
  37. */
  38.  
  39.  
  40.  
  41.  
  42.  
  43. public void testMyPrinter()
  44. {
  45. // put your code here
  46. System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  47. System.out.println("0123456789");
  48. }
  49.  
  50.  
  51. public void Test()
  52. {
  53. MyPrinter p = new MyPrinter();
  54. p.testMyPrinter();
  55. System.out.println("Printer Name: " + p.getName() );
  56. System.out.println("Sheets: " + p.getPaper() );
  57. System.out.println("copies: " + p.getCopies() );
  58. p = new MyPrinter("HP", 200, 0); p.testMyPrinter();
  59. System.out.println("Printer Name: " + p.getName() );
  60. System.out.println("Sheets: " + p.getPaper() );
  61. System.out.println("copies: " + p.getCopies() );
  62. p.printOne("Test printOne"); // ignore return value 
  63. System.out.println("Sheets: " + p.getPaper() ); 
  64. System.out.println("copies: " + p.getCopies() ); 
  65. System.out.println("Sheets: " + p.getPaper() ); 
  66. System.out.println("copies: " + p.getCopies() ); 
  67.  
  68.  
  69. }
  70.  
  71.  
  72. public int getCopies()
  73. {
  74. return prints;
  75. }
  76.  
  77. public int getPaper()
  78. {
  79. return sheets;
  80. }
  81.  
  82. public String getName()
  83. {
  84. return printerName;
  85. }
  86.  
  87. public boolean printOne(String text) 
  88. {
  89. System.out.println("printOne");
  90. return startPaper == sheets - 1;
  91. return end == prints + 1;
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. public void print5(String text)
  100. {
  101. System.out.println("print5");
  102. System.out.println("print5");
  103. System.out.println("print5");
  104. System.out.println("print5");
  105. System.out.println("print5");
  106. startPaper = sheets - 5;
  107. end = prints + 5;
  108.  
  109. }
  110.  
  111.  
  112.  
  113. }

the output is supposed to look like this below:

The output when a test object is created should be
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
Printer Name: Epson
Sheets: 500
copies: 0
ABCDEFGHIJKLMNOPQRSTUVWXYZ
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
Oct 30 '07 #1
11 1413
r035198x
13,262 8TB
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.
Oct 30 '07 #2
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:


Expand|Select|Wrap|Line Numbers
  1. public boolean printOne(String text) 
  2.     {
  3.         System.out.println("Test printOne");
  4.         return sheets == sheets - 1;
  5.     }
  6.         public boolean PrintOne(String text) 
  7.         {
  8.             return prints == prints + 1;
  9.         }

any help would be appreciated

thanks x
Oct 30 '07 #3
r035198x
13,262 8TB
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.println("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.
Oct 30 '07 #4
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
Oct 30 '07 #5
r035198x
13,262 8TB
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?
Oct 30 '07 #6
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
Oct 31 '07 #7
r035198x
13,262 8TB
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?

Expand|Select|Wrap|Line Numbers
  1. public boolean printOne () {
  2. sheets--;
  3.    prints++;
  4.    return true;
  5. }
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?
Oct 31 '07 #8
You want to decrement sheets and increment prints. And you have been asked to use a boolean to do that?

Expand|Select|Wrap|Line Numbers
  1. public boolean printOne () {
  2. sheets--;
  3.    prints++;
  4.    return true;
  5. }
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
Oct 31 '07 #9
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(String 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:
Expand|Select|Wrap|Line Numbers
  1.  public boolean printOne(String text) 
  2.     {
  3.          if(sheets >= 0)
  4.          {
  5.  
  6.                 System.out.println("Test printOne");
  7.                 sheets--;
  8.                 prints++;
  9.  
  10.                 return true;
  11.  
  12.         }
  13.  
  14.  
  15.                 else {
  16.             System.out.println("You have no sheets left, your balance is " + sheets);
  17.             return false;   
  18.     }
  19. }


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
Oct 31 '07 #10
JosAH
11,448 Expert 8TB
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(String 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:
Expand|Select|Wrap|Line Numbers
  1.  public boolean printOne(String text) 
  2.     {
  3.          if(sheets >= 0)
  4.          {
  5.  
  6.                 System.out.println("Test printOne");
  7.                 sheets--;
  8.                 prints++;
  9.  
  10.                 return true;
  11.  
  12.         }
  13.  
  14.  
  15.                 else {
  16.             System.out.println("You have no sheets left, your balance is " + sheets);
  17.             return false;   
  18.     }
  19. }


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:

Expand|Select|Wrap|Line Numbers
  1. int printMany(String text, int nofCopies) {
  2.    for (; nofCopies > 0; nofCopies--)
  3.       if (!printOne(text)) // something failed
  4.          break;
  5.    return nofCopies;
  6. }
  7.  
The above method returns the number of copies still to be made. If it returns zero
everything is printed without errors.

kind regards,

Jos
Oct 31 '07 #11
r035198x
13,262 8TB
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(String 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:
Expand|Select|Wrap|Line Numbers
  1.  public boolean printOne(String text) 
  2.     {
  3.          if(sheets >= 0)
  4.          {
  5.  
  6.                 System.out.println("Test printOne");
  7.                 sheets--;
  8.                 prints++;
  9.  
  10.                 return true;
  11.  
  12.         }
  13.  
  14.  
  15.                 else {
  16.             System.out.println("You have no sheets left, your balance is " + sheets);
  17.             return false;   
  18.     }
  19. }


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
Expand|Select|Wrap|Line Numbers
  1. declare a local boolean
  2. assume true
  3. while (there are copies to be made AND boolean is true) 
and let's see if you can get it right.
Oct 31 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Stef Mientki | last post: by
6 posts views Thread by placid | last post: by
16 posts views Thread by Raxit | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.