Question

In: Computer Science

Describe how you would develop object-oriented features of Java for the Quiz program developed in the...

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.

Solutions

Expert Solution

Answer : Given data

* I have used following for the solution:

  1. Calculator is a class:
  2. which is abstact
  3. which is having abstract method
  4. which will define static methods for the polymorphism
  5. MainCall is another class:
  6. demonstarte super call from Calculator class
  7. which will extends the Calculator to demonstarte inheritance
  8. which will implement th InterfaceForDemo to demonstarte interface
  9. InterfaceForDemo is interface to demonstarte interafce
  10. contain a variable and method that are called by MainCall class
  11. EventListenerDemo is a class to demonstarte EventListener


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_______________


Related Solutions

Describe how you would develop object-oriented features of Java for the Quiz program developed in the...
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. Your Discussion should be at least 250 words in length, but not more than 750 words. Once you’ve completed your initial post, be sure to respond to the posts of at least 3 of your classmates.
PHP You will be using the object oriented features of PHP to design a music album...
PHP You will be using the object oriented features of PHP to design a music album processing page. First you will build a form page called addAlbum.php. This form will contain text fields for album title, artist, publisher (Sony, BMI, etc.) and genre. Add two more fields of your choice. You will post this form to the file process.php. If all the fields have values, we will create a new Album object and print the details of the object. You...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
Describe the types of hierarchy found in object-oriented systemdescriptions.
Describe the types of hierarchy found in object-oriented system descriptions.
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the...
OBJECT-ORIENTED JAVA The catch-or-declare requirement states that you have two choices: You can either catch the exception or you can provide a throws clause. To which exceptions does the catch-or-declare requirement apply? Errors All Exceptions except RuntimeExceptions RuntimeExceptions All Throwable except Errors Flag this Question Question 21 pts Assume you have an array called numbers. It's elements are of type int and it was declared like this: int[] numbers = { 3, 6, 9, 12 }; Assume that you also...
Need a program in java that creates a random addition math quiz The program should ask...
Need a program in java that creates a random addition math quiz The program should ask the user to enter the following The smallest and largest positive numbers to be used when generating the questions - The total number of questions to be generated per quiz - The total number of the quiz's to create from then the program should generate a random math (Just addition) quiz from what the user entered
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named...
Object-Oriented Design and Patterns in Java (the 3rd Edition Chapter 5) - Create a directory named as the question number and save the required solutions in the directory. - Each problem comes with an expected tester name. In the directory for a given problem, including the tester and all java classes successfully run this tester. Exercise 5.2 ObserverTester.java - Improve Exercise 1 by making the graph view editable. Attach a mouse listener to the panel that paints the graph. When...
This question is about the java Object-Oriend program and give the code as follow requirment: You...
This question is about the java Object-Oriend program and give the code as follow requirment: You are a software engineer who has to write the software for a car transmission. The transmission has five gears numbered from 1 to 5 to move forward, one neutral gear position numbered 0 where the car does not move, and one reverse gear numbered -1 to move backward. The transmission has a clutch, and the driver of the car can only change gear if...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and...
Develop a python program to create a quiz with limited time and attempts!!! Add comments and screenshot of running the program quiz could be of anything like , what's the sum of 2&2. There should be number of attempts(limited) suppose if the answer is wrong.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT