From 3c9c588791d40ccd1fe58e4d364ecb02e82b81a9 Mon Sep 17 00:00:00 2001 From: rezahk <58387732+rezahk@users.noreply.github.com> Date: Sat, 25 Apr 2020 00:21:57 +0430 Subject: [PATCH] add lab4 & lab5 --- Mohamadrezahakimi/lab 4/Main.java | 82 +++++++++++++++++++ Mohamadrezahakimi/lab 4/person.java | 22 ++++++ Mohamadrezahakimi/lab 4/vote.java | 23 ++++++ Mohamadrezahakimi/lab 4/voting.java | 92 ++++++++++++++++++++++ Mohamadrezahakimi/lab 4/voting_system.java | 35 ++++++++ Mohamadrezahakimi/lab 5/Circle.java | 33 ++++++++ Mohamadrezahakimi/lab 5/Main.java | 32 ++++++++ Mohamadrezahakimi/lab 5/Paint.java | 53 +++++++++++++ Mohamadrezahakimi/lab 5/Polygon.java | 46 +++++++++++ Mohamadrezahakimi/lab 5/rectangle.java | 25 ++++++ Mohamadrezahakimi/lab 5/shape.java | 22 ++++++ Mohamadrezahakimi/lab 5/triangle.java | 26 ++++++ 12 files changed, 491 insertions(+) create mode 100644 Mohamadrezahakimi/lab 4/Main.java create mode 100644 Mohamadrezahakimi/lab 4/person.java create mode 100644 Mohamadrezahakimi/lab 4/vote.java create mode 100644 Mohamadrezahakimi/lab 4/voting.java create mode 100644 Mohamadrezahakimi/lab 4/voting_system.java create mode 100644 Mohamadrezahakimi/lab 5/Circle.java create mode 100644 Mohamadrezahakimi/lab 5/Main.java create mode 100644 Mohamadrezahakimi/lab 5/Paint.java create mode 100644 Mohamadrezahakimi/lab 5/Polygon.java create mode 100644 Mohamadrezahakimi/lab 5/rectangle.java create mode 100644 Mohamadrezahakimi/lab 5/shape.java create mode 100644 Mohamadrezahakimi/lab 5/triangle.java diff --git a/Mohamadrezahakimi/lab 4/Main.java b/Mohamadrezahakimi/lab 4/Main.java new file mode 100644 index 0000000..5443fa2 --- /dev/null +++ b/Mohamadrezahakimi/lab 4/Main.java @@ -0,0 +1,82 @@ +import java.util.ArrayList; +import java.util.Arrays; + +public class Main { + + public static void main(String[] args) { + + voting_system vs=new voting_system(); + ArrayList choices1=new ArrayList<>(); + + choices1.add("Ali"); + choices1.add("reza"); + choices1.add("gholi"); + choices1.add("saba"); + choices1.add("fateme"); + choices1.add("parsa"); + + person person1=new person("a","A"); + person person2=new person("b","B"); + person person3=new person("c","C"); + person person4=new person("d","D"); + + ArrayList person1_choices=new ArrayList<>(); + ArrayList person2_choices=new ArrayList<>(); + ArrayList person3_choices=new ArrayList<>(); + ArrayList person4_choices=new ArrayList<>(); + + person1_choices.add("Ali"); + person1_choices.add("reza"); + person1_choices.add("unknown"); + + person2_choices.add("fateme"); + person2_choices.add("Ali"); + person2_choices.add("fateme"); + + person3_choices.add("reza"); + person3_choices.add("saba"); + + vs.create_voting("who is the best teacher",1,choices1); + + vs.vote(1,person1,person1_choices); + vs.vote(1,person2,person2_choices); + vs.vote(1,person3,person3_choices); + //showing user cant vote twice or more + vs.vote(1,person1,person2_choices); + + vs.print_results(1); + + person1_choices.removeAll(person1_choices); + person2_choices.removeAll(person2_choices); + person3_choices.removeAll(person3_choices); + + ArrayList choices2=new ArrayList<>(); + + choices2.add("iran"); + choices2.add("america"); + choices2.add("russia"); + choices2.add("germany"); + choices2.add("spain"); + + vs.create_voting("where is the most enjoyable place for you to live?:",0,choices2); + + person1_choices.add("iran"); + //showing he can't choose more than one for voting + person1_choices.add("spain"); + person2_choices.add("russia"); + person3_choices.add("russia"); + person4_choices.add("iran"); + + vs.vote(2,person1,person1_choices); + vs.vote(2,person2,person2_choices); + vs.vote(2,person3,person3_choices); + vs.vote(2,person4,person4_choices); + + vs.print_results(2); + + + + + + } +} diff --git a/Mohamadrezahakimi/lab 4/person.java b/Mohamadrezahakimi/lab 4/person.java new file mode 100644 index 0000000..984d8da --- /dev/null +++ b/Mohamadrezahakimi/lab 4/person.java @@ -0,0 +1,22 @@ +public class person { + private String first_name; + private String last_name; + public person(String first_name,String last_name){ + this.first_name=first_name; + this.last_name=last_name; + + } + + public String getFirst_name() { + return first_name; + } + + public String getLast_name() { + return last_name; + } + + public String to_string(){ + return first_name.concat(" "+last_name); + } +} + diff --git a/Mohamadrezahakimi/lab 4/vote.java b/Mohamadrezahakimi/lab 4/vote.java new file mode 100644 index 0000000..f8f0385 --- /dev/null +++ b/Mohamadrezahakimi/lab 4/vote.java @@ -0,0 +1,23 @@ +public class vote { + private person person; + private String date; + public vote(person person,String date){ + this.date=date; + this.person=person; + } + + public person get_person() { + return person; + } + + public String getDate() { + return date; + } + + public boolean equals(vote object){ + if(object.get_person().to_string().equalsIgnoreCase(person.to_string())){ + return true; + } + else return false; + } +} diff --git a/Mohamadrezahakimi/lab 4/voting.java b/Mohamadrezahakimi/lab 4/voting.java new file mode 100644 index 0000000..4de08bd --- /dev/null +++ b/Mohamadrezahakimi/lab 4/voting.java @@ -0,0 +1,92 @@ +import ir.huri.jcal.JalaliCalendar; + +import java.awt.*; +import java.awt.image.AreaAveragingScaleFilter; +import java.time.LocalDateTime; +import java.util.*; +import java.time.LocalDate; + +public class voting { + + private int type; + private String voting_question; + ArrayList voters; + HashMap> list_of_votes_to_choices; + + public voting(int one_or_more_voting, String voting_question) { + this.voting_question = voting_question; + list_of_votes_to_choices = new HashMap<>(); + voters = new ArrayList<>(); + this.type = one_or_more_voting; + } + + public void create_choice(String choice_explanation) { + if (!list_of_votes_to_choices.containsKey(choice_explanation)) { + list_of_votes_to_choices.put(choice_explanation, new HashSet<>()); + } + } + + public void vote(person person, ArrayList choices) { + if ((type == 0 && choices.size() != 1) ) { + System.out.println("Mr or Ms " + person.to_string() + ": you can't choose more thane one choice then we just " + + "consider your first choice as your final decision "); + if (!voters.contains(person)) { + voters.add(person); + + GregorianCalendar gregorianCalendar = new GregorianCalendar(); + gregorianCalendar.setTime(new Date()); + JalaliCalendar jalaliCalendar = new JalaliCalendar(gregorianCalendar); + + vote vote = new vote(person, jalaliCalendar.toString()); + String choice = choices.get(0); + if (list_of_votes_to_choices.containsKey(choice)) { + list_of_votes_to_choices.get(choice).add(vote); + } else System.out.println("we don't have this choice in this voting->" + choice); + } else { + System.out.println("Mr or Ms " + person.to_string() + ": you voted before"); + } + } + else { + if (!voters.contains(person)) { + voters.add(person); + + GregorianCalendar gregorianCalendar = new GregorianCalendar(); + gregorianCalendar.setTime(new Date()); + JalaliCalendar jalaliCalendar = new JalaliCalendar(gregorianCalendar); + + vote vote = new vote(person, jalaliCalendar.toString()); + for (String choice : choices) { + if (list_of_votes_to_choices.containsKey(choice)) { + list_of_votes_to_choices.get(choice).add(vote); + } + else System.out.println("we don't have this choice in this voting->"+choice); + } + } + else { + System.out.println("Mr or Ms "+person.to_string()+": you voted before"); + } + } + } + + + public String getVoting_question() { + return voting_question; + } + + public ArrayList getVoters() { + return voters; + } + + public void print_votes() { + for (String choice : list_of_votes_to_choices.keySet()) { + System.out.println(choice+":"); + if(list_of_votes_to_choices.get(choice).size()!=0) { + for (vote vote : list_of_votes_to_choices.get(choice)) { + System.out.println("\t\t" + "name: "+vote.get_person().to_string() +"\ttime: "+ vote.getDate()); + } + } + else System.out.println("\t\t"+"no nobody vote to this choice "); + } + } +} + diff --git a/Mohamadrezahakimi/lab 4/voting_system.java b/Mohamadrezahakimi/lab 4/voting_system.java new file mode 100644 index 0000000..7eb5c95 --- /dev/null +++ b/Mohamadrezahakimi/lab 4/voting_system.java @@ -0,0 +1,35 @@ +import java.util.ArrayList; + +public class voting_system { + ArrayList votingList; + public voting_system(){ + votingList=new ArrayList<>(); + } + + public void create_voting(String question_name,int one_or_more_answer,ArrayList choices){ + voting a=new voting(one_or_more_answer,question_name); + votingList.add(a); + for (String choice:choices){ + a.create_choice(choice); + } + } + + public void print_voting_question(){ + System.out.println("print voting's questions:"); + for (voting voting:votingList) { + System.out.println("\t\t"+voting.getVoting_question()); + } + } + + public int vote(int voting_number, person person,ArrayList choices){ + voting_number--; + if(voting_number<0||voting_number>votingList.size())return -1; + votingList.get(voting_number).vote(person,choices); + return 0; + } + + public void print_results(int voting_number){ + voting_number--; + votingList.get(voting_number).print_votes(); + } +} diff --git a/Mohamadrezahakimi/lab 5/Circle.java b/Mohamadrezahakimi/lab 5/Circle.java new file mode 100644 index 0000000..0cdc2f5 --- /dev/null +++ b/Mohamadrezahakimi/lab 5/Circle.java @@ -0,0 +1,33 @@ +public class Circle extends shape{ + private Integer radius=0; + + public Circle(int radius){ + + this.radius=radius; + } + + public boolean equal(Circle x){ + if(this.getRadius()==x.getRadius()){ + return true; + } + else return false; + } + + public String to_string(){ + return radius.toString(); + } + + public double calculate_perimeter() { + return 2*3.14*radius; + } + + public double calculate_Area(){ + return 3.14*(Math.pow(radius,2)); + } + + public int getRadius() { + return radius; + } +} + + diff --git a/Mohamadrezahakimi/lab 5/Main.java b/Mohamadrezahakimi/lab 5/Main.java new file mode 100644 index 0000000..7d1542d --- /dev/null +++ b/Mohamadrezahakimi/lab 5/Main.java @@ -0,0 +1,32 @@ +public class Main { + + public static void main(String[] args) { + Circle circle1=new Circle(1); + Circle circle2=new Circle(2); + + rectangle rectangle1=new rectangle(1,2); + rectangle rectangle2=new rectangle(1,2); + rectangle rectangle3=new rectangle(3,3); + + triangle triangle1=new triangle(2 ,3 ,3); + triangle triangle2=new triangle(2 ,3 ,3); + triangle triangle3=new triangle(3 ,3 ,3); + + Paint paint=new Paint(); + + paint.addShape(circle1); + paint.addShape(circle2); + + paint.addShape(rectangle1); + paint.addShape(rectangle2); + paint.addShape(rectangle3); + + paint.addShape(triangle1); + paint.addShape(triangle2); + paint.addShape(triangle3); + + paint.print_all(); + + + } +} diff --git a/Mohamadrezahakimi/lab 5/Paint.java b/Mohamadrezahakimi/lab 5/Paint.java new file mode 100644 index 0000000..4763292 --- /dev/null +++ b/Mohamadrezahakimi/lab 5/Paint.java @@ -0,0 +1,53 @@ +import javafx.css.converter.LadderConverter; + +import java.util.ArrayList; +import java.util.jar.JarOutputStream; + +public class Paint { + + ArrayList shapes=new ArrayList<>(); + + public void addShape(shape x){ + shapes.add(x); + } + + public void draw_all(){ + for (shape shape:shapes) { + if(shape instanceof Circle){ + System.out.println("Circle:"+"\tperimeter:"+shape.calculate_perimeter()+"\tArea: "+shape.calculate_Area()); + } + else if(shape instanceof rectangle ){ + System.out.println("Rectangle:"+"\tperimeter:"+shape.calculate_perimeter()+"\tArea: "+shape.calculate_Area()); + } + else if(shape instanceof triangle){ + System.out.println("Triangle:"+"\tperimeter:"+shape.calculate_perimeter()+"\tArea: "+shape.calculate_Area()); + + } + } + } + + public void print_all(){ + for (shape shape:shapes) { + System.out.println("\n"); + if(shape instanceof Circle){ + System.out.println("Circle:"+"\tperimeter:"+shape.calculate_perimeter()+"\tArea: "+shape.calculate_Area()); + } + else if(shape instanceof rectangle ){ + if(((rectangle) shape).isSquare()) { + System.out.println("Rectangle(it is square):" + "\tperimeter:" + shape.calculate_perimeter() + "\tArea: " + shape.calculate_Area()); + }else { + System.out.println("Rectangle:" + "\tperimeter:" + shape.calculate_perimeter() + "\tArea: " + shape.calculate_Area()); + } + } + + else if(shape instanceof triangle){ + if(!((triangle) shape).isEquilateral()) { + System.out.println("Triangle:" + "\tperimeter:" + shape.calculate_perimeter() + "\tArea: " + shape.calculate_Area()); + } + else System.out.println("Triangle(equilateral):"+"\tperimeter:"+shape.calculate_perimeter()+"\tArea: "+shape.calculate_Area()); + + } + } + } + +} diff --git a/Mohamadrezahakimi/lab 5/Polygon.java b/Mohamadrezahakimi/lab 5/Polygon.java new file mode 100644 index 0000000..c7db228 --- /dev/null +++ b/Mohamadrezahakimi/lab 5/Polygon.java @@ -0,0 +1,46 @@ +import java.util.ArrayList; + +public abstract class Polygon extends shape { + + ArrayList sides=new ArrayList<>(); + + + public Polygon(int ... args){ + for(int i :args) { + sides.add(i); + } + } + + public String to_string(){ + String string_sides = null; + for (Integer side:this.sides + ) { + string_sides.concat(side.toString()); + } + return string_sides; + } + + public double calculate_perimeter(){ + int perimeter=0; + for (Integer side :sides + ) { + perimeter+=side; + } + return perimeter; + + } + + public abstract double calculate_Area(); + + public ArrayList getSides() { + return sides; + } + + public boolean equal(triangle x){ + if(x.getSides()==this.getSides()){ + return true; + } + else return false; + } + +} diff --git a/Mohamadrezahakimi/lab 5/rectangle.java b/Mohamadrezahakimi/lab 5/rectangle.java new file mode 100644 index 0000000..a1ad0d0 --- /dev/null +++ b/Mohamadrezahakimi/lab 5/rectangle.java @@ -0,0 +1,25 @@ +import java.util.ArrayList; + +public class rectangle extends Polygon { + public rectangle(int width,int length){ + sides.add(width); + sides.add(length); + } + @Override + public double calculate_Area() { + int width=sides.get(0); + int length=sides.get(1); + + return width*length; + } + + public boolean isSquare(){ + if(sides.get(0)==sides.get(1)){ + return true; + + } + else return false; + } + + +} diff --git a/Mohamadrezahakimi/lab 5/shape.java b/Mohamadrezahakimi/lab 5/shape.java new file mode 100644 index 0000000..9076f9a --- /dev/null +++ b/Mohamadrezahakimi/lab 5/shape.java @@ -0,0 +1,22 @@ +import java.util.ArrayList; + +public class shape { + + public String to_string(){ + return "A"; + }; + + public double calculate_perimeter(){ + return 0; + }; + + public double calculate_Area(){ + return 0; + }; + + public boolean equal(){ + return true; + } + + +} diff --git a/Mohamadrezahakimi/lab 5/triangle.java b/Mohamadrezahakimi/lab 5/triangle.java new file mode 100644 index 0000000..8867fea --- /dev/null +++ b/Mohamadrezahakimi/lab 5/triangle.java @@ -0,0 +1,26 @@ +public class triangle extends Polygon{ + + public triangle(int side1,int side2,int side3){ + sides.add(side1); + sides.add(side2); + sides.add(side3); + } + + @Override + public double calculate_Area() { + double primeter=this.calculate_perimeter(); + double x=primeter/2; + x=x*(x-sides.get(0))*(x-sides.get(1))*(x-sides.get(2)); + double Area=Math.pow(x,0.5); + return Area; + } + + public boolean isEquilateral(){ + if((sides.get(0)==sides.get(1))&&(sides.get(1)==sides.get(2))){ + return true; + } + else return false; + } + + +}