Reputation: 110
I'm new to programming so I was going through an online lesson (Java Fundamentals part 1, Basic Composition by John Sonmez on Pluralsight. I'm pretty sure you can start a trial because I can't link to the video directly) when I hit a snag. When calling the print(1) method on the myPrinter object here, the instructor got the output "Load more paper!" while I continue to get the output "MY PRINTER is On!". I believed I had written the code down exactly like his but obviously I'm missing something. From the logic I've deduced of the code(which is clearly wrong somehow), it is supposed to print "MY PRINTER is On!" since the pages variable in the PaperTray class is 0, which makes the isEmpty method in that class return false (because 0>0 is false), a condition that alongside the "copies > 0" condition only allows for the while loop in the print method to execute. On the other hand that would go against the logic of the exercise, which was to show that I would be told to load more paper until I ran the addPaper method of the PaperTray class, which would make pages = 1, meaning the isEmpty method would then resolve to true and myPrinter.print(1) would appropriately output "Load more paper!" (which it does if i change pages to 1 manually!). I went back into intelliJ and coded it exactly as I saw it, down to the coding style but now I've goofed in formatting it here. Am I overlooking something tiny?
public class Main {
public static void main(String[] args) {
Printer myPrinter = new Printer(true, "MY PRINTER");
myPrinter.print(1); //For the instructor this outputs "Load more paper!" while my output is "MY PRINTER is On!"
}
}
Printer class
public class Printer extends Machine{
private String modelNumber;
private PaperTray paperTray = new PaperTray();
public Printer(boolean isOn, String modelNumber)
{
super(isOn);
this.modelNumber = modelNumber;
}
public void print(int copies)
{
String onStatus = "";
if(isOn)
onStatus = " is On!";
else
onStatus=" is Off!";
//Uses Machine to determine onStatus
String textToPrint = modelNumber + onStatus;
while( copies > 0 && !paperTray.isEmpty() ) //while copies > 0 and paperTray.isEmpty() is false
{
System.out.println(textToPrint);
copies--; true
}
if( paperTray.isEmpty() ) // if paperTray.isEmpty is true
{
System.out.println("Load more paper!");
}
}
PaperTray class to simulate paper tray of a printer
class PaperTray{
int pages = 0;
public void addPaper(int count){
pages += count;
}
public void usePage(){
pages--;
}
public boolean isEmpty(){
return pages > 0;
}//Since 0 > 0 is false, only the while loop of the Printer's print method executes!
}
Machine class to classify the printer
class Machine
{
protected boolean isOn;
public Machine(boolean isOn){
this.isOn=isOn;
}
public void TurnOn(){
isOn=true;
}
public void TurnOff(){
isOn=false;
}
}
}
Upvotes: 0
Views: 35
Reputation: 1333
The error is in the isEmpty
of PaperTray
-- you're testing if it's empty by testing if the number of pages is greater than zero, which is the opposition condition of being empty. The method body should be:
return pages == 0;
Upvotes: 0