Java

OOP GUIDE / WORK

 

SINGLETABLE CLASS

 

Topics

  • Inferring instance variables.
  • Implementing a constructor.
  • Implementing instance variables.

 

 

SOLUTIONS

 

 

  1. The instance variables would be:

 

                    int numSeats;

                    double viewQuality;

                    int height;

 

  1. Code:

 

 

 

public class SingleTable

{

     private int seats;

     private int height;

     private double viewQuality;

    

     public SingleTable(int s, int h, double vq)

     {

         seats = s;

         height = h;

         viewQuality = vq;

     }

    

     public int getNumSeats()

     {

         return seats;

     }

    

     public int getHeight()

     {

         return height;

     }

    

     public double getViewQuality()

     {

         return viewQuality;

     }

    

     public void setViewQuality(int qual)

     {

         viewQuality = qual;    //might want to have constraints here

     }

}

 

 

 

  1. Code:

 

 

public class TableTester

{

    public static void main(String[] args)

    {

        SingleTable t1 = new SingleTable(4, 74, 60.0);

        SingleTable t2 = new SingleTable(8, 74, 70.0);

        SingleTable t3 = new SingleTable(12, 76, 75.0);

    }

}