473,373 Members | 1,141 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,373 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 1474
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

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

Similar topics

4
by: Chuck Amadi | last post by:
Hi all Anyone know a good Pygresql Tutorial for Interfacing between Python & Postgresql . Cheers Chuck
4
by: Robert Dufour | last post by:
I heard of this free IDE rfor VB.NET and C Sharp called , it seems, Csharp developper, I tried googling for it but got too many refs back :-( to be able to find it. Anyone have the URL where...
11
by: Stef Mientki | last post by:
hello, Is there some handy/ nice manner to view the properties of some variable ? As a newbie, I often want to see want all the properties of a var, and also some corner values (large arrays)...
6
by: placid | last post by:
Hi all, I'm looking for anyone who is working on a project at the moment that needs help (volunteer). The last project i worked on personally was screen-scraping MySpace profiles (read more at...
14
by: nsamad | last post by:
Hi All, Is there anyone using python script to programm the Telit GM862-GPS module?? I am a newbie in using python. I am developing an application in which i have to open a GPRS connection and...
2
by: jasonjasonjason | last post by:
Hi. Im A complete newbie with php and know only the very basics and wondered if anyone had the answer to my problem? I have been adding/including files within my php pages like so: <?php...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
4
by: maheshgupta0248 | last post by:
Hi all, Im a newbie in php, started learning php on my own. I want to create small website using php, that contains links for two simple webpages > C questions > C++ questions C questions...
1
by: maheshgupta0248 | last post by:
Hi everyone.. Im newbie in the driver development.. till now i have developed some drivers in Linux. Now I have to change to Windows.. can anyone tell me what are the real differences between driver...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.