ASSIGNMENT      

 

DOCUMENTATION:

          Include as a comment at the beginning of your source code:

                   Name

Assignment  number

 

          Include any comments within your source code to clarify your logic in the  cases where this is necessary.

 

SUBMIT:   

1.     Include.JAVA and  .CLASS files.

2.     sample output for each activity (screen capture)

 

DUE DATE: next class

 

Ex 1.

Modify the Rectangle class from the last Assignment  to include methods that calculate the perimeter ( returning 2 times length  plus 2 times width) and the area (returning length times width) of the rectangle. Modify the RectangleDemo class to print the area and perimeter of the r1 and r2 Rectangle objects.

 

Ex 2.

Create a class called Complex for performing arithmetic with complex numbers. Write a demo program to test your class. Use variables of type double to represent the private data of the class. Provide a constructor method that enables an object of this class to be initialized when it is declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods for each of the following:

a)      Addition of two Complex numbers: The real parts are added together and the imaginary parts are added together.

b)      Subtraction of two Complex numbers: The real part is subtracted from the real part of the other and the imaginary part is subtracted from the imaginary part of the other.

c)      Display real part and the imaginary part of the object

 

// Complex.java

// Definition of class Complex

 

public class Complex {

  //instance variables

 

   // constructor - Initialize both parts to 0

 

 

   // constructor- Initialize real part to supplied value and imaginary part to 0

 

 

   // constructor - Initialize real  and imaginary parts to supplied values

 

 

   // Add two Complex numbers and return the results as a new complex object

 

 

   // Subtract two Complex numbers and return the results as a new complex object

 

 

   // display  object

 

}//end of  Complex class

 

// ComplexTest.java

// Test the Complex number class

 

public class ComplexDemo{

   public static void main( String args[] )

   {  //Create two Complex objects a and b

      // with values 9.9 and 7.7 for object a

      // and values 1.2 and 3.1 for object b

 

      //display object a

      //display object b

 

     // display the values after add

     //display the values after subtract

 

   }

}

 

Sample output :

 

a = (9.9 , 7.7)

b = (1.2, 3.1)

a + b = ( 11.1, 10.8 )

a – b = ( 8.7, 4.6 )