diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b72a65f --- /dev/null +++ b/.gitignore @@ -0,0 +1,149 @@ + +# Created by https://www.gitignore.io/api/java,windows,intellij +# Edit at https://www.gitignore.io/?templates=java,windows,intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +# *.iml +# modules.xml +# .idea/misc.xml +# *.ipr + +# Sonarlint plugin +.idea/**/sonarlint/ + +# SonarQube Plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator/ + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Windows ### +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp +SeyyedMahdiMirfendereski/AP-lab4/.idea +SeyyedMahdiMirfendereski/AP-lab4/out +*.iml +*.class +*.xml + +# Windows shortcuts +*.lnk + +# End of https://www.gitignore.io/api/java,windows,intellij diff --git a/RashadAnsari/RashadAnsari b/RashadAnsari/RashadAnsari deleted file mode 100644 index b2afe10..0000000 --- a/RashadAnsari/RashadAnsari +++ /dev/null @@ -1,3 +0,0 @@ -Full Name: Rashad Ansari -Student Number: XXXXXXXXXX - diff --git a/SeyyedMahdiMirfendereski/AP-lab4/src/Main.java b/SeyyedMahdiMirfendereski/AP-lab4/src/Main.java new file mode 100644 index 0000000..c1d9e4b --- /dev/null +++ b/SeyyedMahdiMirfendereski/AP-lab4/src/Main.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; + +public class Main { + public static void main(String[] args){ + VotingSystem testing=new VotingSystem(); + Person firstPerson=new Person("Mahdi","Mirfendereski"); + Person secondPerson=new Person("Ali","Mohammadi"); + ArrayList firstOptions=new ArrayList<>(); + ArrayList secondOptions=new ArrayList<>(); + firstOptions.add("+"); + firstOptions.add("-"); + secondOptions.add("sunday"); + secondOptions.add("friday"); + secondOptions.add("saturday"); + testing.createVoting("Do you come?",0,firstOptions); + testing.createVoting("Which day?",1,secondOptions); + testing.printVotingQuestions(); + testing.printVoting(0); + // print voting testing + testing.printVoting(1); + ArrayList votes1 =new ArrayList<>(); + ArrayList votes2 =new ArrayList<>(); + votes1.add("+"); + votes2.add("friday"); + votes2.add("saturday"); + testing.vote(0,firstPerson,votes1); + testing.vote(1,firstPerson,votes2); + testing.printResult(0); + //2 choices win + testing.printResult(1); + //voting randomly testing + testing.randomVote(0,secondPerson); + votes2.remove(0); + testing.vote(1,secondPerson,votes2); + testing.printResult(0); + testing.printResult(1); + System.out.println("first voting"); + testing.getVoters(0); + System.out.println("second voting"); + testing.getVoters(1); + } +} diff --git a/SeyyedMahdiMirfendereski/AP-lab4/src/Person.java b/SeyyedMahdiMirfendereski/AP-lab4/src/Person.java new file mode 100644 index 0000000..05114a2 --- /dev/null +++ b/SeyyedMahdiMirfendereski/AP-lab4/src/Person.java @@ -0,0 +1,34 @@ +/** + * this class keep first name and last name of person. + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Person { + private String firstName ; + private String lastName ; + /** + * + * @param firstName firstName of person + * @param lastName lastName of person + */ + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + @Override + public String toString() { + return "Person{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + '}'; + } +} diff --git a/SeyyedMahdiMirfendereski/AP-lab4/src/Vote.java b/SeyyedMahdiMirfendereski/AP-lab4/src/Vote.java new file mode 100644 index 0000000..f6e0c21 --- /dev/null +++ b/SeyyedMahdiMirfendereski/AP-lab4/src/Vote.java @@ -0,0 +1,41 @@ +import java.util.Objects; + +/** + * this class represent a vote that include person and data information . + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Vote { + private Person person ; + private String date ; + + /** + * + * @param person who wants voting + * @param date the date of voting + */ + public Vote(Person person, String date) { + this.person = person; + this.date = date; + } + public Person getPerson() { + return person; + } + + public String getDate() { + return date; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Vote)) return false; + Vote vote = (Vote) o; + return Objects.equals(person, vote.person); + } + + @Override + public int hashCode() { + return Objects.hash(person); + } +} diff --git a/SeyyedMahdiMirfendereski/AP-lab4/src/Voting.java b/SeyyedMahdiMirfendereski/AP-lab4/src/Voting.java new file mode 100644 index 0000000..c9871d2 --- /dev/null +++ b/SeyyedMahdiMirfendereski/AP-lab4/src/Voting.java @@ -0,0 +1,90 @@ + +import ir.huri.jcal.JalaliCalendar; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + + +/** + * this class includes type of voting ,question of voting ,voters and list Of Votes To Choices. + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Voting { + private int type; + private String question ; + private ArrayList voters ; + private HashMap> listOfVotesToChoices; + + /** + * + * @param type type of voting + * @param question topic og voting + */ + public Voting(int type, String question) { + this.type = type; + this.question = question; + voters=new ArrayList<>(); + listOfVotesToChoices=new HashMap<>(); + } + + public int getType() { + return type; + } + + public String getQuestion() { + return question; + } + + /** + * adding a new choice to options + * @param choice new option + */ + public void createChoice(String choice) { + listOfVotesToChoices.put(choice,new HashSet<>()); + + } + public ArrayList getChoices() { + return new ArrayList<>(listOfVotesToChoices.keySet()); + } + + /** + * record a vote for voter. + * @param voter who wants to vote + * @param choices choices that were decided by voter. + */ + public void vote(Person voter,ArrayList choices){ + //check if he is legal + if(!voters.contains(voter)){ + voters.add(voter); + for (String s:choices) + listOfVotesToChoices.get(s).add(new Vote(voter,new JalaliCalendar().toString())); + } + } + + /** + * print list of voters + */ + public void getVoters(){ + for (Person p:voters) + System.out.println(p); + } + + /** + * print result of voting + */ + public void printVotes(){ + int max=0; + System.out.println(question); + ArrayList choices=getChoices(); + for (String s:choices) + if(listOfVotesToChoices.get(s).size()>max) + max=listOfVotesToChoices.get(s).size(); + //if we have more than 1 choices that were decided + for (String s1:choices) + if(listOfVotesToChoices.get(s1).size()==max) + System.out.print(s1+":"+max+" | "); + System.out.println(); + } +} diff --git a/SeyyedMahdiMirfendereski/AP-lab4/src/VotingSystem.java b/SeyyedMahdiMirfendereski/AP-lab4/src/VotingSystem.java new file mode 100644 index 0000000..22bdb86 --- /dev/null +++ b/SeyyedMahdiMirfendereski/AP-lab4/src/VotingSystem.java @@ -0,0 +1,93 @@ +import java.util.ArrayList; +import java.util.Random; +/** + * this class includes a list of voting and some methods to add som information to voting. + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class VotingSystem { + private ArrayList votingList; + /** + * create instance of Array list. + */ + public VotingSystem() { + votingList=new ArrayList<>(); + } + + /** + * adding a new voting to list of voting + * @param question the question of voting + * @param type type of voting + * @param choices options of voting + */ + public void createVoting(String question,int type,ArrayList choices){ + Voting newVoting=new Voting(type,question); + votingList.add(newVoting); + for (String s:choices) + newVoting.createChoice(s); + } + + /** + * just printing a questions of voting + */ + public void printVotingQuestions(){ + int i=0; + System.out.println("Question"); + for (Voting v:votingList) { + System.out.println(i+":"+v.getQuestion()); + i++ ; + } + } + + /** + * printing question and options of voting + * @param index which voting from list + */ + public void printVoting(int index){ + System.out.print("Question : "+votingList.get(index).getQuestion()+" Choices: "); + int i=0; + for (String s:votingList.get(index).getChoices()) { + System.out.print(i + ": " + s + " "); + i++; + } + System.out.println(); + } + + /** + * adding a vote of voters and his choices to specific voting + * @param index which voting from list + * @param voter who wants to vote + * @param choices a list of string that include voter's choices + */ + public void vote(int index,Person voter,ArrayList choices){ + votingList.get(index).vote(voter,choices); + + } + + /** + * printing the result of specific voting + * @param index which voting from list + */ + public void printResult(int index){ + votingList.get(index).printVotes(); + } + + /** + * voters votes randomely + * @param index which voting from list + * @param voter who is voters + */ + public void randomVote(int index,Person voter){ + // to check the type of voting. + if(votingList.get(index).getType()==0){ + Random random=new Random(); + ArrayList choices=votingList.get(index).getChoices(); + ArrayList choice=new ArrayList<>(); + choice.add(choices.get(random.nextInt(choices.size()))); + votingList.get(index).vote(voter,choice); + } + } + public void getVoters(int index){ + votingList.get(index).getVoters(); + } +} diff --git a/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Circle.java b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Circle.java new file mode 100644 index 0000000..9016777 --- /dev/null +++ b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Circle.java @@ -0,0 +1,65 @@ + +/** + * this class represents a circle class . + * @version 0.0 + * @author Mahdi Mirfendereski + */ +public class Circle { + private double radius; + + /** + * + * @param radius radius of circle + */ + public Circle(double radius){ + this.radius=radius; + } + + /** + * get the radius of circle + * @return radius + */ + public double getRadius() { + return radius; + } + /** + * calculating circle's Perimeter + * @return circle's Perimeter + */ + public double calculatePerimeter(){ + + return 2*3.14*radius; + } + + /** + * calculating circle's Area + * @return circle's Area + */ + public double calculateArea(){ + return 3.14*radius*radius; + } + /** + * draw a circle in a line + */ + public void draw(){ + System.out.println("Circle Perimeter="+this.calculatePerimeter()+" Area="+this.calculateArea()); + } + /** + * note : parameter of this method is Circle type not Object type. + * @param circle Circle type + * @return true if circle and this object are equal. + */ + public boolean equals(Circle circle){ + if(this==circle) + return true; + + return this.radius == circle.getRadius(); + } + + @Override + public String toString() { + return "Circle{" + + "radius=" +radius+ + '}'; + } +} diff --git a/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Main.java b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Main.java new file mode 100644 index 0000000..b10a597 --- /dev/null +++ b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Main.java @@ -0,0 +1,35 @@ +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(); + Circle circle1Temp = new Circle(19); + Rectangle rect2Temp = new Rectangle(8,5,8,5); + Triangle tri2Temp = new Triangle(4,4,6); + //circle1 equals circle1Temp + System.out.println("Must be true: "+circle1.equals(circle1Temp)); + //rect2 equals rect2Temp + System.out.println("Must be true: "+rect2.equals(rect2Temp)); + //tri2 equals tri2Temp + System.out.println("Must be true: "+tri2.equals(tri2Temp)); + //rect3 is Square + System.out.println("Must be true: "+rect3.isSquare()); + //tri1 is Equilateral + System.out.println("Must be true: "+tri1.isEquilateral()); + } + +} diff --git a/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Paint.java b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Paint.java new file mode 100644 index 0000000..f45c63b --- /dev/null +++ b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Paint.java @@ -0,0 +1,62 @@ +import java.util.ArrayList; + +/** + * this class represents a paint class to include circle,rectangle and triangle array list. + * @version 0.0 + * @author Mahdi Mirfendereski + */ +public class Paint { + private ArrayList circles; + private ArrayList triangles; + private ArrayList rectangles; + public Paint(){ + circles=new ArrayList<>(); + triangles=new ArrayList<>(); + rectangles=new ArrayList<>(); + } + + /** + * + * @param circle circle to add to circle's list + */ + public void addCircle(Circle circle){ + circles.add(circle); + } + /** + * + * @param triangle triangle to add to triangle's list + */ + public void addTriangle(Triangle triangle){ + triangles.add(triangle); + } + /** + * + * @param rectangle rectangle to add to rectangle's list + */ + public void addRectangle(Rectangle rectangle){ + rectangles.add(rectangle); + } + + /** + * draw all shapes + */ + public void drawAll(){ + for (Rectangle r:rectangles) + r.draw(); + for (Circle c:circles) + c.draw(); + for (Triangle t:triangles) + t.draw(); + } + /** + * print all shapes + */ + public void printAll(){ + for (Rectangle r:rectangles) + System.out.println(r); + for (Circle c:circles) + System.out.println(c); + for (Triangle t:triangles) + System.out.println(t); + } +} diff --git a/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Rectangle.java b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Rectangle.java new file mode 100644 index 0000000..5a5bea2 --- /dev/null +++ b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Rectangle.java @@ -0,0 +1,107 @@ +import java.util.ArrayList; +/** + * this class represents a Rectangle class . + * @version 0.0 + * @author Mahdi Mirfendereski + */ + +public class Rectangle { + //4 sides + private ArrayList sides ; + + /** + * create new Array list to contain sides + * @param s1 side 1 + * @param s2 side 2 + * @param s3 side 3 + * @param s4 side 4 + */ + public Rectangle(double s1,double s2,double s3,double s4) { + sides=new ArrayList<>(4); + sides.add(s1); + sides.add(s2); + sides.add(s3); + sides.add(s4); + } + + /** + * + * @return list of sides + */ + public ArrayList getSides() { + return sides; + } + + /** + * to check if it is square or not + * @return true if it is square + */ + public boolean isSquare(){ + if(sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)) && sides.get(2).equals(sides.get(3))) + return true ; + return false ; + } + + /** + * calculating rectangle's Perimeter + * @return rectangle's Perimeter + */ + public double calculatePerimeter(){ + double sum=0; + for (Double i:sides) { + sum += i; + } + return sum; + } + + /** + * calculating rectangle's Area + * @return rectangle's Area + */ + public double calculateArea(){ + double s1=sides.get(0); + for(Double d:sides){ + if(d!=s1) + return d*s1; + } + if(isSquare()) + return sides.get(0)*sides.get(0); + return 0; + } + + /** + * draw a rectangle in a line + */ + public void draw(){ + System.out.println("Rectangle Perimeter="+this.calculatePerimeter()+" Area="+this.calculateArea()); + } + + /** + * note : parameter of this method is Rectangle type not Object type. + * @param rectangle Rectangle type + * @return true if rectangle and this object are equal. + */ + public boolean equals(Rectangle rectangle){ + if(this==rectangle) + return true; + ArrayList temp1=new ArrayList<>(4); + ArrayList temp2=new ArrayList<>(4); + temp1.addAll(sides); + temp2.addAll(rectangle.getSides()); + for(Double d:sides){ + if(!temp2.contains(d)) + return false; + else + temp2.remove(d); + } + return true; + } + + @Override + public String toString() { + return "Rectangle{" + + "sides=" +sides.get(0) +","+ sides.get(1)+","+ sides.get(2)+","+ sides.get(3)+ + '}'; + } +} + diff --git a/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Triangle.java b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Triangle.java new file mode 100644 index 0000000..5f2b477 --- /dev/null +++ b/SeyyedMahdiMirfendereski/FirstDesigning-S5/src/Triangle.java @@ -0,0 +1,94 @@ +import java.util.ArrayList; +/** + * this class represents a Triangle class . + * @version 0.0 + * @author Mahdi Mirfendereski + */ +public class Triangle { + private ArrayList sides ; + + /** + * create new Array list to contain sides + * @param s1 side 1 + * @param s2 side 2 + * @param s3 side 3 + * + */ + public Triangle(double s1,double s2,double s3) { + sides=new ArrayList<>(3); + sides.add(s1); + sides.add(s2); + sides.add(s3); + } + + /** + * @return list of sides + */ + public ArrayList getSides() { + return sides; + } + + /** + * to check if it is equilateral + * @return true if it is equilateral + */ + public boolean isEquilateral(){ + if(sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2))) + return true ; + return false ; + } + + /** + * calculating Triangle's Perimeter + * @return Triangle's Perimeter + */ + public double calculatePerimeter(){ + double sum=0; + for (Double i:sides) { + sum += i; + } + return sum; + } + + /** + * calculating Triangle's Area + * @return Triangle's Area + */ + public double calculateArea(){ + + double s =calculatePerimeter()/2; + return Math.sqrt(s*(s-sides.get(0))*(s-sides.get(1))*(s-sides.get(2))); + } + /** + * draw a Triangle in a line + */ + public void draw(){ + System.out.println("Triangle Perimeter="+this.calculatePerimeter()+" Area="+this.calculateArea()); + } + /** + * note : parameter of this method is Triangle type not Object type. + * @param triangle Rectangle type + * @return true if triangle and this object are equal. + */ + public boolean equals(Triangle triangle){ + if(this==triangle) + return true; + ArrayList temp1=new ArrayList<>(3); + ArrayList temp2=new ArrayList<>(3); + temp1.addAll(sides); + temp2.addAll(triangle.getSides()); + for(Double d:sides){ + if(!temp2.contains(d)) + return false; + else + temp2.remove(d); + } + return true; + } + @Override + public String toString() { + return "Triangle{" + + "sides=" +sides.get(0) +","+ sides.get(1)+","+ sides.get(2)+ + '}'; + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Circle.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Circle.java new file mode 100644 index 0000000..65d8465 --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Circle.java @@ -0,0 +1,49 @@ +/** + * this class represents a Circle generally + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Circle extends Shape { + private double radius; + + /** + * + * @param radius radius of circle + */ + public Circle(double radius){ + this.radius=radius; + } + /** + * get the radius of circle + * @return radius of circle + */ + public double getRadius() { + return radius; + } + @Override + public double calculatePerimeter(){ + + return 2*3.14*radius; + } + @Override + public double calculateArea(){ + return 3.14*radius*radius; + } + @Override + public void draw(){ + System.out.println("Circle Perimeter="+this.calculatePerimeter()+" Area="+this.calculateArea()); + } + @Override + public String toString() { + return "Circle:: "+ this.radius; + } + @Override + public boolean equals(Shape shape){ + if(this==shape) + return true; + if(!(shape instanceof Circle)) + return false; + Circle circle=(Circle)shape; + return this.radius == circle.getRadius(); + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Main.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Main.java new file mode 100644 index 0000000..5a58ea0 --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Main.java @@ -0,0 +1,39 @@ +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.addShape(circle1); + paint.addShape(circle2); + paint.addShape(rect1); + paint.addShape(rect2); + paint.addShape(rect3); + paint.addShape(tri1); + paint.addShape(tri2); + paint.drawAll(); + paint.paintAll(); + Circle circle1Temp = new Circle(19); + Rectangle rect2Temp = new Rectangle(8,5,8,5); + Triangle tri2Temp = new Triangle(4,4,6); + //equal method for circle testing + System.out.println("Must be true: "+circle1.equals(circle1Temp)); + //equal method for rectangle testing + System.out.println("Must be true: "+rect2.equals(rect2Temp)); + //equal method for triangle testing + System.out.println("Must be true: "+tri2.equals(tri2Temp)); + //is square method for rectangle testing + System.out.println("Must be true: "+rect3.isSquare()); + //is Equilateral method for triangle testing + System.out.println("Must be true: "+tri1.isEquilateral()); + paint.describeEqualSides(); + + + + + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Paint.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Paint.java new file mode 100644 index 0000000..afb64bd --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Paint.java @@ -0,0 +1,55 @@ +import java.util.ArrayList; + +/** + * keeping all shapes in this class + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Paint { + private ArrayList shapes ; + + /** + * create a array list for shapes + */ + public Paint(){ + shapes=new ArrayList<>(); + } + + /** + * adding a shape to list + * @param shape which shape? + */ + public void addShape(Shape shape){ + shapes.add(shape); + } + + /** + * draw all shapes with polymorphism + */ + public void drawAll(){ + for(Shape shape:shapes) + shape.draw(); + } + /** + * print all shapes with polymorphism + */ + public void paintAll(){ + for(Shape shape:shapes) + System.out.println(shape); + } + + /** + * print all shapes that are square or equilateral + */ + public void describeEqualSides(){ + for (Shape shape:shapes){ + if(shape instanceof Rectangle ) + if(((Rectangle)shape).isSquare()) + System.out.println(shape); + if(shape instanceof Triangle) + if(((Triangle)shape).isEquilateral()) + System.out.println(shape); + + } + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Polygon.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Polygon.java new file mode 100644 index 0000000..3b0a57e --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Polygon.java @@ -0,0 +1,42 @@ +import java.util.ArrayList; + +/** + * this class represents a polygon generally + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Polygon extends Shape { + protected ArrayList sides ; + /** + * + * @param args sides of polygon + */ + public Polygon(double... args){ + sides=new ArrayList<>(); + for (Double d:args) { + sides.add(d); + } + } + + /** + * grt list of sides + * @return list of sides + */ + public ArrayList getSides() { + return sides; + } + @Override + public void draw(){ + super.draw(); + } + @Override + public String toString() { + String temp =""; + int i=1; + for (Double d:sides) { + temp+="Side"+i+":"+d.doubleValue()+", "; + i++; + } + return temp; + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Rectangle.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Rectangle.java new file mode 100644 index 0000000..3925996 --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Rectangle.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; +/** + * this class represents a Rectangle generally + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Rectangle extends Polygon { + /** + * + * @param args sides of Rectangle + */ + public Rectangle(double... args){ + super(args); + } + @Override + public String toString() { + return "Rectangle:: "+super.toString(); + } + @Override + public double calculatePerimeter(){ + double sum=0; + for (Double i:sides) { + sum += i; + } + return sum; + } + @Override + public double calculateArea(){ + double s1=sides.get(0); + for(Double d:sides){ + if(d!=s1) + return d*s1; + } + + return sides.get(0)*sides.get(0); + } + @Override + public void draw(){ + System.out.print("Rectangle "); + super.draw(); + } + public boolean isSquare(){ + if(sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2)) && sides.get(2).equals(sides.get(3))) + return true ; + return false ; + } + @Override + public boolean equals(Shape shape){ + if(this==shape) + return true; + if(!(shape instanceof Rectangle)) + return false; + Rectangle rectangle=(Rectangle)shape; + ArrayList temp1=new ArrayList<>(4); + ArrayList temp2=new ArrayList<>(4); + temp1.addAll(sides); + temp2.addAll(rectangle.getSides()); + for(Double d:sides){ + if(!temp2.contains(d)) + return false; + else + temp2.remove(d); + } + return true; + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Shape.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Shape.java new file mode 100644 index 0000000..f40500c --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Shape.java @@ -0,0 +1,44 @@ +/** + * this class represents a shape generally + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Shape { + /** + * calculating shape's Perimeter + * @return shape's Perimeter + */ + public double calculatePerimeter(){ + return 0 ; + } + + /** + * calculating shape's Area + * @return shape's Area + */ + public double calculateArea(){ + return 0; + } + + /** + * draw a shape in one line + */ + public void draw(){ + System.out.println("Perimeter="+this.calculatePerimeter()+" Area="+this.calculateArea()); + } + + /** + * note : parameter of this method is Shape type not Object type. + * to check if both are equal or not + * @param shape kind of shape + * @return true if both are equal + */ + public boolean equals(Shape shape){ + return false; + } + + @Override + public String toString() { + return ""; + } +} diff --git a/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Triangle.java b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Triangle.java new file mode 100644 index 0000000..96ef9f8 --- /dev/null +++ b/SeyyedMahdiMirfendereski/SecondDesigning-S5/src/Triangle.java @@ -0,0 +1,63 @@ +import java.util.ArrayList; +/** + * this class represents a Triangle generally + * @author Mahdi Mirfendereski + * @version 0.0 + */ +public class Triangle extends Polygon { + /** + * + * @param args sides of Triangle + */ + public Triangle(double... args){ + super(args); + } + @Override + public String toString() { + return "Triangle:: "+super.toString(); + } + @Override + public double calculatePerimeter(){ + double sum=0; + for (Double i:sides) { + sum += i; + } + return sum; + } + @Override + public double calculateArea(){ + + double s =calculatePerimeter()/2; + return Math.sqrt(s*(s-sides.get(0))*(s-sides.get(1))*(s-sides.get(2))); + } + @Override + public void draw(){ + System.out.print("Triangle "); + super.draw(); + } + public boolean isEquilateral(){ + if(sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2))) + return true ; + return false ; + } + @Override + public boolean equals(Shape shape){ + if(this==shape) + return true; + if(!(shape instanceof Triangle)) + return false; + Triangle triangle=(Triangle)shape; + ArrayList temp1=new ArrayList<>(3); + ArrayList temp2=new ArrayList<>(3); + temp1.addAll(sides); + temp2.addAll(triangle.getSides()); + for(Double d:sides){ + if(!temp2.contains(d)) + return false; + else + temp2.remove(d); + } + return true; + } + +} diff --git a/SeyyedMahdiMirfendereski/SeyyedMahdiMirfendereski b/SeyyedMahdiMirfendereski/SeyyedMahdiMirfendereski new file mode 100644 index 0000000..df56e3c --- /dev/null +++ b/SeyyedMahdiMirfendereski/SeyyedMahdiMirfendereski @@ -0,0 +1,3 @@ +Full Name: Seyyed Mahdi Mirfendereski +Student Number: 9723093 +