Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class Calculaator { public static void main(Str

ID: 3841262 • Letter: I

Question

import java.util.Scanner;

public class Calculaator {
  
  
   public static void main(String[] args) {
int choice, sum = 0, i = 0, difference = 0;
double num1, num2;
Scanner input = new Scanner(System.in);

System.out.println("Welcome to Fancy Calculator");
System.out.println("Please choose your operation: ");
System.out.println("1 for Addition" + ' ' + "2 for Subtraction");
System.out.println("3 for Multiplication" + ' ' + "4 for Division" + ' ' + "5 for calculating average");
choice = input.nextInt();

//Addition and Subtraction program
if (choice == 1) {
System.out.println("You have selected Addition");
System.out.println("Enter numbers to add (select enter after each one) to add all numbers, enter 0: ");
int number;
do {
number = input.nextInt();
sum += number;
}
while (number != 0);
System.out.println("Sum of all numbers is = " + sum);

} else if (choice == 2) {
System.out.println("You have selected Subtraction. You will subtract 2 numbers");
System.out.println("Enter first number: ");
int subnum1 = input.nextInt();
System.out.println("Enter second number: ");
int subnum2 = input.nextInt();

System.out.println("Difference of " + subnum1 + " and " + subnum2 + " is = " + (subnum1 - subnum2));

} else if (choice == 3) {
System.out.println("You have selected Multiplication. ");
System.out.println("Enter number 1: ");
double multnum1 = input.nextDouble();
System.out.println("Enter number 2: ");
double multnum2 = input.nextDouble();
System.out.println(multnum1 + " times " + multnum2 + " = " + CalcMultiplication.multiplication(multnum1, multnum2));

} else if (choice == 4) {
System.out.println("You have selected Division. ");
System.out.println("Enter number 1: ");
double divnum1 = input.nextDouble();
System.out.println("Enter number 2: ");
double divnum2 = input.nextDouble();
System.out.println(divnum1 + " divided by " + divnum2 + " = " + CalcDivision.division(divnum1, divnum2));
} else if (choice == 5) {
Scanner sc = new Scanner(System.in);
System.out.println("You have selected calculating an average");
System.out.println("Enter number of values you want to average: ");
int n = sc.nextInt();
int sum1 = 0;

int[] numbers = new int[n];

for(int k=0; k < numbers.length ; k++)
{
System.out.println("Enter grade " + (k + 1) + " :");
int a = sc.nextInt();
sum1 = sum1 + a;
}

double average = sum1 / (double) n;

System.out.println("Average value of array elements is : " + average);
sc.close();

}
}

}

public class CalcMultiplication {
   public static double multiplication(double x, double y) {
double result;
result = x * y;
return result;
}
}

public class CalcDivision {
   public static double division(double x, double y) {
   double result;
   if (y == 0) {
   System.out.println("The result is undefinded");
   }
   else {
   System.out.println(x + "divided by" + y + "is" + (x/y));

   }
   result = x / y;
   return result;
   }

}

I made this simple calculator program using two different classes. I need help making this code more object oriented by adding features such as abstraction inheritance, constructors, and encapsulation.

Explanation / Answer

My_Calculation.java

----------------------------

package venkanna;

import java.util.Scanner;

abstract class Calculation //Abstraction
{
   int z;
   private int h=10;//Encapsulation
   abstract void draw();
   public void addition(int x, int y)
   {
       z = x + y;
       System.out.println("The sum of the given numbers:" + z);
   }
   public void Subtraction(int x, int y)
   {
       z = x - y;
       System.out.println("The difference between the given numbers:" + z);
   }
}

public class My_Calculation extends Calculation //Inheritance
{
   public void multiplication(int x, int y)
   {
       z = x * y;
       System.out.println("The product of the given numbers:" + z);
   }
   int z;
   public void addition(int x, int y)
   {
       z = x + y;
       System.out.println("The sum of the given numbers:" + z);
   }
   public void Subtraction(int x, int y)
   {
       z = x - y;
       System.out.println("The difference between the given numbers:" + z);
   }
public void draw()
{
   System.out.println("abstarct method");
}
   public static void main(String args[])
   {
       int a = 20, b = 10;
       My_Calculation demo = new My_Calculation();
       Scanner input = new Scanner(System.in);
       int choice, sum = 0, i = 0, difference = 0;
       System.out.println("Welcome to Fancy Calculator");
System.out.println("Please choose your operation: ");
System.out.println("1 for Addition");
System.out.println("2 for Subtraction");
System.out.println("3 for Multiplication");
System.out.println("4 for Division");
System.out.println("5 for calculating average");
choice = input.nextInt();
if (choice == 1)
{
System.out.println("You have selected Addition");
System.out.println("Enter numbers to add (select enter after each one) to add all numbers, enter 0: ");
int number;
do
{
number = input.nextInt();
sum += number;
}while (number != 0);
System.out.println("Sum of all numbers is = " + sum);
}
else if (choice == 2)
{
System.out.println("You have selected Subtraction. You will subtract 2 numbers");
System.out.println("Enter first number: ");
int subnum1 = input.nextInt();
System.out.println("Enter second number: ");
int subnum2 = input.nextInt();
System.out.println("Difference of " + subnum1 + " and " + subnum2 + " is = " + (subnum1 - subnum2));
}
else if (choice == 3)
{
System.out.println("You have selected Multiplication. ");
System.out.println("Enter number 1: ");
double multnum1 = input.nextDouble();
System.out.println("Enter number 2: ");
double multnum2 = input.nextDouble();
System.out.println(multnum1 + " times " + multnum2 + " = " + CalcMultiplication.multiplication(multnum1, multnum2));
}
else if (choice == 4)
{
System.out.println("You have selected Division. ");
System.out.println("Enter number 1: ");
double divnum1 = input.nextDouble();
System.out.println("Enter number 2: ");
double divnum2 = input.nextDouble();
System.out.println(divnum1 + " divided by " + divnum2 + " = " + CalcDivision.division(divnum1, divnum2));
}
else if (choice == 5)
{
Scanner sc = new Scanner(System.in);
System.out.println("You have selected calculating an average");
System.out.println("Enter number of values you want to average: ");
int n = sc.nextInt();
int sum1 = 0;
int[] numbers = new int[n];
for(int k=0; k < numbers.length ; k++)
{
System.out.println("Enter grade " + (k + 1) + " :");
int a2 = sc.nextInt();
sum1 = sum1 + a2;
}
double average = sum1 / (double) n;
System.out.println("Average value of array elements is : " + average);
sc.close();
}
       demo.addition(a, b);
       demo.Subtraction(a, b);
       demo.multiplication(a, b);
   }
}