View Single Post
  #1 (permalink)  
Old 10-30-2007, 10:45 PM
sylo18 sylo18 is offline
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
Name Coming Up As "null"
hey all 1st post so sorry if i dont give all the right info etc.
Basically im doing a course at UNI (Digital Media) but first year all computing classes do the same basic stuff, incase you want to change course to something else in computing..

anyway, part of it is Java and im pretty average at it, but im stuck on something and thought you guys could help.

I started with this code:

Code:
/** * @author (Zaid Alkayat) * @version (1.0) */ public class MyPrinter { // Number of prints made private int copies; // Number of sheets of paper avaliable private int paper; // Name of the printer private String name; /** * Constructor for objects of class MyPrinter */ public MyPrinter() { copies = 0; paper = 500; name = "Epson"; } public int getCopies() { return copies; } public int getPaper() { return paper; } public String getName() { return name; } public void testMyPrinter() { System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); System.out.println("0123456789"); } }
and was told to do this to it:
Edit the MyPrinter class by
making a copy of the Constructor and editing it so that it has the signature
public MyPrinter(String name, int startPaper)
and initialises the values of the printername and the number of sheets of paper to the values of the parameters passed (the number of copies made obviously stays as 0 for a new printer.

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.

so i ended up with this code:

Code:
/** * MyPrinter. * * @author (Zaid Alkayat) * @version (1.0) */ public class MyPrinter { // Number of prints made private int copies; // Number of sheets of paper avaliable private int paper; // Name of the printer private String name; /** * Constructor for objects of class MyPrinter */ public MyPrinter() { copies = 0; paper = 500; name = "Epson"; } public MyPrinter(String name, int startPaper) { copies = 0; paper = 200; name = "HP"; } public int getCopies() { return copies; } public int getPaper() { return paper; } public String getName() { return name; } public void testMyPrinter() { System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); System.out.println("0123456789"); } public boolean printOne(String text) { boolean success = true; { System.out.println("Test printOne"); copies = copies + 1; paper = paper - 1; } return success; } public void print5(String text) { System.out.println("Test print5"); System.out.println("Test print5"); System.out.println("Test print5"); System.out.println("Test print5"); System.out.println("Test print5"); copies = copies + 5; paper = paper - 5; } }
However when i test it, my second Printer name comes up "Null"
its ment to display:
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

But like i said, instead of HP it comes up null.
any help would be awesome. THANKS!
Reply With Quote
Sponsored Links