473,666 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can anyone help me? newbie- code errors!

9 New Member
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
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
Oct 30 '07 #1
11 1494
r035198x
13,262 MVP
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
itgetsharder
9 New Member
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 MVP
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.
Oct 30 '07 #4
itgetsharder
9 New Member
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 MVP
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
itgetsharder
9 New Member
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 MVP
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
itgetsharder
9 New Member
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
itgetsharder
9 New Member
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:
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

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

Similar topics

4
5807
by: Chuck Amadi | last post by:
Hi all Anyone know a good Pygresql Tutorial for Interfacing between Python & Postgresql . Cheers Chuck
4
987
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 this can be downloaded from? Your help IS appreciated, Bob
11
1527
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) etc. Probably it's not so difficult, but I don't see how to distinguish for example between a string and an array. An array has a shape, a string not etc.
6
1249
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 the following link) http://placid.programming.projects.googlepages.com/screen-scrapingmyspaceprofiles but that didn't go all to well, so im kinda bored and need something to
14
8776
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 download the webpage and save in the GM862-GPS memory. I encounter a problem today which drive me crazy... I could open a connection and download the http page using AT command through hyperterminal (without python script). When i used python...
2
1230
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 include("http://www.website.com/webpage.php"); ?> This works fine except when i have a large list to add.(almost at the top of the page).then it seems to take forever for the page to open. I was wondering if there was a way to first load the page and...
5
1137
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 newbie. So, unable to take a call as to how-important or desirable the newer language features are -- so whether to write my app for v2.5 of Python, versus, as few others on this list have recommended, i.e. to stick to v2.3 ?? Are the preformance...
16
1872
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 in c++/java !? Tnx, Raxit
4
1576
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 page, contains a sequence of questions. Each question is a link to another page which details the answers of the specific question, any user can be able to enter his comments to the question. so the new comment should be queued to the existing...
1
1971
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 development in other versions of windows and Windows Vista.. What are the advantages of driver development in Windows Vista.. I have googled but came without any information.. Can anyone help me it is very urgent.. Also if possible please tell...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.