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/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 - diff --git a/SetayeshSanavi/SetayeshSanavi b/SetayeshSanavi/SetayeshSanavi new file mode 100644 index 0000000..28441a4 --- /dev/null +++ b/SetayeshSanavi/SetayeshSanavi @@ -0,0 +1,4 @@ +Full Name: Setayesh Sanavi +Student Number: 9628024 + + diff --git a/Workshop_4/JalaliCalendar/JalaliCalendar.java b/Workshop_4/JalaliCalendar/JalaliCalendar.java new file mode 100644 index 0000000..04050d8 --- /dev/null +++ b/Workshop_4/JalaliCalendar/JalaliCalendar.java @@ -0,0 +1,442 @@ +package JalaliCalendar; + +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +public class JalaliCalendar { + + private int year, month, day; + + /** + * Today Jalali Date + */ + public JalaliCalendar() { + fromGregorian(new GregorianCalendar()); + } + + /** + * Create a ir.huri.jcal.JalaliCalendar object + * @param year Jalali Year + * @param month Jalali Month + * @param day Jalali Day + */ + public JalaliCalendar(int year, int month, int day) { + set(year, month, day); + } + + + /** + * Create a ir.huri.jcal.JalaliCalendar object from gregorian calendar + * @param gc gregorian calendar object + */ + public JalaliCalendar(GregorianCalendar gc){ + fromGregorian(gc); + } + + + /** + * Create a ir.huri.jcal.JalaliCalendar object from Localdate(java 8) + * @param ld local date object + */ + public JalaliCalendar(LocalDate ld) { + fromGregorian(GregorianCalendar.from(ld.atStartOfDay(ZoneId.systemDefault()))); + } + + + /** + * Create a ir.huri.jcal.JalaliCalendar object from Date object + * @param date Date object + */ + public JalaliCalendar(Date date) { + GregorianCalendar gc = new GregorianCalendar(); + gc.setTime(date); + fromGregorian(gc); + } + + /** + * Convert current jalali date to gregorian date + * @return date converted gregorianDate + */ + public GregorianCalendar toGregorian() { + int julianDay = toJulianDay(); + return julianDayToGregorianCalendar(julianDay); + } + + /** + * set date from gregorian date + * @param gc input gregorian calendar + */ + public void fromGregorian(GregorianCalendar gc){ + int jd = gregorianToJulianDayNumber(gc); + fromJulianDay(jd); + } + + /** + * @return yesterday date + */ + public JalaliCalendar getYesterday() { + return getDateByDiff(-1); + } + + /** + * @return tomorrow date + */ + public JalaliCalendar getTomorrow() { + return getDateByDiff(1); + } + + /** + * get Jalali date by day difference + * @param diff number of day diffrents + * @return jalali calendar diffحزن + */ + public JalaliCalendar getDateByDiff(int diff) { + GregorianCalendar gc = toGregorian(); + gc.add(Calendar.DAY_OF_MONTH, diff); + return new JalaliCalendar(gc); + } + + /** + * @return day Of Week + */ + public int getDayOfWeek() { + return toGregorian().get(Calendar.DAY_OF_WEEK); + } + + /** + * @return get first day of week + */ + public int getFirstDayOfWeek() { + return toGregorian().getFirstDayOfWeek(); + } + + /** + * + * @return day name + */ + public String getDayOfWeekString() { + switch (getDayOfWeek()) { + case 1: + return "یک‌شنبه"; + case 2 : + return "دوشنبه"; + case 3: + return "سه‌شنبه"; + case 4: + return "چهارشنبه"; + case 5: + return "پنجشنبه"; + case 6: + return "جمعه"; + case 7: + return "شنبه"; + default: + return "نامعلوم"; + } + } + + /** + * + * @return month name + */ + public String getMonthString() { + switch (getMonth()) { + case 1: + return "فروردین"; + case 2 : + return "اردیبهشت"; + case 3: + return "خرداد"; + case 4: + return "تیر"; + case 5: + return "مرداد"; + case 6: + return "شهریور"; + case 7: + return "مهر"; + case 8: + return "آبان"; + case 9: + return "آذر"; + case 10: + return "دی"; + case 11: + return "بهمن"; + case 12: + return "اسفند"; + default: + return "نامعلوم"; + } + } + + + /** + * get String with the following format : + * یکشنبه ۱۲ آبان + * @return String format + */ + + public String getDayOfWeekDayMonthString() { + return getDayOfWeekString() + " " + getDay() + " " + getMonthString() ; + } + + /** + * + * @return return whether this year is a jalali leap year + */ + public boolean isLeap() { + return getLeapFactor(getYear()) == 0; + } + + public int getYearLength() { + return isLeap() ? 366 : 365; + } + + public int getMonthLength() { + if ( getMonth() < 7 ) { + return 31; + } else if ( getMonth() < 12 ) { + return 30; + } else if ( getMonth() == 12 ) { + if (isLeap()) + return 30; + else + return 29; + } + return 0; + } + + public int getDay() { + return day; + } + + public int getMonth() { + return month; + } + + public int getYear() { + return year; + } + + public void setMonth(int month) { + this.month = month; + } + + public void setYear(int year) { + this.year = year; + } + + public void setDay(int day) { + this.day = day; + } + + public void set(int year, int month, int day) { + setYear(year); + setMonth(month); + setDay(day); + } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + JalaliCalendar that = (JalaliCalendar) o; + + return year == that.year && month == that.month && day == that.day; + } + + private int gregorianToJulianDayNumber(GregorianCalendar gc) { + int gregorianYear = gc.get(GregorianCalendar.YEAR); + int gregorianMonth = gc.get(GregorianCalendar.MONTH) + 1; + int gregorianDay = gc.get(GregorianCalendar.DAY_OF_MONTH); + + return (((1461 * (gregorianYear + 4800 + (gregorianMonth - 14) / 12)) / 4 + + (367 * (gregorianMonth - 2 - 12 * ((gregorianMonth - 14) / 12))) / 12 + - (3 * ((gregorianYear + 4900 + (gregorianMonth - 14) / 12) / 100)) / 4 + gregorianDay + - 32075) - (gregorianYear + 100100 + (gregorianMonth - 8) / 6) / 100 * 3 / 4 + 752); + } + + private int julianToJulianDayNumber(JulianCalendar jc) { + int julianYear = jc.getYear(); + int julianMonth = jc.getMonth(); + int JulianDay = jc.getDay(); + + return (1461 * (julianYear + 4800 + (julianMonth - 14) / 12)) / 4 + + (367 * (julianMonth - 2 - 12 * ((julianMonth - 14) / 12))) / 12 + - (3 * ((julianYear + 4900 + (julianMonth - 14) / 12) / 100)) / 4 + JulianDay + - 32075; + } + + private GregorianCalendar julianDayToGregorianCalendar(int JulianDayNumber) { + + int j = 4 * JulianDayNumber + 139361631 + (4 * JulianDayNumber + 183187720) / 146097 * 3 / 4 * 4 - 3908; + int i = (j % 1461) / 4 * 5 + 308; + + int gregorianDay = (i % 153) / 5 + 1; + int gregorianMonth = ((i / 153) % 12) + 1; + int gregorianYear = j / 1461 - 100100 + (8 - gregorianMonth) / 6; + + return new GregorianCalendar(gregorianYear, gregorianMonth - 1, gregorianDay); + } + + private void fromJulianDay(int JulianDayNumber) { + GregorianCalendar gc = julianDayToGregorianCalendar(JulianDayNumber); + int gregorianYear = gc.get(GregorianCalendar.YEAR); + + int jalaliYear, jalaliMonth, jalaliDay; + + jalaliYear = gregorianYear - 621; + + GregorianCalendar gregorianFirstFarvardin = new JalaliCalendar(jalaliYear, 1, 1).getGregorianFirstFarvardin(); + int JulianDayFarvardinFirst = gregorianToJulianDayNumber(gregorianFirstFarvardin); + int diffFromFarvardinFirst = JulianDayNumber - JulianDayFarvardinFirst; + + + + if (diffFromFarvardinFirst >= 0) { + if (diffFromFarvardinFirst <= 185) { + jalaliMonth = 1 + diffFromFarvardinFirst / 31; + jalaliDay = (diffFromFarvardinFirst % 31) + 1; + set(jalaliYear, jalaliMonth, jalaliDay); + return; + } else { + diffFromFarvardinFirst = diffFromFarvardinFirst - 186; + } + } else { + diffFromFarvardinFirst = diffFromFarvardinFirst + 179; + if (getLeapFactor(jalaliYear) == 1) + diffFromFarvardinFirst = diffFromFarvardinFirst + 1; + jalaliYear -= 1; + } + + + jalaliMonth = 7 + diffFromFarvardinFirst / 30; + jalaliDay = (diffFromFarvardinFirst % 30) + 1; + set(jalaliYear, jalaliMonth, jalaliDay); + } + + private int toJulianDay() { + int jalaliMonth = getMonth(); + int jalaliDay = getDay(); + + GregorianCalendar gregorianFirstFarvardin = getGregorianFirstFarvardin(); + int gregorianYear = gregorianFirstFarvardin.get(Calendar.YEAR); + int gregorianMonth = gregorianFirstFarvardin.get(Calendar.MONTH) + 1; + int gregorianDay = gregorianFirstFarvardin.get(Calendar.DAY_OF_MONTH); + + JulianCalendar julianFirstFarvardin = new JulianCalendar(gregorianYear, gregorianMonth, gregorianDay) ; + + + int julianDay = julianToJulianDayNumber(julianFirstFarvardin) + (jalaliMonth - 1) * 31 - jalaliMonth / 7 * (jalaliMonth - 7) + + jalaliDay - 1; + + return julianDay; + } + + + private GregorianCalendar getGregorianFirstFarvardin() { + int marchDay = 0; + int[] breaks = { -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, + 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178 }; + + int jalaliYear = getYear(); + int gregorianYear = jalaliYear + 621; + int jalaliLeap = -14; + int jp = breaks[0]; + + int jump = 0; + for (int j = 1; j <= 19; j++) { + int jm = breaks[j]; + jump = jm - jp; + if (jalaliYear < jm) { + int N = jalaliYear - jp; + jalaliLeap = jalaliLeap + N / 33 * 8 + (N % 33 + 3) / 4; + + if ((jump % 33) == 4 && (jump - N) == 4) + jalaliLeap = jalaliLeap + 1; + + int GregorianLeap = (gregorianYear / 4) - (gregorianYear / 100 + 1) * 3 / 4 - 150; + + marchDay = 20 + (jalaliLeap - GregorianLeap); + + if ((jump - N) < 6) + N = N - jump + (jump + 4) / 33 * 33; + + break; + } + + jalaliLeap = jalaliLeap + jump / 33 * 8 + (jump % 33) / 4; + jp = jm; + } + + return new GregorianCalendar(gregorianYear, 2, marchDay); + } + + private int getLeapFactor(int jalaliYear) { + int leap = 0; + int[] breaks = { -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, + 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178 }; + + int jp = breaks[0]; + + int jump = 0; + for (int j = 1; j <= 19; j++) { + int jm = breaks[j]; + jump = jm - jp; + if (jalaliYear < jm) { + int N = jalaliYear - jp; + + if ((jump - N) < 6) + N = N - jump + (jump + 4) / 33 * 33; + + leap = ((((N + 1) % 33) - 1) % 4); + + if (leap == -1) + leap = 4; + + break; + } + + jp = jm; + } + + return leap; + } + + @Override + public String toString() { + return String.format("%04d-%02d-%02d", getYear(), getMonth(), getDay()); + } + + + private class JulianCalendar { + int year; + int month; + int day; + + public JulianCalendar(int year, int month, int day) { + this.year = year; + this.month = month; + this.day = day; + } + + public int getYear() { + return year; + } + public int getMonth() { + return month; + } + public int getDay() { + return day; + } + } + + + +} \ No newline at end of file diff --git a/Workshop_4/Main.java b/Workshop_4/Main.java new file mode 100644 index 0000000..0bf8456 --- /dev/null +++ b/Workshop_4/Main.java @@ -0,0 +1,103 @@ +import java.util.ArrayList; + +public class Main { + + public static void main(String[] args) { + VotingSystem votingSystem = new VotingSystem(); + + String question = "Do you come?"; + ArrayList choices = new ArrayList(); + + choices.add("Yes"); + choices.add("No"); + + votingSystem.createVoting(question, 0, choices); + + choices.clear(); + question = null; + + System.out.println("print voting: "); + votingSystem.printVoting(0); + + Person voter1 = new Person("setayesh1", "sanavi1"); + Person voter2 = new Person("setayesh2", "sanavi2"); + Person voter3 = new Person("setayesh3", "sanavi3"); + Person voter4 = new Person("setayesh4", "sanavi4"); + ArrayList myChoices = new ArrayList<>(); + myChoices.add("Yes"); + votingSystem.vote(0, voter1, myChoices); + + myChoices.clear(); + + myChoices.add(votingSystem.randomVote(0)); + votingSystem.vote(0, voter2, myChoices); + + myChoices.clear(); + + myChoices.add("Yes"); + votingSystem.vote(0, voter1, myChoices); + + myChoices.clear(); + + myChoices.add("No"); + votingSystem.vote(0, voter3, myChoices); + + myChoices.clear(); + + myChoices.add("No"); + votingSystem.vote(0, voter4, myChoices); + + myChoices.clear(); + + myChoices.add("No"); + votingSystem.vote(0, voter1, myChoices); + + myChoices.clear(); + + myChoices.clear(); + System.out.println("print result: "); + votingSystem.printResult(0); + System.out.println("\n\n\n\n"); + + + question = "which day ?"; + choices.add("sunday."); + choices.add("monday"); + choices.add("friday"); + choices.add("none"); + votingSystem.createVoting(question, 1, choices); + + choices.clear(); + System.out.println("print voting questions:"); + votingSystem.printVotingQuestions(); + System.out.println("\n\n"); + + myChoices.add("none"); + votingSystem.vote(1, voter1, myChoices); + + myChoices.clear(); + myChoices.add("monday"); + myChoices.add("friday"); + votingSystem.vote(1, voter2, myChoices); + + myChoices.clear(); + myChoices.add("sunday"); + myChoices.add("monday"); + myChoices.add("friday"); + votingSystem.vote(1, voter3, myChoices); + + myChoices.clear(); + + myChoices.add("friday"); + votingSystem.vote(1, voter4, myChoices); + + myChoices.clear(); + + votingSystem.printVoting(1); + System.out.println("\n\n\n\n"); + + votingSystem.printResult(1); + votingSystem.deleteVoting(0); + votingSystem.deleteVoting(1); + } +} diff --git a/Workshop_4/Person.java b/Workshop_4/Person.java new file mode 100644 index 0000000..3e8ad2e --- /dev/null +++ b/Workshop_4/Person.java @@ -0,0 +1,23 @@ +import JalaliCalendar.JalaliCalendar; + +public class Person { + private String firstName; + private 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 + " " + ":::date of voting --> " + new JalaliCalendar().toString() + "\n"; + } +} diff --git a/Workshop_4/Vote.java b/Workshop_4/Vote.java new file mode 100644 index 0000000..9b5e458 --- /dev/null +++ b/Workshop_4/Vote.java @@ -0,0 +1,32 @@ +import java.util.Objects; + +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 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/Workshop_4/Voting.java b/Workshop_4/Voting.java new file mode 100644 index 0000000..f8bb41f --- /dev/null +++ b/Workshop_4/Voting.java @@ -0,0 +1,89 @@ +import JalaliCalendar.JalaliCalendar; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; + +public class Voting { + private int type;//voting type(single vote = 0 and multiple vote = 1) + private String question;//question of voting + private ArrayList voters; + private HashSet voteHashSet = new HashSet<>(); + private HashMap> listOfVotesToChoices; + public Voting(int type, String question){ + voters = new ArrayList(); + listOfVotesToChoices = new HashMap>(); + this.type = type; + this.question = question; + } + public void delete(Person person,String choice) { + if (getListOfVotesToChoices().containsKey(choice)) + getListOfVotesToChoices().remove(choice); + voters.remove(person); + } + public String getQuestion() { + return question; + } + + /** + * add one choice to list + * @param choice + */ + public void createChoice(String choice){ + HashSet votes = new HashSet<>(); + listOfVotesToChoices.put(choice, votes); + } + + /** + * put a vote in voting + * @param person voter + * @param choices options + */ + public void vote(Person person,ArrayList choices){ + if(voters.contains(person)) + System.out.println(person.getLastName() + " you can not vote twice ;)"); + else { + if ((type == 0 && choices.size() == 1) || (type == 1 && choices.size() >= 1)) { + Vote newVote = new Vote(person,new JalaliCalendar().getDayOfWeekDayMonthString()); + for (String s : choices) { + if (listOfVotesToChoices.containsKey(s)) + listOfVotesToChoices.get(s).add(newVote); + + else + System.out.println("invalid choice"); + } + voters.add(person); + } + } + } + + public int getType() { + return type; + } + + /** + * @return list of choices of a vote + */ + public ArrayList getChoices(){ + return new ArrayList<>(listOfVotesToChoices.keySet()); + } + + public ArrayList getVoters() { + return voters; + } + + public HashMap> getListOfVotesToChoices() { + return listOfVotesToChoices; + } + + /** + * print number of each vote and voter and the date of that vote + */ + public void printVotes() { + for (String s : listOfVotesToChoices.keySet()) { + System.out.println(s + " = " + listOfVotesToChoices.get(s).size()); + for (Vote vote : listOfVotesToChoices.get(s)) + System.out.print(vote.getPerson()); + } + } +} diff --git a/Workshop_4/VotingSystem.java b/Workshop_4/VotingSystem.java new file mode 100644 index 0000000..bacca18 --- /dev/null +++ b/Workshop_4/VotingSystem.java @@ -0,0 +1,57 @@ +import java.util.ArrayList; +import java.util.Random; + +public class VotingSystem { + private ArrayList votingList; + Random random = new Random(); + public VotingSystem() { + votingList = new ArrayList(); + } + + public void createVoting(String question, int type, ArrayList choices) { + Voting voting = new Voting(type,question); + for (String choice : choices) + voting.createChoice(choice); + votingList.add(voting); + } + public void deleteVoting(int number) { + for (int i = 0; i < votingList.size(); i++){ + if (i == number) + votingList.remove(i); + } + } + + public ArrayList getVotingList() { + return votingList; + } + + public void printVotingQuestions() { + for (Voting v : votingList) + System.out.println((votingList.indexOf(v)+1)+ ". " + v.getQuestion()); + } + + public void printVoting(int votingNumber) { + System.out.println("question : " + votingList.get(votingNumber).getQuestion() + "\nchoices : "); + for (int c = 0; c < votingList.get(votingNumber).getChoices().size(); c++) + System.out.println(votingList.get(votingNumber).getChoices().get(c)); + System.out.println(); + } + public void vote(int votingNumber, Person person, ArrayList choices) { + votingList.get(votingNumber).vote(person,choices); + } + public String randomVote(int number){ + ArrayList arrayList = votingList.get(number).getChoices(); + if(votingList.get(number).getType() == 1) { + return null; + } + int max = arrayList.size(); + return arrayList.get(random.nextInt(max)); + } + + public void printResult(int votingNumber) { + System.out.println("((((result of voting))))"); + votingList.get(votingNumber).printVotes(); + System.out.println(); + } +} + diff --git a/azure-pipelines-1.yml b/azure-pipelines-1.yml new file mode 100644 index 0000000..aa91291 --- /dev/null +++ b/azure-pipelines-1.yml @@ -0,0 +1,19 @@ +# Starter pipeline +# Start with a minimal pipeline that you can customize to build and deploy your code. +# Add steps that build, run tests, deploy, and more: +# https://aka.ms/yaml + +trigger: +- master + +pool: + vmImage: 'ubuntu-latest' + +steps: +- script: echo Hello, world! + displayName: 'Run a one-line script' + +- script: | + echo Add other tasks to build, test, and deploy your project. + echo See https://aka.ms/yaml + displayName: 'Run a multi-line script' diff --git a/azure-pipelines-2.yml b/azure-pipelines-2.yml new file mode 100644 index 0000000..aa91291 --- /dev/null +++ b/azure-pipelines-2.yml @@ -0,0 +1,19 @@ +# Starter pipeline +# Start with a minimal pipeline that you can customize to build and deploy your code. +# Add steps that build, run tests, deploy, and more: +# https://aka.ms/yaml + +trigger: +- master + +pool: + vmImage: 'ubuntu-latest' + +steps: +- script: echo Hello, world! + displayName: 'Run a one-line script' + +- script: | + echo Add other tasks to build, test, and deploy your project. + echo See https://aka.ms/yaml + displayName: 'Run a multi-line script' diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 0000000..aa91291 --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,19 @@ +# Starter pipeline +# Start with a minimal pipeline that you can customize to build and deploy your code. +# Add steps that build, run tests, deploy, and more: +# https://aka.ms/yaml + +trigger: +- master + +pool: + vmImage: 'ubuntu-latest' + +steps: +- script: echo Hello, world! + displayName: 'Run a one-line script' + +- script: | + echo Add other tasks to build, test, and deploy your project. + echo See https://aka.ms/yaml + displayName: 'Run a multi-line script' diff --git a/workshop_5/5.1/Circle.java b/workshop_5/5.1/Circle.java new file mode 100644 index 0000000..2d7cc4b --- /dev/null +++ b/workshop_5/5.1/Circle.java @@ -0,0 +1,70 @@ +import java.util.Objects; + +/** + * circle is a subclass of paint to save special behavior of circles + * @author Setayesh + * @version 0.1 + */ +public class Circle { + private int radius; + private final double PI = 3.14; + + /** + * constructor to assign radius in class + * @param radius radius + */ + public Circle(int radius){ + this.radius = radius; + } + + /** + * @return radius the radius + */ + public int getRadius() { + return radius; + } + + /** + * calculate perimeter of circle + * @return perimeter + */ + public double calculatePerimeter(){ + double perimeter = 2.0*PI*getRadius(); + return perimeter; + } + + /** + * calculate area + * @return area + */ + public double calculateArea(){ + return PI*radius*getRadius();//auto casting because of PI + } + + /** + * show information of a circle + */ + public void draw(){ + System.out.println("\ncircle:\nArea: " + calculateArea() + "\nPerimeter: " + calculatePerimeter()); + } + + /** + * override of equals method to compare two shapes + */ + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Circle)) return false; + Circle circle = (Circle) o; + return getRadius() == circle.getRadius(); + } + + /** + * override of to string method to change to a suitable format + */ + @Override + public String toString() { + return "Circle:\nRadius: " + radius; + } +} \ No newline at end of file diff --git a/workshop_5/5.1/Main.java b/workshop_5/5.1/Main.java new file mode 100644 index 0000000..d9802b8 --- /dev/null +++ b/workshop_5/5.1/Main.java @@ -0,0 +1,29 @@ +/** + * the main class run the project and make instances od other classes + and add new objects and print shapes + * @author Setayesh + * @version 0.1 + */ +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); + System.out.println("((((draw all : ))))"); + paint.drawAll(); + System.out.println("\n((((print all info of shapes: ))))"); + paint.printAll(); +} +} \ No newline at end of file diff --git a/workshop_5/5.1/Paint.java b/workshop_5/5.1/Paint.java new file mode 100644 index 0000000..c0a7e8a --- /dev/null +++ b/workshop_5/5.1/Paint.java @@ -0,0 +1,74 @@ +import java.util.*; + +/** + * this class is use to add shapes and print information in methods + * @author Setayesh + * @version 0.1 + */ +public class Paint { + private ArrayList triangles; + private ArrayList circles; + private ArrayList rectangles; + + /** + * constructor of Paint class that define array lists of sub classes + */ + public Paint(){ + triangles = new ArrayList(); + circles = new ArrayList(); + rectangles = new ArrayList(); + } + + /** + * adding a triangle to array list + * @param triangle the new triangle to add to array list + */ + public void addTriangle(Triangle triangle){ + triangles.add(triangle); + } + + /** + * adding a circle to array list + * @param circle the new circle to add to array list + */ + public void addCircle(Circle circle){ + circles.add(circle); + } + + /** + * adding a rectangle to array list + * @param rectangle the new rectangle to add to array list + */ + public void addRectangle(Rectangle rectangle){ + rectangles.add(rectangle); + } + + /** + * print all perimeter and area of array list shapes + */ + public void drawAll(){ + for(Triangle triangle : triangles){ + triangle.draw(); + } + for(Circle circle : circles){ + circle.draw(); + } + for(Rectangle rectangle : rectangles){ + rectangle.draw(); + } + } + /** + * print information of shapes + */ + public void printAll(){ + for(Triangle triangle : triangles){ + System.out.println(triangle + "\n"); + } + for(Circle circle : circles){ + System.out.println(circle + "\n"); + } + for(Rectangle rectangle : rectangles){ + System.out.println(rectangle + "\n"); + } + } +} \ No newline at end of file diff --git a/workshop_5/5.1/Rectangle.java b/workshop_5/5.1/Rectangle.java new file mode 100644 index 0000000..73466e1 --- /dev/null +++ b/workshop_5/5.1/Rectangle.java @@ -0,0 +1,92 @@ +import java.util.*; + + +/** + * Rectangle is a subclass of paint to save special behavior of Rectangle + * @author setayesh + * @version 0.1 + */ +public class Rectangle { + private ArrayList sides; + /** + * constructor to add sides + * @param side1 + * @param side2 + * @param side3 + * @param side4 + */ + public Rectangle(double side1 , double side2 , double side3 ,double side4){ + sides = new ArrayList<>(); + sides.add(side1); + sides.add(side2); + sides.add(side3); + sides.add(side4); + } + + /** + * getter of list of sides + * @return sides arraylist + */ + public ArrayList getSides() { + return sides; + } + + /** + * @return if is square true + */ + public boolean isSquare(){ + if (!((getSides().get(0).equals(getSides().get(1))) && + (getSides().get(1).equals(getSides().get(2))) && + (getSides().get(2).equals(getSides().get(3))))) + return false; + return true; + } + + /** + * calculate perimeter of triangle + * @return perimeter + */ + public double calculatePerimeter(){ + return getSides().get(0) + getSides().get(1) + getSides().get(2) + getSides().get(3); + } + + /** + * calculate area + * @return area + */ + public double calculateArea(){ + return sides.get(0)*sides.get(1); + } + + /** + * show information of a triangle + */ + public void draw(){ + System.out.print("\nrectangle:\nArea: " + calculateArea() + "\nPerimeter: " + calculatePerimeter()); + System.out.println(); + if(isSquare()) + System.out.print("is a square." ); + else + System.out.print("isn't square"); + System.out.println(); + } + /** + * override of equals method to compare two shapes + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null || getClass() != obj.getClass()) + return false; + Rectangle rectangle = (Rectangle) obj; + return Objects.equals(sides, rectangle.sides); + } + /** + * override of to string method to change to a suitable format + */ + @Override + public String toString() { + return "Rectangle:\nside 1 : " + getSides().get(0) + ", side 2 : " + getSides().get(1) + ", side 3 : " + getSides().get(2) + ", side 4 : " + getSides().get(3); + } +} \ No newline at end of file diff --git a/workshop_5/5.1/Triangle.java b/workshop_5/5.1/Triangle.java new file mode 100644 index 0000000..7853a37 --- /dev/null +++ b/workshop_5/5.1/Triangle.java @@ -0,0 +1,85 @@ +import java.util.ArrayList; +import java.util.Objects; + +/** + * Triangle is a subclass of paint to save special behavior of triangle + * @author setayesh + * @version 0.1 + */ +public class Triangle { + private ArrayList sides; + /** + * constructor to add sides + * @param side1 + * @param side2 + * @param side3 + */ + public Triangle(double side1, double side2, double side3) { + sides = new ArrayList<>(); + sides.add(side1); + sides.add(side2); + sides.add(side3); + } + + /** + * @return sides list of sides + */ + public ArrayList getSides() { + return sides; + } + + /** + * @return true if is Equilateral + */ + public boolean isEquilateral(){ + if (!((getSides().get(0).equals(getSides().get(1))) && (getSides().get(1).equals(getSides().get(2))))) + return false; + return true; + } + + /** + * calculate perimeter + * @return perimeter + */ + public double calculatePerimeter(){ + return getSides().get(0) + getSides().get(1) + getSides().get(2); + } + + /** + * calculate area + * @return area + */ + public double calculateArea(){ + double h = calculatePerimeter() / 2; + return (Math.sqrt(h * (h - getSides().get(0)) * (h - getSides().get(1)) * (h - getSides().get(2)))); //Heron formula + } + + /** + * show information of a triangle + */ + public void draw(){ + System.out.print("\ntriangle:\nArea: " + calculateArea() + "\nPerimeter: " + calculatePerimeter()); + System.out.println(); + if(isEquilateral()) + System.out.println("is a Equilateral" ); + else + System.out.println("is'n a Equilateral" ); + } + /** + * override of equals method to compare two shapes + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Triangle)) return false; + Triangle triangle = (Triangle) o; + return Objects.equals(getSides(), triangle.getSides()); + } + /** + * override of to string method to change to a suitable format + */ + @Override + public String toString() { + return "Triangle\nside 1 = " + getSides().get(0) + ", side 2 = " + getSides().get(1) + ", side 3 = " + getSides().get(2); + } +} \ No newline at end of file diff --git a/workshop_5/5.2/Circle.java b/workshop_5/5.2/Circle.java new file mode 100644 index 0000000..7b97f5e --- /dev/null +++ b/workshop_5/5.2/Circle.java @@ -0,0 +1,69 @@ +import java.util.Objects; +/** + * Triangle is a subclass of shape to save special behavior of circle + * @author setayesh + * @version 0.1 + */ +public class Circle extends Shape{ + private int radius; + private final double PI = 3.14; + /** + * make a circle by valid radius. if the radius is smaller or equal to 0 , would ignore the action. + * @param radius radius of circle + */ + public Circle(int radius) { + this.radius = radius; + } + + + /** + * @return radius the radius + */ + public int getRadius() { + return radius; + } + + /** + * calculate perimeter of circle + * @return perimeter + */ + public double calculatePerimeter(){ + double perimeter = 2.0*PI*getRadius(); + return perimeter; + } + + /** + * calculate area + * @return area + */ + public double calculateArea(){ + return PI*getRadius()*getRadius();//auto casting because of PI + } + + /** + * show information of a circle + */ + public void draw(){ + System.out.println("\ncircle:\nArea: " + calculateArea() + "\nPerimeter: " + calculatePerimeter()); + } + + /** + * override of equals method to compare two shapes + */ + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Circle)) return false; + Circle circle = (Circle) o; + return getRadius() == circle.getRadius(); + } + + /** + * override of to string method to change to a suitable format + */ + @Override + public String toString() { + return "Circle:\nRadius: "; + } +} \ No newline at end of file diff --git a/workshop_5/5.2/Main.java b/workshop_5/5.2/Main.java new file mode 100644 index 0000000..60ec91a --- /dev/null +++ b/workshop_5/5.2/Main.java @@ -0,0 +1,46 @@ +/** + * A test for our program. + * + */ +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(); + + //Adding shapes in different order + paint.addShape(tri1); + + paint.addShape(circle1); + + paint.addShape(rect1); + + paint.addShape(rect2); + + paint.addShape(circle2); + + paint.addShape(rect3); + + paint.addShape(tri2); + + paint.drawAll(); + paint.printAll(); + System.out.print("The Equal Sides :\n"); + paint.describeEqualSides(); + } +} \ No newline at end of file diff --git a/workshop_5/5.2/Paint.java b/workshop_5/5.2/Paint.java new file mode 100644 index 0000000..45a81d0 --- /dev/null +++ b/workshop_5/5.2/Paint.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; + +/** + * this class is use to add shapes and print information in methods + * @author Setayesh + * @version 0.1 + */ +public class Paint { + ArrayList shapes; + + /** + * Constructor. + */ + public Paint() { + shapes = new ArrayList<>(); + } + + /** + * add a shape to list of shapes . + * @param shape shape to be added. + */ + public void addShape(Shape shape) { + shapes.add(shape); + } + + + /** + * print all perimeter and area of array list shapes + */ + public void drawAll () + { + for(Shape shape : shapes) + shape.draw(); + } + + /** + * print information of shapes + */ + public void printAll() { + for (Shape shape : shapes) { + System.out.println(shape + "\n"); + } + } + + /** + * prints all squares and equilateral triangles. + */ + public void describeEqualSides(){ + for(Shape shape : shapes) { + if (shape instanceof Triangle) + if (((Triangle) shape).isEquilateral()) + System.out.println(shape); + if(shape instanceof Rectangle) + if(((Rectangle) shape).isSquare()) + System.out.println(shape); + } + } + + + /** + * @return all shapes of this class + */ + public ArrayList getShapes() { + return shapes; + } +} \ No newline at end of file diff --git a/workshop_5/5.2/Polygon.java b/workshop_5/5.2/Polygon.java new file mode 100644 index 0000000..171c97a --- /dev/null +++ b/workshop_5/5.2/Polygon.java @@ -0,0 +1,98 @@ +import java.util.ArrayList; +import java.util.Objects; +/** + * subclass of shapes + * @author Setayesh + * @version 0.1 + */ +public abstract class Polygon extends Shape{ + + protected ArrayList sides; + + /** + *Polygon constructor. + * @param side sides of this polygon. + */ + public Polygon(double... side) { + sides = new ArrayList<>(); + for(Double s : side) + sides.add(s); + } + + /** + * perimeter and area of this polygon + */ + public void draw(){ + boolean isEquilateral = false; + boolean isSquare = false; + + if(this instanceof Rectangle) + isSquare = ((Rectangle) this).isSquare(); + if(this instanceof Triangle) + isEquilateral = ((Triangle) this).isEquilateral(); + + System.out.println(this.getClass().getName() + (isEquilateral ?" Equilateral :":isSquare?"\n Square :":" :") + + " \n Area:\n " + calculateArea() + "\nperimeter:\n " + calculatePerimeter()); + } + + /** + * prints kind and sizes of this shape. + * @return make a string that contains kind and sizes of this shape + */ + @Override + public String toString(){ + boolean isEquilateral = false; + boolean isSquare = false; + + if(this instanceof Rectangle) + isSquare = ((Rectangle) this).isSquare(); + if(this instanceof Triangle) + isEquilateral = ((Triangle) this).isEquilateral(); + + StringBuilder sSides = new StringBuilder(); + int counter = 1; + for(double side : getSides()) { + sSides.append("\nside").append(counter).append(" : ").append(side); + counter ++; + } + + + return (this.getClass().getName() + (isEquilateral ?"Equilateral : ":isSquare?"\nSquare":" ") + + " sides: \n" + sSides); + } + + /** + * calculate the perimeter of this polygon + * @return perimeter of this polygon + */ + public double calculatePerimeter() + { + double sum = 0; + for(Double side : sides) + sum += side; + return sum; + } + + + /** + * get the sides of this polygon + * @return sides of this polygon + */ + public ArrayList getSides() { + return sides; + } + + /** + * compare this object with this shape depends on sides . + * @param o object to be checked + * @return true if are equal else false + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Polygon)) return false; + Polygon polygon = (Polygon) o; + return getSides().equals(polygon.getSides()); + } + +} \ No newline at end of file diff --git a/workshop_5/5.2/Rectangle.java b/workshop_5/5.2/Rectangle.java new file mode 100644 index 0000000..9ae62fb --- /dev/null +++ b/workshop_5/5.2/Rectangle.java @@ -0,0 +1,58 @@ +import java.util.ArrayList; + +/** + * sub class of polygon + * @author Setayesh + * @version 0.1 + */ +public class Rectangle extends Polygon{ + + /** + * constructor + * @param side sides of this rectangle + */ + public Rectangle(double... side) { + super(side); + } + + + /** + * calculate the area of this rectangle + * @return area of this rectangle + */ + public double calculateArea() { + return getSides().get(0) * getSides().get(1); + } + + + + /** + * get the sides of this rectangle + * @return sides of this rectangle + */ + public ArrayList getSides() { + return sides; + } + + /** + * checks if all sides are equal + * @return true if equals else false. + */ + public boolean isSquare() + { + return getSides().get(0).equals(getSides().get(1)); + } + + + /** + * compare this object with this shape depends on sides . + * @param o object check + * @return true if are equal + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Rectangle)) return false; + return super.equals(o); + } +} \ No newline at end of file diff --git a/workshop_5/5.2/Shape.java b/workshop_5/5.2/Shape.java new file mode 100644 index 0000000..406140d --- /dev/null +++ b/workshop_5/5.2/Shape.java @@ -0,0 +1,40 @@ +import java.util.ArrayList; +import java.util.Objects; + +/** + * all shapes to be draw or print + * @author Setayesh + * @version 0.1 + */ +public abstract class Shape { + /** + * calculate the perimeter of this shape + * @return perimeter of this shape + */ + public abstract double calculatePerimeter(); + + + /** + * calculate the area of this shape + * @return area of this shape + */ + public abstract double calculateArea(); + + /** + * prints the kind , perimeter and area of this shape + */ + public abstract void draw(); + + /** + * @return make a string that contains kind and sizes of this shape + */ + @Override + public abstract String toString() ; + + /** + * @param obj object to be checked + * @return tru if same else false + */ + @Override + public abstract boolean equals(Object obj); +} \ No newline at end of file diff --git a/workshop_5/5.2/Triangle.java b/workshop_5/5.2/Triangle.java new file mode 100644 index 0000000..163a697 --- /dev/null +++ b/workshop_5/5.2/Triangle.java @@ -0,0 +1,45 @@ +import java.util.ArrayList; +import java.util.Objects; +/** + * Triangle is a subclass of polygon to save special behavior of triangle + * @author setayesh + * @version 0.1 + */ +public class Triangle extends Polygon { + + /** + * constructor makes a valid triangle. + * @param side sides of this triangle + */ + public Triangle(double... side) { + super(side); + } + /** + * calculate area + * @return area + */ + public double calculateArea(){ + double h = calculatePerimeter() / 2; + return (Math.sqrt(h * (h - getSides().get(0)) * (h - getSides().get(1)) * (h - getSides().get(2)))); //Heron formula + } + /** + * @return true if is Equilateral + */ + public boolean isEquilateral(){ + if (!((getSides().get(0).equals(getSides().get(1))) && (getSides().get(1).equals(getSides().get(2))))) + return false; + return true; + } + + /** + * compare this object with this shape depends on sides . + * @param o object to be checked + * @return true if are equal else false + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Triangle)) return false; + return super.equals(o); + } +} \ No newline at end of file