ngunha02
ngunha02

Reputation: 1729

New Java programmer, basic java composition

I am a new computer programming student. I watched a video about Java, basic composition, and the guy in the video made an example about this topic like this:

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 if the paper tray is empty, then in class PaperTray the method isEmpty() will return false. Therefore, the if statement in the class Printer will not be executed. And if the paper tray is not empty, the method isEmpty() in class PaperTray will return true, so the while statement in the class Printer will not be executed. Am I wrong, or the instructor in the video clip made some mistakes?

Thank you

Upvotes: 5

Views: 340

Answers (6)

John Sonmez
John Sonmez

Reputation: 7206

I am the instructor from the video. Thanks for catching this mistake. You are correct. At some point in recording the video I must have had the > 0 in there instead of <= 0.

Good catch. It should be

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

I checked the sample code for the course that is downloadable and it is correct there and everywhere else that class appears in the course, so it must have been just that one spot.

Thanks again for catching that error and my apologies for the mistake and the confusion it cost you. Hopefully you were still able to benefit from the course.

Upvotes: 2

John Kane
John Kane

Reputation: 4443

It seems like there are a couple of issues with this code:

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");
}

Upvotes: 0

Simeon Visser
Simeon Visser

Reputation: 122376

There is something missing in the code: when copies are being made then only copies is decremented. But the code should also decrement the value of pages in PaperTray, otherwise there is no paper being used to make the copies.

When that happens, the code is fine:

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

It will keep making the desired number of copies as long as there is paper in the tray. When there is no paper anymore, the while loop will exit. Either way, a check is performed at the end to tell the user the refill the paper tray when it is empty.

Upvotes: 0

Anurag Ramdasan
Anurag Ramdasan

Reputation: 4340

Yes what you say is correct. isEmpty() should return true when it is empty. Probably an error on the instructors part.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

The logic of the isEmpty does not make sense: I wold expect either

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

or

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

Upvotes: 5

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116266

if the paper tray is empty, then in class PaperTray the method isEmpty() will return false

It should return true (for any sensible implementation, that is :-). For a method called isEmpty(), common sense dictates that it returns true when the enclosing object / collection is empty, and false when it is not empty.

In other words, the implementation you show above has a bug.

Upvotes: 3

Related Questions