Question: Using the given Addition class as a parent class, create two child classes Multiplication and Subtraction that override the nNumbers() method in the Addition class

Using the given Addition class as a parent class, create two child classes Multiplication and Subtraction that override the nNumbers() method in the Addition class for their own. Your Multiplication and Subtraction classes should NOT have ANY of their own class variables but instead utilize their parents (Addition) class variable. Your program should run without error using this Driver class without any modifications.

Addition Class:

public class Addition {

int[] array;

Addition(int[] array) {

this.array = array;

}

public int nNumbers() {

int result = 0;

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

result += array[i];

return result;

}

}

BasicCalculator:

import java.util.Scanner;

public class BasicCalculator {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

String answer, result, choice = "";

int[] array;

int n;

Addition add;

Multiplication multiply;

Subtraction subtract;

do {

result = "";

System.out.println("Would you like to add, multiply or

subtract?");

answer = scan.next();

answer = answer.toLowerCase();

switch(answer) {

/*******************************ADD*******************************/

case "add":

System.out.print("How many numbers do you want to add? ");

n = scan.nextInt();

if (n > 1) {

array = new int[n];

System.out.println("Please enter " + n + " numbers

separated by enters");

for(int i = 0; i < n; i++)

array[i] = scan.nextInt();

add = new Addition(array);

for(int i = 0; i < array.length; i++) {

result += array[i];

if(i != array.length-1)

result += " + ";

}

result += " = " + add.nNumbers();

System.out.println(result);

}

else {

System.out.println("Not enough numbers. Try

again!");

}

break;

/*******************************MULTIPLY*******************************/

case "multiply":

System.out.print("How many numbers do you want to multiply?

");

n = scan.nextInt();

if (n > 1) {

array = new int[n];

System.out.println("Please enter " + n + " numbers

separated by enters");

for(int i = 0; i < n; i++)

array[i] = scan.nextInt();

multiply = new Multiplication(array);

for(int i = 0; i < array.length; i++) {

result += array[i];

if(i != array.length-1)

result += " * ";

}

result += " = " + multiply.nNumbers();

System.out.println(result);

}

else {

System.out.println("Not enough numbers. Try

again!");

}

break;

/*******************************SUBTRACT*******************************/

case "subtract":

System.out.print("How many numbers do you want to subtract?

");

n = scan.nextInt();

if (n > 1) {

array = new int[n];

System.out.println("Please enter " + n + " numbers

separated by enters");

for(int i = 0; i < n; i++)

array[i] = scan.nextInt();

subtract = new Subtraction(array);

for(int i = 0; i < array.length; i++) {

result += array[i];

if(i != array.length-1)

result += " - ";

}

result += " = " + subtract.nNumbers();

System.out.println(result);

}

else {

System.out.println("Not enough numbers. Try

again!");

}

break;

default:

System.out.println("What was that?");

}

System.out.println("Would you like to calculate more numbers?");

choice = scan.next();

}while (choice.equalsIgnoreCase("yes"));

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!