Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java 1st program crits [closed]

Tags:

java

Just started learning Java, wrote a basic program that calculates the area of user selected shapes. Can I get crits and comments on things I've done right and wrong. I'm guessing a lot will be bad programming but that's why I'm here.

Also 1 question, when calling my methods I need to put the full path i.e. areaprog.areacode.. Do you know why this is? Code for both classes below:

Main Program

package areaprog;

import java.util.Scanner;

public class mainprog {


    public static void main (String [] args){

        //Area Menu Selection
        System.out.println("What shape do you need to know the area of?\n" +
        "1: Square?\n" +
        "2: Rectangle?\n" +
        "3: Triangle?\n" +
        "4: Circle? \n" +
        "5: Exit\n"     
        );

        //User input for menu
        Scanner reader = new Scanner(System.in);
        System.out.println("Number: ");
        int input = reader.nextInt();
        reader.nextLine();

        //Depending on user selection, depends on what method is called using switch.
    Scanner scan = new Scanner(System.in);

        //Square selection
        if (input == 1){
            System.out.println("What is a length of 1 side of the Square?\n");
                double s1 = scan.nextInt();
                double SqAns = areaprog.areacode.square(s1);
            System.out.println("The area of you square is: " + SqAns);
        }

        //Rectangle selection    
            if (input == 2){
            System.out.println("What is the width of your rectangle?.\n");
                double r1 = scan.nextInt();
            System.out.println("What is the height of your rectangle?\n");
                double r2 = scan.nextInt();
                double RecAns = areaprog.areacode.rect(r1, r2);
            System.out.println("The area of your rectangle is: " + RecAns);    
            }
        //Triangle selection
        if (input == 3){
            System.out.println("What is the base length of the triangle?.");
                double t1 = scan.nextInt();
            System.out.println("What is the height of your triangle?");
                double t2 = scan.nextInt();
                double TriAns = areaprog.areacode.triangle(t1, t2);
            System.out.println("The area of your triangle is " + TriAns);
        }
        //Circle selection
        if (input == 4){
            System.out.println("What is the radius of your circle?.");
                double c1 = scan.nextInt();
                double CircAns = areaprog.areacode.circle(c1);
            System.out.println("The area of your circle is " + CircAns);    

        }
        //Exit application
        if (input == 5){
            System.out.println("Goodbye.");
        System.exit(0);
        }


    }

}

Area Calculations

package areaprog;


public class areacode {

    public static double rect(double width, double height) {
        double a_value = width * height;

        return a_value;


    }

    public static double circle(double radius){
        double PI = Math.PI;
        double a_value = PI * Math.pow(radius, 2);

        return a_value;


    }

    public static double square(double side) {
        double a_value = Math.pow(side, 2);

        return a_value;


    }

    public static double triangle(double base , double height) {
        double a_value = (base/2)* height;

        return a_value;


    }
}
like image 348
tmcraig Avatar asked Mar 18 '26 02:03

tmcraig


1 Answers

As benjamin says, your question belongs elsewhere. But here are a few remarks.

  • You should use a capital letter for your classes.

  • Naming things is important. Areacode is a weird name, you should describe the functionality, like AreaCalculator for example.

  • A function name should describe what it does, so instead of circle, you should use something like getCircleArea or something

  • In your main method, it is probably better to import the package so you just have to say AreaCalculator.getCircle(5); for example instead of typing the package every time.

But if you look at the whole thing, this is a very good first program. If you know why you do everything you do (for example why it is a good practice for this second class to be static), you should be doing great in no time.

You could put several things that you do in the main method in separate methods for a better structure of your program and easier debugging.

edit/ Your question: If you import the package at the beginning of the file you don't need to write the whole path. It's one of the remarks above.

like image 131
J. Maes Avatar answered Mar 21 '26 00:03

J. Maes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!