1

I'm working on a school project and am pretty hung up. I'm trying to implement methods from the shape2d class (circle, specifically) to get a user input "radius" for a circle to out put the area of circle. If I could figure out ONE SHAPE, I could apply to the rest.


package shape;

import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import shape.shape2d.Circle;

public class shapeMenu {
    public static void main(String[] args) {
                Date date = new Date(); 
                Calendar cal = Calendar.getInstance();
         
        Scanner in = new Scanner(System.in);                
        System.out.println("*********Welcome to the Java OO Shapes Program **********");
                System.out.println("Select from the menu below:");
                System.out.println("1. Construct a Circle");
                System.out.println("2. Construct a Rectangle");
                System.out.println("3. Construct a Square");
                System.out.println("4. Construct a Triangle");
                System.out.println("5. Construct a Sphere");
                System.out.println("6. Construct a Cube");
                System.out.println("7. Construct a Cone");
                System.out.println("8. Construct a Cylinder");
                System.out.println("9. Construct a Torus");
                System.out.println("10. Exit the program");
        
        boolean quit = false;
        int selection;
        do {
            selection = in.nextInt();
            switch (selection) {
            case 1: //1. Construct a Circle
                System.out.println("What is the radius?");
                                double rad = Double.parseDouble(in.nextLine());
                                Circle c = shape2d.Circle(rad);             
                                System.out.println("The area of the circle is " + c + ".");
                break;
            case 2: //2. Construct a Rectangle
                System.out.println("What is the length?");
                                
                                System.out.println("What is the width?");
                // do something...
                break;
            case 3: //3. Construct a Square
                System.out.println("What is the length?");
                // do something...
                break;
            case 4: //4. Construct a Triangle
                System.out.println("What is the base length?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;
            case 5: //5. Construct a Sphere
                System.out.println("What is the radius?");
                // do something...
                break;                                
                        case 6: //6. Construct a Cube
                System.out.println("What is the length?");
                // do something...
                break;
                        case 7: //7. Construct a Cone
                System.out.println("What is the radius?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;                                                                                               
                        case 8: //8. Construct a Cylinder
                System.out.println("What is the radius?");
                                
                                System.out.println("What is the height?");
                // do something...
                break;                                
                         case 9: //9. Construct a Torus
                System.out.println("What is the radius major?");
                                
                                System.out.println("What is the radius minor?");
                // do something...
                break;
                                                                
            case 0:  //10. Exit the program
                quit = true;
                break;
            default:
                System.out.println("Invalid choice.");
            }
        } while (!quit);
        System.out.println("Thanks for using the program. Today is " + date + ".");
    }
}

This gets the menu started, and I have created shape2d and shape3d classes with individual shape (circle, triangle, etc) within them to get area and volume.

package shape;

public class shape2d extends shapeMenu {
    public double a;

    public static class Circle extends shape2d{
        private double r;
        public Circle(double r){
            this.r = r;
            
        }
        public void area(double r){
            a = Math.PI * Math.pow(r, 2);
        }
    }    

    public class Rectangle extends shape2d{
        private double l;
        private double w;
        public Rectangle(double l, double w){
            a = l * w;
        }
    }
    
    public class Square extends shape2d{
        private double l;
        public Square(double l){
            a =  Math.pow(l, 2);
        }

    }
    public class Triangle extends shape2d{
        public double b;
        public double h;
        public Triangle(double b, double h){
            a = b * h / 2;
        }
    }
}   

I'm kind of burned out right now....will get some sleep and come back to it. Thank you for looking!

0 Answers0