In: Computer Science
Describe how you would develop object-oriented features of Java for the Quiz program developed in the Programming Assignments. In particular, describe how the program could use each of the following: class variables, instance variables, inheritance, polymorphism, abstract classes, "this", "super", interfaces, and event listeners.
Answer : Given data
* I have used following for the solution:
Explanation:
CALCULATOR CLASS CODE:
package demo1;
abstract class Calculator {
//Class variable
static String batch = "CE";
//Instance variable
int z;
//Abstract method definition
//Abstract method has no body
abstract void abc();
//Defining a method in abstract classes
void xyz()
{
System.out.println("\nI am demonstrating ABSTRACT class");
}
public Calculator() {
System.out.println("I am demonstrating SUPER call from Calculator class");
}
public void addition(int x, int y) {
System.out.println("\nHere z is demonstarting THIS");
this.z = x + y;
System.out.println("\nSum is: " +z);
}
public void Subtraction(int x, int y) {
this.z = x - y;
System.out.println("\nDifference is: "+z);
}
//The Product will demonstrate the POLYMORPHISM
// Method with 2 int parameter
static int Product(int a, int b)
{
return a * b;
}
// Method with the same name but 2 double parameter
static double Product(double a, double b)
{
return a * b;
}
}
MAINCALL CLASS CODE:
package demo1;
//Here MainCall is inheriting Calculator and implementing InterfaceForDemo
public class MainCall extends Calculator implements InterfaceForDemo {
//Abstract Class
void abc() {
System.out.println("\nI am implementing from ABSTRACT class");
}
//Super constructor call
public MainCall() {
super();
System.out.println("\nI am inside maincall class \n\nCalling SUPER constructor from Calculator class");
}
public void multiplication(int x, int y) {
this.z = x * y;
System.out.println("\nProduct is: "+z);
}
//Method from interface
public void display() {
System.out.println("\nI am a method from INTERFACE\n");
}
//Main method
public static void main(String args[]) {
int a = 20, b = 10;
//Creating an object for the class
MainCall demo = new MainCall();
//Printing class variable
System.out.println("\nI am demonstrating CLASS VARIABLE: " + batch);
//Super is executing these function call from Calculator class
demo.addition(a, b);
demo.Subtraction(a, b);
demo.multiplication(a, b);
//Method from abstract class implemented
demo.xyz();
demo.abc();
//Final string value and Method from Interface called here
System.out.println("\nI am final string value from INTERFACE: "+ value);
demo.display();
//Polymorphism methods called here from Calculator class
//These methods are called by using CLASS NAME
System.out.println("I am demonstrating POLYMORPHISM: ");
System.out.println(Calculator.Product(3, 4));
System.out.println(Calculator.Product(3.0, 4.3));
}
}
INTERFACEFORDEMO INTERAFCE CODE:
package demo1;
public interface InterfaceForDemo {
//Variable and method declaration in Interface
final String value = "I am final value in interface";
void display();
}
EVENTLISTENERDEMO CLASS CODE:
package demo1;
import java.awt.*;
import java.awt.event.*;
class EventListenerDemo extends Frame implements ActionListener{
TextField textField;
EventListenerDemo(){
//create components
textField=new TextField();
textField.setBounds(60,50,170,20);
Button b=new Button("Click here!!");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(textField);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
textField.setText("Got Event Listener ");
}
public static void main(String args[]){
new EventListenerDemo();
}
}
___________THE END_______________