Java

OOP GUIDE / WORK

 

PEN CLASS SOLUTIONS

 

 

TASK – PART 1 – SETUP

 

No solutions required.

 

 

TASK – PART 2 – PEN CLASS

 

Here is my solution (though many others are possible):

 

 

public class Pen

{

     private int inkLeft;

    

     public Pen(int ink)

     {

           inkLeft = ink;

     }

    

     public boolean isEmpty()

     {

           return inkLeft == 0;

     }

 

     public int getRemainingInk()

     {

           return inkLeft;

     }

    

     public void write(String s)

     {

           int i=0;

           while (i < s.length() && inkLeft > 0)

           {

                char c = s.charAt(i);

                System.out.print(c);

                if (c != ' ')

                {

                      inkLeft--;

                }

                i++;

           }

     }

}