Exercise - User definedClass-2

Task 1

// The Triangle class stores and manipulates data for a //triangle.
 
public class Triangle
{
     private double height;
     private double base;

     // The setHeight method accepts an argument which is   

     //stored in the height field.

     public void setHeight(double len)
     {
          height = len;
     }

     // The setBase method accepts an argument which is

     //stored in the base field.
   

     public void setBase(double b)
     {
          base = b;
     }

     //The set method accepts two arguments which are

     //stored in the height and base fields.
 

     public void set(double len, double b)
     {
          height = len;
          base = b;
     }

    // The getHeight method returns the value stored in the

    //  height field.

     public double getHeight()
     {
          return height;
     }

     // The getBase method returns the value stored in the  

     //base field

     public double getBase()
     {
          return base;
     }

     // The getArea method returns the value of area

    // with formula : 0.5 * height * base

     public double getArea()
     {
          return 0.5 * height * base;
     }

} 


Type the above definition of a Triangle class and save in a file.

1.     What is the name to be given to this file?

    Triangle.java

2.     Compile this file.  If you get any error, it means that you haven’t typed correctly the given codes.

    No error

3.     After a successful compilation, now try to run. Explain what happened and why.

    Unable to run due to no main method.


Task 2

import java.util.Scanner;


public class TriangleDemo

{

   public static void main(String[] args)

   {

      //create a Scanner object

      Scanner scan = new Scanner(System.in);


      // Prompt user to input value for height and base                 

      System.out.println("Enter height: ");

      double height = scan.nextDouble();

      System.out.println("Enter length: ");

      double base = scan.nextDouble();

      

      // Create a Triangle object. (default constructor) Create after user input if already constructed in Triangle class

      Triangle triangle = new Triangle(height,base);

      

      //Set the height and base (use mutator) if constructed in Triangle, just create object with the parameters

//      triangle.setHeight(height);

//      triangle.setBase(base);

      

      //triangle.set(height,base);

      

      // Display the height, base and area (use accessor)

      System.out.println("The pyramid's height is "

                         + triangle.getHeight());

      System.out.println("The pyramid's base is "

                         + triangle.getBase());

      

     System.out.println("The pyramid's area is "

                         + triangle.getArea());

      

   }

}



3.     List ALL the following items based on the Triangle class:

 

a) user-defined methods with return value: all the accessor methods (getHeight and etc.)

b) user-defined methods with passing-parameters : the mutator methods (setBase and etc.)

c) accessor methods: get

d) mutator methods: set


Task 3

Develop an Employee class which consists of employee ID number, gross pay, state tax and federal tax. You also must create input () method to prompt a user to insert employee ID number, gross pay, state tax and federal tax.

 

Next, develop a Payroll class that consists of two user-defined methods, namely calculateNetPay() and printOutput() methods.  The calculateNetPay() method is used to  calculate the employee’s net pay, as follows:

Net pay = gross pay – state tax - federal tax



In the same class, print the net pay value in the printOutput() method.

 

 

Then, develop a PayrollDemo class which consists of the main method. Inside the main method, create TWO (2) objects to invoke input(), calculateNetPay() and printOutput() methods. 

 

Your program should produce the following output:

                Enter your employee ID number: 2150
                Enter your Gross Pay         :RM 4000
                Enter your State Tax         :RM 300
                Enter your Federal Tax       :RM 500

                Net pay is : RM 3200.00

 

 

 

 

 

 

 

 

 


Employee Class



Payroll Class


PayrollDemo Class





Comments

Popular posts from this blog

Exercise 3 - PredefinedClass

Exercise - User definedClass-3