Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

explain what is meant by the terms unitarism and pluralism

Answered: 1 week ago