Skip to content
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
1 change: 1 addition & 0 deletions src/Course.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record Course(int courseID, int score) {}
46 changes: 46 additions & 0 deletions src/Date.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
41 changes: 41 additions & 0 deletions src/H16.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class H16 extends HelpCommands {

public static Node<Integer> ex54Apg117(Node<Integer> node) {
if (node.getNext().getValue() % 2 == 1) {
return node.getNext();
}
return ex54Apg117(node.getNext());
}

public static Node<Integer> ex54Bpg117(Node<Integer>[] nodes) {
Node<Integer> ret = new Node<>(0);
for (Node<Integer> node : nodes) {
setLast(ret, ex54Apg117(node));
}
return ret;
}

public static int pg119ex56part2(Node<Student> students) {
double max = 0;
int maxCourse = 0;
Queue<Course> 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) {

}

}
37 changes: 29 additions & 8 deletions src/HelpCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ public static Queue<Integer> buildQueue(int n, int max) {
* @param queue
* @return the length of the queue
*/
public static int lengthQueue(Queue<Integer> queue) {
public static <T> int lengthQueue(Queue<T> queue) {
int len = 0;
Queue<Integer> temp = new Queue<>();
Queue<T> temp = new Queue<>();
while (!queue.isEmpty()) {
temp.insert(queue.remove());
len++;
Expand Down Expand Up @@ -615,12 +615,31 @@ public static int countVal(Node<Character> 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<String> setFirst(Node<String> node, String value) {
Node<String> ret = new Node<>(value);
public static <T> Node<T> setFirst(Node<T> node, T value) {
Node<T> ret = new Node<>(value);
ret.setNext(node);
return ret;
}

public static <T> Node<T> setFirst(Node<T> node, Node<T> value) {
value.setNext(node);
return value;
}

public static <T> void setLast(Node<T> node, T value) {
while (node.getNext() != null) {
node = node.getNext();
}
node.setNext(new Node<>(value));
}

public static <T> void setLast(Node<T> node, Node<T> 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
Expand All @@ -640,10 +659,11 @@ public static Node<Integer> reverseNode(Node<Integer> fst) {

/**
* A generic function
* @param node the node to search
*
* @param node the node to search
* @param index the index
* @param <T> the Type of the node
* @return the value in the node in the given index
* @param <T> the Type of the node
*/
public static <T> T valueAt(Node<T> node, int index) {
for (int i = 0; i < index; i++) {
Expand All @@ -654,10 +674,11 @@ public static <T> T valueAt(Node<T> node, int index) {

/**
* A generic function
* @param node the node to search
*
* @param node the node to search
* @param index the index
* @param <T> the Type of the node
* @return the node in the given index
* @param <T> the Type of the node
*/
public static <T> Node<T> nodeAt(Node<T> node, int index) {
for (int i = 0; i < index; i++) {
Expand Down
33 changes: 33 additions & 0 deletions src/Month.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
7 changes: 7 additions & 0 deletions src/Report.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
45 changes: 45 additions & 0 deletions src/ReportDataBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
public class ReportDataBase {

private Node<Report> reports;

public ReportDataBase(){
this.reports = new Node<>(new Report());
}

public void moveReportToLawyer(Date date){
/*
לא כתבו אם לכתוב את זה או לא... אני מניח שלא צריך
*/
}

public int carPaymentTotal(int carID){
Node<Report> temp = reports;
int sum = 0;
while (temp != null) {
if (temp.getValue().carID() == carID) {
sum += temp.getValue().payment();
}
}
return sum;
}

/**
*
* במקום רשימה אפשר להשתמש במערך
* @return pg118ex55C
*/
public Node<Integer> findDriver(int offenceID){
Node<Integer> ret = new Node<>(-1);
Node<Integer> p = ret;
Node<Report> 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();
}

}
50 changes: 50 additions & 0 deletions src/Student.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
public class Student {

private String name;
private int ID;
private int yearOfStudy;
public Queue<Course> 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;
}
}
Binary file added src/ex12A.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/ex12B.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/ex13.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.