Skip to content

Instantly share code, notes, and snippets.

@Tman15000
Created October 30, 2019 15:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tman15000/1e610a9370239ccacde95d002f39850f to your computer and use it in GitHub Desktop.
Save Tman15000/1e610a9370239ccacde95d002f39850f to your computer and use it in GitHub Desktop.
// Thomas Worrell
import java.util.Scanner;
public class AreaCalculator {
public static void main(String[] args) {
System.out.println("Shape Area Calculator V1.0");
Scanner input = new Scanner(System.in);
double option = 0;
do {
System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
System.out.println();
System.out.println("1) Triangle");
System.out.println("2) Rectangle");
System.out.println("3) Square");
System.out.println("4) Circle");
System.out.println("5) Quit");
System.out.print("Which shape: ");
option = input.nextInt();
input.nextLine(); // Clear Keyboard
if (option == 1) {
System.out.println("You selected Triangle Area Calculation.");
System.out.println("Now continue with Area Calculation Logic");
System.out.println("...");
System.out.print("Please enter Triangle Base Length: ");
double base = input.nextInt();
System.out.print("Please enter Triangle Height: ");
double height = input.nextInt();
double area = area_triangle(base, height);
System.out.println("Based on the values entered, the Triangle has an area of " + area);
} else if (option == 2) {
System.out.println("You selected Rectangle Area Calculation.");
System.out.println("Now continue with Area Calculation Logic");
System.out.println("...");
System.out.print("Please enter Rectangle Length: ");
double length = input.nextInt();
System.out.print("Please enter Rectangle Width: ");
double width = input.nextInt();
double area = area_rectangle(length, width);
System.out.println("Based on the values entered, the Rectangle has an area of " + area);
} else if (option == 3) {
System.out.println("You selected Square Area Calculation.");
System.out.println("Now continue with Area Calculation Logic");
System.out.println("...");
System.out.print("Please enter Square Side Length: ");
double side = input.nextInt();
double area = area_square(side);
System.out.println("Based on the value entered, the Square has an area of " + area);
} else if (option == 4) {
System.out.println("You selected Circle Area Calculation.");
System.out.println("Now continue with Area Calculation Logic");
System.out.println("...");
System.out.print("Please enter Circle Radius: ");
double radius = input.nextInt();
double area = area_circle(radius);
System.out.println("Based on the values entered, the Circle has an area of " + area);
} else if (option != 5) {
System.out.println("Illegal Option");
}
} while (option != 5);
input.close();
}
private static double area_circle(double radius) {
return Math.PI * (radius * radius);
}
private static double area_square(double side) {
return side * side;
}
private static double area_rectangle(double length, double width) {
return length * width;
}
private static double area_triangle(double base, double height) {
return .5 * base * height;
}
}
@muhammad-alani
Copy link

muhammad-alani commented Dec 22, 2021

Hello
I study at teacher Tim's Buchalka in Udemy Academy, and he gave me Exercise in Java enterprise:
Area Calculator
Write a method named area with one double parameter named radius.

The method needs to return a double value that represents the area of a circle.

If the parameter radius is negative then return -1.0 to represent an invalid value.

Write another overloaded method with 2 parameters x and y (both doubles), where x and y represent the sides of a rectangle.

The method needs to return an area of a rectangle.

If either or both parameters is/are a negative return -1.0 to indicate an invalid value.

For formulas and PI value please check the tips below.

Examples of input/output:

area(5.0); should return 78.53975

area(-1); should return -1 since the parameter is negative

area(5.0, 4.0); should return 20.0 (5 * 4 = 20)

area(-1.0, 4.0); should return -1 since first the parameter is negative
--------------------------------------------------------------------------------------------- >

My solution is:
public static void main(String[] args) {

    System.out.println(area(9));
    System.out.println(area(8, 9));
}

public static double area(double radius) {
    if (radius < 0) {
        System.out.println("Invalid value");
        return -1.0;
    } else if (radius > 0){
        System.out.println("It's works");
        return +1.0;
    } else {
        return 0.0;
    }
}

public static double area(double x, double y) {
    if ((x < 0) || (y < 0)) {
        System.out.println("Invalid value");
        return -1.0;
    } else if (x > 0 || y > 0){
        System.out.println("Works");
        return +1.0;
    } else {
        return 0.0;
    }
}

And I think he needs a better solution, what do you think about my solution? Can you help me?

@Nareshdayalan
Copy link

public class AreaCalculator {
public static double area (double radius){
if(radius < 0){
return -1;
}else{
double PI =Math.PI;
double areaofcircle = radiusradiusPI;
return areaofcircle;
}
}
public static double area (double x,double y){
if(x<0){
return -1;
}else if(y<0){
return -1;
}else{
double areaofretangle = x*y;
return areaofretangle;
}
}
}
use this code to run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment