Home > Net >  Is this code good as a beginner? How can i make it more concise? (Condition is that: We have to demo
Is this code good as a beginner? How can i make it more concise? (Condition is that: We have to demo

Time:10-01

Is this code good as a beginner? If no how can I make it more concise? If yes any more suggestions?

(Condition is that: We have to demonstrate OOPs Concept using this)

About Code: This is a java program which demonstrates the use of OOPs functionality (beginner level) by Making a program about 3D Mensuration in which user have to select shape then enter measures as per requirements of shape and it will Print Curved Surface Area, Volume etc.

This is my code:

import java.util.*;
import java.lang.*;

class Mensuration {

    public static int selectShape(){
        System.out.print("1. Cube\n2. Cuboid\n3. Cylinder\n4. Cone\n5. Sphere\n6. Hemisphere\nSelect the Shape to perform action upon: ");
        Scanner selectShape = new Scanner(System.in);
        return selectShape.nextInt();
    }
}

class singleInputShapes {
    static float getInput() {
        Scanner inp = new Scanner(System.in);
        return inp.nextFloat();
    }
}

class Cube extends singleInputShapes{
    private float side;

    public float CSA() {
        System.out.print("Enter Side: ");
        this.side = singleInputShapes.getInput();
        return 4*side*side;
    }

    public float TSA() {
        return 6*side*side;
    }

    public float Volume() {
        return side*side*side;
    }


}

class Cuboid {
    private final float l;
    private final float b;
    private final float h;

    Cuboid() {
        System.out.print("Enter Length: ");
        this.l = singleInputShapes.getInput();
        System.out.print("Enter Breadth: ");
        this.b = singleInputShapes.getInput();
        System.out.print("Enter Height: ");
        this.h = singleInputShapes.getInput();

    }

    public float CSA() {
        return 2*h*(l b);
    }

    public float TSA() {
        return 2*(l*b b*h h*l);
    }

    public float Volume() {
        return l*b*h;
    }
}

class Cylinder {
    private final float r;
    private final float h;

    Cylinder() {
        System.out.print("Enter radius: ");
        this.r = singleInputShapes.getInput();
        System.out.print("Enter Height/Length: ");
        this.h = singleInputShapes.getInput();
    }
    public float CSA() {
        return (float) (2*Math.PI*r*h);
    }

    public float TSA() {
        return (float) (2*Math.PI*r*(r h));
    }

    public float Volume() {
        return (float) (Math.PI*r*r*h);
    }
}

class Cone {
    private final float r;
    private final float h;

    Cone() {
        System.out.print("Enter radius: ");
        this.r = singleInputShapes.getInput();
        System.out.print("Enter Height/Length: ");
        this.h = singleInputShapes.getInput();
    }
    public float slantHeight() {
        return (float) Math.sqrt(r*r   h*h);
    }

    public float CSA() {
        return (float) (Math.PI*r*slantHeight());
    }

    public float TSA() {
        return (float) (Math.PI*r*(r slantHeight()));
    }

    public float Volume() {
        return (float) (Math.PI*r*r*h/3);
    }
}

class Sphere extends singleInputShapes{
    private float radius;

    public double CSA() {
        System.out.print("Enter Radius: ");
        this.radius = singleInputShapes.getInput();
        return 4*Math.PI*radius*radius;
    }
    public double HemiTSA() {
        return 3*Math.PI*radius*radius;
    }
    public float Volume() {
        return (float) (4*Math.PI*radius*radius*radius/3);
    }
}

public class Main {

    public static void main(String[] args) {
        int shapeNumber = Mensuration.selectShape();
        switch (shapeNumber) {
            case 1 -> {
                Cube c1 = new Cube();
                System.out.println("- - - - - Cube - - - - -");
                System.out.println("Curved Surface Area of Cube is: "   c1.CSA());
                System.out.println("Total Surface Area of Cube is: "  c1.TSA());
                System.out.println("Volume of Cube is: "  c1.Volume());
            }
            case 2 -> {
                System.out.println("- - - - - Cuboid - - - - -");
                Cuboid cd1 = new Cuboid();
                System.out.println("Curved Surface Area of Cuboid is: "   cd1.CSA());
                System.out.println("Total Surface Area of Cuboid is: "  cd1.TSA());
                System.out.println("Volume of Cuboid is: "  cd1.Volume());
            }
            case 3 -> {
                System.out.println("- - - - - Cylinder - - - - -");
                Cylinder cy1 = new Cylinder();
                System.out.println("Curved Surface Area of Cylinder is: "   cy1.CSA());
                System.out.println("Total Surface Area of Cylinder is: "  cy1.TSA());
                System.out.println("Volume of Cylinder is: "  cy1.Volume());
            }
            case 4 -> {
                System.out.println("- - - - - Cone - - - - -");
                Cone co1 = new Cone();
                System.out.println("Slant height of Cone is: "   co1.slantHeight());
                System.out.println("Curved Surface Area of Cone is: "   co1.CSA());
                System.out.println("Total Surface Area of Cone is: "  co1.TSA());
                System.out.println("Volume of Cylinder is: "  co1.Volume());
            }
            case 5 -> {
                System.out.println("- - - - - Sphere - - - - -");
                Sphere s1 = new Sphere();
                System.out.println("Surface Area of Sphere is: "   s1.CSA());
                System.out.println("Volume of Sphere is: "  s1.Volume());
            }
            case 6 -> {
                System.out.println("- - - - - Hemisphere - - - - -");
                Sphere s1 = new Sphere();
                System.out.println("Curved Surface Area of HemiSphere is: "   s1.CSA()/2);
                System.out.println("Total Surface Area of HemiSphere is: "   s1.HemiTSA());
                System.out.println("Volume of HemiSphere is: "  s1.Volume()/2);
            }
        }
    }
}

CodePudding user response:

In my opinion, for beginners, you will need more clarification for them to be more understandable. Initially, can suggest several examples of a form of procedural programming without applying the principle of (OOP) in order for them to know the benefit of it.

To improve the current code I will divide it down into several parts:

1 - In the main:

One of the benefits of what will be said for beginners on the theoretical side is that it reduces repetition in each process. But when you see the main function, many of the sentences are repeated and do not apply a beautiful principle, which is (DRY: "do not repeat yourself").

Note the repetition of the following sentences in each geometric shape such as the name and (Curved Surface Area of) and (Volume of) If there was a function for that and the exceptions were implemented according to the shape, it would be better to apply the joins or another manual technique to String.

2 - In the classes:

  • There are things that are incomprehensible to a beginner if the first time he sees (OOP) such as private.
  • The OOP principle understands that every geometric shape or more precisely every object has its own values ​​so why use (final) I remember that it is used only in constants but then ready-made examples were used like (Math.PI) in equations.
  • The naming method for those who have forgotten why it stands for (CSA) if it is written completely, which is (curved_Surface_Area) will be clearer for the beginner and focus more on understanding the principles of this method and not be confused by the meaning of the abbreviation, or if it remains, a comment will be added.

As a whole, it is considered a beautiful and different example from the famous to the beginners, and I think they will remember it when applying the principle in future works, good luck :)

  • Related