Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Last active October 27, 2021 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jananpatel2002/8c03f6d187bfb7b9a75b528174fe67ef to your computer and use it in GitHub Desktop.
Save jananpatel2002/8c03f6d187bfb7b9a75b528174fe67ef to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 10/25/2021
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 6
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Creating classes to get areas and perimeters!
*/
package shapes;
public class Circle extends Shape {
private double radius;
public Circle() {
this(1, "white", false);
}
public Circle(double radius) {
this(radius, "white", false);
}
public Circle(double radius, String color, boolean filled) {
super(color, filled);
setRadius(radius);
}
/** Return radius */
public double getRadius() {
return radius;
}
/** Set a new radius */
public void setRadius(double radius) {
if (radius < 0)
throw new InvalidRadiusException(radius);
this.radius = radius;
}
/** Return area */
public double getArea() {
return this.radius * this.radius * Math.PI;
}
/** Return diameter */
public double getDiameter() {
return 2 * this.radius;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * this.radius * Math.PI;
}
public String toString() {
String retval = "";
retval += "Circle: Radius = " + this.radius + "\n";
retval += super.toString();
return retval;
}
}
@SuppressWarnings("serial")
class InvalidRadiusException extends RuntimeException {
private double radius;
/** Construct an exception */
public InvalidRadiusException(double radius) {
super("Invalid radius " + radius);
this.radius = radius;
}
/** Return the radius */
public double getRadius() {
return radius;
}
}
/*
* Name: Janan Patel
* Date: 10/25/2021
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 6
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Creating classes to get areas and perimeters!
*/
package shapes;
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle() {
}
public Rectangle(double width, double height) {
this(width, height, "white", false);
}
public Rectangle(double width, double height, String color, boolean filled) {
super(color, filled);
this.width = width;
this.length = height;
}
/** Return width */
public double getWidth() {
return width;
}
/** Set a new width */
public void setWidth(double width) {
this.width = width;
}
/** Return height */
public double getLength() {
return length;
}
/** Set a new height */
public void setHeight(double height) {
this.length = height;
}
/** Return area */
public double getArea() {
return this.width * this.length;
}
/** Return perimeter */
public double getPerimeter() {
return 2 * (width + length);
}
public String toString() {
return "Rectangle: " + this.length + " x " + this.width + "\n" +
super.toString();
}
}
/*
* Name: Janan Patel
* Date: 10/25/2021
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 6
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Creating classes to get areas and perimeters!
*/
package shapes;
import java.util.*;
abstract public class Shape {
private String color;
private boolean filled;
private Date dateCreated;
/** Construct a default geometric object */
public Shape() {
this("white", false);
}
/**
* Construct a geometric object with the specified color and filled value
*/
public Shape(String color, boolean filled) {
this.dateCreated = new Date();
this.color = color;
this.filled = filled;
}
/** Return color */
public String getColor() {
return color;
}
/** Set a new color */
public void setColor(String color) {
this.color = color;
}
/**
* Return filled. Since filled is boolean, its get method is named isFilled
*/
public boolean isFilled() {
return filled;
}
/** Set a new filled */
public void setFilled(boolean filled) {
this.filled = filled;
}
/** Get dateCreated */
public Date getDateCreated() {
return dateCreated;
}
/** Return a string representation of this object */
public String toString() {
return "Shape: created on " + dateCreated + "\nColor: " + color
+ " and Filled: " + filled;
}
abstract public double getArea();
abstract public double getPerimeter();
}
/*
* Name: Janan Patel
* Date: 10/25/2021
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 6
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Creating classes to get areas and perimeters!
*/
package shapes;
import java.util.Scanner;
public class TestShapes {
private final static String TITLE = "Test Shapes V1.0";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
// **********************************************
// Put as many methods you need here
private static String getFirstCharacter(String str) {
str = str.trim().toUpperCase();
return str.isEmpty() ? "" : str.substring(0, 1);
}
// **********************************************
private static void process(Scanner sc, String args[]) {
System.out.print("Enter Shape to create [C]ircle, [R]ectangle, [T]riangle : ");
String strShape = getFirstCharacter(sc.nextLine());
Shape shape = null;
switch (strShape) {
case "C": {
System.out.print("Enter radius: ");
double radius = sc.nextDouble();
System.out.print("Enter color: ");
String color = sc.next();
System.out.print("Enter is filled: ");
boolean isFilled = sc.nextBoolean();
sc.nextLine();
shape = new Circle(radius, color, isFilled);
break;
}
case "R": {
System.out.print("Enter length & width: ");
double length = sc.nextDouble();
double width = sc.nextDouble();
System.out.print("Enter color: ");
String color = sc.next();
System.out.print("Enter is filled: ");
boolean isFilled = sc.nextBoolean();
sc.nextLine();
shape = new Rectangle(length, width, color, isFilled);
break;
}
case "T": {
System.out.print("Enter the three sides: ");
double side1 = sc.nextDouble();
double side2 = sc.nextDouble();
double side3 = sc.nextDouble();
System.out.print("Enter color: ");
String color = sc.next();
System.out.print("Enter is filled: ");
boolean isFilled = sc.nextBoolean();
sc.nextLine();
shape = new Triangle(side1, side2, side3, color, isFilled);
break;
}
default:
System.out.println("Bad Shape Specified.");
break;
}
if (shape != null) {
System.out.println(shape);
System.out.printf("Perimeter = %.2f\n", shape.getPerimeter());
System.out.printf("Area = %.2f\n", shape.getArea());
}
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner sc, String prompt) {
System.out.print(prompt);
String doOver = sc.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method
public static void main(String args[]) {
System.out.println("Welcome to " + TITLE);
Scanner sc = new Scanner(System.in);
do {
process(sc, args);
} while (doThisAgain(sc, CONTINUE_PROMPT));
sc.close();
System.out.println("Thank you for using " + TITLE);
}
}
/*
* public static void main(String[] args) { Shape s1 = new Circle(2);
* System.out.println("A circle " + s1.toString());
* System.out.println("The color is " + s1.getColor());
* System.out.println("The area is " + s1.getArea());
* System.out.println("The perimeter is " + s1.getPerimeter()); Circle circle =
* (Circle)s1; System.out.println("The radius is " + circle.getRadius());
* System.out.println("The diameter is " + circle.getDiameter()); Shape s2 = new
* Rectangle(2, 4); System.out.println("\nA rectangle " + s2);
* System.out.println("The color is " + s2.getColor());
* System.out.println("The area is " + s2.getArea());
* System.out.println("The perimeter is " + s2.getPerimeter()); Rectangle rect =
* (Rectangle)s2; System.out.println("The length is " + rect.getLength());
* System.out.println("The diameter is " + rect.getWidth()); //Shape s3 = new
* Triangle(10, 8, 3); //System.out.println("\nA triangle " + s3);
* //System.out.println("The color is " + s3.getColor());
* //System.out.println("The area is " + s3.getArea());
* //System.out.println("The perimeter is " + s3.getPerimeter()); //Triangle tri
* = (Triangle)s3; //System.out.println("Side 1 is " + tri.getSide1());
* //System.out.println("Side 2 is " + tri.getSide2());
* //System.out.println("Side 3 is " + tri.getSide3()); Shape s4 = null; switch
* ((int)(3 * Math.random())) { case 0: s4 = new Rectangle(2, 4); break; case 1:
* s4 = new Circle(5); break; case 2: //s4 = new Triangle(1, 2, 2); break; }
* System.out.println(s4); if (s4 instanceof Circle) {
* System.out.println("Diameter " + ((Circle)s4).getDiameter()); } else
* System.out.println("Not a circle");
*
* }
*/
/*
* Name: Janan Patel
* Date: 10/25/2021
* Course Number: 220
* Course Name: Data Structures
* Problem Number: 6
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Creating classes to get areas and perimeters!
*/
package shapes;
public class Triangle extends Shape {
private double side1 = 1.0;
private double side2 = 1.0;
private double side3 = 1.0;
public boolean isValid(double side1, double side2, double side3) {
return (((side1 + side2 > side3) && (side2 + side3 > side1) && (side1 + side3 > side2))
&& ((side1 > 0) && (side2 > 0) && (side3 > 0)));
}
public Triangle() {
this(1.0, 1.0, 1.0);
}
public Triangle(double side1, double side2, double side3) {
setSide1(side1);
setSide2(side2);
setSide3(side3);
}
public Triangle(double side1, double side2, double side3, String color, boolean filled) {
super(color, filled);
if (isValid(side1, side2, side3)) {
setSide1(side1);
setSide2(side2);
setSide3(side3);
}
System.out.print("Invalid Triangle sides: Please try again with valid sides. Here are examples of valid sides: (1,1,1) (5,12,13) etc"
+ "\nThe program will default the values to 1 for you so you can try again");
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
public void setSide1(double side1) {
this.side1 = side1;
}
public void setSide2(double side2) {
this.side2 = side2;
}
public void setSide3(double side3) {
this.side3 = side3;
}
@Override
public double getArea() {
double semiPerimeter = (getSide1() + getSide2() + getSide3()) / 2;
return Math.sqrt((semiPerimeter)
* ((semiPerimeter - getSide1()) * (semiPerimeter - getSide2()) * (semiPerimeter - getSide3())));
}
@Override
public double getPerimeter() {
return (getSide1() + getSide2() + getSide3());
}
@Override
public String toString() {
return "\nTriangle: Side 1 = " + getSide1() + " Side 2 = " + getSide2() + " Side 3 = " + getSide3() + "\n"
+ super.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment