Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbolfazlBakiasay9831123 #35

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions AbolfazlBakiasay/AbolfazlBakiasay
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Full Name: Abolfazl Bakiasay
Student Number: 9831123
67 changes: 67 additions & 0 deletions AbolfazlBakiasay/Session4/VotingApp/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.ArrayList;
import java.util.Arrays;



public class Main {


public static void main(String[] args) {

VotingSystem vs = new VotingSystem();

Person p1 = new Person("Mahdi", "Kashefi");
Person p2 = new Person("Ali", "Amiri");
Person p3 = new Person("Mohamad", "Sadeghi");
Person p4 = new Person("Reza", "Mohammadi");
Person p5 = new Person("Sepehr", "Abbasi");
Person p6 = new Person("Alireza", "Moradi");
Person p7 = new Person("Morteza", "Pashayi");


vs.createVoting("How often do you code?", 0, new ArrayList<String>(Arrays.asList(
"Almost Always",
"Most of the times",
"Sometimes",
"Often",
"Never"
)));


vs.vote(0, p1, new ArrayList<>(Arrays.asList("Often")));
vs.vote(0, p2, new ArrayList<>(Arrays.asList("Almost Always")));
vs.vote(0, p3, new ArrayList<>(Arrays.asList("Never")));
vs.vote(0, p4, new ArrayList<>(Arrays.asList("Often")));
vs.vote(0, p5, new ArrayList<>(Arrays.asList("Sometimes")));
vs.randomVote(0, p6);
vs.randomVote(0, p7);

vs.getResult(0);



vs.createVoting("Which counteries have you been to?", 1, new ArrayList<String>(Arrays.asList(
"Iran",
"Turkey",
"Finland",
"Germany",
"Japan",
"Italy"
)));


vs.vote(1, p1, new ArrayList<>(Arrays.asList("Iran", "Italy")));
vs.vote(1, p2, new ArrayList<>(Arrays.asList("Turkey", "Japan")));
vs.vote(1, p3, new ArrayList<>(Arrays.asList("Italy", "Finland", "Iran")));
vs.vote(1, p4, new ArrayList<>(Arrays.asList("Japan")));
vs.vote(1, p5, new ArrayList<>(Arrays.asList("Germany", "Iran")));
vs.vote(1, p6, new ArrayList<>(Arrays.asList("Finland", "Iran", "Japan")));

vs.getResult(1);



}


}
32 changes: 32 additions & 0 deletions AbolfazlBakiasay/Session4/VotingApp/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

public class Person {
private String firstName, lastName;

public Person(String first, String last) {
firstName = first;
lastName = last;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

@Override
public String toString() {
return firstName + " " + lastName;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof Person)) return false;
Person person = (Person) obj;
return (
person.getFirstName().equals(firstName) &&
person.getLastName().equals(lastName)
);
}
}
51 changes: 51 additions & 0 deletions AbolfazlBakiasay/Session4/VotingApp/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

public class Vote {
private Person person;
private 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 obj) {
if(obj == this)
return true;
if(!(obj instanceof Vote))
return false;
Vote vote = (Vote) obj;
return (
vote.getPerson().getFirstName().equals(person.getFirstName()) &&
vote.getPerson().getLastName().equals(person.getLastName()) &&
vote.getDate().equals(date)
);
}

@Override
public int hashCode() {
long hashBase = 257;
long hashMax = (long) (1e9+7);
long res = 0;
for(byte c:person.getFirstName().getBytes())
res = (res * hashBase + c) % hashMax;
for(byte c:person.getLastName().getBytes())
res = (res * hashBase + c) % hashMax;
for(byte c:date.getBytes())
res = (res * hashBase + c) % hashMax;
return (int) res;
}

@Override
public String toString() {
return person.toString() + " " + date;
}
}
87 changes: 87 additions & 0 deletions AbolfazlBakiasay/Session4/VotingApp/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import ir.huri.jcal.JalaliCalendar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Random;


