Wednesday, July 11, 2012

CSC435 - PAST EXAM QUESTION AND ANSWER


this is the past year question and the answer. have fun!




QUESTION 5  (APRIL 2008)

Given the following program segment:

public class Land
{    protected String owner;
     protected double area;

     abstract double calcTax();
     abstract void display();
     public Land( String own, double area)
           {
            //method definition
           }
}

public class Housing extends Land
{
     private char houseType;
     private double tax_rate;

     public Housing(String own,double area,char Hsetype)
     {
         //method definition
     }
     public double calcTax()
     {
        //method definition
     }
     public void display()
     {
        //method definition
     }
}

public class Agriculture extends Land
{
     private double fixed_tax_rate = 3.00;

     public Agriculture(String own,double area)
     {
        //method definition
     }
          public double calcTax()
     {
        //method definition
     }
     public void display()
     {
        //method definition
     }     }

a)         Define the method calcTax()of class Housing based on the following:

The tax for this type of land depends on its area and the type of house built on the land:

House Type
Description
Tax Rate (RM/m2)
T
Terrace
10
S
Semi-Detached
15
B
Bungalow
20
C
Condominium
5
                       
                                                                                                  (4 marks)
b)       Define method calcTax()of class Agriculture. Agricultural lands are all charged a fixed rate of RM 3 per meter square (m2).
                                            (2 marks)
                                   
b)            Write an application program that uses the concept of polymorphism to

i.          store data on various types of land. The number of data to be stored and information on each land are given by the user. The input process stops when the users key-in a sentinel value.

            ii.          Display the total Tax for all lands.
           
iii.         Display the Tax amount for Housing lands only.

iv.      Determine and display the highest tax for Agriculture land.
                                                                                                                        (12 marks)

APRIL 2008

a)    Answer:
       
        public double calcTax()
        {
       double tax=0.0;          ->0.5 mark
        if (houseType == 'T')
       { 
             tax_rate = 5;
             tax = area * tax_rate;
        }                        ->0.5 mark
       else if (houseType == 'S')
       {
            tax_rate = 10;
            tax = area * tax_rate;
        }                        -> 0.5 mark
        else if (houseType == 'B')
        {
             tax_rate = 15;
             tax = area * tax_rate;
        }                        ->0.5 mark
        else if (houseType == 'C')
        {
             tax_rate = 20;
             tax = area * tax_rate;
       }                          -> 0.5 mark
             return tax;          -> 0.5 mark
   }note : 1 mark if program correct

b)    Answer:
   private double fixed_tax_rate = 3.00; ->0.5 mark
   public double calcTax()
  {
       double tax = fixed_tax_rate * area;  -> 1 mark
       return tax;       -> 0.5 mark   }
                         
