HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Calculator (Java)

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
calculatorjavastackoverflow

Problem

I am a beginner learning Java and this is a calculator program I've made. I'm looking for suggestions to improve it and things I could've done better. I don't know OOP principles much, so any and all suggestions are welcome.

```
import java.util.Scanner;

public class BasicCalci {
Scanner sc = new Scanner(System.in);
int x = 0;
int choice = 0;
Maths m = new Maths();

public static void main(String[] args) {

BasicCalci ob = new BasicCalci();
ob.menu();

}

public void menu() {

System.out.println(" Basic Calculater ");
System.out.println();
System.out.println("1. Add.");
System.out.println("2. Subtract.");
System.out.println("3. Multiply.");
System.out.println("4. Divide.");
System.out.println("5. Square Root.");
System.out.println("6. Exponent.");
System.out.println("7. Prime Check.");
System.out.println("8. HCF.");
System.out.println("9. LCM.");
System.out.println("10. Exit.");

choice = sc.nextInt();
accepter(choice);

}

public void accepter(int c) {

double a, b;

switch (c) {

case 1: {
System.out.println("Enter the numbers you want to Add.");
a = sc.nextDouble();
b = sc.nextDouble();
add(a, b);
break;
}

case 2: {
System.out.println("Enter the numbers you want to Subtract.");
a = sc.nextDouble();
b = sc.nextDouble();
subtract(a, b);
break;

}
case 3: {
System.out.println("Enter the numbers you want to Multiply.");
a = sc.nextDouble();
b = sc.nextDouble();
multiply(a, b);
break;

}
case 4: {
System.out.println("Enter the numbers you want to Divide.");
a = sc.nextDouble();

Solution

Here are few suggestions to improve your code:

  • You can get rid of variables that you are not using: line 6: int x = 0;



  • You can declare Line 8: Maths m = new Maths(); inside acceptor() as you are


not using 'm' in the whole Test class.

-
Redundant code can be avoided:
inside primeList() :

if (count == 1) {
        if (a == b) {
            System.out.print((int) a + " is a Prime nummber.");
            pcount++;
        } else {
            pcount++;
            System.out.print((int) x + "\t");

            if (pcount % 8 == 0) {
                System.out.println();
            }
        }
        }


can be written as (since same statement is repeated in both if and else block) :

if (count == 1) {
    pcount++;
        if (a == b) {
            System.out.print((int) a + " is a Prime nummber.");

        } else {

            System.out.print((int) x + "\t");

            if (pcount % 8 == 0) {
                System.out.println();
            }
        }
        }


-
I would create two separate classes- one for functionality where all the operations such as add, subtract, hcf, primeList, etc are defined and other for providing menu options and results.This is in accordance with class design principles. Reference: http://howtodoinjava.com/best-practices/5-class-design-principles-solid-in-java/

Also, you can extend your functionality by accepting input from user multiple times, that is your program will show menu again after your computation for previous values is done.(Hint: use do while loop).

Hope this helps :)

Code Snippets

if (count == 1) {
        if (a == b) {
            System.out.print((int) a + " is a Prime nummber.");
            pcount++;
        } else {
            pcount++;
            System.out.print((int) x + "\t");

            if (pcount % 8 == 0) {
                System.out.println();
            }
        }
        }
if (count == 1) {
    pcount++;
        if (a == b) {
            System.out.print((int) a + " is a Prime nummber.");

        } else {

            System.out.print((int) x + "\t");

            if (pcount % 8 == 0) {
                System.out.println();
            }
        }
        }

Context

StackExchange Code Review Q#159542, answer score: 5

Revisions (0)

No revisions yet.