diff --git a/AP-LabManual-Session4.zip b/AP-LabManual-Session4.zip deleted file mode 100644 index 4ed1f76..0000000 Binary files a/AP-LabManual-Session4.zip and /dev/null differ diff --git a/AP-LabManual-Session5.pdf b/AP-LabManual-Session5.pdf deleted file mode 100644 index db26fda..0000000 Binary files a/AP-LabManual-Session5.pdf and /dev/null differ diff --git a/LICENSE b/LICENSE deleted file mode 100644 index da6c555..0000000 --- a/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2020 AUT AP 2020 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/Mohammad Javad Rajabi - 9831025/report_wk4.pdf b/Mohammad Javad Rajabi - 9831025/report_wk4.pdf new file mode 100644 index 0000000..ebc87fe Binary files /dev/null and b/Mohammad Javad Rajabi - 9831025/report_wk4.pdf differ diff --git a/Mohammad Javad Rajabi - 9831025/report_wk5.pdf b/Mohammad Javad Rajabi - 9831025/report_wk5.pdf new file mode 100644 index 0000000..3eaf7a0 Binary files /dev/null and b/Mohammad Javad Rajabi - 9831025/report_wk5.pdf differ diff --git a/Mohammad Javad Rajabi - 9831025/s4/Main.java b/Mohammad Javad Rajabi - 9831025/s4/Main.java new file mode 100644 index 0000000..2d5bf84 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s4/Main.java @@ -0,0 +1,77 @@ +import java.util.ArrayList; +import java.util.Scanner; + +/** + * The Voting program is implement an application that create voting system + * that can create new voting with question and choices to person can vote. + * + * @author Mohammad Javad Rajabi + * @version 1.0 + * @since 2020-03-24 + */ +public class Main { + + /** + * This method simulate a voting system + * @param args + */ + public static void main(String[] args){ + + Scanner myObj = new Scanner(System.in); + + VotingSystem votingSystem = new VotingSystem(); + Person voter_0 = new Person("Javad", "Rajabi"); + Person voter_1 = new Person("Parsa", "Ahani"); + Person voter_2 = new Person("Erfan", "Karimian"); + Person voter_3 = new Person("Erfan", "Naderi"); + Person voter_4 = new Person("Ali", "Pasyar"); + ArrayList travel = new ArrayList(); + ArrayList city = new ArrayList(); + ArrayList days = new ArrayList(); + travel.add("agree"); + travel.add("disagree"); + travel.add("random"); + city.add("Isfahan"); + city.add("Kish"); + city.add("Gheshm"); + city.add("Shomal"); + city.add("Mashad"); + city.add("random"); + days.add("saturday"); + days.add("sunday"); + days.add("Thursday"); + days.add("friday"); + votingSystem.creatingVoting("let's go trip",0,travel); + votingSystem.creatingVoting("where?",0,city); + votingSystem.creatingVoting("when?",1,days); + System.out.println("print voting questions"); + votingSystem.printVotingQuestions(); + System.out.println("print voting"); + votingSystem.printVoting(0); + ArrayList votes0_0 = new ArrayList(); + ArrayList votes0_1 = new ArrayList(); + ArrayList votes0_2 = new ArrayList(); + votes0_0.add("agree"); + votes0_1.add("random"); + votes0_2.add("sunday"); + votes0_2.add("friday"); + votingSystem.vote(0,voter_0,votes0_0); + votingSystem.vote(1,voter_0,votes0_1); + votingSystem.vote(2,voter_0,votes0_2); + votingSystem.vote(2,voter_0,votes0_2); + votingSystem.vote(2,voter_1,votes0_2); +// myObj.next(); + System.out.println("print result"); + votingSystem.printResults(0); + votingSystem.printResults(1); + votingSystem.printResults(2); + + + + + + + + } + +} diff --git a/Mohammad Javad Rajabi - 9831025/s4/Person.java b/Mohammad Javad Rajabi - 9831025/s4/Person.java new file mode 100644 index 0000000..aefd88c --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s4/Person.java @@ -0,0 +1,72 @@ +import java.util.Objects; + +/** + * This class declare a person who can vote + * + * @author Mohammad Javad Rajabi + * @version 1.0 + * @since 2020-03-24 + */ +public class Person { + + //fields + + // first name of person who vote + private String firstName; + // last name of person who vote + private String lastName; + + + //constructor + + /** + * person constructor, for create new object of this class. + * @param firstName This is first name of person who vote + * @param lastName This is last name of person who vote + */ + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + + //methods + + /** + * This method is used to be able to access first name. + * @return String This returns first name of this person. + */ + public String getFirstName() { + return firstName; + } + + /** + * This method is used to be able to access last name. + * @return String This returns last name of this person. + */ + public String getLastName() { + return lastName; + } + + @Override + public String toString() { + return "Person{" + + "firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + '}'; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Person)) return false; + Person person = (Person) o; + return getFirstName().equals(person.getFirstName()) && + getLastName().equals(person.getLastName()); + } + + @Override + public int hashCode() { + return Objects.hash(getFirstName(), getLastName()); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s4/Vote.java b/Mohammad Javad Rajabi - 9831025/s4/Vote.java new file mode 100644 index 0000000..7ff0cf6 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s4/Vote.java @@ -0,0 +1,64 @@ +import java.util.Objects; + +/** + * This class declare vote that a person can do it. + * + * @author Mohammad Javad Rajabi + * @version 1.0 + * @since 2020-03-24 + */ +public class Vote { + + //fields + + // object of person type + private Person person; + // date that person vote + private String date; + + + //constructor + + /** + * vote constructor, for create new object of this class. + * @param person This is person who vote + * @param date This is date that person vote + */ + public Vote(Person person, String date) { + this.person = person; + this.date = date; + } + + + //methods + + /** + * This method is used to be able to access person. + * @return String This returns person. + */ + public Person getPerson() { + return person; + } + + /** + * This method is used to be able to access date of vote. + * @return String This returns date of vote. + */ + 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 getPerson().equals(vote.getPerson()) && + getDate().equals(vote.getDate()); + } + + @Override + public int hashCode() { + return Objects.hash(getPerson(), getDate()); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s4/Voting.java b/Mohammad Javad Rajabi - 9831025/s4/Voting.java new file mode 100644 index 0000000..db97e7e --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s4/Voting.java @@ -0,0 +1,140 @@ +import javax.lang.model.type.NullType; +import java.util.*; +import ir.huri.jcal.JalaliCalendar; + +/** + * This class create a voting that have question and choices. + * + * @author Mohammad Javad Rajabi + * @version 1.0 + * @since 2020-03-24 + */ +public class Voting { + + //fields + + // this field declare type of voting + private int type; + // question of voting + private String question; + // list of voter + private ArrayList voters; + // list of option that map with number of vote + private HashMap> listOfVoteToChoices; + + + //constructor + + /** + * Voting constructor, for create new object of this class. + * @param type This is parameter that declare type of voting + * @param question This is question of voting + */ + public Voting(int type, String question) { + this.type = type; + this.question = question; + voters = new ArrayList(); + listOfVoteToChoices = new HashMap>(); + } + + //methods + + /** + * This method is used to be able to access question. + * @return String This returns question of this voting. + */ + public String getQuestion() { + return question; + } + + /** + * This method is used to be able to access type of voting. + * @return int This returns type of this voting. + */ + public int getType() { + return type; + } + + /** + * This method is used to be able to access list of vote to choices. + * @return HashMap This returns list of vote to choices of this voting. + */ + public HashMap> getListOfVoteToChoices() { + return listOfVoteToChoices; + } + + /** + * This method is used to add a person to list of voter if he already voted he cant vote. + * @param option This parameter add to list of choices in this voting. + */ + public void createChoice(String option) { + HashSet set = new HashSet(); + listOfVoteToChoices.put(option, set); + } + + /** + * This method is used to add a person to list of voter if he already voted he cant vote. + * @param person This is a person who want to vote. + * @param options This is a list of choice that person choose. + */ + public void vote(Person person, ArrayList options){ + for (String option:options){ + HashSet votes = listOfVoteToChoices.get(option); + if(votes != null) { + for (Vote vote : votes) { + if (vote.getPerson().equals(person)) { + System.out.println(person.getFirstName() +" "+ person.getLastName() + " already voted"); + return; + } + } + } + GregorianCalendar gc = new GregorianCalendar(); + JalaliCalendar jalaliCalendar = new JalaliCalendar(gc); + Vote vote = new Vote(person, jalaliCalendar.toString()); + HashSet x = listOfVoteToChoices.get(option); + x.add(vote); + voters.add(person); + System.out.println("vote submitted!"); + } + } + + /** + * This method is used to print list of voter who vote in this voting. + */ + public void getVoters() { + System.out.println("-------------------------------------------"); + System.out.println("list of person who voted"); + for (Person person: voters){ + System.out.println((voters.indexOf(person)+1)+")"+person.getFirstName() + person.getLastName()); + } + System.out.println("-------------------------------------------"); + + } + + /** + * This method is used to print the choice and list of voter who vote to this choice. + */ + public void printVotes() { + Set sets = listOfVoteToChoices.keySet(); + ArrayList stringsList = new ArrayList<>(sets); + for (int i =0 ; i< stringsList.size() ; i++) { + System.out.println((i+1)+")"+ stringsList.get(i)+ "->" + listOfVoteToChoices.get(stringsList.get(i)).size()); + for (Vote vote :listOfVoteToChoices.get(stringsList.get(i))){ + System.out.println(vote.getPerson().getFirstName() + " " + vote.getPerson().getLastName() + " " + + vote.getDate().toString()); + } + System.out.println(); + + } + } + + /** + * This method is used to add get array list of choices. + * @return ArrayList This returns list of choices + */ + public ArrayList getChoices() { + Set sets = listOfVoteToChoices.keySet(); + ArrayList stringsList = new ArrayList<>(sets); + return stringsList; + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s4/VotingSystem.java b/Mohammad Javad Rajabi - 9831025/s4/VotingSystem.java new file mode 100644 index 0000000..7c3c59e --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s4/VotingSystem.java @@ -0,0 +1,96 @@ +import java.util.*; + +/** + * This class create a voting system that can create voting and print result. + * + * @author Mohammad Javad Rajabi + * @version 1.0 + * @since 2020-03-24 + */ +public class VotingSystem { + + //fields + + //list of created voting + private ArrayList votingList; + + //constructor + /** + * Voting system constructor, for create new object of this class. + */ + public VotingSystem() { + votingList = new ArrayList(); + } + + //methods + + /** + * This method is used to create a new voting. + * @param question This is question of voting. + * @param type This is parameter that declare type of voting. + * @param options This is array list of choices of this new voting. + */ + public void creatingVoting(String question, int type, ArrayList options) { + Voting voting = new Voting(type, question); + for (String option:options){ + voting.createChoice(option); + } + votingList.add(voting); + } + + /** + * This method is used to print a question of voting. + */ + public void printVotingQuestions() { + System.out.println("list of questions"); + for (Voting voting:votingList){ + + System.out.println((votingList.indexOf(voting)+1)+")"+voting.getQuestion()); + } + } + + /** + * This method is used to print a question of voting and its choices. + * @param index This a parameter that declare the index of voting in this system. + */ + public void printVoting(int index) { + Voting voting = votingList.get(index); + System.out.println("-------------------------------------------"); + System.out.println(voting.getQuestion()); + for (String option:voting.getChoices()) { + System.out.println(option); + } + } + + /** + * This method is used to add a person to list of voter if he already voted he cant vote. + * @param person This is a person who want to vote. + * @param options This is a list of choice that person choose. + * @param index This a parameter that declare the index of voting in this system. + */ + public void vote(int index, Person person, ArrayList options) { + Voting voting = votingList.get(index); + String option = options.get(0); + if (voting.getType() == 0){ + if (option == "random"){ + Random r = new Random(); + Set sets = voting.getListOfVoteToChoices().keySet(); + ArrayList stringsList = new ArrayList<>(sets); + option = stringsList.get(r.nextInt(voting.getListOfVoteToChoices().size()-1)); + } + options.remove(0) ; + options.add(option); + } + voting.vote(person,options); + } + + /** + * This method is used to print the choice and list of voter who vote to this choice. + * @param index This a parameter that declare the index of voting in this system. + */ + public void printResults(int index) { + System.out.println("-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-"); + System.out.println("result voting : "+ (index+1)); + votingList.get(index).printVotes(); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.1/Circle.java b/Mohammad Javad Rajabi - 9831025/s5.1/Circle.java new file mode 100644 index 0000000..fced2b5 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.1/Circle.java @@ -0,0 +1,102 @@ +import java.util.Objects; + +/** + * circle class + * this class is for circle shapes + * and has methods that can calculate perimeter and area of shape + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Circle { + + //fields + + /** + * variable that store value of radius + */ + private double radius; + + // constructor + + /** + * Constructor for create circle object + * @param radius this parameter is value of radius. + */ + public Circle(double radius) { + this.radius = radius; + } + + // methods + + /** + * This method is used to be able to access value of radius. + * @return double This returns value of radius. + */ + public double getRadius() { + return radius; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + public double calculatePerimeter() { + double perimeter; + perimeter = 2 * Math.PI * radius; + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + public double calculateArea() { + double area; + area = Math.PI * radius * radius; + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area + */ + public void draw() { + System.out.printf("Circle{perimeter = %.2f area = %.2f}", calculatePerimeter(), calculateArea()); + System.out.println(); + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Circle)) return false; + Circle circle = (Circle) o; + return Double.compare(circle.getRadius(), getRadius()) == 0; + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getRadius()); + } + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and radius of this shape. + */ + @Override + public String toString() { + return "Circle:: " + + "radius:" + radius; + } + +} \ No newline at end of file diff --git a/Mohammad Javad Rajabi - 9831025/s5.1/Main.java b/Mohammad Javad Rajabi - 9831025/s5.1/Main.java new file mode 100644 index 0000000..b46ec01 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.1/Main.java @@ -0,0 +1,34 @@ +/** + * this class is for test project + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Main { + + public static void main(String[] args) { + + Circle circle1 = new Circle(19); + Circle circle2 = new Circle(3); + + Rectangle rect1 = new Rectangle(1.0, 4.0); + Rectangle rect2 = new Rectangle(8.0, 5.0); + Rectangle rect3 = new Rectangle(6.0, 6.0); + + Triangle tri1 = new Triangle(2.0, 2.0, 2.0); + Triangle tri2 = new Triangle(4.0, 4.0, 6.0); + + 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/Mohammad Javad Rajabi - 9831025/s5.1/Paint.java b/Mohammad Javad Rajabi - 9831025/s5.1/Paint.java new file mode 100644 index 0000000..1501fd3 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.1/Paint.java @@ -0,0 +1,109 @@ +import java.util.ArrayList; + +/** + * this class is for all shapes + * and can add three type of shape with addTriangle,addCircle and addRectangle method + * also has methods that can draw and print all shape + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Paint { + + + //fields + + /** + * list of circles + */ + private ArrayList circles; + /** + * list of triangles + */ + private ArrayList triangles; + /** + * list of rectangles + */ + private ArrayList rectangles; + + + //constructors + + /** + * Constructor for create paint object + */ + public Paint() { + circles = new ArrayList<>(); + triangles = new ArrayList<>(); + rectangles = new ArrayList<>(); + } + + // methods + + /** + * This method is used to add a new shape to list. + * @param triangle This is object that add to list of shapes. + */ + public void addTriangle(Triangle triangle) { + triangles.add(triangle); + } + + /** + * This method is used to add a new shape to list. + * @param circle This is object that add to list of shapes. + */ + public void addCircle(Circle circle) { + circles.add(circle); + } + + /** + * This method is used to add a new shape to list. + * @param rectangle This is object that add to list of shapes. + */ + public void addRectangle(Rectangle rectangle) { + rectangles.add(rectangle); + } + + /** + * This method is used to draw type of all shapes + * and their perimeter and area are printed + */ + public void drawAll() { + System.out.println("----------------------"); + System.out.println("draw list of shapes"); + for (Circle circle : + circles) { + circle.draw(); + } + for (Triangle triangle : + triangles) { + triangle.draw(); + } + for (Rectangle rectangle : + rectangles) { + rectangle.draw(); + } + } + + /** + * This method is used to draw type of all shapes + * and their sides or radius. + */ + public void printAll() { + System.out.println("----------------------"); + System.out.println("print list of shapes"); + for (Circle circle : + circles) { + System.out.println(circle.toString()); + } + for (Triangle triangle : + triangles) { + System.out.println(triangle.toString()); + } + for (Rectangle rectangle : + rectangles) { + System.out.println(rectangle.toString()); + } + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.1/Rectangle.java b/Mohammad Javad Rajabi - 9831025/s5.1/Rectangle.java new file mode 100644 index 0000000..1124b07 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.1/Rectangle.java @@ -0,0 +1,118 @@ +import java.util.ArrayList; +import java.util.Objects; + +/** + * rectangle class + * this class is for polygon shapes that are rectangle + * and has methods that can calculate perimeter and area of shape + * also method that check this shape is square or not + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Rectangle { + + //fields + + /** + * list of sides of rectangle + */ + private ArrayList sides; + + //constructors + + /** + * Constructor for create rectangle object + * @param sides this parameter is list of sides of rectangle + */ + public Rectangle(Double... sides) { + this.sides = new ArrayList(); + for (Double side : sides) { + this.sides.add(side); + } + } + + // methods + + /** + * This method is used to be able to access list of sides. + * @return ArrayList This returns list of sides. + */ + public ArrayList getSides() { + return sides; + } + + /** + * This method is used to check this shape is square or not + * @return boolean This returns true if this shape is square. + */ + public boolean isSquare() { + if (sides.get(0).equals(sides.get(1))) { + return true; + } + return false; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + public double calculatePerimeter() { + double perimeter; + perimeter = (sides.get(0) + sides.get(1)) * 2; + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + public double calculateArea() { + double area; + area = sides.get(0) * sides.get(1); + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area + */ + public void draw() { + System.out.printf("Rectangle{perimeter = %.2f area = %.2f}", calculatePerimeter(), calculateArea()); + System.out.println(); + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Rectangle)) return false; + Rectangle rectangle = (Rectangle) o; + return getSides().equals(rectangle.getSides()); + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getSides()); + } + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and sides of this shape. + */ + @Override + public String toString() { + return "Rectangle:: " + + "side1:" + sides.get(0) + ", " + + "side2:" + sides.get(1); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.1/Triangle.java b/Mohammad Javad Rajabi - 9831025/s5.1/Triangle.java new file mode 100644 index 0000000..4dc9e01 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.1/Triangle.java @@ -0,0 +1,120 @@ +import java.util.ArrayList; +import java.util.Objects; + +/** + * triangle class + * this class is for polygon shapes that are triangle + * and has methods that can calculate perimeter and area of shape + * also method that check this shape is equilateral or not + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Triangle { + + //fields + + /** + * list of sides of Triangle + */ + private ArrayList sides; + + //constructors + + /** + * Constructor for create Triangle object + * @param sides this parameter is list of sides of Triangle + */ + public Triangle(Double... sides) { + this.sides = new ArrayList(); + for (Double side : sides) { + this.sides.add(side); + } + } + + // methods + + /** + * This method is used to be able to access list of sides. + * @return ArrayList This returns list of sides. + */ + public ArrayList getSides() { + return sides; + } + + /** + * This method is used to check this shape is equilateral or not + * @return boolean This returns true if this shape is equilateral. + */ + public boolean isEquilateral() { + if (sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2))){ + return true; + } + return false; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + public double calculatePerimeter() { + double perimeter; + perimeter = sides.get(0) + sides.get(1) +sides.get(2); + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + public double calculateArea() { + double area; + double p= (sides.get(0) + sides.get(1) +sides.get(2))/2; + area = Math.sqrt(p*(p-sides.get(0))*(p-sides.get(1))*(p-sides.get(2))); + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area + */ + public void draw() { + System.out.printf("Triangle{perimeter = %.2f area = %.2f}", calculatePerimeter(), calculateArea()); + System.out.println(); + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Triangle)) return false; + Triangle triangle = (Triangle) o; + return getSides().equals(triangle.getSides()); + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getSides()); + } + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and sides of this shape. + */ + @Override + public String toString() { + return "Triangle:: " + + "side1:" + sides.get(0) + ", " + + "side2:" + sides.get(1) + ", " + + "side3:" + sides.get(2); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Circle.java b/Mohammad Javad Rajabi - 9831025/s5.2/Circle.java new file mode 100644 index 0000000..2e0daf9 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Circle.java @@ -0,0 +1,104 @@ +import java.util.Objects; + +/** + * circle class are extended of shape class + * this class is for circle shapes + * and has methods that can calculate perimeter and area of shape + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Circle extends Shape { + + //fields + + /** + * variable that store value of radius + */ + private double radius; + + // constructor + + /** + * Constructor for create circle object + * @param radius this parameter is value of radius. + */ + public Circle(double radius) { + this.radius = radius; + } + + // methods + + /** + * This method is used to be able to access value of radius. + * @return ArrayList This returns value of radius. + */ + public double getRadius() { + return radius; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + @Override + public double calculatePerimeter() { + double perimeter; + perimeter = 2 * Math.PI * radius; + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + @Override + public double calculateArea() { + double area; + area = Math.PI * radius * radius; + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area are printed via super class + */ + @Override + public void draw() { + System.out.printf("Circle"); + super.draw(); + } + + /** + * This method is used to create a string that declare type and radius of this shape. + * @return String This returns a string that declare type and radius of this shape. + */ + @Override + public String toString() { + return "Circle:: " + + "radius:" + radius; + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Circle)) return false; + Circle circle = (Circle) o; + return Double.compare(circle.getRadius(), getRadius()) == 0; + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getRadius()); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Main.java b/Mohammad Javad Rajabi - 9831025/s5.2/Main.java new file mode 100644 index 0000000..b1d77e8 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Main.java @@ -0,0 +1,37 @@ +/** + * this class is for test project + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Main { + + public static void main(String[] args) { + Circle circle1 = new Circle(19); + Circle circle2 = new Circle(3); + + Rectangle rect1 = new Rectangle(1.0, 4.0); + Rectangle rect2 = new Rectangle(8.0, 5.0); + Rectangle rect3 = new Rectangle(6.0, 6.0); + + Triangle tri1 = new Triangle(2.0, 2.0, 2.0); + Triangle tri2 = new Triangle(4.0, 4.0, 6.0); + + 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.printAll(); + + paint.describeEqualSides(); + } +} + + diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Paint.java b/Mohammad Javad Rajabi - 9831025/s5.2/Paint.java new file mode 100644 index 0000000..02bf9ce --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Paint.java @@ -0,0 +1,85 @@ +import java.util.ArrayList; + +/** + * this class is for all shapes + * and can add all type of shape with addShape method + * also has methods that can draw and print all shape + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Paint { + + //fields + + /** + * list of shapes + */ + private ArrayList shapes; + + //constructors + + /** + * Constructor for create paint object + */ + public Paint() { + shapes = new ArrayList(); + } + + // methods + + /** + * This method is used to add a new shape to list. + * @param shape This is object that add to list of shapes. + */ + public void addShape(Shape shape) { + shapes.add(shape); + } + + /** + * This method is used to draw type of all shapes + * and their perimeter and area are printed + */ + public void drawAll() { + System.out.println("----------------------"); + System.out.println("draw list of shapes"); + for (Shape shape : shapes) { + shape.draw(); + } + } + + /** + * This method is used to draw type of all shapes + * and their sides or radius. + */ + public void printAll() { + System.out.println("----------------------"); + System.out.println("print list of shapes"); + for (Shape shape : shapes) { + System.out.println(shape.toString()); + } + } + + /** + * This method is used to check which polygon have equal sides. + */ + public void describeEqualSides() { + for (Shape shape : shapes) { + if (shape instanceof Triangle){ + if (((Triangle) shape).isEquilateral()){ + System.out.println("----------------------"); + System.out.println("this shape is equilateral"); + System.out.println(shape.toString()); + } + } + else if (shape instanceof Rectangle){ + if (((Rectangle) shape).isSquare()){ + System.out.println("----------------------"); + System.out.println("this shape is square"); + System.out.println(shape.toString()); + } + } + } + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Polygon.java b/Mohammad Javad Rajabi - 9831025/s5.2/Polygon.java new file mode 100644 index 0000000..d92293e --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Polygon.java @@ -0,0 +1,70 @@ +import java.util.ArrayList; + +/** + * polygon class are extended of shape class + * this class is for shapes that are polygon + * and has list of sides for this shape + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Polygon extends Shape { + + + //fields + + /** + * list of sides of polygon + */ + protected ArrayList sides; + + + //constructors + + /** + * Constructor for create polygon object + * @param sides this parameter is list of sides of polygon + */ + public Polygon(Double... sides) { + this.sides = new ArrayList(); + for (Double side : sides) { + this.sides.add(side); + } + } + + + // methods + + /** + * This method is used to be able to access list of sides. + * @return ArrayList This returns list of sides. + */ + public ArrayList getSides() { + return sides; + } + + @Override + public double calculatePerimeter() { + return 0; + } + + @Override + public double calculateArea() { + return 0; + } + + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and sides of this shape. + */ + @Override + public String toString() { + String string = " "; + for (Double side : sides) { + string += "side" + (sides.indexOf(side) + 1) + ":" + side + " "; + } + return string; + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Rectangle.java b/Mohammad Javad Rajabi - 9831025/s5.2/Rectangle.java new file mode 100644 index 0000000..c855250 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Rectangle.java @@ -0,0 +1,96 @@ +import java.util.Objects; + +/** + * rectangle class are extended of polygon class + * this class is for polygon shapes that are rectangle + * and has methods that can calculate perimeter and area of shape + * also method that check this shape is square or not + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Rectangle extends Polygon { + + // constructor + + /** + * Constructor for create rectangle object + * @param sides this parameter is list of sides of rectangle + */ + public Rectangle(Double... sides) { + super(sides); + } + + /** + * This method is used to check this shape is square or not + * @return boolean This returns true if this shape is square. + */ + public boolean isSquare() { + if (sides.get(0).equals(sides.get(1))) { + return true; + } + return false; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + public double calculatePerimeter() { + double perimeter; + perimeter = (sides.get(0) + sides.get(1)) * 2; + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + public double calculateArea() { + double area; + area = sides.get(0) * sides.get(1); + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area are printed via super class + */ + public void draw() { + System.out.printf("Rectangle"); + super.draw(); + } + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and sides of this shape. + */ + @Override + public String toString() { + String string = "Rectangle::"; + return string += super.toString(); + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Rectangle)) return false; + Rectangle rectangle = (Rectangle) o; + return getSides().equals(rectangle.getSides()); + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getSides()); + } +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Shape.java b/Mohammad Javad Rajabi - 9831025/s5.2/Shape.java new file mode 100644 index 0000000..966b3a4 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Shape.java @@ -0,0 +1,52 @@ +/** + * this class is for all shapes + * and has abstract methods that can calculate perimeter and area of shape via sub class + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public abstract class Shape { + + // constructor + + /** + * Constructor for create shape object + */ + public Shape() { + } + + /** + * This method is used to calculate perimeter in sub class. + * @return double This returns value of perimeter. + */ + abstract public double calculatePerimeter(); + + /** + * This method is used to calculate area in sub class. + * @return double This returns value of area. + */ + abstract public double calculateArea(); + + /** + * This method is used to draw type of shape are printed via super class + * and its perimeter and area + */ + public void draw(){ + System.out.printf("{perimeter = %.2f area = %.2f}", calculatePerimeter(), calculateArea()); + System.out.println(); + } + + @Override + public int hashCode() { + return super.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + abstract public String toString(); +} diff --git a/Mohammad Javad Rajabi - 9831025/s5.2/Triangle.java b/Mohammad Javad Rajabi - 9831025/s5.2/Triangle.java new file mode 100644 index 0000000..2e048b9 --- /dev/null +++ b/Mohammad Javad Rajabi - 9831025/s5.2/Triangle.java @@ -0,0 +1,98 @@ +import java.util.Objects; + +/** + * triangle class are extended of polygon class + * this class is for polygon shapes that are triangle + * and has methods that can calculate perimeter and area of shape + * also method that check this shape is equilateral or not + * + * @author Mohammad Javad Rajabi + * @version 1.0.0 + * @since 2020-04-10 + */ +public class Triangle extends Polygon { + + // constructor + + /** + * Constructor for create triangle object + * @param sides this parameter is list of sides of triangle + */ + public Triangle(Double... sides) { + super(sides); + } + + /** + * This method is used to check this shape is equilateral or not + * @return boolean This returns true if this shape is equilateral. + */ + public boolean isEquilateral() { + if (sides.get(0).equals(sides.get(1)) && sides.get(1).equals(sides.get(2))){ + return true; + } + return false; + } + + /** + * This method is used to calculate perimeter. + * @return double This returns value of perimeter. + */ + public double calculatePerimeter() { + double perimeter; + perimeter = sides.get(0) + sides.get(1) +sides.get(2); + return perimeter; + } + + /** + * This method is used to calculate area. + * @return double This returns value of area. + */ + public double calculateArea() { + double area; + double p= (calculatePerimeter())/2; + area = Math.sqrt(p*(p-sides.get(0))*(p-sides.get(1))*(p-sides.get(2))); + return area; + } + + /** + * This method is used to draw type of shape + * and its perimeter and area are printed via super class + */ + public void draw() { + System.out.printf("Triangle"); + super.draw(); + } + + /** + * This method is used to create a string that declare type and sides of this shape. + * @return String This returns a string that declare type and sides of this shape. + */ + @Override + public String toString() { + String string = "Triangle::"; + return string += super.toString(); + } + + /** + * This method is used to check equality of this shape and o obj + * @param o This is a object that we want check the equality + * @return boolean this returns true if input object is equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Triangle)) return false; + Triangle triangle = (Triangle) o; + return getSides().equals(triangle.getSides()); + } + + /** + * This method is used to create hash code + * @return int this return hash code + */ + @Override + public int hashCode() { + return Objects.hash(getSides()); + } + +} diff --git a/README.md b/README.md deleted file mode 100644 index ab9e3fe..0000000 --- a/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Workshop 2 - -AUT AP Workshop 2 - 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 -