c)    Answer:
      public class TestLand
      {
        static Scanner console = new Scanner(System.in);
        public static void main(String[] args)
        {
           String owner,type;
           double area;
           char hsetype;
           double taxRate;
           char input = 'Y';
           int count =0;
       
           Land[] L = new Land[20];
           while(input != 'N')
           {
               System.out.print("Enter the land owner : ");
               owner = console.next();
               System.out.print("\nEnter the land area : ");
               area = console.nextDouble();
               System.out.print("Enter the land type: H-Housing,A- 
                                   Agriculture ");
               type = console.next();
           
               switch(type.charAt(0))
               {
                case 'H' :   System.out.println("Enter house type : ");
                             hsetype = console.next().charAt(0);
                             //taxRate = console.nextDouble();
                             L[count] = new Housing(owner,area,hsetype);
                             break;
                case 'A' :   L[count] = new Agriculture(owner,area);
                             break;
                default  : System.out.println( "Wrong land type");
            }
            count++;
            System.out.println("Do you want to continue ?(Y or N) : ");
            
            input = console.next().charAt(0);
        }
        System.out.println("length ="+L.length);
        for(int k=0; k<count;k++)
        {
            L[k].Display();
        }
        System.out.println("Count ="+count);
        double totalTax=0.0;
        for(int k=0; k<count;k++)
        {
           
            totalTax = totalTax + L[k].calcTax();
        }
        System.out.println("Total tax = "+totalTax);
        for (int k=0; k < count; k++)                   
         {   
           
            if (L[k] instanceof Housing) 
            {
               System.out.print("Tax amount for Housing land = ");
                 System.out.println(L[k].calcTax());    
              }
          }
        
double highest = 0.0;
         for (int k=0; k < count; k++)                  
         {   
            if (L[k] instanceof Agriculture)
                 if (L[k].calcTax() > highest)
                        highest = L[k].calcTax();
         }
         System.out.println("Highest tax for agriculture land = "+highest);

    }








QUESTION 3 (APRIL 2009)

Given the following inheritance hierarchy
 






And the definition for the BankAccount class:

abstract class BankAccount
{
     private String name;
     private long accNum;
     private double balance;   

   public BankAccount() {}

   public BankAccount(String name, long accNum, double balance)
   { //method definition}

     public String getName()
{  //method definition  }

public long getAcc()
{  //method definition  }

public double getBalance()
{  //method definition  }

public String toString()
{  //method definition  }

//method to update the balance for the account
public abstract double updateBalance();
}

a)    Define the Saving class that inherits from the BankAccount class. The attributes and methods are as follows:

Attributes: interest rate

Methods:
·         Constructor with arguments
·         toString(), to return the string representation of the object
·         updateBalance(), given the formulae:
update = (interest rate X balance) + balance;
 (10 marks)

b)    Define the Current class that inherits from the BankAccount class. The attributes and methods are as follows:

Attributes: interest rate and transaction fee
Methods:
·         Constructor with arguments
·         toString(), to return the string representation of the object
·         updateBalance(), given the formulae:
update = (interest rate X balance) + balance – transaction fee;
 (10 marks)

c)    Write a Java application class called BankApp that uses the concept of polymorphism to perform the following tasks:
·         Declare an array of objects to store data on various types of BankAccount.
·         Display the details for all bank customers.
·         Display the details of customer whose balance exceeding RM 100,000
·         Calculate and display the total balance of all Saving accounts and total balance of all Current accounts.
 .(15 marks)

APRIL 2009               
a)     
public class Saving extends BankAccount
{
    private double rate;

    public Saving(String name, long accNum, double balance,
double rate)
    {
        super(name, accNum, balance);
        this.rate = rate;
    }

   public String toString()
   {   
return super.toString() + " Interest rate: " + rate;
    }

    public double updateBalance()
    {
        double update = (rate * super.getBalance()) +
                             super.getBalance();
        return update;
    }
}



b)  public class Current extends BankAccount
{
   
    private double rate;
    private double fee;
   
    public Current(String name, long accNum, double balance,
double rate, double fee)
    {
        super(name, accNum, balance);
        this.rate = rate;
        this.fee = fee;
    }

    public String toString()
    {    return super.toString() + " Interest rate: " + rate + " Fee:"
 + fee;   
     }

    public double updateBalance()
    {
        double update = (rate * super.getBalance()) +
 super.getBalance() - fee;
        return update;
    }
}

c)  import javax.swing.*;

public class BankApp
{
    public static void main(String args[])
    {
      int n = Integer.parseInt(JOptionPane.showInputDialog(" Number
                of customers: "));
       
       BankAccount[] acct = new BankAccount[n];
       
      for(int i = 0; i < acct.length; i++)
      {
         String nm = JOptionPane.showInputDialog("Customer's Name: ");
         long accNo = Long.parseLong(JOptionPane.showInputDialog
(" Account #: "));
         double bal = Double.parseDouble(JOptionPane.showInputDialog
(" Balance: "));
           
       String type = JOptionPane.showInputDialog("Type - (S)aving/"
 + "(C)hecking ");
        char accType = type.toUpperCase().charAt(0);
            
     if (accType == 'S')
    {
     double rt = Double.parseDouble(JOptionPane.showInputDialog
(" Interest rate: "));
       acct[i] = new Saving(nm, accNo, bal, rt);
     }
     if (accType == 'C')
     {
         double irate =  Double.parseDouble
  (JOptionPane.showInputDialog (" Interest rate: "));
         double tfee = Double.parseDouble(JOptionPane.showInputDialog                  
                        (" Transaction fee: "));
         acct[i] = new Current(nm, accNo, bal, irate, tfee);
     }
   }
   for (int i = 0; i < acct.length; i++)
   {
        System.out.println(" The details of customers : " +
                     acct[i].toString());
           
        if (acct[i].updateBalance() > 100000)
            System.out.println(" The customers with balance > 100000:"
                            + acct[i].toString());
    }
     double saveBalance = 0, currBalance = 0;
     for(int i = 0; i < acct.length; i++)
     {
            if(acct[i] instanceof Saving)
                saveBalance += acct[i].updateBalance();
            if(acct[i] instanceof Current)
                currBalance += acct[i].updateBalance();
     }
     System.out.println(" Total Balance for Saving account: " +
                     saveBalance);
     System.out.println(" Total Balance for Current account: " +
                     currBalance);
    }
}










QUESTION 3 (NOV 2009)

Given the following definition

public abstract class Student
{
     private String name;
     private double test1;
     private double test2;
     private double test3;
     protected char grade;

      public Student(String n, double tl, double t2, double t3)
     { . . . }
     public abstract char computeGrade();

// Accessors
     public String getName() {...}
     public double getTest1() {...}
     public double getTest2() {...}
     public double getTest3() {...}
     public char getGrade (){...}
     public String toString (){...}
}

Suppose Undergraduate and Graduate extends Students, with no additional attributes. Using polymorphism, write a Java application to

a.    get the number of students and necessary data from the user. Store these data into an array of students
(10 mark)


b.    calculate and display the total number of students who get grade A and count how many of these students from each subclass, Undergraduate and Graduate.
(6 mark)


c.    Find and display the information of student whose name is given by the user
(4 mark)

QUESTION 3 (Nov 2009)

import javax.swing.*;
public class StudentApp
{
   public static void main(String args[])
  {
      int n = Integer.parseInt(JOptionPane.showInputDialog
(" Number of Students: "));
      Student[] stu = new Student[n];
      int under = grad = 0; 
      for(int i = 0; i < acct.length; i++)
      {
       String nm = JOptionPane.showInputDialog("Student's Name: ");
       double t1 = Double.parseDouble(JOptionPane.showInputDialog
(" Test 1: "));
       double t2 = Double.parseDouble(JOptionPane.showInputDialog
(" Test 2: "));
       double t3 = Double.parseDouble(JOptionPane.showInputDialog
(" Test 3: "));
          
      String type = JOptionPane.showInputDialog
("Type - (U)ndergraduate/" + "(G)raduate ");
       char stuType = type.toUpperCase().charAt(0);
       

if (stuType == 'U')
       {
          stu[i] = new Undergraduate(nm, t1, t2, t3);
       }
       if (stuType == 'G')
       {
          stu[i] = new Undergraduate(nm, t1, t2, t3);
       }  }
     for (int i = 0; i < stu.length; i++)
     {
         if( stu[i].getGrade() == ‘A’)
          {   
                if(stu[i] instanceof Undergraduate)
                     under++;
                if(stu[i] instanceof Graduate)
                           grad++;
      }    }
    System.out.println(" Total A student from Undergraduate: " +
 under);
    System.out.println(" Total A student from Graduate: " + grad);
    String input = JOptionPane.showInputDialog
("Search Student's Name: ");
    for (int i = 0; i < stu.length; i++)
    {
     if(input.equalsIgnoreCase(stu[i].getName())
           System.out.println(“Found “ + stu[i].toString());
    } } }


QUESTION 4 (NOV 2009)

Given the following inheritance hierarchy
 






and the list of data members and methods for ThemePark, WaterPark, and WildlifePark classes:

Superclass : abstract class ThemePark

Data member(s):
      String name;    // customer's name
     String icNo;    // customer's identification card number
     boolean member; // either member or not member of the park

Methods:
      ThemePark();
ThemePark(String name, String icNo, boolean member);
String getName();    // return the customer's name
String getlc();      // return the ic number
boolean getMember(); // return the membership
String toString();   // return the details of objects
abstract double calCharges(); // calculate the charges

Subclass : class WaterPark

Data member(s):
     boolean surfBeach; // either true or false to surf
     boolean waterRides; // either true or false to ride

Methods:
boolean getSurf();// return the surfing status
boolean getRide();// return the riding status
double calCharges();// calculate the charges
String toString();// return the details of objects

Subclass : class WildlifePark

Data member(s):
String category; // category of the customers

Methods:
String getCategory();// return the category
double calCharges();// calculate the charges



a)      Write the normal constructors for superclass and both subclasses.
 (6 marks)

b)      Given the details of ticket charges in table 5.1 and table 5.2 for both Water and Wildlife Parks below:

Activity
Cost(RM)
Surf Beach
25.00
Water Rides
20.00
Table 5.1 Details of ticket charges for Water Park

Category
Cost(RM)
Adult
35.00
Child
20.00
Table 5.2 Details of ticket charges for Wildlife Park


Write abstract methods named calcharges() to calculate the charges of the activities for both WaterPark and WildlifePark classes. The customers who are the members of the theme park will be given a special discount of 25% for every total charges.

(9 marks)

QUESTION 4 (Nov 2009)

a)         public ThemePark(String name, String ic, boolean member)
{
           this.name = name;
           this.icNo = ic;
           this.member = member;
}
public WaterPark(String name, String ic, boolean member, boolean surf,
 boolean ride)
{
     super(name, ic, member);
     this.surfBeach = surf;
     this.waterRides = ride;
}
public WildlifePark(String name, String ic, boolean member,
 String category)
{
super(name, ic, member);
     this.category = category;
}


b)         public double calCharges() // waterpark
{
          double total = 0;
          if( getSurf() == true)
                total = total + 25;
          if(getRide == true)
                total = total + 20;
    
          if(getMember() == true)
                total = total – (total * 0.25);

          return total;
}

public double calCharges() // wildlifepark
{
          double total = 0;
          if( getCategory().equalsIgnoreCase(“Adult”))
                total = total + 35;
          else if(getCategory().equalsIgnoreCase(“Child”))
                total = total + 20.00;
    
          if(getMember() == true)
                total = total – (total * 0.25);

          return total;
}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Pleaseeeeee.. please upload more the answers scheme, the latest one.. so useful for our reference.. thanks

    ReplyDelete
    Replies
    1. sorry guys...

      i'm just done with this oop...

      Delete

Say whut you want, but be responsible on whut you said...