In: Computer Science
what i have:
import java.util.Scanner;
public class examples1 {
public static void main(String[] args) {
Square shade = new Square(getside());
System.out.println("Your chooses are:");
System.out.println("\nCircle");
System.out.println("Triangle");
System.out.println("Rectangle");
System.out.println("Square");
boolean moreShapes = true;
boolean userChoice = true;
while(moreShapes) {
Scanner shapes = new Scanner (System.in);
System.out.println("\nWhat shape do you want:");
String yourShape = shapes.nextLine();
if (userChoice = shade != null) {
shade.getSide();
}
System.out.println("\nWhat are two size paramters of the shape you choose:");
String yourParameter = shapes.nextLine();
System.out.println("\nYour shape is: " + yourShape + " \nYour shape parameters are:" + " " + yourParameter);
if (yourShape.equalsIgnoreCase("quit"))
moreShapes = false;
else {
if (yourParameter.equalsIgnoreCase("quit"));
}
}
//Rectangle ruby = new Rectangle(12, 15);
//Triangle trudy = new Triangle(15, 25, 0.5);
//Circle curty = new Circle(5, 3.14159);
//shady.calculateArea1();
//ruby.calculateArea2();
//trudy.calculateArea3();
//curty.calculateArea4();
}
private static int getside() {
// TODO Auto-generated method stub
return 0;
}
class Square {
int side;
//constructor
Square(int newside) {
setSide(newside);
}
// accessor
int getSide() {return side;}
void setSide(int newside) { side = newside; }
// Method to calculate Area of Square
void calculateArea1() {
int Area1;
Area1 = side * side;
System.out.println("Area of a Square is: " + Area1);
}
}
}
directions:
•Implement classes Square, Rectangle, Triangle, and Circle:
–With attributes for size parameters which determine area
–As well as an attribute for the area
–Also constructor, mutator, and accessor methods
–And toString and equals methods
•Implement a main program which repeatedly:
–Asks the user to choose a shape
–Then asks for the size parameters for 2 objects of that shape
–Instantiates the objects and sets the area for each
–Use the toString method to output the formatted data
•e.g., “Triangle 1 has base=7, height=5, and area=17.5”
–Then outputs a statement describing “equality”
•e.g., “The triangles have the same area, but different base and height”
•or, “The rectangles have the same length, but different area”
•Keep “playing” until the user indicates they want to stop
•Submit by 10/12/20
Source Code:
Square.java File:
class Square {
int side;
//constructor
Square(int newside) {
setSide(newside);
}
// accessor
int getSide() {
return side;
}
void setSide(int newside) {
side = newside;
}
// Method to calculate Area of Square
double calculateArea() {
return side * side;
}
@Override
public String toString() {
return "Side= " + side + " and Area
= " + calculateArea();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return
true;
}
if (obj == null) {
return
false;
}
if (getClass() != obj.getClass())
{
return
false;
}
Square other = (Square) obj;
return side == other.side;
}
}
Rectangle.java File:
import java.util.Objects;
class Rectangle {
int width,length;
//constructor
Rectangle(int width, int length) {
setSide(width, length);
}
// accessor
int getWidth() {
return width;
}
int getLength() {
return length;
}
void setSide(int width, int length) {
this.width = width;
this.length = length;
}
// Method to calculate Area of Rectangle
int calculateArea() {
return width * length;
}
@Override
public String toString() {
// TODO Auto-generated method
stub
return "Width= " + this.width + ",
Length= " + this.length + " and Area = " + calculateArea();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return
true;
}
if (obj == null) {
return
false;
}
if (getClass() != obj.getClass())
{
return
false;
}
Rectangle other = (Rectangle)
obj;
if(length == other.length
&& width == other.width) {
System.out.println("Rectangles have same width, length and
area");
return
true;
}
else if(length == other.length)
{
if(this.calculateArea() == other.calculateArea()) {
System.out.println("Rectangles have same length
and area, but different width");
return true;
}
}
else if (this.width == other.width)
{
if(this.calculateArea() == other.calculateArea()) {
System.out.println("Rectangles have same width
and area, but different length");
return true;
}
}
else if(this.calculateArea() ==
other.calculateArea()) {
System.out.println("Rectangles have different width and length, but
same area");
return
true;
}
System.out.print("Rectangles are
different");
return false;
}
}
Triangle.java File:
import java.util.Objects;
class Triangle {
int breadth, height;
double half = 0.5;
//constructor
Triangle(int breadth, int height) {
this.setSide(breadth,
height);
}
// accessor
int getBreadth() {
return breadth;
}
int getHeight() {
return breadth;
}
void setSide(int breadth, int height) {
this.breadth = breadth;
this.height = height;
}
// Method to calculate Area of Square
double calculateArea() {
return breadth * height *
half;
}
@Override
public String toString() {
return "base= " + this.breadth + ",
height= " + this.height + " , and area= " +
this.calculateArea();
}
@Override
public int hashCode() {
return Objects.hash(breadth,
height);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return
true;
}
if (obj == null) {
return
false;
}
if (getClass() != obj.getClass())
{
return
false;
}
Triangle other = (Triangle)
obj;
if (breadth == other.breadth
&& height == other.height) {
System.out.println("Triangles have same base and height");
return
true;
}
else if (breadth == other.breadth)
{
if(this.calculateArea() == other.calculateArea()) {
System.out.println("Triangles have same base and
area, but different height");
}
else {
System.out.println("Triangles have same base,
but different height and area");
}
return
true;
}
else if (height == other.height)
{
if(this.calculateArea() == other.calculateArea()) {
System.out.println("Triangles have same height
and area, but different base");
}
else {
System.out.println("Triangles have same height,
but different base and area");
}
return
true;
}
else if(this.calculateArea() ==
other.calculateArea()) {
System.out.println("Triangles have same area, but different base
and height");
return
true;
}
System.out.println("Triangles are
NOT equal");
return false;
}
}
Circle.java File:
import java.util.Objects;
class Circle {
int radius;
double pi = 3.14159;
// constructor
Circle(int radius) {
setSide(radius);
}
// accessor
int getRadius() {
return radius;
}
void setSide(int radius) {
this.radius = radius;
}
// Method to calculate Area of Square
double calculateArea() {
return radius * radius * pi;
}
@Override
public String toString() {
return "Radius = " + this.radius +
" and Area = " + calculateArea();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return
true;
}
if (obj == null) {
return
false;
}
if (getClass() != obj.getClass())
{
return
false;
}
Circle other = (Circle) obj;
if( radius == other.radius) {
System.out.println("Circles have same radius and area");
return
true;
}
else {
System.out.println("Circles are NOT equal");
return
false;
}
}
}
examples1.java File:
import java.util.Scanner;
public class examples1 {
public static void main(String[] args) {
Scanner keyboard = new
Scanner(System.in);
boolean moreShapes = true;
while (moreShapes) {
System.out.println("Your chooses are:");
System.out.println("\nCircle");
System.out.println("Triangle");
System.out.println("Rectangle");
System.out.println("Square");
System.out.println("Quit");
System.out.println("\nWhat shape do you want:");
String yourShape
= keyboard.nextLine();
int side, width,
length, radius, base, height;
switch(yourShape.toLowerCase()) {
case "circle":
System.out.print("Enter
radius of Circle 1: ");
radius =
keyboard.nextInt();
keyboard.nextLine();
Circle circle1 = new
Circle(radius);
System.out.print("Enter
radius of Circle 2: ");
radius =
keyboard.nextInt();
keyboard.nextLine();
Circle circle2 = new
Circle(radius);
//Use the toString method to
output the formatted data
System.out.println("Circle 1:
" + circle1.toString());
System.out.println("Circle 2:
" + circle2.toString());
circle1.equals(circle2);
break;
case "triangle":
System.out.print("Enter base
of Triangle 1: ");
base =
keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter
height of Triangle 1: ");
height =
keyboard.nextInt();
keyboard.nextLine();
Triangle triangle1 = new
Triangle(base, height);
System.out.print("Enter base
of Triangle 2: ");
base =
keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter
height of Triangle 2: ");
height =
keyboard.nextInt();
keyboard.nextLine();
Triangle triangle2 = new
Triangle(base, height);
//Use the toString method to
output the formatted data
System.out.println("Triangle
1: " + triangle1.toString());
System.out.println("Triangle
2: " + triangle2.toString());
triangle1.equals(triangle2);
break;
case "rectangle":
System.out.print("Enter width
of Rectangle 1: ");
width =
keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter
length of Rectangle 1: ");
length =
keyboard.nextInt();
keyboard.nextLine();
Rectangle rectangle1 = new
Rectangle(width, length);
System.out.print("Enter width
of Rectangle 2: ");
width =
keyboard.nextInt();
keyboard.nextLine();
System.out.print("Enter
length of Rectangle 2: ");
length =
keyboard.nextInt();
keyboard.nextLine();
Rectangle rectangle2 = new
Rectangle(width, length);
//Use the toString method to
output the formatted data
System.out.println("Rectangle
1: " + rectangle1.toString());
System.out.println("Rectangle
2: " + rectangle2.toString());
rectangle1.equals(rectangle2);
break;
case "square":
System.out.print("Enter side
of square 1: ");
side =
keyboard.nextInt();
keyboard.nextLine();
Square sq1 = new
Square(side);
System.out.print("Enter side
of square 2: ");
side =
keyboard.nextInt();
keyboard.nextLine();
Square sq2 = new
Square(side);
//Use the toString method to
output the formatted data
System.out.println("Square 1:
" + sq1.toString());
System.out.println("Square 2:
" + sq2.toString());
if(sq1.equals(sq2)) {
System.out.println("Square 1 and Square 2 are equal");
}
else {
System.out.println("Square 1 and Square 2 are NOT equal");
}
break;
case "quit":
moreShapes = false;
System.out.println("Good Bye,
see you soon!");
break;
default:
System.out.println("Invalid
choice, retry!");
break;
}
}
}
}
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************