CS335
Fall, 2008
Homework 3 (20 points)
Due: September 23, 2008 (Tuesday)

************************************************************************

1.  (4 pts)
For each of the items in the following list, tell how to properly
access the requested information:

(1) length of a String object
(2) number of columns in row i of a 2-D array
(3) number of rows of a 2-D array
(4) number of elements in a 1-D array


2.  (4 pts)
Tell if the following statements are true. If not true, tell why.

(1) Any "static" class variables and "static" class methods exist
and can be used even if no objects of that class have been
instantiated.
(2) An array declaration reserves space for the array.
(3) A String can be modified after it is created.
(4) A method declared "static" can access nonstatic class members.


3.  (2 points)
What happens when a return type, even "void", is specified for
a constructor?


4.  (4 pts)
When you execute the following Java program "Test.java",
you get the following output:

////////////////////////////////////////////////////////
Point constructor: Center = [72, 29]; Radius = 0.0
Circle constructor: Center = [72, 29]; Radius = 4.5
point object: Center = [72, 29]; Radius = 4.5
circle object: Center = [72, 29]; Radius = 4.5
////////////////////////////////////////////////////////

You would get the following two lines only if "circle2"
and "pointref" are set to "null" too. Why?

////////////////////////////////////////////////////////
Circle finalizer: Center = [72, 29]; Radius = 4.5
Point finalizer: Center = [72, 29]; Radius = 4.5
////////////////////////////////////////////////////////




///////////////////////////////////////////////////////////
// Definition of class Point
///////////////////////////////////////////////////////////
public class Point {
protected int x, y; // coordinates of the Point

public Point() { 
setPoint( 0, 0 ); 
System.out.println( "Point constructor: " + toString() );
}

public Point( int a, int b ) { 
setPoint( a, b ); 
System.out.println( "Point constructor: " + toString() );
}

// finalizer
protected void finalize() throws Throwable
{
System.out.println( "Point finalizer: " + toString() );
super.finalize();  // call superclass finalize method
}

public void setPoint( int a, int b ) {
x = a; y = b;
}

// get x coordinate
public int getX() { return x; }  

// get y coordinate
public int getY() { return y; }

// convert the point into a String representation
public String toString() 
{ return "[" + x + ", " + y + "]"; }
}


///////////////////////////////////////////////////////////
// Definition of class Circle
///////////////////////////////////////////////////////////
public class Circle extends Point {  // inherits from Point
protected double radius;

public Circle() {
setRadius( 0 ); 
System.out.println( "Circle constructor: " + toString() );
}

public Circle( double r, int a, int b ) {
super( a, b );
setRadius( r );  
System.out.println( "Circle constructor: " + toString() );
}

// finalizer
protected void finalize() throws Throwable {
System.out.println( "Circle finalizer: " + toString() );
super.finalize();
}

public void setRadius( double r ) 
{ radius = ( r >= 0 ? r : 0 ); }

public double getRadius() { return radius; }

public double area()
{ return Math.PI * radius * radius; }

public String toString() {
return "Center = " + super.toString() +
     "; Radius = " + radius;
}
}


///////////////////////////////////////////////////////////
// Driver program Test.java
///////////////////////////////////////////////////////////
public class Test {
public static void main( String args[ ] ) {

private Circle circle1, circle2;
private Point pointref;

circle1 = new Circle( 4.5, 72, 29 );
pointref =  circle1;
System.out.println( "point object: " +
		pointref.toString( ) );
circle2 = (Circle) pointref;
System.out.println( "circle object: " +
		circle2.toString( ) );

circle1 = null;
System.gc();  
}
}
///////////////////////////////////////////////////////////


5.  (6 points)
Many programs written with inheritance could be written
with composition instead, and vice versa. Rewrite class
"BasePlusCommissionEmployee4" of the
"CommissionEmployee3 - BasePlusCommissionEmployee4"
hierarchy to use composition rather than inheritance.
In this case, which approach is more natural? Why?



////////////////////////////////////////////////////////////////////
// CommissionEmployee3.java
////////////////////////////////////////////////////////////////////
public class CommissionEmployee3
{
    private String firstName;
    private String lastName;
    private String socialSecurityNumber;
    private double grossSales;
    private double commissionRate;

    public CommissionEmployee3( String first, String last,
        String ssn, double sales, double sate )
    {
        firstName = first;        
        lastName = last;        
        socialSecurityNumber = ssn;
        setGrossSales( sales );
        setCommissionRate( rate );
    }

    public void setFirstName( String first )
    {
        firstName = first;
    }

    public String getFirstName( )
    {
        return firstName;
    }

    public void setLastName( String last )
    {
        lastName = last;
    }

    public String getLastName( )
    {
        return lastName;
    }

    public void setSocialSecurityNumber( )
    {
        socialSecurityNumber = ssn;
    }

    public String getSocialSecurityNumber( )
    {
        return socialSecurityNumber;
    }

    public void setGrossSales( double sales )
    {
        grossSales = ( sales < 0.0 ) ? 0.0 : sales ;
    }

    public double getGrossSales( )
    {
        return grossSales;
    }

    public void setCommissionRate( double rate )
    {
        commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;
    }

    public void getCommissionRate( )
    {
        return commissionRate;
    }

    public double earnings()
    {
        return getCommissionRate( ) * getGrossSales( );
    }

    public String toString( )
    {
        return String.format( "%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",
           "commission employee", getFirstName( ), getLastName( ),
           "social security number", getSocialSecurityNumber( ),
           "gross sales", getGrossSales( ),
           "commission rate", getCommissionRate( ) );
    }
}
////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////
// BasePlusCommissionEmployee4.java
////////////////////////////////////////////////////////////////////
public class BasePlusCommissionEmployee4 extends CommissionEmployee3
{
    private double baseSalary;

    public BasePlusCommissionEmployee4( String first, String last,
        String ssn, double sales, double sate, double salary )
    {
        super( first, last, ssn, sales, rate );
        setBaseSalary( salary );
    }

    public void setBaseSalary( double salary )
    {
        baseSalary = ( salary < 0.0 ) ? 0.0 : salary ;
    }

    public double getBaseSalary( )
    {
        return baseSalary;
    }

    public double earnings( )
    {
        return getBaseSalary + super.earnings( );
    }

    public String toString( )
    {
        return String.format( "%s %s\n%s: %.2f", "base-salaried",
                   super.toString( ), "base salary", getBaseSalary( ) );
    }
}
////////////////////////////////////////////////////////////////////

************************************************************************


Put you solution set in a TEXT file with the following header and send it
to the grader (xuwei.liang@uky.edu) before midnight of the due day.

   CS335 (Fall 2008)
   Solution of Homework 3
   Due: 9/23/08
   Name: xxxxxx xxxxxx