diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..26cd33e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +production +FarshidNooshi9831068.iml \ No newline at end of file diff --git a/FarshidNooshi9831068/FarshidNooshi9831068.txt b/FarshidNooshi9831068/FarshidNooshi9831068.txt new file mode 100644 index 0000000..2bf0902 --- /dev/null +++ b/FarshidNooshi9831068/FarshidNooshi9831068.txt @@ -0,0 +1,2 @@ +Name: Farshid Nooshi +Student ID: 9831068 \ No newline at end of file diff --git a/FarshidNooshi9831068/JalaliCalendar-1.3.1.jar b/FarshidNooshi9831068/JalaliCalendar-1.3.1.jar new file mode 100644 index 0000000..236f2b6 Binary files /dev/null and b/FarshidNooshi9831068/JalaliCalendar-1.3.1.jar differ diff --git a/FarshidNooshi9831068/src/lab4/Main.java b/FarshidNooshi9831068/src/lab4/Main.java new file mode 100644 index 0000000..188bd9b --- /dev/null +++ b/FarshidNooshi9831068/src/lab4/Main.java @@ -0,0 +1,40 @@ +// In The Name Of GOD +package lab4; + +import java.util.ArrayList; + +public class Main { + + public static void main(String[] args) { + VotingSystem votingSystem = new VotingSystem(); + Person[] p = new Person[]{new Person("s1", "e1"), new Person("s2", "e2"), new Person("s3", "e3"), new Person("s4", "e4")}; + ArrayList[] choices = new ArrayList[4]; + for (int i = 0; i < 4; i++) + choices[i] = new ArrayList<>(); + votingSystem.createVoting("q1", 0); + for (int i = 1; i <= 4; i++) + votingSystem.getVoting(0).createChoice("c" + i); + for (int i = 0; i < 4; i++) + choices[i].add("c" + (i + 1)); + for (int i = 0; i < 4; i++) + votingSystem.vote(0, p[i], choices[i]); + votingSystem.printResult(0); + System.out.println(); + votingSystem.createVoting("q2", 1); + for (int i = 1; i <= 4; i++) + votingSystem.getVoting(1).createChoice("c" + i); + for (int i = 0; i < 4; i++) + votingSystem.vote(1, p[i], choices[i]); + votingSystem.printResult(1); + System.out.println(); + votingSystem.printListOfVoting(); + System.out.println(); + votingSystem.printVoting(0); + votingSystem.printVoting(1); + System.out.println(); + votingSystem.printResult(0); + System.out.println(); + votingSystem.printResult(1); + System.out.println(); + } +} diff --git a/FarshidNooshi9831068/src/lab4/Person.java b/FarshidNooshi9831068/src/lab4/Person.java new file mode 100644 index 0000000..9a696aa --- /dev/null +++ b/FarshidNooshi9831068/src/lab4/Person.java @@ -0,0 +1,44 @@ +// In The Name Of GOD +package lab4; + +public class Person { + private final String firstName, lastName; + + /** + * + * @param firstName is the first name of person(voter) + * @param lastName is the last name of person(voter) + */ + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getLastName() { + return lastName; + } + + public String getFirstName() { + return firstName; + } + @Override + public String toString() { + return firstName + " " + lastName; + } + + /** + * checks if two objects of person type are equal + * @param obj is the object to check + * @return true if they're equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Person tmp = (Person) obj; + return tmp.getLastName().equals(lastName) && + tmp.getFirstName().equals(firstName); + } +} diff --git a/FarshidNooshi9831068/src/lab4/Vote.java b/FarshidNooshi9831068/src/lab4/Vote.java new file mode 100644 index 0000000..94cb202 --- /dev/null +++ b/FarshidNooshi9831068/src/lab4/Vote.java @@ -0,0 +1,59 @@ +// In The Name Of GOD +package lab4; + +import java.util.Objects; + +public class Vote { + private final Person person; + private final String date; + + + /** + * @param person is the person who votes + * @param date is the date of the vote + */ + public Vote(Person person, String date) { + this.person = person; + this.date = date; + } + + public Person getPerson() { + return person; + } + + public String getDate() { + return date; + } + + /** + * almost copied this part from here : + * https://www.sitepoint.com/how-to-implement-javas-hashcode-correctly/ + * + * @return an integer representing the hash value of this vote + */ + @Override + public int hashCode() { + return Objects.hash(person, date); + } + + /** + * checks if two objects are equal + * @param obj is the object to be checked + * @return true if the objects are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Vote tmp = (Vote) obj; + return tmp.date.equals(date) && + tmp.person.equals(person); + } + + @Override + public String toString() { + return person.toString() + " " + date; + } +} diff --git a/FarshidNooshi9831068/src/lab4/Voting.java b/FarshidNooshi9831068/src/lab4/Voting.java new file mode 100644 index 0000000..f9cb5a4 --- /dev/null +++ b/FarshidNooshi9831068/src/lab4/Voting.java @@ -0,0 +1,107 @@ +// In The Name Of GOD +package lab4; + +import ir.huri.jcal.JalaliCalendar; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class Voting { + private final int type; + private final String question; + private ArrayList voters; + private HashMap> choices; + + /** + * + * @param type is the type of the voting + * @param question is the question of the voting + */ + public Voting(int type, String question) { + this.type = type; + this.question = question; + voters = new ArrayList<>(); + choices = new HashMap<>(); + } + + public String getQuestion() { + return question; + } + + /** + * @param person is a person to get checked + * @return true if voters arrayList doesn't contain this voter + */ + private boolean valid(Person person) { + for (Person it : voters) + if (person.equals(it)) + return false; + return true; + } + + /** + * this method votes for someone + * + * @param person is the person who wanna attend the voting + * @param choices is the persons choices + */ + public void vote(Person person, ArrayList choices) { + if (choices.size() > 1 && type == 1) { + System.out.println("You should choose one choice in this voting"); + return; + } + + if (!valid(person)) { + System.out.println("person already voted!"); + return; + } + + voters.add(person); + JalaliCalendar date = new JalaliCalendar(); + for (String it : choices) + this.choices.get(it).add(new Vote(person, date.toString())); + } + + /** + * this method is a getter for voters + * + * @return the voters ArrayList + */ + public ArrayList getVoters() { + return voters; + } + + /** + * this method prints a single voting in the program by telling the voters of each option & number of votes for each choice + */ + public void printVotes() { + System.out.println("Printing the votes"); + { + System.out.println(voters.size() + " is the number of voters"); + for (String it : choices.keySet()) { + System.out.println("choice " + it + " has : " + choices.get(it).size() + " votes"); + System.out.println("Voters are : "); + for (Vote vote : choices.get(it)) { + System.out.print(vote + " "); + } + System.out.println(); + } + } + } + + public HashMap> getChoices() { + return choices; + } + + /** + * this method adds a new choice + * + * @param option is a new choice to add + */ + public void createChoice(String option) { + if (choices.containsKey(option)) + System.out.println("choice already exists!"); + else + choices.put(option, new HashSet<>()); + } +} diff --git a/FarshidNooshi9831068/src/lab4/VotingSystem.java b/FarshidNooshi9831068/src/lab4/VotingSystem.java new file mode 100644 index 0000000..e5e0329 --- /dev/null +++ b/FarshidNooshi9831068/src/lab4/VotingSystem.java @@ -0,0 +1,80 @@ +// In The Name Of GOD +package lab4; + +import java.util.ArrayList; + +public class VotingSystem { + private ArrayList votingList; + + /** + * a simple constructor for this class + */ + public VotingSystem() { + votingList = new ArrayList<>(); + } + + /** + * this class creates a new voting + * + * @param question := question of the problem + * @param type := type of the question, 0 -> single choice, 1 -> multiple choice + */ + public void createVoting(String question, int type) { + votingList.add(new Voting(type, question)); + } + + /** + * prints all of the voting with their number and questions + */ + public void printListOfVoting() { + int idx = 1; + for (Voting voting : votingList) + System.out.println("" + idx++ + ") " + voting.getQuestion()); + } + + /** + * prints the results of a voting + * + * @param idx is an index for the voting + */ + public void printResult(int idx) { + votingList.get(idx).printVotes(); + } + + /** + * prints the specific voting with their choices + * + * @param idx is the index of the voting + */ + public void printVoting(int idx) { + System.out.println("" + (idx + 1) + ") " + votingList.get(idx).getQuestion()); + for (String it : votingList.get(idx).getChoices().keySet()) + System.out.println(it); + System.out.println(); + } + + /** + * @return votingList of the system + */ + public ArrayList getVotingList() { + return votingList; + } + + /** + * @param idx is the index of the voting to be returned + * @return the voting[idx] from the ArrayList + */ + public Voting getVoting(int idx) { + return votingList.get(idx); + } + + /** + * generates a vote for person + * @param idx is index of a voting + * @param person is a voter + * @param choices is the voters choices + */ + public void vote(int idx, Person person, ArrayList choices) { + votingList.get(idx).vote(person, choices); + } +} diff --git a/FarshidNooshi9831068/src/lab5New/Circle.java b/FarshidNooshi9831068/src/lab5New/Circle.java new file mode 100644 index 0000000..fa2415c --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Circle.java @@ -0,0 +1,83 @@ +// In The Name Of GOD + +package lab5New; + +import java.util.Objects; + +/** + * this is the circle class and represents a circle in the lab + */ + +public class Circle extends Shape { + private static final double PI = 3.14159265; + private final double radius; + + /** + * a constructor for our class + * + * @param radius is the radius of the circle + */ + public Circle(double radius) { + this.radius = radius; + } + + /** + * this method just draws our shape in the console + */ + @Override + public void draw() { + System.out.print("Circle and "); + super.draw(); + System.out.println("."); + } + + public double getRadius() { + return radius; + } + + /** + * @return the arena of the shape + */ + @Override + public double calculateArea() { + return PI * radius * radius; + } + + /** + * @return the perimeter of the shape + */ + @Override + public double calculatePerimeter() { + return 2 * PI * radius; + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Circle tmp = (Circle) obj; + return tmp.getRadius() == radius; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(radius); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Circle and radius equals to " + radius; + } +} \ No newline at end of file diff --git a/FarshidNooshi9831068/src/lab5New/Main.java b/FarshidNooshi9831068/src/lab5New/Main.java new file mode 100644 index 0000000..b1000e6 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Main.java @@ -0,0 +1,33 @@ +// In The Name Of GOD + +package lab5New; + +/** + * the main class for running the program + */ +public class Main { + public static void main(String[] args) { + Circle circle1 = new Circle(19); + Circle circle2 = new Circle(3); + Rectangle rect1 = new Rectangle(1, 4, 1, 4); + Rectangle rect2 = new Rectangle(8, 5, 8, 5); + Rectangle rect3 = new Rectangle(6, 6, 6, 6); + Triangle tri1 = new Triangle(2, 2, 2); + Triangle tri2 = new Triangle(4, 4, 6); + Triangle tri3 = new Triangle(2, 2, 2); + Paint paint = new Paint(); + paint.addShape(circle1); + paint.addShape(circle2); + paint.addShape(rect1); + paint.addShape(rect2); + paint.addShape(rect3); + paint.addShape(tri1); + paint.addShape(tri2); + paint.addShape(tri3); + paint.drawAll(); + paint.printAll(); + System.out.println(); + paint.describeEqualSides(); + System.out.println("BYE"); + } +} diff --git a/FarshidNooshi9831068/src/lab5New/Paint.java b/FarshidNooshi9831068/src/lab5New/Paint.java new file mode 100644 index 0000000..5cbefb9 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Paint.java @@ -0,0 +1,54 @@ +// In The Name Of GOD + +package lab5New; + +import java.util.ArrayList; + +/** + * the paint class to better managing + */ +public class Paint { + private ArrayList shapeArrayList; + + /** + * a simple constructor for just building the initial array lists + */ + public Paint() { + shapeArrayList = new ArrayList<>(); + } + + /** + * this method prints all the shapes that we saved + */ + public void drawAll() { + for (Shape it : shapeArrayList) + it.draw(); + } + + /** + * this method prints information about each shape + */ + public void printAll() { + for (Shape it : shapeArrayList) + System.out.println(it); + } + + + public void addShape(Shape shape) { + shapeArrayList.add(shape); + } + + /** + * this method prints the equal sides + * means the shapes that has equal side lengths + */ + public void describeEqualSides() { + for (Shape it : shapeArrayList) { + if (it instanceof Rectangle && ((Rectangle) it).isSquare()) + it.draw(); + else if (it instanceof Triangle && ((Triangle) it).isEquilateral()) + it.draw(); + } + } + +} diff --git a/FarshidNooshi9831068/src/lab5New/Polygon.java b/FarshidNooshi9831068/src/lab5New/Polygon.java new file mode 100644 index 0000000..5ef959f --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Polygon.java @@ -0,0 +1,65 @@ +// In The Name Of GOD + +package lab5New; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * it's the polygon class that is a subClass of shape + * it helps to better manage rectangle and triangle also helps to improve the quality of our code + */ +public class Polygon extends Shape { + protected ArrayList sides; + + /** + * its the constructor for our class + * + * @param s is the sides of our polygon + */ + public Polygon(double... s) { + sides = new ArrayList<>(); + for (int i = 0; i < s.length; i++) + sides.add(s[i]); + } + + public ArrayList getSides() { + return sides; + } + + /** + * @return the perimeter of our polygon + */ + @Override + public double calculatePerimeter() { + double ret = 0; + for (Double it : sides) + ret += it; + return ret; + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Polygon tmp = (Polygon) obj; + for (int i = 0; i < sides.size(); i++) + if (!sides.get(i).equals(tmp.sides.get(i))) + return false; + return true; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(sides); + } +} diff --git a/FarshidNooshi9831068/src/lab5New/Rectangle.java b/FarshidNooshi9831068/src/lab5New/Rectangle.java new file mode 100644 index 0000000..f15566f --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Rectangle.java @@ -0,0 +1,93 @@ +// In The Name Of GOD + +package lab5New; + +import java.util.Objects; + +/** + * the rectangle class that represents a rectangle in our lab + */ +public class Rectangle extends Polygon { + /** + * is a constructor for our class + * @param s is the sides of the rectangle + */ + public Rectangle(double... s) { + super(s); + } + + + /** + * @return true if the rectangle is square + */ + public boolean isSquare() { + return sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)) && sides.get(1).equals(sides.get(3)); + } + + /** + * @return the arena of the shape + */ + @Override + public double calculateArea() { + for (Double it : sides) + if (!it.equals(sides.get(0))) + return it * sides.get(0); + return sides.get(0) * sides.get(0); + } + + /** + * @return the perimeter of the shape + */ + @Override + public double calculatePerimeter() { + double ret = 0; + for (Double it : sides) + ret += it; + return ret; + } + + /** + * this method draws our shape + */ + public void draw() { + System.out.print("Rectangle "); + super.draw(); + if (isSquare()) + System.out.println(" its Square."); + else + System.out.println("."); + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Rectangle tmp = (Rectangle) obj; + for (int i = 0; i < 4; i++) + if (!sides.get(i).equals(tmp.sides.get(i))) + return false; + return true; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(sides); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Rectangle and sides equal to " + "side 1 = " + sides.get(0) + " side 2 = " + sides.get(1) + " side 3 = " + sides.get(2) + " side 4 = " + sides.get(3); + } +} diff --git a/FarshidNooshi9831068/src/lab5New/Shape.java b/FarshidNooshi9831068/src/lab5New/Shape.java new file mode 100644 index 0000000..ae65392 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Shape.java @@ -0,0 +1,40 @@ +// In The Name Of GOD + +package lab5New; + +/** + * this class represents an ordinary shape no matter which shape it is because of inheritance + */ +public class Shape { + + /** + * @return the arena of the shape + */ + public double calculateArea() { + return 0; + } + + /** + * @return the perimeter of the shape + */ + public double calculatePerimeter() { + return 0; + } + + /** + * this method tells the area and perimeter of the shape + */ + public void draw() { + System.out.print("Area: " + calculateArea() + " And perimeter: " + calculatePerimeter()); + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public String toString() { + return super.toString(); + } +} diff --git a/FarshidNooshi9831068/src/lab5New/Triangle.java b/FarshidNooshi9831068/src/lab5New/Triangle.java new file mode 100644 index 0000000..ec939b9 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5New/Triangle.java @@ -0,0 +1,92 @@ +// In The Name Of GOD + +package lab5New; + +import java.util.ArrayList; +import java.util.Objects; + +/** + * like circle and rectangle this is triangle class that represents a triangle in our class + */ +public class Triangle extends Polygon { + /** + * its a constructor for our class + * @param s is the sides of the triangle + **/ + public Triangle(double... s) { + super(s); + } + + /** + * @return true if the triangle is equilateral + */ + public boolean isEquilateral() { + return sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)); + } + + /** + * @return the arena of the shape with the heron formula + */ + @Override + public double calculateArea() { + double p = calculatePerimeter() / 2; + return Math.sqrt(p * (p - sides.get(0)) * (p - sides.get(1)) * (p - sides.get(2))); + } + + /** + * @return the perimeter of the shape + */ + @Override + public double calculatePerimeter() { + double ret = 0; + for (Double it : sides) + ret += it; + return ret; + } + + /** + * this method draws our shape + */ + public void draw() { + System.out.print("Triangle "); + super.draw(); + if (isEquilateral()) + System.out.println(" its equilateral."); + else + System.out.println("."); + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Triangle tmp = (Triangle) obj; + for (int i = 0; i < 3; i++) + if (!sides.get(i).equals(tmp.getSides().get(i))) + return false; + return true; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(sides); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Triangle and sides equal to " + "side 1 = " + sides.get(0) + " side 2 = " + sides.get(1) + " side 3 = " + sides.get(2); + } + +} diff --git a/FarshidNooshi9831068/src/lab5Old/Circle.java b/FarshidNooshi9831068/src/lab5Old/Circle.java new file mode 100644 index 0000000..9945a87 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5Old/Circle.java @@ -0,0 +1,74 @@ +//In The Name Of GOD + +package lab5Old; + +import java.util.Objects; + +public class Circle { + private static final double PI = 3.14159265; + private final double radius; + + /** + * a constructor for our class + * + * @param radius is the radius of the circle + */ + public Circle(double radius) { + this.radius = radius; + } + + /** + * this method just draws our shape in the console + */ + public void draw() { + System.out.println("Circle with " + calculateArea() + " area and " + calculatePerimeter() + " perimeter."); + } + + public double getRadius() { + return radius; + } + + /** + * @return the arena of the shape + */ + public double calculateArea() { + return PI * radius * radius; + } + + /** + * @return the perimeter of the shape + */ + public double calculatePerimeter() { + return 2 * PI * radius; + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Circle tmp = (Circle) obj; + return tmp.getRadius() == radius; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(radius); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Circle and radius equals to " + radius; + } +} diff --git a/FarshidNooshi9831068/src/lab5Old/Main.java b/FarshidNooshi9831068/src/lab5Old/Main.java new file mode 100644 index 0000000..542451e --- /dev/null +++ b/FarshidNooshi9831068/src/lab5Old/Main.java @@ -0,0 +1,25 @@ +//In The Name Of GOD + +package lab5Old; + +public class Main { + public static void main(String[] args) { + Circle circle1 = new Circle(19); + Circle circle2 = new Circle(3); + Rectangle rect1 = new Rectangle(1, 4, 1, 4); + Rectangle rect2 = new Rectangle(8, 5, 8, 5); + Rectangle rect3 = new Rectangle(6, 6, 6, 6); + Triangle tri1 = new Triangle(2, 2, 2); + Triangle tri2 = new Triangle(4, 4, 6); + Paint paint = new Paint(); + paint.addCircle(circle1); + paint.addCircle(circle2); + paint.addRectangle(rect1); + paint.addRectangle(rect2); + paint.addRectangle(rect3); + paint.addTriangle(tri1); + paint.addTriangle(tri2); + paint.drawAll(); + paint.printAll(); + } +} diff --git a/FarshidNooshi9831068/src/lab5Old/Paint.java b/FarshidNooshi9831068/src/lab5Old/Paint.java new file mode 100644 index 0000000..acc15b8 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5Old/Paint.java @@ -0,0 +1,72 @@ +//In The Name Of GOD + +package lab5Old; + +import java.util.ArrayList; + +public class Paint { + private ArrayList circleArrayList; + private ArrayList triangleArrayList; + private ArrayList rectangleArrayList; + + /** + * a simple constructor for just building the initial array lists + */ + public Paint() { + circleArrayList = new ArrayList<>(); + triangleArrayList = new ArrayList<>(); + rectangleArrayList = new ArrayList<>(); + } + + /** + * this method prints all the shapes that we saved + */ + public void drawAll() { + for (Circle it : circleArrayList) + it.draw(); + + for (Triangle it : triangleArrayList) + it.draw(); + + for (Rectangle it : rectangleArrayList) + it.draw(); + } + + /** + * this method prints information about each shape + */ + public void printAll() { + for (Circle it : circleArrayList) + System.out.println(it); + + for (Triangle it : triangleArrayList) + System.out.println(it); + + for (Rectangle it : rectangleArrayList) + System.out.println(it); + } + + + /** + * @param circle is a new circle to be added + */ + public void addCircle(Circle circle) { + circleArrayList.add(circle); + } + + /** + * @param triangle is a new triangle to be added + */ + + public void addTriangle(Triangle triangle) { + triangleArrayList.add(triangle); + } + + /** + * @param rectangle is a new rectangle to be added + */ + public void addRectangle(Rectangle rectangle) { + rectangleArrayList.add(rectangle); + } + +} diff --git a/FarshidNooshi9831068/src/lab5Old/Rectangle.java b/FarshidNooshi9831068/src/lab5Old/Rectangle.java new file mode 100644 index 0000000..f976b4b --- /dev/null +++ b/FarshidNooshi9831068/src/lab5Old/Rectangle.java @@ -0,0 +1,101 @@ +//In The Name Of GOD + +package lab5Old; + +import java.util.ArrayList; +import java.util.Objects; + +public class Rectangle { + private ArrayList sides; + + /** + * is a constructor for our class + * + * @param s1 is a side of our shape + * @param s2 is a side of our shape + * @param s3 is a side of our shape + * @param s4 is a side of our shape + */ + public Rectangle(double s1, double s2, double s3, double s4) { + sides = new ArrayList<>(); + sides.add(s1); + sides.add(s2); + sides.add(s3); + sides.add(s4); + } + + public ArrayList getSides() { + return sides; + } + + /** + * @return true if the rectangle is square + */ + public boolean isSquare() { + return sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)) && sides.get(1).equals(sides.get(3)); + } + + /** + * @return the arena of the shape + */ + public double calculateArea() { + for (Double it : sides) + if (!it.equals(sides.get(0))) + return it * sides.get(0); + return sides.get(0) * sides.get(0); + } + + /** + * @return the perimeter of the shape + */ + public double calculatePerimeter() { + double ret = 0; + for (Double it : sides) + ret += it; + return ret; + } + + /** + * this method draws our shape + */ + public void draw() { + System.out.print("Rectangle with " + calculateArea() + " area and " + calculatePerimeter() + " perimeter"); + if (isSquare()) + System.out.println(" its square."); + else + System.out.println("."); + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Rectangle tmp = (Rectangle) obj; + for (int i = 0; i < 4; i++) + if (!sides.get(i).equals(tmp.sides.get(i))) + return false; + return true; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(sides); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Rectangle and sides equal to " + sides.get(0) + " " + sides.get(1) + " " + sides.get(2) + " " + sides.get(3); + } +} diff --git a/FarshidNooshi9831068/src/lab5Old/Triangle.java b/FarshidNooshi9831068/src/lab5Old/Triangle.java new file mode 100644 index 0000000..e72caa3 --- /dev/null +++ b/FarshidNooshi9831068/src/lab5Old/Triangle.java @@ -0,0 +1,97 @@ +//In The Name Of GOD + +package lab5Old; + +import java.util.ArrayList; +import java.util.Objects; + +public class Triangle { + private ArrayList sides; + + /** + * its a constructor for our class + * + * @param s1 is a side of our Triangle + * @param s2 is a side of our Triangle + * @param s3 is a side of our Triangle + */ + public Triangle(double s1, double s2, double s3) { + sides = new ArrayList<>(); + sides.add(s1); + sides.add(s2); + sides.add(s3); + } + + public ArrayList getSides() { + return sides; + } + + /** + * @return true if the triangle is equilateral + */ + public boolean isEquilateral() { + return sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)); + } + + /** + * @return the arena of the shape with the heron formula + */ + public double calculateArea() { + double p = calculatePerimeter() / 2; + return Math.sqrt(p * (p - sides.get(0)) * (p - sides.get(1)) * (p - sides.get(2))); + } + + /** + * @return the perimeter of the shape + */ + public double calculatePerimeter() { + double ret = 0; + for (Double it : sides) + ret += it; + return ret; + } + + /** + * this method draws our shape + */ + public void draw() { + System.out.print("Triangle with " + calculateArea() + " area and " + calculatePerimeter() + " perimeter"); + if (isEquilateral()) + System.out.println(" its equilateral."); + else + System.out.println("."); + } + + /** + * @param obj is the object to be checked + * @return true if the shapes are equal + */ + @Override + public boolean equals(Object obj) { + if (obj == this) + return true; + if (obj == null || obj.getClass() != getClass()) + return false; + Rectangle tmp = (Rectangle) obj; + for (int i = 0; i < 3; i++) + if (!sides.get(i).equals(tmp.getSides().get(i))) + return false; + return true; + } + + /** + * @return the hash value of the shape depending on its properties + */ + @Override + public int hashCode() { + return Objects.hash(sides); + } + + /** + * @return an string showing the properties of the shape + */ + @Override + public String toString() { + return "Triangle and sides equal to " + sides.get(0) + " " + sides.get(1) + " " + sides.get(2); + } +}