Objects and Classes

Class Definition How is a class defined? You must answer the following 5 questions:
  1. What tasks will the object perform?
  2. What information will it need to perform its tasks?
  3. What methods will it use to process its information?
  4. What information will it make public for other objects?
  5. What information will it hide from other objects?
Example Rectangle:
/*
* File: Rectangle.java
*
* Description: This program represents a geometric rectangle.
* This class is not runnable, because it does not have a main() method.
*/

public class Rectangle
{
private double length; // Instance variables, this one is private
double width; // this one is package private

public Rectangle() // Default constructor, one that takes no parameters
{
length = 0;
width = 0;
}
public Rectangle(double l, double w) // Constructor method
{
length = l;
width = w;
} // Rectangle() constructor


public double calculateArea() // Access method
{
return length * width;
} // calculateArea()

public double calculateCircumference()
{
return length*2 + width*2;
}

// accessor method for length
public double getLength()
{
return length;
}

// mutator method for length

public void setLength(double l)
{
length = l;
}

} // Rectangle

Class definitions, some tips



Defining Classes

 [modifer] class ClassName [extends SuperClassName] [implements ImplementedClassName] {
                .... define attributes and methods }
 

example:
public class Pet {
   String breed;
  double weight;
  String name;
   String ownersName;

// constructor method

public Pet()
{
   breed = "unknown";
   weight = 0;
   name = "unknown";
   ownersName = "unknown";
}

 public  Pet(String b, String n)
  {
    breed = b;
    name = n;
  }

  public void  setWeight(double w)
 {
   weight = w;
 }

public double getWeight()
{
   return weight;
}

public String toString()
{
   return name +" : " + breed;
}

} // end of the class
 

Attributes / Variables:


 [access specifier] [storage specifier(s)]  TYPE attributeName [ = initial value] ;
 



Defining Methods

 [access specifier] [storage specifier(s)]  TYPE methodName(arguments) { local variables and statements }
 


Constructors


Naming conventions:


Creating and using Classes and Objects

Rectangle r1 = new Rectangle(10,20);  // create a rectangle with a length of 10 and a width of 20
Rectangle r2 = new Rectangle(); // create a with a length and width of 0
System.out.println("The area of r1 is "+r1.calculateArea());
System.out.println("The circumference of r1 is "+r1.calculateCircumference());


Object Class

class Employee {
     String name;            // instance variable, every employee will have their own name
     double payRate;      // instance variable

...

  public void raisePay() {
       double percent = .03;   // define a local variable holding the percent raise
       payRate = payRate * (1+percent);   // increase pay
      }

    public String toString()
   {
      return name;  // return the employee's name for possible display
   }

  public boolean equals(Object other)
   {
     String text = toString();
     return text.equals(other.toString());
    }

   }