diff --git a/src/Course.java b/src/Course.java new file mode 100644 index 0000000..3078c44 --- /dev/null +++ b/src/Course.java @@ -0,0 +1 @@ +public record Course(int courseID, int score) {} diff --git a/src/Date.java b/src/Date.java new file mode 100644 index 0000000..a20af12 --- /dev/null +++ b/src/Date.java @@ -0,0 +1,46 @@ +public record Date(int day, Month month, int year) { + + public boolean isBiggerThan(Date other) { + if (this.year > other.year) { + return true; + } else if (this.year < other.year) { + return false; + } + + if (this.month.getNumeral() > other.month.getNumeral()) { + return true; + } else if (this.month.getNumeral() < other.month.getNumeral()) { + return false; + } + + return this.day > other.day; + } + + public int differenceFrom(Date other) { + int days1 = toDays(this); + int days2 = toDays(other); + + return Math.abs(days1 - days2); + } + + private int toDays(Date date) { + int totalDays = 0; + + for (int y = 0; y < date.year; y++) { + totalDays += isLeapYear(y) ? 366 : 365; + } + + for (int m = 0; m < date.month.ordinal(); m++) { + Month currentMonth = Month.values()[m]; + totalDays += currentMonth.getDays(isLeapYear(date.year)); + } + + totalDays += date.day; + + return totalDays; + } + + private boolean isLeapYear(int year) { + return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); + } +} \ No newline at end of file diff --git a/src/H16.java b/src/H16.java new file mode 100644 index 0000000..d0bd744 --- /dev/null +++ b/src/H16.java @@ -0,0 +1,41 @@ +public class H16 extends HelpCommands { + + public static Node ex54Apg117(Node node) { + if (node.getNext().getValue() % 2 == 1) { + return node.getNext(); + } + return ex54Apg117(node.getNext()); + } + + public static Node ex54Bpg117(Node[] nodes) { + Node ret = new Node<>(0); + for (Node node : nodes) { + setLast(ret, ex54Apg117(node)); + } + return ret; + } + + public static int pg119ex56part2(Node students) { + double max = 0; + int maxCourse = 0; + Queue q; + while (students != null) { + q = students.getValue().courses; + q.insert(null); + while (q.head() != null) { + if (students.getValue().getScoreAverage() > max) { + max = students.getValue().getScoreAverage(); + maxCourse = students.getValue().courses.head().courseID(); + } + q.insert(q.remove()); + } + students = students.getNext(); + } + return maxCourse; + } + + public static void main(String[] args) { + + } + +} diff --git a/src/HelpCommands.java b/src/HelpCommands.java index e34c21c..b8cac21 100644 --- a/src/HelpCommands.java +++ b/src/HelpCommands.java @@ -287,9 +287,9 @@ public static Queue buildQueue(int n, int max) { * @param queue * @return the length of the queue */ - public static int lengthQueue(Queue queue) { + public static int lengthQueue(Queue queue) { int len = 0; - Queue temp = new Queue<>(); + Queue temp = new Queue<>(); while (!queue.isEmpty()) { temp.insert(queue.remove()); len++; @@ -615,12 +615,31 @@ public static int countVal(Node node, char value) { * @param value * @return returns a new node with first value being param value and the next is the given node */ - public static Node setFirst(Node node, String value) { - Node ret = new Node<>(value); + public static Node setFirst(Node node, T value) { + Node ret = new Node<>(value); ret.setNext(node); return ret; } + public static Node setFirst(Node node, Node value) { + value.setNext(node); + return value; + } + + public static void setLast(Node node, T value) { + while (node.getNext() != null) { + node = node.getNext(); + } + node.setNext(new Node<>(value)); + } + + public static void setLast(Node node, Node value) { + while (node.getNext() != null) { + node = node.getNext(); + } + node.setNext(value); + } + /** * @param fst the node to reverse * @return a reversed form of the given node @@ -640,10 +659,11 @@ public static Node reverseNode(Node fst) { /** * A generic function - * @param node the node to search + * + * @param node the node to search * @param index the index + * @param the Type of the node * @return the value in the node in the given index - * @param the Type of the node */ public static T valueAt(Node node, int index) { for (int i = 0; i < index; i++) { @@ -654,10 +674,11 @@ public static T valueAt(Node node, int index) { /** * A generic function - * @param node the node to search + * + * @param node the node to search * @param index the index + * @param the Type of the node * @return the node in the given index - * @param the Type of the node */ public static Node nodeAt(Node node, int index) { for (int i = 0; i < index; i++) { diff --git a/src/Month.java b/src/Month.java new file mode 100644 index 0000000..18cb902 --- /dev/null +++ b/src/Month.java @@ -0,0 +1,33 @@ +public enum Month { + JANUARY(1, 31), + FEBRUARY(2, 28), + MARCH(3, 31), + APRIL(4, 30), + MAY(5, 31), + JUNE(6, 30), + JULY(7, 31), + AUGUST(8, 31), + SEPTEMBER(9, 30), + OCTOBER(10, 31), + NOVEMBER(11, 30), + DECEMBER(12, 31); + + private final int numeral; + private final int days; + + Month(int numeral, int days) { + this.numeral = numeral; + this.days = days; + } + + public int getNumeral() { + return numeral; + } + + public int getDays(boolean isLeapYear) { + if (this == FEBRUARY && isLeapYear) { + return 29; + } + return days; + } +} diff --git a/src/Report.java b/src/Report.java new file mode 100644 index 0000000..620339f --- /dev/null +++ b/src/Report.java @@ -0,0 +1,7 @@ +public record Report(Date dateGiven, int carID, int offenceID, int payment) { + + Report(){ + this(new Date(-1, Month.DECEMBER, -1), -1, -1, -1); + } + +} diff --git a/src/ReportDataBase.java b/src/ReportDataBase.java new file mode 100644 index 0000000..452a40a --- /dev/null +++ b/src/ReportDataBase.java @@ -0,0 +1,45 @@ +public class ReportDataBase { + + private Node reports; + + public ReportDataBase(){ + this.reports = new Node<>(new Report()); + } + + public void moveReportToLawyer(Date date){ + /* + לא כתבו אם לכתוב את זה או לא... אני מניח שלא צריך + */ + } + + public int carPaymentTotal(int carID){ + Node temp = reports; + int sum = 0; + while (temp != null) { + if (temp.getValue().carID() == carID) { + sum += temp.getValue().payment(); + } + } + return sum; + } + + /** + * + * במקום רשימה אפשר להשתמש במערך + * @return pg118ex55C + */ + public Node findDriver(int offenceID){ + Node ret = new Node<>(-1); + Node p = ret; + Node temp = reports; + while (temp != null){ + if (temp.getValue().offenceID() == offenceID){ + p.setNext(new Node<>(temp.getValue().carID())); + p = p.getNext(); + } + temp = temp.getNext(); + } + return ret.getNext(); + } + +} diff --git a/src/Student.java b/src/Student.java new file mode 100644 index 0000000..4ac76de --- /dev/null +++ b/src/Student.java @@ -0,0 +1,50 @@ +public class Student { + + private String name; + private int ID; + private int yearOfStudy; + public Queue courses; + + public Student(String name, int ID, int yearOfStudy) { + this.name = name; + this.ID = ID; + this.yearOfStudy = yearOfStudy; + } + + //A + public double getScoreAverage(){ + double sum = 0; + courses.insert(new Course(-1, 100)); + while (courses.head().courseID() != -1) { + sum += courses.head().score(); + courses.insert(courses.remove()); + } + courses.remove(); + return sum / HelpCommands.lengthQueue(courses); + } + // + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getID() { + return ID; + } + + public void setID(int ID) { + this.ID = ID; + } + + public int getYearOfStudy() { + return yearOfStudy; + } + + public void setYearOfStudy(int yearOfStudy) { + this.yearOfStudy = yearOfStudy; + } +} \ No newline at end of file diff --git a/src/ex12A.png b/src/ex12A.png new file mode 100644 index 0000000..d42f502 Binary files /dev/null and b/src/ex12A.png differ diff --git a/src/ex12B.png b/src/ex12B.png new file mode 100644 index 0000000..b4e932a Binary files /dev/null and b/src/ex12B.png differ diff --git a/src/ex13.png b/src/ex13.png new file mode 100644 index 0000000..a79dd41 Binary files /dev/null and b/src/ex13.png differ