CS 1073 Introductory Programming
for Scientific Applications
Classes in Java


The Rectangle class: Start with a Rectangle that represents a rectangle. Notice that there can be several instances of this class.

Version with one class, including main function Version with main function in separate class

// Rectangle.java: the rectangle class
public class Rectangle{

   // data fields: usually private
   private int length, width;

   // constructor, must be public, no parameters
   public Rectangle(){
      length = 0;
      width = 0;
   }

   // constructor, must be public, two parameters
   public Rectangle(int len, int wid){
      length = len;
      width = wid;
   }

   // accessors, usually public, return data members
   public int getLength(){ return length; }

   public int getWidth(){ return width; }

   // other functions, called methods
   public int getArea(){ return length * width; }

   public int getPerim(){ return 2*length + 2*width; }

   // special toString function
   public String toString(){
      return "Rectangle. width: " +  width +
             ", length: " + length +
             ", area: " + getArea() +
             ", Perim:  " + getPerim();
   }

   
   
   
   public static void main(String args[]) {
      int l, w;

      Rectangle r1 = new Rectangle();
      System.out.println(r1);  // calls toString of r1

      Rectangle r2 = new Rectangle(4,3);
      System.out.println(r2);  // calls toString of r2

      System.out.println("Test getLength, getWidth ...");
      l = r2.getLength();
      w = r2.getWidth();
      System.out.println("  r2 length: " + l);
      System.out.println("  r2 width: " + w);

      System.out.println("Test getArea, getPerim ...");
      System.out.println("  r1 area: " + r1.getArea());
      System.out.println("  r1 Perim: " + r1.getPerim());
      System.out.println("  r2 area: " + r2.getArea());
      System.out.println("  r2 Perim: " + r2.getPerim());
   }
}


// Rectangle.java: the rectangle class
public class Rectangle{

   // data fields: usually private
   private int length, width;

   // constructor, must be public, no parameters
   public Rectangle(){
      length = 0;
      width = 0;
   }

   // constructor, must be public, two parameters
   public Rectangle(int len, int wid){
      length = len;
      width = wid;
   }

   // accessors, usually public, return data members
   public int getLength(){ return length; }

   public int getWidth(){ return width; }

   // other functions, called methods
   public int getArea(){ return length * width; }

   public int getPerim(){ return 2*length + 2*width; }

   // special toString function
   public String toString(){
      return "Rectangle. width: " +  width +
             ", length: " + length +
             ", area: " + getArea() +
             ", Perim:  " + getPerim();
   }
}
======== separate file: RectangleMain.java =============
// RectangleMain: holds main for Rectangle class
public class RectangleMain {
   public static void main(String args[]) {
      int l, w;

      Rectangle r1 = new Rectangle();
      System.out.println(r1);  // calls toString of r1

      Rectangle r2 = new Rectangle(4,3);
      System.out.println(r2);  // calls toString of r2

      System.out.println("Test getLength, getWidth ...");
      l = r2.getLength();
      w = r2.getWidth();
      System.out.println("  r2 length: " + l);
      System.out.println("  r2 width: " + w);

      System.out.println("Test getArea, getPerim ...");
      System.out.println("  r1 area: " + r1.getArea());
      System.out.println("  r1 Perim: " + r1.getPerim());
      System.out.println("  r2 area: " + r2.getArea());
      System.out.println("  r2 Perim: " + r2.getPerim());
   }
}

Here are results of a run. Both versions produce the same output.


A second example: the Sheep class:

Sheep Class

   public class Sheep {
      private static int count = 0;
      private String name;

      public Sheep (String sName) {
          name = sName;
          count++;
      }

      public static int getCount( ) {
          return count;
      }

      public String getName( ) {
          return name;
      }

      public String sayBaa( ) {
          return "Baa " + name;
      }
   }

Exercise: Write code in a main function to create 3 Sheep and output the count. Compare the use of static versus non-static qualifiers. Answer: The static getCount can be referenced through the class name, Sheep. The non-static getName and sayBaa are referenced by object.

In the main function

   Sheep s1 = new Sheep("gracie");
   Sheep s2 = new Sheep("george");
   Sheep s3 = new Sheep("gerald");
   System.out.println("We have " + Sheep.getCount() + " sheep");
   System.out.println("Sheep " + s1.getName() + " says " + s1.sayBaa());


A third example: the Student class:


A fourth example: the Rational class: See Rational class. See also long Rational class