public class Voting {
private int type;
private String question;
private ArrayList<Person> voters;
private HashMap<String, HashSet<Vote>> polls;
private Random rnd;

public Voting(int type, String question) {
this.type = type;
this.question = question;
voters = new ArrayList<>();
polls = new HashMap<>();
rnd = new Random();
}

public String getQuestion() {
return question;
}

public void createPoll(String poll) {
if(!polls.containsKey(poll))
polls.put(poll, new HashSet<Vote>());
}

public void vote(Person person, ArrayList<String> options) {
if(voters.contains(person)) return;
voters.add(person);
switch (type) {
case 0:
if(options.size()>0 && polls.containsKey(options.get(0)))
polls.get(options.get(0)).add(new Vote(person, new JalaliCalendar().toString()));
break;
case 1:
for(String s:options) if(polls.containsKey(s))
polls.get(s).add(new Vote(person, new JalaliCalendar().toString()));
break;
default: break;
}
}

public void randomVote(Person person) {
if(type == 1 || polls.isEmpty()) return;
int ind = rnd.nextInt(polls.size());
String poll = (String) polls.keySet().toArray()[ind];
polls.get(poll).add(new Vote(person, new JalaliCalendar().toString()));
}

public void getVoters() {
for(Person p:voters)
System.out.println(p);
}

public void printVotes() {
for(String poll:polls.keySet()) {
System.out.println(poll);
for(Vote vote:polls.get(poll))
System.out.println(" " + vote);
}
}

public ArrayList<String> getPolls() {
ArrayList<String> res = new ArrayList<>();
Collections.addAll(res, (String[]) polls.keySet().toArray());
return res;
}
}












48 changes: 48 additions & 0 deletions AbolfazlBakiasay/Session4/VotingApp/VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;


public class VotingSystem {

private ArrayList<Voting> votingList;

public VotingSystem() {
votingList = new ArrayList<>();
}

public void createVoting(String question, int type, ArrayList<String> polls) {
Voting voting = new Voting(type, question);
for(String poll:polls)
voting.createPoll(poll);
votingList.add(voting);
}

public ArrayList<Voting> getVotingList() {
return votingList;
}

public Voting getVoting(int index) {
if(index<0 || index>=votingList.size()) return null;
return votingList.get(index);
}

public void vote(int index, Person person, ArrayList<String> options) {
Voting voting = getVoting(index);
if(voting == null) return;
voting.vote(person, options);
}

public void randomVote(int index, Person person) {
Voting voting = getVoting(index);
if(voting == null) return;
voting.randomVote(person);
}

public void getResult(int index) {
Voting voting = getVoting(index);
if(voting == null) return;
System.out.println("");
System.out.println(voting.getQuestion());
voting.printVotes();
System.out.println("");
}
}
1 change: 1 addition & 0 deletions AbolfazlBakiasay/Session4/temp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

40 changes: 40 additions & 0 deletions AbolfazlBakiasay/Session5/Paint1/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@



public class Circle {
private double radius;

public Circle(double radius) {
this.radius = radius;
}

public double getRadius() {
return radius;
}

public double calculatePerimeter() {
return 2 * Math.PI * radius;
}

public double calculateArea() {
return Math.PI * Math.pow(radius, 2);
}

public void draw() {
/// Draw The Circle Here
System.out.println("Drawing Cirle");
}

@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(!(obj instanceof Circle)) return false;
return radius == ((Circle)obj).getRadius();
}

@Override
public String toString() {
return "Circle: "+Double.toString(radius);
}

}
28 changes: 28 additions & 0 deletions AbolfazlBakiasay/Session5/Paint1/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@



public class Main {

public static void main(String[] args) {

Circle circle1 = new Circle(19);
Circle circle2 = new Circle(3);
Rectangle rect1 = new Rectangle(1,4,1,4);
Rectangle rect2 = new Rectangle(8,5,8,5);
Rectangle rect3 = new Rectangle(6,6,6,6);
Triangle tri1 = new Triangle(2,2,2);
Triangle tri2 = new Triangle(4,4,6);
Paint paint = new Paint();

paint.addCircle(circle1);
paint.addCircle(circle2);
paint.addRectangle(rect1);
paint.addRectangle(rect2);
paint.addRectangle(rect3);
paint.addTriangle(tri1);
paint.addTriangle(tri2);

paint.drawAll();
paint.printAll();
}
}
Loading