Need help making a UML class diagram showing the type of class relationships for my program: These
Fantastic news! We've Found the answer you've been seeking!
Question:
Need help making a UML class diagram showing the type of class relationships for my program: These are the classes I have:
- Circle.java
- Cone.java
- Cube.java
- Cylinder.java
- Rectangle.java
- Sphere.java
- Square.java
- Torus.java
- Triangle.java
- Shape.java
- ThreeDimensionalShape.java
- TwoDimensionalShape.java
- main.java
Circle.java
// implementation of Circle class class Circle implements TwoDimensionalShape { // a variable int radius; // class constructor // parameter : radius public Circle(int radius) { //when class object is created, a variable is initialized. this.radius = radius; } // method to get area of the Circle public double getArea() { // area = pi x radius x radius // pi = 3.14 return 3.14 * radius * radius; } @Override // method to return the dimension of the shape public int numberOfDimensions(){ /* dimension is equal to because the square is a two-dimensional shape. hence, return 2. */ return 2; } }
Cone.java
// implementation of the cone class class Cone implements ThreeDimensionalShape{ // parameter: radius and height // variable declaration int radius, height; // class constructor public Cone(int radius, int height) { // when class object is created, the variables are initialized this.radius = radius; this.height = height; } // method to calculate the volume of the code public double getVolume() { // volume = pi x radius x radius x (height/3) return (3.14 * radius * radius)*(height/3); } @Override // method to return the number of dimension public int numberOfDimensions() { // returned dimension is 3 return 3; } }
Cube.java
// Cube class implementation class Cube implements ThreeDimensionalShape{ int area; public Cube(int area) { this.area = area; } public double getVolume() { return area * Math.sqrt(area); } @Override public int numberOfDimensions() { return 3; } }
Cylinder.java
// implementation of the Cylinder class class Cylinder implements ThreeDimensionalShape { // parameter: radius and height // variable declaration int radius, height; // class constructor public Cylinder(int radius, int height) { this.radius = radius; this.height = height; } // method to calculate the volume of the cylinder public double getVolume() { // return the volume of the cylinder return 3.14 * radius * radius * height; } // return the dimension @Override public int numberOfDimensions() { // returned 3 return 3; } }
Rectangle.java
// implementation of the Rectangle class class Rectangle implements TwoDimensionalShape{ int width, length; // method to get area of the Rectangle public double getArea() { //return the area of the rectangle return width * length; } // class constructor public Rectangle(int width, int length) { this.width = width; this.length = length; } //return the dimension of the rectangle @Override public int numberOfDimensions() { // return two return 2; } }
Sphere.java
// implementation of the Sphere class class Sphere implements ThreeDimensionalShape { // parameter: radius // variable declaration int radius; //class constructor public Sphere(int radius) { this.radius = radius; } // method to get the volume public double getVolume() { // volume = (4/3) x 3.14 x radius x radius x radius return (4/3) * 3.14 * radius*radius*radius; } // return the number of dimensions @Override public int numberOfDimensions() { // return dimension as three return 3; } }
Square.java
// implement a square class class Square implements TwoDimensionalShape{ // variable declaration int squareSide; public Square(int squareSide) { this.squareSide = squareSide; } public double getArea() { return squareSide * squareSide; } @Override public int numberOfDimensions() { return 2; } }
Torus.java
// Implementation of Torus class class Torus implements ThreeDimensionalShape { // parameter: majorRadius, minorRadius // variable declaration int majorRadius, minorRadius; // class constructor public Torus(int majorRadius, int minorRadius) { this.majorRadius = majorRadius; this.minorRadius = minorRadius; } //method to get the Torus volume public double getVolume() { // volume = (pi x minorRadius x minorRadius) x (2 x 3.14 x majorRadius) return (3.14 * minorRadius * minorRadius) * (2 * 3.14 * majorRadius); } // return the number of dimensions @Override public int numberOfDimensions() { // return dimension as three return 3; } }
Triangle.java
class Triangle implements TwoDimensionalShape { // parameter: height, base // variable declaration int height, base; //method to get the area public double getArea() { // area = (height x base)/2 return (height * base)/2; } // class constructor public Triangle(int height, int base) { this.height = height; this.base = base; } // return number of dimensions @Override public int numberOfDimensions() { // return dimension as two return 2; } }
Shape.java
// implementation of shape interface interface Shape { // int type int numberOfDimensions(); }
ThreeDimensionalShape.java
// implementation of ThreeDimensionalShape interface interface ThreeDimensionalShape extends Shape { }
TwoDimensionalShape.java
// implementation of TwoDimensionalShape interface interface TwoDimensionalShape extends Shape { }
main.java
import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Scanner; class Main { // ================================================ // function to validate user input public static int validateUserInput(){ // declare an integer variable int input; boolean k = true; Scanner s = new Scanner(System.in); // use of do while to validate user input and .. // .. continuously take input until a valid input is given do{ // hasNextInt() checks if input is integer or not and returns boolean value while (!s.hasNextInt()){ // capture the above input into userInput variable String userInput = s.next(); // display message to ask for valid input System.out.printf("\"%s\" is invalid. ", userInput); System.out.println("please provide a valid input as a positive Integer: "); } // take the user input input = s.nextInt(); // if input is less than zero, it is converted to positive number if(input<0){ System.out.println("Entered number is negative...Converted it to positive...."); input = (-1)*input; } }while(!k); //return the user input return input; } // ======================================================= public static void main(String[] args) { Scanner sc = new Scanner(System.in); char choice; //Set the format for the display of time and date DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd, yyyy hh:mm a"); //LocalDateTime.now() method returns the instance of LocalDateTime class LocalDateTime now = LocalDateTime.now(); System.out.println("*********Welcome to the Java OO Shapes Program **********"); do { 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"); int ch = sc.nextInt(); switch (ch){ case 1: System.out.println("You have selected a Circle"); System.out.println("Enter radius"); // call the function to validate user input radius int circleRadius = validateUserInput(); Circle circle = new Circle(circleRadius); System.out.println("The area of the Circle is: " + circle.getArea()); break; case 2: System.out.println("You have selected a Rectangle"); System.out.println("Enter Width"); // call the function to validate user input(rectangle width) int rectWidth = validateUserInput(); System.out.println("Enter Length"); // call the function to validate user input(rectangle length) int rectLength = validateUserInput(); Rectangle rectangle = new Rectangle(rectWidth, rectLength); System.out.println("The area of the Rectangle is: " + rectangle.getArea()); break; case 3: System.out.println("You have selected a Square"); System.out.println("Enter Side"); // call the function to validate user input(side of square) int squareSide = validateUserInput(); Square square = new Square(squareSide); System.out.println("The area of the Square is: " + square.getArea()); break; case 4: System.out.println("You have selected a Triangle"); System.out.println("Enter Height"); // call the function to validate user input(height of a triangle) int triangleHeight = validateUserInput(); System.out.println("Enter Base"); // call the function to validate user input(base of a triangle) int triangleBase = validateUserInput(); Triangle triangle = new Triangle(triangleHeight, triangleBase); System.out.println("The area of the Triangle is: " + triangle.getArea()); break; case 5: System.out.println("You have selected a Sphere"); System.out.println("Enter Radius"); // call the function to validate user input(radius of sphere) int sphereRadius = validateUserInput(); Sphere sphere = new Sphere(sphereRadius); System.out.println("The Volume of the Sphere is: " + sphere.getVolume()); break; case 6: System.out.println("You have selected a Cube"); System.out.println("Enter area"); // call the function to validate user input(area of a cube) int cubeArea = validateUserInput(); Cube cube = new Cube(cubeArea); System.out.println("The Volume of the Cube is: " + cube.getVolume()); break; case 7: System.out.println("You have selected a Cone"); System.out.println("Enter Radius"); // call the function to validate user input(radius of a cone) int coneRadius = validateUserInput(); System.out.println("Enter Height"); // call the function to validate user input(height of a cone) int coneHeight = validateUserInput(); Cone cone = new Cone(coneRadius, coneHeight); System.out.println("The Volume of the Cone is: " + cone.getVolume()); break; case 8: System.out.println("You have selected a Cylinder"); System.out.println("Enter Radius"); // call the function to validate user input(radius of a cylinder) int cylinderRadius = validateUserInput(); System.out.println("Enter Height"); // call the function to validate user input(height of a cylinder) int cylinderHeight = validateUserInput(); Cylinder cylinder = new Cylinder(cylinderRadius, cylinderHeight); System.out.println("The Volume of the Cylinder is: " + cylinder.getVolume()); break; case 9: System.out.println("You have selected a Torus"); System.out.println("Enter MinorRadius"); // call the function to validate user input(MinorRadius of a Torus) int minorRadius = validateUserInput(); System.out.println("Enter MajorRadius"); // call the function to validate user input(MajorRadius of a Torus) int majorRadius = validateUserInput(); Torus torus = new Torus(majorRadius, minorRadius); System.out.println("The Volume of the Torus is: " +torus.getVolume()); break; case 10: System.out.println("Exiting........"); System.out.print("Thanks for using the program. Today is "); System.out.println(dtf.format(now)); System.exit(0); break; default: System.out.println("Exiting........"); } System.out.println("Would you like to continue? (Y or N)"); choice = sc.next().charAt(0); }while (choice == 'Y' || choice == 'y'); System.out.print("Thanks for using the program. Today is "); System.out.println(dtf.format(now)); } }
Related Book For
Posted Date: