7
\$\begingroup\$

This is a simple Area Calculator coded in Java. What this does is that it prompts the user to select a shape, and asks for the details needed to calculate the area of that specific shape.

Main.java:

package com.AreaCalculator;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Area Calculator! Enter 1 to find the area of a Rectangle, Enter 2 to find the area of a Square, Enter 3 to find the area of a Triangle or Enter 4 to find the area of a Circle");

        int userInput = Integer.parseInt(scanner.next());

        if(userInput == 1) {
            System.out.println("Please enter the length of the rectangle: ");
            int length = Integer.parseInt(scanner.next());
            System.out.println("Please enter the width of the rectangle: ");
            int width = Integer.parseInt(scanner.next());

            System.out.println(length * width);
        } else if(userInput == 2) {
            System.out.println("Please enter the length of a side: ");
            int lengthOfASide = Integer.parseInt(scanner.next());

            System.out.println(lengthOfASide * lengthOfASide);
        } else if(userInput == 3) {
            System.out.println("Please enter the base of the triangle: ");
            int triangleBase = Integer.parseInt(scanner.next());
            System.out.println("Please enter the height of the triangle: ");
            int triangleHeight = Integer.parseInt(scanner.next());

            System.out.println((0.5 * triangleBase) * triangleHeight);
        } else if(userInput == 4) {
            System.out.println("Please enter the radius of the circle: ");
            int radius = Integer.parseInt(scanner.next());

            System.out.println(Math.PI * (radius * radius));
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ I just wanted to say something about formatting. If it's at all possible to keep your code under 80 or 100 characters per line do so, it improves readability of the source. Take a look at this post here. Anyways, good job though! \$\endgroup\$
    – user61611
    Jan 31, 2015 at 15:35

3 Answers 3

6
\$\begingroup\$

Making a package for a one-class program seems like overkill. You would be better off naming the class AreaCalculator, which would be more meaningful than Main.

Instead of writing the menu as a very long string, it would be more manageable to split it:

System.out.println("Welcome to the Area Calculator! " +
    "Enter 1 to find the area of a Rectangle, " +
    "Enter 2 to find the area of a Square, " +
    "Enter 3 to find the area of a Triangle or " +
    "Enter 4 to find the area of a Circle");

(This has no effect on performance. The compiler will embed just one long string constant in the bytecode.)

A chain of if-elses would be better written using a switch block. Not only is it easier to see the patten at a glance, it is also more efficient, in that it compiles to a single tableswitch opcode.

Since there are so many places where you write System.out.println() followed by scanner.nextInt(), it makes sense to write a function for that. Should you later decide to add validation and error handling, you can implement it in one central place. I suggest changing System.out.println() to System.out.print().

I don't see any reason why the inputs should be restricted to ints. You could just as easily accept doubles.

I recommend splitting each calculation into a separate function, to ensure that they do not interfere with each other. For example, if you were to add support for parallelograms and trapezoids, you might have a conflict for a variable named height, if you lumped everything into main(). The resulting main() would be just a menu.

import java.util.Scanner;

public class AreaCalculator {
    private static double ask(Scanner scanner, String prompt) {
        System.out.print(prompt);
        return scanner.nextDouble();
    }

    private static void rectangle(Scanner scanner) {
        double length = ask(scanner, "Please enter the length of the rectangle: ");
        double width = ask(scanner, "Please enter the width of the rectangle: ");
        System.out.println(length * width);
    }

    private static void square(Scanner scanner) {
        double lengthOfASide = ask(scanner, "Please enter the length of a side: ");
        System.out.println(lengthOfASide * lengthOfASide);
    }

    private static void triangle(Scanner scanner) {
        double triangleBase = ask(scanner, "Please enter the base of the triangle: ");
        double triangleHeight = ask(scanner, "Please enter the height of the triangle: ");
        System.out.println(0.5 * triangleBase * triangleHeight);
    }

    private static void circle(Scanner scanner) {
        double radius = ask(scanner, "Please enter the radius of the circle: ");
        System.out.println(Math.PI * (radius * radius));
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Welcome to the Area Calculator! " +
            "Enter 1 to find the area of a Rectangle, " +
            "Enter 2 to find the area of a Square, " +
            "Enter 3 to find the area of a Triangle or " +
            "Enter 4 to find the area of a Circle");

        switch (scanner.nextInt()) {
          case 1:
            rectangle(scanner);
            break;
          case 2:
            square(scanner);
            break;
          case 3:
            triangle(scanner);
            break;
          case 4:
            circle(scanner);
            break;
        }
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Amazing answer! Thanks a lot man! I hope my next application would be much better than the one I have. \$\endgroup\$ Jan 31, 2015 at 7:06
4
\$\begingroup\$

Using package com.areacalculator implies that the domain areacalculator.com is under the control of the programmer or the organization for whom the code was written[1]. The idea is to produce unique Java namespaces by leveraging the uniqueness of internet domain names.

It's a convention designed for the age of the internet and a sound strategy for avoiding namespace conflicts. It makes assigning responsibility a more reasoned process: only one or none of the packages can be named correctly.


[1] Clearly I have assumed that the author of the review code does not control the domain. My apologies if I am in error. The bases for this assumption were the standard parking content currently on the domain and the shape of the code relative to the shape of code expected when serving of area calculations from a website.

\$\endgroup\$
0
3
\$\begingroup\$

I'd like to add something to 200_success's great answer

You can separate some things in different classes. For example you can Change the Method circle as follows.

private Shape void circle(Scanner scanner) {
  double radius = ask(scanner, "Please enter the radius of the circle: ");
  return new Circle(radius);
}

An Shape and a Circle can be defined like this.

public interface Shape {
  double getArea();
}

public class Circle implements Shape {
  private final double radius;
  public Circle(double radius) {
    this.radius = radius;
  }

  public double getArea() {
    return Math.PI * radius * radius;
  }
}

So you can extract the switch Statement and let it return an Shape and call the Method getArea() on the returned Shape.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.