Question

Write ONE application program by using the following requirements: Using JAVA File input and outp...

Write ONE application program by using the following requirements: Using JAVA File input and output Exception handling Inheritance At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods At least one interface: this interface needs to have at least two abstract methods At least one method overloading At least one method overriding At least one static member and one static method Polymorphism: you need to have at least one superclass reference to reference objects of a subclass At least one aggregation in a class include comments at the places where you use the above items.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

package com.abc.others;

//superclass
public class Calculator implements ValidateInput{
   // data members
   int num1;
   int num2;

   /**
   * @param num1
   * @param num2
   */
   public Calculator(int num1, int num2) {
       super();
       validate();
       this.num1 = num1;
       this.num2 = num2;
   }

   // accessor
   /**
   * @return the num1
   */
   public int getNum1() {
       return num1;
   }

   // mutator
   /**
   * @param num1 the num1 to set
   */
   public void setNum1(int num1) {
       this.num1 = num1;
   }

   // accessor
   /**
   * @return the num2
   */
   public int getNum2() {
       return num2;
   }

   // mutator
   /**
   * @param num2 the num2 to set
   */
   public void setNum2(int num2) {
       this.num2 = num2;
   }

   // toString
   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "Calculator [num1=" + num1 + ", num2=" + num2 + "]";
   }

   public int add() {
       return num1 + num2;
   }

   // Overloading
   public double add(int num1, int num2) {
       return num1 + num2;
   }

   public int sub() {
       return num1 - num2;
   }

  
   @Override
   public boolean validate() {
       // input validation exception
       if(num1 < 0 || num2 < 0)
           throw new IllegalArgumentException("Input can not be negatibe");
       return true;
   }

}

--------------
package com.abc.others;

//subclass
public class ScienceCalculator extends Calculator {
   // data members
   int num3;
   // static member
   static int callCount = 0;

   public ScienceCalculator(int num1, int num2) {
       super(num1, num2);
       // TODO Auto-generated constructor stub
   }

   // accessor
   /**
   * @return the num3
   */
   public int getNum3() {
       return num3;
   }

//mutator
   /**
   * @param num3 the num3 to set
   */
   public void setNum3(int num3) {
       this.num3 = num3;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "ScienceCalculator [num3=" + num3 + "]";
   }

   // Overriding
   public double add(double num1, double num2) {
       return num1 + num2;
   }

   public double squareRoot() {
       return Math.sqrt(num3);
   }

   // Static method
   public static double squareRoot(int num3) {
       return Math.sqrt(num3);
   }

}
------------------
package com.abc.others;

//Interface
public interface ValidateInput {
  
   public boolean validate();

}
-----------------------------
package com.abc.others;

public class CalculatorTest {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Calculator genericCalculator = new Calculator(10,20);
      
       System.out.println(genericCalculator.add());
      
       Calculator scienceCalculator = new ScienceCalculator(20, 30);
       System.out.println(scienceCalculator.add());
      
       System.out.println(ScienceCalculator.squareRoot(9));
      
   }

}

---------
Sample output
30
50
3.0

Add a comment
Know the answer?
Add Answer to:
Write ONE application program by using the following requirements: Using JAVA File input and outp...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class....

    In JAVA Algorithm Workbench 1. Write the first line of the definition for a Poodle class. The class should extend the Dog class. 2. Look at the following code, which is the first line of a class definition: public class Tiger extends Felis In what order will the class constructors execute? 3. Write the statement that calls a superclass constructor and passes the arguments x, y, and z. 4. A superclass has the following method: public void setValue(int v) value...

  • You are given a specification for some Java classes as follows.   A building has a number...

    You are given a specification for some Java classes as follows.   A building has a number of floors, and a number of windows. A house is a building. A garage is a building. (This isn’t Florida-like … it’s a detached garage.) A room has a length, width, a floor covering, and a number of closets. You can never create an instance of a building, but every object that is a building must have a method that calculates the floor space,...

  • Java Program Please help me with this. It should be pretty basic and easy but I...

    Java Program Please help me with this. It should be pretty basic and easy but I am struggling with it. Thank you Create a superclass called VacationInstance Variables destination - String budget - double Constructors - default and parameterized to set all instance variables Access and mutator methods budgetBalance method - returns the amount the vacation is under or over budget. Under budget is a positive number and over budget is a negative number. This method will be overwritten in...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • Java Questions A class must implement at least one interface. Question 1 options: True False Question...

    Java Questions A class must implement at least one interface. Question 1 options: True False Question 2 (1 point) Abstract methods can be called. Question 2 options: True False Question 3 (1 point) Constructors Question 3 options: Are inherited but cannot be accessed Are inherited and can be accessed Are accessible but are not inherited Are not inherited and cannot be accessed Question 4 (1 point) We can instantiate an object from an abstract class. Question 4 options: True False...

  • JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance...

    JAVA - Abstraction and Encapsulation are one pillar of OOP (Object Oriented Programming). Another is inheritance and polymorphism. In this assignment we will use inheritance and polymorphism to solve a problem. Part (a) of the figure below shows a symbolic representation of an electric circuit called an amplifier. The input to the amplifier is the voltage vi and the output is the voltage vo. The output of an amplifier is proportional to the input. The constant of proportionality is called...

  • 44 A method signature defines which of the following? (choose all that apply) return type name...

    44 A method signature defines which of the following? (choose all that apply) return type name call parameters body 45 An abstract class can be implemented using the implements keyword. True False 46 An abstract class can be extended using the extends keyword. True False 47 Abstract classes are represented in UML by: italics underline plus/minus bold 48 An abstract class is a superclass for a subclass. True False 49 These symbols are used to denote an interface in a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT