Slip24
Q1) Create an abstract class 'Bank' with an abstract method 'getBalance'. Rs.100, Rs.150 and Rs.200 are deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are subclasses of class 'Bank', each having a method named 'getBalance'. Call this method by creating an object of each of the three classes. [10 marks]
abstract class Bank
{
abstract int getBalance();
}
class BankA extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
int getBalance()
{
return balance;
}
}
class BankB extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
int getBalance()
{
return balance;
}
}
class BankC extends Bank
{
private int balance;
void deposit(int money)
{
balance += money;
}
int getBalance()
{
return balance;
}
}
public class Slip24_1
{
public static void main(String[] args)
{
BankA bankA = new BankA();
BankB bankB = new BankB();
BankC bankC = new BankC();
bankA.deposit(100);
bankB.deposit(150);
bankC.deposit(200);
System.out.println("Balance of Bank A = "+bankA.getBalance());
System.out.println("Balance of Bank B = "+bankB.getBalance());
System.out.println("Balance of Bank C = "+bankC.getBalance());
}
}
Q2) Program that displays three concentric circles where ever the user clicks the mouse on a frame. The program must exit when user clicks ‘X’ on the frame. [20 marks]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Slip24_2 extends JFrame
{
public Slip24_2()
{
setTitle("Concentric Circles");
setSize(400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
// Draw circles based on mouse click position
getGraphics().clearRect(0, 0, getWidth(), getHeight()); // Clear the previous circles
drawConcentricCircles(x, y);
}
});
setVisible(true);
}
public void drawConcentricCircles(int x, int y) {
Graphics g = getGraphics();
g.setColor(Color.LIGHT_GRAY);
g.fillOval(x - 50, y - 50, 100, 100);
g.setColor(Color.GRAY);
g.fillOval(x - 35, y - 35, 70, 70);
g.setColor(Color.DARK_GRAY);
g.fillOval(x - 20, y - 20, 40, 40);
}
public static void main(String[] args)
{
new Slip24_2();
}
}
Comments
Post a Comment