You are on page 1of 13

For Exam – Revision – Part III

Chapter 13 – Inheritance (16 Marks)

Question:
Write a Java Program to create a class known as BankAccount with methods called deposit() and
withdraw(). Create a subclass called SavingAccount that overrides the withdraw() method to prevent
withdrawals if the account balance falls below one hundred. Create a Bank Account class including
the following data member- accountName, accountNumber, balance and the following methods:
▪ double deposit (double amount) that will increase the balance with given amount and return
the new balance.
▪ double withdraw (double amount) that will check the balance and make the operation when
enough. If the operation is successful, the method will return updated amount.

Answer:

public class BankAccount {


String accountNumber;
String accountName;
double balance;

BankAccount(){

BankAccount(String accNumber, String accName, double balance){


accountNumber = accNumber;
accountName = accName;
this.balance = balance;
}

public double deposit(double amount) {


balance += amount;
return balance;
}

public double withdraw(double amount) {


if(balance > amount)
{
balance -= amount;
}
else
System.out.println("Withdraw amount is not enough!");
return balance;
}
}
public class SavingAccount extends BankAccount{

BankAccount b_account = new BankAccount();

SavingAccount(String accNumber, String accName, double bal)


{
super(accNumber, accName, bal);

public double withdraw(double amount) {

if(amount<100) {
System.out.println("Invalid Transaction!");

}
else if(balance < amount) {
System.out.println("Withdraw amount is not enough!");
}
else {
super.balance -= amount; // b_account.balance -= amount;

}
return super.balance;
}
}

public class BankAccountMain {

public static void main(String[] args) {

BankAccount account = new BankAccount("A001","Daw Aye Aye",100000);


System.out.println("Acc Number: " + account.accountNumber);
System.out.println("Acc Name: " + account.accountName);
System.out.println("Current Balance :" + account.deposit(50000));
System.out.println("Current Balance :" + account.withdraw(2000));
System.out.println("Current Balance :" + account.withdraw(50));
System.out.println();

account = new SavingAccount("A001", "Daw Aye Aye", 100000);


System.out.println("Acc Number: " + account.accountNumber);
System.out.println("Acc Name: " + account.accountName);
System.out.println("Current Balance :" + account.deposit(50000));
System.out.println("Current Balance :" + account.withdraw(2000));
System.out.println("Current Balance :" + account.withdraw(50));

}
}
***************************************************
Question:

Suppose we want to maintain a class roster for a class whose enrolled students include both
undergraduate and graduate students. For each student, we record her or his name, three test scores,
and the final course grade. The final course grade, either pass or fail, is determined by the following
formula in Table:
▪ The computeCourseGrade() method is an abstract method and need to provide the detail
specific implementation in each subclasses.
▪ Create an array to store the given three test scores and the setTestScore(int, int) method is used
to assign the score of each Test

public abstract class Students {


public static final int NUM_OF_TESTS = 3;
protected String name;
protected int[] test = new int[NUM_OF_TESTS];
protected String courseGrade;

public Students(){}
public Students(String n)
{
name = n;
}
public String getCourseGrade()
{
return courseGrade;
}
public String getName()
{
return name;
}
public int getTestScore(int i)
{
return test[i];
}
public void setName(String name)
{
this.name = name;
}
public void setTestScore(int i, int score)
{
test[i] = score;
}
public abstract String computeCourseGrade();
}

class UndergraduateStudent extends Students


{
private int total = 0;
public String computeCourseGrade()
{
for(int j=0; j< test.length; j++)
total += getTestScore(j);
if(total/3 >= 70)
courseGrade = "Pass";
else
courseGrade = "Not Pass";
return courseGrade;
}
}

class GraduateStudent extends Students


{
private int total = 0;
public String computeCourseGrade()
{
for(int j=0; j< test.length; j++)
total += getTestScore(j);
if(total/3 >= 80)
courseGrade = "Pass";
else
courseGrade = "Not Pass";

return courseGrade;
}
}

import java.util.*;

public class StudentAbstractMain {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
System.out.print("Type of Student: Undergraduate Student (U),
Graduate Student(G):");
char ch = scan.next().charAt(0);
System.out.print("Enter your name: ");
String name = scan.next();

if(ch == 'U')
{
UndergraduateStudent u_stud = new UndergraduateStudent();
u_stud.setName(name);
for(int i = 1; i <= u_stud.NUM_OF_TESTS; i++)
{
System.out.print("Enter Test Score " + i + " : ");
u_stud.setTestScore(i-1, scan.nextInt());
}
System.out.println(u_stud.getName());
System.out.println(u_stud.computeCourseGrade());
}
else if(ch == 'G')
{
GraduateStudent g_stud = new GraduateStudent();
g_stud.setName(name);
for(int i = 1; i <= g_stud.NUM_OF_TESTS; i++)
{
System.out.print("Enter Test Score " + i + ":");
g_stud.setTestScore(i-1, scan.nextInt());
}
System.out.println(g_stud.getName());
System.out.println(g_stud.computeCourseGrade());
}
}

*******************************************************
Review Exercises

Mona Lisa

Java
16
gram
Jog
Ping

-1
Error: Not zero

0
I'm happy with the input.

12xy
Invalid Entry
-1
Error: Not zero
Finally Clause Executed

0
I'm happy with the input.
Finally Clause Executed

12xy
Invalid Entry
Finally Clause Executed

v = (int)x;
y = j/i * x;
One
Two
ThreeFour

FiveSix

The output is 12
The output is 3
Output:
3
Output:
6

Explanation for Above Output Result:


(a)false
(b)false
(c)true
(d)true
(e)false
(f)true
(g)false
(h)true

(a) 605
(b) 60
(c) 55
(d) 165

**********************************************************
Old Question

2. What would be the output of the following code segment? (18 Marks)

(a) String str = "Computer University";


4
System.out.println(str.indexOf("u")); Uni
System.out.println(str.substring(9,12)); Computer Universities

System.out.println(str.substring(0, str.length()-1)+ "ies");

(b) int i=20, j=10, k=3;


3
System.out.println(j/i+k); 6
22
System.out.println(i% (j-k));

System.out.println(i-- + --k);

(c) int x=10, y=20, z=30;


(1) true
System.out.println("(1) "+((x<y+z) && (x+10<=20)));
(2) true
System.out.println("(2) "+(z-y==x && Math.abs(y-z)==x));
(3) false
System.out.println("(3) "+(!(x<y+z) || !(x+10<=20)));

(d) try{
int arr[]= {10,20,30};
System.out.println(arr[3]);
}
catch(ArithmeticException e1)
{
System.out.println("Arithmetic Exception"); ArrayIndexOutOfBounds Exception
}
catch(ArrayIndexOutOfBoundsException e2) This clause is executed
{
System.out.println("ArrayIndexOutOfBounds Exception");
}
finally {
System.out.println("This clause is executed");
}
(e) String original = "I love Java programming.";

String modified = original.replaceAll("[aeiou]","@");

System.out.println(modified); I l@v@ J@v@ pr@gr@mm@ng.


Java Program
StringBuilder name = new StringBuilder("Java"); margorP avaJ

name.append(" Program");

System.out.println(name);

System.out.println(name.reverse());

(f) double array[][]= {{1,2,3},{4,5,6},{7,8,9}};

double sum =0.0, itemcount =0.0;


Sum = 45.0
for (int i=0;i<array.length;i++) Average = 5.0
{

for (int j=0;j<array[i].length;j++)

sum += array[i][j];

itemcount++;

System.out.println("Sum = " + sum);

System.out.println("Average = " + sum/itemcount);

You might also like