diff --git a/RohamHayedi/RohamHayedi.txt b/RohamHayedi/RohamHayedi.txt new file mode 100644 index 0000000..842491b --- /dev/null +++ b/RohamHayedi/RohamHayedi.txt @@ -0,0 +1,2 @@ +Full Name: Roham Hayedi +Student Number: 9831107 \ No newline at end of file diff --git a/RohamHayedi/lab4/Person.java b/RohamHayedi/lab4/Person.java new file mode 100644 index 0000000..c570818 --- /dev/null +++ b/RohamHayedi/lab4/Person.java @@ -0,0 +1,25 @@ +public class Person { + + private final String firstName; + private final String lastName; + + 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 firstName +" "+ + lastName ; + } +} diff --git a/RohamHayedi/lab4/Run.java b/RohamHayedi/lab4/Run.java new file mode 100644 index 0000000..313c81a --- /dev/null +++ b/RohamHayedi/lab4/Run.java @@ -0,0 +1,66 @@ +import ir.huri.jcal.JalaliCalendar; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class Run { + public static void main(String[] args) { + + VotingSystem votingSystem = new VotingSystem(); + ArrayList options0 = new ArrayList<>(); + ArrayList options1 = new ArrayList<>(); + ArrayList options2 = new ArrayList<>(); + + options0.add("Pizza"); + options0.add("Sandwich"); + options0.add("Neither"); + + options1.add("Saturday"); + options1.add("Sunday"); + options1.add("Monday"); + + votingSystem.createVoting("Pizza or Sandwich?", 0, options0); + votingSystem.createVoting("When are you available?", 1, options1); + + Person me = new Person("Roham", "Hayedi"); + ArrayList myChoices0 = new ArrayList<>(); + ArrayList myChoices1 = new ArrayList<>(); + myChoices0.add("Pizza"); + myChoices1.add("Saturday"); + myChoices1.add("Saturday"); + myChoices1.add("Sunday"); + + votingSystem.vote(0, me, myChoices0); + votingSystem.vote(1, me, myChoices1); + System.out.println("Print questions:"); + votingSystem.printVotingQuestions(); + System.out.println("\nPrint voting 0 :"); + votingSystem.printVoting(0); + System.out.println("\nPrint result 0 :"); + votingSystem.printResult(0); + System.out.println("\nPrint voting 1 :"); + votingSystem.printVoting(1); + System.out.println("\nPrint result 1 :"); + votingSystem.printResult(1); + + Person person = new Person("kc", "sd"); + ArrayList kcsdChoices0 = new ArrayList(); + ArrayList kcsdChoices1 = new ArrayList(); + kcsdChoices0.add("Neither"); + kcsdChoices1.add("Saturday"); + votingSystem.vote(1, person, kcsdChoices1); + votingSystem.vote(0, person, kcsdChoices0); + votingSystem.vote(0, person, kcsdChoices0); + + System.out.println("\nPrint voting 0 :"); + votingSystem.printVoting(0); + System.out.println("\nPrint result 0 :"); + votingSystem.printResult(0); + System.out.println("\nPrint voting 1 :"); + votingSystem.printVoting(1); + System.out.println("\nPrint result 1 :"); + votingSystem.printResult(1); + + } +} diff --git a/RohamHayedi/lab4/Vote.java b/RohamHayedi/lab4/Vote.java new file mode 100644 index 0000000..8e01ea3 --- /dev/null +++ b/RohamHayedi/lab4/Vote.java @@ -0,0 +1,36 @@ +import ir.huri.jcal.JalaliCalendar; + +import java.util.Objects; + +public class Vote { + + private final Person person; + private final String date; + + 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) && + Objects.equals(date, vote.date); + } + + @Override + public int hashCode() { + return Objects.hash(person, date); + } +} diff --git a/RohamHayedi/lab4/Voting.java b/RohamHayedi/lab4/Voting.java new file mode 100644 index 0000000..4ae8916 --- /dev/null +++ b/RohamHayedi/lab4/Voting.java @@ -0,0 +1,79 @@ +import ir.huri.jcal.JalaliCalendar; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; + +public class Voting { + + private final int type; + private final String question; + private ArrayList voters; + private HashMap> polls; + + public Voting(String question, int type, ArrayList choices){ + this.type = type; + this.question = question; + polls = new HashMap<>(); + voters = new ArrayList<>(); + for (String choice : choices){ + polls.put(choice, new HashSet<>()); + } + } + + public void creatChoice(String choice){ + polls.put(choice, new HashSet<>()); + } + + public void vote(Person person, ArrayList choices){ + if (type == 0 && choices.size() > 1) { + System.out.println(person + "-> This voting (" + question + ") accepts only single choices"); + } + else { + if (!voters.contains(person)) { + voters.add(person); + Vote vote = new Vote(person, new JalaliCalendar().toString()); + for (String choice : choices) + if (polls.containsKey(choice)) + polls.get(choice).add(vote); + else { + System.out.println(person + "->The input (" + choice + ") is not among the choices"); + } + }else{ + System.out.println("The same person cannot vote twice"); + } + } + } + + public String getQuestion() { + return question; + } + + public void printVoters() { + int i = 0; + for (Person person : voters) { + System.out.println(i + "-" + person); + } + } + + public HashMap> getPolls() { + return polls; + } + + public void printChoices(){ + AtomicInteger i = new AtomicInteger(1); + polls.forEach((k,v) -> System.out.println(i.getAndIncrement() + "- " + k)); + } + + public void printResult(){ + System.out.println(question); + for (Map.Entry> entry : polls.entrySet()) { + String choice = entry.getKey(); + HashSet votes = entry.getValue(); + System.out.println(choice + " -> " + votes.size()); + votes.forEach(vote -> System.out.println(vote.getPerson() + " " + vote.getDate())); + } + } +} diff --git a/RohamHayedi/lab4/VotingSystem.java b/RohamHayedi/lab4/VotingSystem.java new file mode 100644 index 0000000..067d269 --- /dev/null +++ b/RohamHayedi/lab4/VotingSystem.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; + +public class VotingSystem { + + private ArrayList votingList; + + public VotingSystem(){ + votingList = new ArrayList<>(); + } + + public void createVoting(String question, int type, ArrayList choices ){ + votingList.add(new Voting(question, type, choices)); + } + + public void deleteVoting(int index){ + votingList.remove(index); + } + + public void vote(int index, Person person, ArrayList votes){ + votingList.get(index).vote(person, votes); + } + + public void printVotingQuestions() { + for (Voting voting : votingList){ + System.out.println(voting.getQuestion()); + } + } + + public void printVoting(int index){ + if (index < votingList.size()) { + System.out.println(votingList.get(index).getQuestion()); + votingList.get(index).printChoices(); + }else + System.out.println("Wrong index"); + } + + public void printResult(int index) { + if (index < votingList.size()) { + votingList.get(index).printResult(); + }else + System.out.println("Wrong index"); + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Circle.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Circle.java new file mode 100644 index 0000000..42b6ccb --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Circle.java @@ -0,0 +1,32 @@ +public class Circle extends Shape { + + private double radius; + + public Circle(double radius){ + this.radius = radius; + } + + public double getRadius() { + return radius; + } + + @Override + public void draw() { + System.out.println((char) 0x25EF); + } + + @Override + public double calculateParameter() { + return 2 * 3.14 * radius; + } + + @Override + public double calculateArea() { + return 3.14 * radius*radius; + } + + @Override + public String toString() { + return "radius = " + radius; + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Paint.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Paint.java new file mode 100644 index 0000000..ed2d6eb --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Paint.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.logging.SocketHandler; + +public class Paint { + + private ArrayList shapes; + + public Paint(){ + shapes = new ArrayList<>(); + } + + public void addShape(Shape shape){ + shapes.add(shape); + } + + public void drawAll(){ + for(Shape shape : shapes){ + shape.draw(); + } + } + + public void describeEqualSides(){ + System.out.println("describeEqualSides::"); + for (Shape shape : shapes){ + if (shape instanceof Triangle) { + if (((Triangle) shape).isEquilateral()) + System.out.println(shape); + }else if (shape instanceof Rectangle) + if (((Rectangle) shape).isSquare()) + System.out.println(shape); + } + } + + public void printAll(){ + for (Shape shape : shapes){ + System.out.println(shape); + } + } + +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Polygon.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Polygon.java new file mode 100644 index 0000000..545d4d1 --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Polygon.java @@ -0,0 +1,20 @@ +import java.util.ArrayList; + +public abstract class Polygon extends Shape { + + private ArrayList sides; + + public Polygon(double... args){ + sides = new ArrayList<>(); + for (double side : args){ + sides.add(side); + } + } + + @Override + public abstract void draw(); + + public ArrayList getSides() { + return sides; + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Rectangle.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Rectangle.java new file mode 100644 index 0000000..3f43db9 --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Rectangle.java @@ -0,0 +1,50 @@ +import java.util.ArrayList; + +public class Rectangle extends Polygon { + + public Rectangle(double... args){ + super(args); + } + + @Override + public void draw() { + System.out.println((char) 0x25AD); + } + + @Override + public double calculateArea() { + ArrayList sides = super.getSides(); + return sides.get(0).equals(sides.get(1)) ? sides.get(0) * sides.get(2) : sides.get(0) * sides.get(1); + } + + @Override + public double calculateParameter() { + double param = 0; + for (double side : super.getSides()){ + param += side; + } + return param; + } + + public boolean isSquare(){ + ArrayList sides = super.getSides(); + double sampleSide = sides.get(0); + for (double side : sides){ + if (side != sampleSide) + return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + ArrayList sides = super.getSides(); + stringBuilder.append("First pair: "); + stringBuilder.append(sides.get(0)); + stringBuilder.append(", Second pair: "); + stringBuilder.append(!sides.get(0).equals(sides.get(1)) ? sides.get(1) : sides.get(2)); + + return super.toString() + stringBuilder; + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Shape.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Shape.java new file mode 100644 index 0000000..127a797 --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Shape.java @@ -0,0 +1,28 @@ +public class Shape extends Paint { + + public double calculateParameter() { + return 0; + } + + public double calculateArea() { + return 0; + } + + public void draw(){} + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + + @Override + public String toString() { + if (this instanceof Triangle) + return "Triangle:: "; + else if (this instanceof Rectangle) + return "Rectangle:: "; + else if (this instanceof Circle) + return "Circle:: "; + return super.toString(); + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Triangle.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Triangle.java new file mode 100644 index 0000000..8b3cc85 --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/Triangle.java @@ -0,0 +1,31 @@ +import java.util.ArrayList; + +public class Triangle extends Polygon { + + public Triangle(double... args){ + super(args); + } + + @Override + public void draw() { + System.out.println((char) 0x25B3); + } + + public boolean isEquilateral(){ + return super.getSides().get(0).equals(super.getSides().get(1)) && + super.getSides().get(0).equals(super.getSides().get(2)); + } + + @Override + public String toString() { + StringBuilder stringBuilder = new StringBuilder(); + ArrayList sides = super.getSides(); + stringBuilder.append("side1: "); + stringBuilder.append(sides.get(0)); + stringBuilder.append(", side2: "); + stringBuilder.append(sides.get(1)); + stringBuilder.append(", side3: "); + stringBuilder.append(sides.get(2)); + return super.toString() + stringBuilder.toString(); + } +} diff --git a/RohamHayedi/lab5/Part1-PaintWithPolymorphism/main.java b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/main.java new file mode 100644 index 0000000..b8a92f9 --- /dev/null +++ b/RohamHayedi/lab5/Part1-PaintWithPolymorphism/main.java @@ -0,0 +1,31 @@ +public class main { + + public static void main(String[] args) { + Paint paint = new Paint(); + + Triangle triangle1 = new Triangle(1,2,3); + Triangle triangle2 = new Triangle(2,2,2); + Triangle triangle3 = new Triangle(3,4,5); + + Rectangle rectangle1 = new Rectangle(1,2,1,2); + Rectangle rectangle2 = new Rectangle(2,2,2,2); + Rectangle rectangle3 = new Rectangle(3,2,3,2); + + Circle circle1 = new Circle(2); + Circle circle2 = new Circle(3); + Circle circle3 = new Circle(5); + + paint.addShape(triangle1); + paint.addShape(triangle2); + paint.addShape(triangle3); + paint.addShape(rectangle1); + paint.addShape(rectangle2); + paint.addShape(rectangle3); + paint.addShape(circle1); + paint.addShape(circle2); + paint.addShape(circle3); + + paint.drawAll(); + paint.printAll(); + } +} diff --git a/RohamHayedi/lab5/Part2-Correction.txt b/RohamHayedi/lab5/Part2-Correction.txt new file mode 100644 index 0000000..6a7393d --- /dev/null +++ b/RohamHayedi/lab5/Part2-Correction.txt @@ -0,0 +1,19 @@ +public static void main(String[] args) { + Circle circle1 = new Shape(19); + Shape circle2 = new Circle(3); + Triangle rect1 = new Triangle(1,4,1); + Polygon rect2 = new Rectangle(8,5,8,5); + Rectangle rec3 = new Shape(6,6,6,6); + Polygon tri1 = new Triangle(2,2,2); + Triangle tri2 = new Triangle(4,4,6); + Shape tri3 = new Triangle(2,2,2); + + circle1 = (Circle) circle2; + rect2 = rec3; + tri1 = (Polygon) tri3; + circle2 = tri3; + tri3 = tri2; + + rec3 = new Shape(2,3,2); + System.out.println(rec3); + } \ No newline at end of file