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/AmirparsaSalmankhah/Paint/src/com/company/Circle.java b/AmirparsaSalmankhah/Paint/src/com/company/Circle.java new file mode 100644 index 0000000..f96e704 --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Circle.java @@ -0,0 +1,67 @@ +package com.company; + +/** + * Represents a circle. + * @author Amirparsa Salmnakhah + * @version 1.0.0 + */ +public class Circle extends Shape{ + //radius + private double radius; + + /** + * Constructor with a paramter. + * @param radius Radius of the circle + */ + public Circle(double radius) { + this.radius = radius; + } + + /** + * Gets radius of the circle + * @return Radius of the circle + */ + public double getRadius() { + return radius; + } + + /** + * Calculates area of the shape + * @return Area of the shape + */ + @Override + public double calculateArea() { + return Math.pow(radius, 2) * Math.PI; + } + + /** + * Calculates perimeter of the shape. + * @return Perimeter of the shape + */ + @Override + public double calculatePerimeter() { + return 2 * radius * Math.PI; + } + + /** + * Draws the circle + */ + @Override + public void draw() { + System.out.println("Circle "); + super.draw(); + } + + /** + * Checks the equality between the circle and another object. + * @param o Another object + * @return true if they are equal and false if not. + */ + @Override + public boolean equals(Object o){ + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Circle circle = (Circle) o; + return radius == circle.radius; + } +} diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Main.java b/AmirparsaSalmankhah/Paint/src/com/company/Main.java new file mode 100644 index 0000000..225db7f --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Main.java @@ -0,0 +1,32 @@ +package com.company; + +/** + * This is a class for testing our painting. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +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(); + System.out.println(); + paint.printAll(); + System.out.println(); + paint.describeEqualSides(); + } +} diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Paint.java b/AmirparsaSalmankhah/Paint/src/com/company/Paint.java new file mode 100644 index 0000000..223aef5 --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Paint.java @@ -0,0 +1,56 @@ +package com.company; + +import java.util.ArrayList; + +/** + * Represents a class for painting shapes. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Paint { + //list of shapes + private ArrayList shapes; + + /** + * Constructor with no parameter. + */ + public Paint() { + shapes = new ArrayList<>(); + } + + /** + * Adds a shape to list of the shapes. + * @param shape The shape + */ + public void addShape(Shape shape) { + shapes.add(shape); + } + + /** + * Draws all of the shapes + */ + public void drawAll() { + for(Shape shape : shapes) + shape.draw(); + } + + /** + * Prints all of the shapes + */ + public void printAll() { + for(Shape shape : shapes) + System.out.println(shape); + } + + /** + * Prints all of equal side shapes. + */ + public void describeEqualSides(){ + for(Shape shape : shapes){ + if(shape instanceof Rectangle && ((Rectangle) shape).isSquare()) + System.out.println(shape); + else if(shape instanceof Triangle && ((Triangle) shape).isEquilateral()) + System.out.println(shape); + } + } +} diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Polygon.java b/AmirparsaSalmankhah/Paint/src/com/company/Polygon.java new file mode 100644 index 0000000..b633d43 --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Polygon.java @@ -0,0 +1,45 @@ +package com.company; + +import java.util.ArrayList; +import java.util.Collections; + +/** + * Represents a polygon. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +abstract public class Polygon extends Shape { + //sides + private ArrayList sides; + + /** + * Constructor with var args parameters. + * @param sides Some double numbers.(sides of the polygon) + */ + public Polygon(double... sides) { + this.sides = new ArrayList<>(); + for (double side : sides) + this.sides.add(side); + Collections.sort(this.sides); + } + + /** + * Gets list of sides. + * @return List of sides + */ + public ArrayList getSides() { + return sides; + } + + /** + * Returns an string containing polygon details. + * @return polygon details + */ + @Override + public String toString() { + String str = super.toString() + " "; + for (int i = 0; i < sides.size(); i++) + str = str + "Side" + (i + 1) + ":" + sides.get(i) + " "; + return str; + } +} diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Rectangle.java b/AmirparsaSalmankhah/Paint/src/com/company/Rectangle.java new file mode 100644 index 0000000..4ed298e --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Rectangle.java @@ -0,0 +1,75 @@ +package com.company; + +/** + * Represents a rectangle. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Rectangle extends Polygon { + + /** + * Constructor with 4 parameters. + * @param side1 first side + * @param side2 second side + * @param side3 third side + * @param side4 forth side + */ + public Rectangle(double side1, double side2, double side3, double side4) { + super(side1, side2, side3, side4); + } + + /** + * Checks if the rectangle is an square. + * @return true if it is and false if not. + */ + public boolean isSquare() { + boolean check = true; + for (int i = 0; i < 4; i++) + for (int j = i + 1; j < 4; j++) + if (!getSides().get(i).equals(getSides().get(j))) { + check = false; + break; + } + return check; + } + + /** + * Calculates area of the shape + * @return Area of the shape + */ + @Override + public double calculateArea() { + return getSides().get(0) * getSides().get(2); + } + + /** + * Calculates perimeter of the shape. + * @return Perimeter of the shape + */ + @Override + public double calculatePerimeter() { + return 2 * (getSides().get(0) + getSides().get(2)); + } + + /** + * Draws the rectangle + */ + @Override + public void draw() { + System.out.println("Rectangle "); + super.draw(); + } + + /** + * Checks equality between the rectangle and another object. + * @param o Another object + * @return true if they are equal and false if not. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Rectangle rectangle = (Rectangle) o; + return rectangle.getSides().get(0).equals(getSides().get(0)) && rectangle.getSides().get(2).equals(getSides().get(2)); + } +} \ No newline at end of file diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Shape.java b/AmirparsaSalmankhah/Paint/src/com/company/Shape.java new file mode 100644 index 0000000..3440a4c --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Shape.java @@ -0,0 +1,47 @@ +package com.company; + +/** + * Represents a shape. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +abstract public class Shape { + /** + * Calculates area of the shape. + * @return Area of the shape + */ + abstract public double calculateArea(); + + /** + * Calculates perimeter of the shape. + * @return Perimeter of the shape + */ + abstract public double calculatePerimeter(); + + /** + * Draws the shape + */ + public void draw(){ + System.out.println("Area: " + calculateArea() + " Perimeter: " + calculatePerimeter()); + } + + /** + * Checks equality between the shape and another object + * @param o Another object + * @return true if they are equal and false if not + */ + abstract public boolean equals(Object o); + + /** + * returns an string containing shape details. + * @return shape details + */ + @Override + public String toString() { + if(this instanceof Triangle) + return "Triangle"; + else if(this instanceof Rectangle) + return "Rectangle"; + return "Circle: Radius:" + ((Circle)this).getRadius(); + } +} diff --git a/AmirparsaSalmankhah/Paint/src/com/company/Triangle.java b/AmirparsaSalmankhah/Paint/src/com/company/Triangle.java new file mode 100644 index 0000000..1db78eb --- /dev/null +++ b/AmirparsaSalmankhah/Paint/src/com/company/Triangle.java @@ -0,0 +1,67 @@ +package com.company; + +/** + * Represents a triangle. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Triangle extends Polygon{ + + /** + * Constructor with 3 parameters. + * @param side1 first side + * @param side2 second side + * @param side3 third side + */ + public Triangle(double side1, double side2, double side3) { + super(side1,side2,side3); + } + + /** + * Checks if the triangle is equilateral. + * @return true if it is and false if not. + */ + public boolean isEquilateral() { + return getSides().get(0).equals(getSides().get(1)) && getSides().get(0).equals(getSides().get(2)) && getSides().get(1).equals(getSides().get(2)); + } + + /** + * Calculates area of the shape + * @return Area of the shape + */ + @Override + public double calculateArea(){ + double p = calculatePerimeter() / 2; + return Math.sqrt(p * (p - getSides().get(0)) * (p - getSides().get(1)) * (p - getSides().get(2))); + } + + /** + * Calculates perimeter of the shape. + * @return Perimeter of the shape + */ + @Override + public double calculatePerimeter() { + return getSides().get(0) + getSides().get(1) + getSides().get(2); + } + + /** + * Draws the triangle. + */ + @Override + public void draw() { + System.out.println("Triangle "); + super.draw(); + } + + /** + * Checks the equality between the triangle and another object. + * @param o Another object + * @return true if they are equal and false if not. + */ + public boolean equals(Object o){ + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Triangle triangle = (Triangle) o; + return triangle.getSides().get(0).equals(getSides().get(0)) && triangle.getSides().get(1).equals(getSides().get(1)) && triangle.getSides().get(2).equals(getSides().get(2)); + } +} \ No newline at end of file diff --git a/AmirparsaSalmankhah/Vote/JalaliCalendar-1.3.1.jar b/AmirparsaSalmankhah/Vote/JalaliCalendar-1.3.1.jar new file mode 100644 index 0000000..236f2b6 Binary files /dev/null and b/AmirparsaSalmankhah/Vote/JalaliCalendar-1.3.1.jar differ diff --git a/AmirparsaSalmankhah/Vote/src/com/company/Main.java b/AmirparsaSalmankhah/Vote/src/com/company/Main.java new file mode 100644 index 0000000..fabe310 --- /dev/null +++ b/AmirparsaSalmankhah/Vote/src/com/company/Main.java @@ -0,0 +1,48 @@ +package com.company; + +import java.util.ArrayList; + +/** + * This is a class for testing voting systems. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Main { + public static void main(String[] args) { + VotingSystem vs = new VotingSystem(); + Person p1 = new Person("Amirparsa","Salmankhah"); + Person p2 = new Person("Ali","Ansari"); + Person p3 = new Person("Parham","Moonesi"); + ArrayList choices1 = new ArrayList<>(); + choices1.add("Messi"); + ArrayList choices2 = new ArrayList<>(); + choices2.add("Ronaldo"); + ArrayList choices3 = new ArrayList<>(); + choices3.add("Messi"); + choices3.add("Ronaldo"); + vs.createVoting("Who is the best player?",0); + vs.getVoting(1).createChoice("Messi"); + vs.getVoting(1).createChoice("Ronaldo"); + vs.vote(1,p1,choices1); + vs.vote(1,p1,choices1); + vs.vote(1,p2,choices2); + vs.vote(1,p3,choices3); + vs.printResult(1); + System.out.println("\n\n\n\n"); + vs.createVoting("Who is your favourite player?",1); + vs.getVoting(2).createChoice("Messi"); + vs.getVoting(2).createChoice("Ronaldo"); + vs.getVoting(2).createChoice("Neymar"); + vs.vote(2,p1,choices1); + vs.vote(2,p2,choices2); + vs.vote(2,p3,choices3); + vs.printResult(2); + System.out.println("\n\n\n\n"); + vs.printListOfVotings(); + System.out.println(); + vs.printResult(1); + System.out.println(); + vs.printResult(2); + + } +} diff --git a/AmirparsaSalmankhah/Vote/src/com/company/Person.java b/AmirparsaSalmankhah/Vote/src/com/company/Person.java new file mode 100644 index 0000000..6769d92 --- /dev/null +++ b/AmirparsaSalmankhah/Vote/src/com/company/Person.java @@ -0,0 +1,43 @@ +package com.company; + +public class Person { + //First name of the person + private String firstName; + //Last name of the person + private String lastName; + + /** + * Constructor with 2 paramters + * @param firstName First name of the person + * @param lastName Last name of the person + */ + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + /** + * Gets first name of the person + * @return First name of the person + */ + public String getFirstName() { + return firstName; + } + + /** + * Gets last name of the person + * @return Last name of the person + */ + public String getLastName() { + return lastName; + } + + /** + * Returns an string containing person's name + * @return Person's name + */ + @Override + public String toString() { + return firstName + " " + lastName; + } +} diff --git a/AmirparsaSalmankhah/Vote/src/com/company/Vote.java b/AmirparsaSalmankhah/Vote/src/com/company/Vote.java new file mode 100644 index 0000000..485e802 --- /dev/null +++ b/AmirparsaSalmankhah/Vote/src/com/company/Vote.java @@ -0,0 +1,64 @@ +package com.company; + +import java.util.Objects; + +/** + * Represents a vote + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Vote { + //Person + private Person person; + //Date + private String date; + + /** + * Constructor with 2 parameters + * @param person Person + * @param date Date + */ + public Vote(Person person, String date) { + this.person = person; + this.date = date; + } + + /** + * Person getter + * @return Person + */ + public Person getPerson() { + return person; + } + + /** + * Date getter + * @return Date + */ + public String getDate() { + return date; + } + + /** + * Checks equality between the vote and another object. + * @param o Another object + * @return true if they are equal and false if not + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Vote vote = (Vote) o; + return person.equals(vote.person); + } + + /** + * Calculates hash of the vote. + * @return Vote object's hash + */ + @Override + public int hashCode() { + return Objects.hash(person, date); + } + +} diff --git a/AmirparsaSalmankhah/Vote/src/com/company/Voting.java b/AmirparsaSalmankhah/Vote/src/com/company/Voting.java new file mode 100644 index 0000000..b35cc3b --- /dev/null +++ b/AmirparsaSalmankhah/Vote/src/com/company/Voting.java @@ -0,0 +1,117 @@ +package com.company; + +import ir.huri.jcal.JalaliCalendar; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +/** + * Represents a voting. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class Voting { + //type of the voting + private int type; + //question of the voting + private String question; + //list of voters + private ArrayList voters; + //choices of the voting + private HashMap> choices; + + /** + * Constructor with 2 parameters. + * @param type Type of the voting + * @param question Question of the voting + */ + public Voting(int type, String question) { + this.type = type; + this.question = question; + voters = new ArrayList<>(); + choices = new HashMap<>(); + } + + /** + * Gets question of the voting. + * @return Question of the voting + */ + public String getQuestion() { + return question; + } + + /** + * Gets list of voters. + * @return List of voters + */ + public ArrayList getVoters() { + return voters; + } + + /** + * Gets choices of the voting. + * @return Hash map of choises + */ + public HashMap> getChoices() { + return choices; + } + + /** + * Creates a choice + * @param choice choice name + */ + public void createChoice(String choice) { + boolean checkIfExists = false; + for (String c : choices.keySet()) + if (c.equals(choice)) { + checkIfExists = true; + break; + } + if (!checkIfExists) + choices.put(choice, new HashSet<>()); + } + + /** + * votes from a person to some choices + * @param person Person + * @param choices List of choices + */ + public void vote(Person person, ArrayList choices) { + if (choices.size() > 1 && type == 0) { + System.out.println("You cannot choose multiple choices in this voting!"); + return; + } + boolean isVoted=false; + for(Person p : voters) + if(p.equals(person)) + isVoted=true; + if(!isVoted) + voters.add(person); + else{ + System.out.println("You have already submitted your vote!"); + return; + } + for (String choice : choices) { + JalaliCalendar date = new JalaliCalendar(); + HashSet votes = this.choices.get(choice); + votes.add(new Vote(person,date.toString())); + System.out.println(person.toString() + " voted to " + choice); + } + } + + /** + * Prints votes + */ + public void printVotes(){ + System.out.println("Vote result: (Choice Number of votes)"); + int index = 1; + for(String choice : choices.keySet()){ + HashSet votes = choices.get(choice); + System.out.println(index + ") " + choice + " " + votes.size()); + for(Vote v : votes) + System.out.println(v.getPerson().toString() + " " + v.getDate().toString()); + index++; + } + } +} diff --git a/AmirparsaSalmankhah/Vote/src/com/company/VotingSystem.java b/AmirparsaSalmankhah/Vote/src/com/company/VotingSystem.java new file mode 100644 index 0000000..eedd0ca --- /dev/null +++ b/AmirparsaSalmankhah/Vote/src/com/company/VotingSystem.java @@ -0,0 +1,92 @@ +package com.company; + +import java.util.ArrayList; + +/** + * Represents a voting system containing some votings. + * @author Amirparsa Salmankhah + * @version 1.0.0 + */ +public class VotingSystem { + //List of votings + private ArrayList votingList; + + /** + * Constructor with no parameter. + */ + public VotingSystem(){ + votingList = new ArrayList<>(); + } + + /** + * Creates a voting and adds it to the list. + * @param question Question of voting + * @param type Type of voting + */ + public void createVoting(String question, int type){ + votingList.add(new Voting(type,question)); + } + + /** + * Prints votings list + */ + public void printListOfVotings(){ + System.out.println("Active votings:"); + int index = 1; + for(Voting v : votingList) { + System.out.println(index + ") " + v.getQuestion()); + index++; + } + } + + /** + * Prints a voting + * @param votingNumber Number of the voting + */ + public void printVoting(int votingNumber){ + Voting voting = votingList.get(votingNumber-1); + System.out.println(voting.getQuestion()); + int index = 1; + for(String choice : voting.getChoices().keySet()) { + System.out.println(index + ") " + choice); + index++; + } + } + + /** + * Votes from a person to a an specific voting. + * @param votingNumber Number of the voting + * @param person Person + * @param choices Choices of the person + */ + public void vote(int votingNumber, Person person, ArrayList choices){ + Voting voting = votingList.get(votingNumber-1); + voting.vote(person,choices); + } + + /** + * Prints result of a voting + * @param votingNumber Number of the voting + */ + public void printResult(int votingNumber){ + Voting voting = votingList.get(votingNumber-1); + voting.printVotes(); + } + + /** + * Gets an specific voting. + * @param votingNumber Number of the voting + * @return + */ + public Voting getVoting(int votingNumber){ + return votingList.get(votingNumber-1); + } + + /** + * Gets votings list. + * @return List of votings. + */ + public ArrayList getVotingList() { + return votingList; + } +} \ No newline at end of file diff --git a/README.md b/README.md index ab9e3fe..d9f994c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ # Workshop 2 - -AUT AP Workshop 2 +## Amirparsa Salmankhah 9831034 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 -