0% found this document useful (0 votes)
51 views

Java Calculator

This document contains the code for a calculator application with nested classes. It includes code for addition, subtraction, multiplication and division classes within the main Calculator class. The code takes input from the user, calls the appropriate operation class based on their selection, performs the calculation, outputs the result and validates if it is an even or odd number. Screenshots and outputs validating the classes and calculations are also included.

Uploaded by

NONO P STUART
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Java Calculator

This document contains the code for a calculator application with nested classes. It includes code for addition, subtraction, multiplication and division classes within the main Calculator class. The code takes input from the user, calls the appropriate operation class based on their selection, performs the calculation, outputs the result and validates if it is an even or odd number. Screenshots and outputs validating the classes and calculations are also included.

Uploaded by

NONO P STUART
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

NONO P STUART 2020/BIT/133/PS

MBARARA UNIVERSITY OF SCIENCE AND TECHNOLOGY

FACULTY OF COMPUTING AND INFORMATICS

DEPARTMENT OF INFORMATION TECHNOLOGY

NAME: NONO P STUART

REG NUMBER: 2020/BIT/133/PS

THE SCREENSHOTS
NONO P STUART 2020/BIT/133/PS
NONO P STUART 2020/BIT/133/PS

THE OUTPUT
NONO P STUART 2020/BIT/133/PS

VALIDATION OF THE CLASSES

THE RAW CODE


NONO P STUART 2020/BIT/133/PS

package calculator;

import java.util.Scanner;

public class Calculator {

// global variable declaration

public static int a;

public static int b;

public static int c;

public static int d;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("\t WELCOME TO THE CALCULATOR");

System.out.println("Enter first number-");

a = sc.nextInt();

System.out.println("Enter second number-");

b = sc.nextInt();

System.out.println("Enter third number-");

c = sc.nextInt();

System.out.println("\t CHOICE MENUS");

System.out.println("1.For ADDITION:");
NONO P STUART 2020/BIT/133/PS

System.out.println("2.For SUBTRACTION:");

System.out.println("3.For MULTIPLICATION:");

System.out.println("4.For DIVISION:");

System.out.println("5.For ALL OPERATIONS AT ONCE:");

System.out.println("Enter your choice:");

int ch=sc.nextInt();

switch(ch){

case 1 -> {

// how to access inner nested classes from the outer class

Calculator adi= new Calculator();

Calculator.Addition ad = adi.new Addition();

ad.Addit();

ad.Add();

case 2 -> {

Calculator adi= new Calculator();

Calculator.Subtraction su = adi.new Subtraction();

su.Sub();

case 3 -> {

Calculator adi= new Calculator();

Calculator.Multiplication mu = adi.new Multiplication();

mu.Multi();
NONO P STUART 2020/BIT/133/PS

case 4 -> {

Calculator adi = new Calculator();

Calculator.Division di=adi.new Division();

di.Div();

case 5 -> {

// calls all classes and methods nested in the Calculator class

Calculator adi =new Calculator();

Calculator.Addition ad = adi.new Addition();

Calculator.Subtraction su = adi.new Subtraction();

Calculator.Multiplication mu = adi.new Multiplication();

Calculator.Division di=adi.new Division();

ad.Addit();

ad.Add();

su.Subtract();

su.Sub();

mu.Multiplic();

mu.Multi();

di.Divis();

di.Div();

default -> {
NONO P STUART 2020/BIT/133/PS

System.out.println("You entered a wrong choice");

// the addition class

class Addition{

int d =a+b+c;

public int Add(){

System.out.println("The Sum is "+d);

Addition valu=new Addition();

Addition.Validator val= valu.new Validator();

val.vali();

return 0;

public void Addit(){

System.out.println("\t **THE ADDITION MENU**");

// the validation class

class Validator{

int h =d % 2;

public int vali(){


NONO P STUART 2020/BIT/133/PS

if(h==0){

System.out.println("The sum is an Even number");

}else{

System.out.println("The sum is An Odd number");

return 0;}

// the subtraction class

class Subtraction{

public int Sub(){

int d =a-b-c;

System.out.println("The Subtration is "+d);

return 0;}

public void Subtract(){

System.out.println("\t **THE SUBTRACTION MENU**");}

// the multiplication class

class Multiplication{

public int Multi(){


NONO P STUART 2020/BIT/133/PS

int d =a*b*c;

System.out.println("The Multiple is "+d);

return 0;}

public void Multiplic(){

System.out.println("\t **THE MULTIPLICATION MENU**");}

// the division class

class Division{

public int Div(){

float e =a/b/c;

System.out.println("The Division is "+e);

return 0;}

public void Divis(){

System.out.println("\t **THE DIVISION MENU**");}

You might also like