New Java programmer, basic java composition

I am a new student in computer programming. I watched a video about Java, the main composition, and the guy in the video made an example on this subject as follows:

public class PaperTray
{
  int pages = 0;
  ....
  public boolean isEmpty()
  {
    return pages > 0;
  }
}

public class Printer extends Machine
{
  private PaperTray paperTray = new PaperTray();
  ....
  public void print(int copies)
  {
  ....
  while(copies > 0 && !paperTray.isEmpty() )
  {
    System.out.println("some text to print");
    copies--;
  }
  if(paperTray.isEmpty())
  {
    System.out.println("load paper");
  }
}

My question is that the paper tray is empty and then in the PaperTray class the isEmpty () method will return false. Therefore, the if statement in the Printer class will not be executed. And if the paper tray is not empty, the isEmpty () method in the PaperTray class will return true, so the while statement in the Printer class will not be executed. Am I mistaken, or did the instructor in the video make some mistakes?

thank

+5
source share
6 answers

Logic isEmptymakes no sense: I expect either

public boolean isEmpty() {
    return pages == 0;
}

or

public boolean isNotEmpty() {
    return pages > 0;
}
+5
source

, PaperTray isEmpty() false

true ( , :-). , isEmpty(), , true, / , false, .

, , , .

+3

. , . . - > 0 <= 0.

.

public class PaperTray
{
  int pages = 0;
  ....
  public boolean isEmpty()
  {
    return pages <= 0;
  }
}

, , , , .

, , . , .

+2

, , . isEmpty() true, . , .

0

- : , copies. pages PaperTray, .

, :

while(copies > 0 && !paperTray.isEmpty() )
{
System.out.println("some text to print");
copies--;
}
if(paperTray.isEmpty())
{
System.out.println("load paper");
}

, . , while . , , , .

0

, :

public boolean isEmpty(){
    //return pages > 0; this doesnt make sense
    return pages==0; 
}

public void print(int copies){
    while(copies > 0 && !paperTray.isEmpty()){
        System.out.println("some text to print");
        pages--;//this is not enough. You need to decrement copies as well
    }

    if(paperTray.isEmpty())
        System.out.println("load paper");
}
0

All Articles