Commit 771f9ac3 authored by MostafaRahmati's avatar MostafaRahmati

final version

parents
Pipeline #5951 failed with stages
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/../../../../../../../:\Users\Mostafa\Desktop\session5\toDo2\.idea/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="openjdk-15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/toDo2.iml" filepath="$PROJECT_DIR$/toDo2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import java.util.Objects;
/**
* This class represents a simulation for a shape with uncountable sides (Circle)
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor for this class.
*
* @param radius radius of circle
*/
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculatePerimeter() {
return 2 * 3.14 * this.radius;
}
@Override
public double calculateArea() {
return this.radius * this.radius * 3.14;
}
@Override
public void draw() {
System.out.println("Type: Circle\nPerimeter: " + this.calculatePerimeter()+ "\nArea: "
+ this.calculateArea());
}
@Override
public String toString() {
return "Type: Circle\nRadius = " + this.radius;
}
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
if (!super.equals(object)) return false;
Circle circle = (Circle) object;
return Double.compare(circle.radius, radius) == 0;
}
@Override
public int hashCode() {
return Objects.hash(radius);
}
}
\ No newline at end of file
/**
* This piece of code will instantiate triangles, rectangles and circles, 2 of each
* and prints all necessary information about them
* related classes are designed with inheritance structure
*/
public class Main {
public static void main(String[] args) {
Paint paint = new Paint();
Triangle triangle1 = new Triangle(5.0, 4.0, 8.5);
Triangle triangle2 = new Triangle(1.0, 1.0, 1.0);
Rectangle rectangle1 = new Rectangle(14.0, 8.0, 8.0, 14.0);
Rectangle rectangle2 = new Rectangle(63.0, 63.0, 63.0, 63.0);
Circle circle1 = new Circle(12);
Circle circle2 = new Circle(8.6);
paint.addShape(triangle1);
paint.addShape(triangle2);
paint.addShape(rectangle1);
paint.addShape(rectangle2);
paint.addShape(circle1);
paint.addShape(circle2);
paint.drawAll();
paint.printAll();
}
}
\ No newline at end of file
import java.util.ArrayList;
/**
* This class will handle some methods related to add or draw or print shapes
*/
public class Paint {
private ArrayList<Shape> shapes = new ArrayList<>();
/**
* @param shape shape which you want to add to list
*/
public void addShape(Shape shape) {
this.shapes.add(shape);
}
/**
* simulation for drawing all shapes in the list
*/
public void drawAll() {
for (Shape shape : this.shapes) {
shape.draw();
}
}
/**
* Print mentioned information in session instructions about all shapes
*/
public void printAll() {
for (Shape shape : this.shapes) {
System.out.println(shape);
}
}
}
\ No newline at end of file
import java.util.ArrayList;
/**
* This class is an abstract class that represents a shape with multiple sides
* Polygons will inherit this
* Thus we imply all methods that are in common grounds through polygons
*/
public abstract class Polygon extends Shape {
private ArrayList<Double> sides;
/**
* Constructor for polygon class
* @param sides The sides of a polygon
*/
public Polygon(double ... sides) {
this.sides = new ArrayList<>();
for (double side : sides) {
this.sides.add(side);
}
}
/**
* Constructor for polygon class
* @param sides The sides of a polygon
*/
public Polygon(ArrayList<Double> sides) {
this.sides = sides;
}
@Override
public double calculatePerimeter() {
double perimeter=0;
for (double side: sides)
perimeter=perimeter+side;
return perimeter;
}
/**
* @return Sides of Polygon
*/
public ArrayList<Double> getSides() {
return sides;
}
}
/**
* This class represents a simulation for a polygon with 4 sides
*/
public class Rectangle extends Polygon {
/**
* Constructor for the class with multiple args
* @param sides multiple sides for this rectangle
*/
public Rectangle(double ...sides) {
super(sides);
}
@Override
public double calculateArea() {
double a = super.getSides().get(0);
double b = super.getSides().get(1);
double c= super.getSides().get(2);
if (a==b)
return b*c;
else if (b==c)
return a*c;
else
return a*b;
}
@Override
public void draw() {
System.out.println("Type: Rectangle\nPerimeter: " + this.calculatePerimeter()+ "\nArea: "
+ this.calculateArea());
}
@Override
public String toString() {
return "Type: Rectangle with sides: "+super.getSides();
}
@Override
public boolean equals(Object object) {
if (this == object) return true;
if (object == null || getClass() != object.getClass()) return false;
if (!super.equals(object)) return false;
Rectangle rectangle = (Rectangle) object;
return this.getSides().equals(rectangle.getSides());
}
/**
* @return true if the rectangle is a square
*/
public boolean isSquare() {
double a = super.getSides().get(0);
double b = super.getSides().get(1);
double c = super.getSides().get(1);
return (a == b && b==c);
}
}
\ No newline at end of file
/**
* An Abstract Class For Shape
* This class contains some abstract methods that the child classes will inherit
*/
public abstract class Shape {
/**
* @return perimeter of the shape
*/
public abstract double calculatePerimeter();
/**
* @return Area of the shape
*/
public abstract double calculateArea();
/**
* Abstract Method for printing properties of a shape
*/
public abstract void draw();
}
\ No newline at end of file
/**
* This class represents a simulation for a polygon with 3 sides
*/
public class Triangle extends Polygon {
/**
* Constructor for the class with multiple args
*
* @param sides multiple sides for this triangle
*/
public Triangle(double... sides) {
super(sides);
}
@Override
public double calculateArea() {
double a = super.getSides().get(0);
double b = super.getSides().get(1);
double c = super.getSides().get(2);
double s = this.calculatePerimeter() / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}
@Override
public void draw() {
System.out.println("Type: Triangle\nPerimeter: " + this.calculatePerimeter()+ "\nArea: "
+ this.calculateArea());
}
@Override
public String toString() {
return "Type: Triangle with sides: "+super.getSides();
}
/**
* @return boolean value of being equilateral
*/
public boolean isEquilateral() {
double a = super.getSides().get(0);
double b = super.getSides().get(1);
double c = super.getSides().get(2);
return (a == b && b == c);
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment