diff --git a/src/oop/exe0/Fraction.java b/src/oop/exe0/Fraction.java new file mode 100644 index 0000000..c589acd --- /dev/null +++ b/src/oop/exe0/Fraction.java @@ -0,0 +1,56 @@ +package oop.exe0; + +public class Fraction { + + /* Definitions of fields (variables) */ + + // public variable, accessible from other classes + // (Accessible to other programmers who will use this class) + public int numerator; + + // private variable, accessible from only within the class + private int denominator; + + + + /* Definitions of methods of that class */ + + /*** + * The programmer that uses the class may ask this class to change + * its denominator via this method. + * Since the denominator is a private field, this is the only way of changing + * the denominator of the class + * + * This method asks for a number as a denominator, and if it's not 0 + * it sets the denominator to be that number + * if the number is zero, an error message will be displayed + */ + public void setDenominator(int t) { + if (t != 0) { + denominator = t; + } else { + // Denominator cannot be 0... + System.out.println("ERROR OMG"); + } + } + + + /*** + * Since the denominator field (=variable) is private, the programmer + * does not have access even to read its value. + * We do however like to give access to the programmer to read that value. + * So we provide a public method (which the programmer can call) + * and inside it we retrieve the value of the denominator and return it + * @return The value of denominator + */ + public int getDenominator() { + return denominator; + } + + /** + * This function prints the values of the fields in a nice format + */ + public void print() { + System.out.println(numerator + "/" + denominator); + } +} diff --git a/src/oop/exe0/Mainer.java b/src/oop/exe0/Mainer.java new file mode 100644 index 0000000..36ef0f3 --- /dev/null +++ b/src/oop/exe0/Mainer.java @@ -0,0 +1,26 @@ +package oop.exe0; + +public class Mainer { + public static void main(String[] args) { + // This code represents the "programmer" referred to in the Fraction class + + Fraction f1 = new Fraction(); // Create a new "Fraction" variable + f1.numerator = 1; // Change f1's numerator + f1.setDenominator(3); // Change f1's denominator + + Fraction f2 = new Fraction(); // Create a new "Fraction" variable + f2.numerator = 2; + f2.setDenominator(5); + + // After creating 2 objects, we can play around with them for a bit + f1.print(); // Prints the data of f1 + f1.setDenominator(9); + f1.print(); // Now it will print 1/9 because f1's denominator changed + + f1.setDenominator(0); // This will print "ERROR" and will not change the denominator + + f2.print(); + f2.numerator = 3; + f2.print(); + } +} diff --git a/src/oop/exe1/Fraction.java b/src/oop/exe1/Fraction.java new file mode 100644 index 0000000..e16fb62 --- /dev/null +++ b/src/oop/exe1/Fraction.java @@ -0,0 +1,71 @@ +package oop.exe1; + +public class Fraction { + + public void setNumerator(int n) { + // YOUR CODE HERE + } + + public void setDenominator(int n) { + // YOUR CODE HERE + } + + public int getNumerator() { + // YOUR CODE HERE + return -1; // change this + } + + public int getDenominator() { + // YOUR CODE HERE + return -1; + } + + /*** + * This function checks if the current fraction is equal to another fracion + * given as an argument. + * Note: Comparing Fractions cannot be done with the '==' operator alone + * You have to extract other's numerator and denominator and compare them! + * + * @param other Another Fraction object + * @return true if they are equal, false otherwise + */ + public boolean isEqual(Fraction other) { + // YOUR CODE HERE + return false; + } + + + /*** + * Prints the fraction in a nice format + */ + public void print() { + // YOUR CODE HERE + } + + /*** + * This function returns the double value of the fraction. + * For example: if the fraction contains num as 5 and denom as 10 + * The value returned from the function is 0.5 + * + * @return A double value representing the fraction + */ + public double getDoubleValue() { + // YOUR CODE HERE + return 0.0; // change this + } + + + /** + * This method returns an integer representing the fraction, + * the integer is a rounded value of the fraction itself. + * For example: if the fraction is 5/10 -> the method rounds UP => 1 + * if the fraction is 6/10 -> the method rounds UP => 1 + * if the fraction is 4/10 -> the method rounds DOWN => 0 + * @return An integer representing the rounded value of the fraction + */ + public int round() { + return -1; // change this + } + + +} diff --git a/src/oop/exe1/Main.java b/src/oop/exe1/Main.java deleted file mode 100644 index 9d6e5af..0000000 --- a/src/oop/exe1/Main.java +++ /dev/null @@ -1,32 +0,0 @@ -package oop.exe1; - -public class Main { - public static void main(String[] args) { - Bank bank = new Bank(5); - - // These should print-out "true" - System.out.println(bank.addPerson("Samuel", 10.0)); - System.out.println(bank.addPerson("Andrew", 5.0)); - System.out.println(bank.addPerson("Dan", 7.5)); - System.out.println(bank.addPerson("Eric", 10.0)); - System.out.println(bank.addPerson("John", 10.0)); - - // This one should print-out "false" - System.out.println(bank.addPerson("Shaqed", 10.0)); - - - bank.deposit("andrew1", 5.0); // This should be false - bank.deposit("Andrew", 5.0); // This should be true - - bank.deposit("Dan", 2.5); // This should be true - - bank.withdraw("Shaqed", 11); // This should return -1 - bank.withdraw("Eric", 10); // This should return 10 - - bank.transfer("Dan", "Eric", 5.0); // This should be true - bank.transfer("Samuel", "Andrew", 15); // This should be false - - bank.print(); - } - -} diff --git a/src/oop/exe1/Mainer.java b/src/oop/exe1/Mainer.java new file mode 100644 index 0000000..52a9ba2 --- /dev/null +++ b/src/oop/exe1/Mainer.java @@ -0,0 +1,54 @@ +package oop.exe1; + +public class Mainer { + + static Fraction multiplyFractions(Fraction a, Fraction b) { + Fraction ans = new Fraction(); + + // Your code here + + return ans; // Return value is 'ans' which its data-type is a Fraction + // As required by the function return type + } + + + /*** + * In this exercise, you are to complete the empty methods of Fraction + * So the described output will be received. + * + * In the main you can an example of a usage of your Fraction class + * by another programmer, you are not to touch the main! + */ + + public static void main(String[] args) { + Fraction f1 = new Fraction(); + f1.setNumerator(1); + f1.setDenominator(0); // Should print "ERROR" + f1.setDenominator(3); + + System.out.println(f1.round()); // 0 + System.out.println(f1.getDoubleValue()); // 0.333... + System.out.println(f1.getNumerator()); // 1 + System.out.println(f1.getDenominator()); // 3 + + + Fraction f2 = new Fraction(); + f2.setNumerator(4); + f2.setDenominator(5); + + + System.out.println(f2.isEqual(f1)); // false + f2.setNumerator(1); + f2.setDenominator(3); + System.out.println(f2.isEqual(f1)); // true + System.out.println(f1.isEqual(f2)); // true + + f1.setDenominator(30); + + f1.print(); // 1/3 + f2.print(); // 1/30 + + Fraction f3 = multiplyFractions(f1, f2); + f3.print(); // 1/90 + } +} diff --git a/src/oop/exe1/Bank.java b/src/oop/exe2/Bank.java similarity index 99% rename from src/oop/exe1/Bank.java rename to src/oop/exe2/Bank.java index f18e552..bd2dc01 100644 --- a/src/oop/exe1/Bank.java +++ b/src/oop/exe2/Bank.java @@ -1,4 +1,4 @@ -package oop.exe1; +package oop.exe2; /** * * Object Oriented Programming - Bank diff --git a/src/oop/exe2/Main.java b/src/oop/exe2/Main.java index a891be9..5bc496a 100644 --- a/src/oop/exe2/Main.java +++ b/src/oop/exe2/Main.java @@ -1,54 +1,32 @@ package oop.exe2; - -/** - * Object Oriented - PC Gamer - * - * In this exercise, you will be implementing a computer trying to install games on it - * The computer will have a fixed amount of memory installed, and the amount will not change - * - * Each game has its required memory, and the computer will install the game only if it has enough space for it - * Each game also has a release date, indicated by the SimpleDate class, games that are not available yet cannot be installed - * - * You are required to implement the methods in each class according to their description, - * also, you may add more inner methods as you wish if you think that it would help you. - * And eventually, the main should run as described in the comments - */ public class Main { - public static void main(String[] args) { - Game game1 = new Game("001", "Need For Speed: Most Wanted", 16, new SimpleDate(1,1,2000)); - Game game2 = new Game("002", "Half Life 3", 56, new SimpleDate(29,12,2037)); - Game game3 = new Game("003", "Counter Strike 1.6", 8, new SimpleDate(1,1,2002)); - Game game4 = new Game("004", "Smite", 24, new SimpleDate(1,12,2017)); - Game game5 = new Game("005", "World of Warcraft", 56, new SimpleDate(24,3,2004)); + Bank bank = new Bank(5); + // These should print-out "true" + System.out.println(bank.addPerson("Samuel", 10.0)); + System.out.println(bank.addPerson("Andrew", 5.0)); + System.out.println(bank.addPerson("Dan", 7.5)); + System.out.println(bank.addPerson("Eric", 10.0)); + System.out.println(bank.addPerson("John", 10.0)); - Computer myPC = new Computer(64); + // This one should print-out "false" + System.out.println(bank.addPerson("Shaqed", 10.0)); - myPC.printInstalledGames(); // Should print nothing - System.out.println(myPC.installGame(game2)); // Fails, game not released yet + bank.deposit("andrew1", 5.0); // This should be false + bank.deposit("Andrew", 5.0); // This should be true - System.out.println(myPC.installGame(game1)); // Success - System.out.println(myPC.installGame(game3)); // Success - System.out.println(myPC.installGame(game5)); // Fails, not enough memory + bank.deposit("Dan", 2.5); // This should be true - myPC.printInstalledGames(); // Should print only 2 games + bank.withdraw("Shaqed", 11); // This should return -1 + bank.withdraw("Eric", 10); // This should return 10 - System.out.println(myPC.removeGame(game5)); // Fails, game is not installed yet - System.out.println(myPC.removeGame(game1)); // Success - System.out.println(myPC.installGame(game5)); // Success - System.out.println(myPC.installGame(game4)); // Fails, not enough memory + bank.transfer("Dan", "Eric", 5.0); // This should be true + bank.transfer("Samuel", "Andrew", 15); // This should be false - // Print games installed - // This should print game 3 and game 5 - for (Game game : myPC.getInstalledGames()) { - System.out.println(game.getUID() + ": " + game.getName()); - } - - // Pick a random game - Game randomGame = myPC.pickRandomGame(); - System.out.println("My random game: " + randomGame.getName()); + bank.print(); } + } diff --git a/src/oop/exe2/Computer.java b/src/oop/exe3/Computer.java similarity index 99% rename from src/oop/exe2/Computer.java rename to src/oop/exe3/Computer.java index 4fa0ea1..0245226 100644 --- a/src/oop/exe2/Computer.java +++ b/src/oop/exe3/Computer.java @@ -1,4 +1,4 @@ -package oop.exe2; +package oop.exe3; public class Computer { diff --git a/src/oop/exe2/Game.java b/src/oop/exe3/Game.java similarity index 95% rename from src/oop/exe2/Game.java rename to src/oop/exe3/Game.java index beaab76..1d178de 100644 --- a/src/oop/exe2/Game.java +++ b/src/oop/exe3/Game.java @@ -1,6 +1,4 @@ -package oop.exe2; - -import java.util.Date; +package oop.exe3; public class Game { diff --git a/src/oop/exe3/Main.java b/src/oop/exe3/Main.java new file mode 100644 index 0000000..3569dd3 --- /dev/null +++ b/src/oop/exe3/Main.java @@ -0,0 +1,54 @@ +package oop.exe3; + + +/** + * Object Oriented - PC Gamer + * + * In this exercise, you will be implementing a computer trying to install games on it + * The computer will have a fixed amount of memory installed, and the amount will not change + * + * Each game has its required memory, and the computer will install the game only if it has enough space for it + * Each game also has a release date, indicated by the SimpleDate class, games that are not available yet cannot be installed + * + * You are required to implement the methods in each class according to their description, + * also, you may add more inner methods as you wish if you think that it would help you. + * And eventually, the main should run as described in the comments + */ +public class Main { + + public static void main(String[] args) { + Game game1 = new Game("001", "Need For Speed: Most Wanted", 16, new SimpleDate(1,1,2000)); + Game game2 = new Game("002", "Half Life 3", 56, new SimpleDate(29,12,2037)); + Game game3 = new Game("003", "Counter Strike 1.6", 8, new SimpleDate(1,1,2002)); + Game game4 = new Game("004", "Smite", 24, new SimpleDate(1,12,2017)); + Game game5 = new Game("005", "World of Warcraft", 56, new SimpleDate(24,3,2004)); + + + Computer myPC = new Computer(64); + + myPC.printInstalledGames(); // Should print nothing + + System.out.println(myPC.installGame(game2)); // Fails, game not released yet + + System.out.println(myPC.installGame(game1)); // Success + System.out.println(myPC.installGame(game3)); // Success + System.out.println(myPC.installGame(game5)); // Fails, not enough memory + + myPC.printInstalledGames(); // Should print only 2 games + + System.out.println(myPC.removeGame(game5)); // Fails, game is not installed yet + System.out.println(myPC.removeGame(game1)); // Success + System.out.println(myPC.installGame(game5)); // Success + System.out.println(myPC.installGame(game4)); // Fails, not enough memory + + // Print games installed + // This should print game 3 and game 5 + for (Game game : myPC.getInstalledGames()) { + System.out.println(game.getUID() + ": " + game.getName()); + } + + // Pick a random game + Game randomGame = myPC.pickRandomGame(); + System.out.println("My random game: " + randomGame.getName()); + } +} diff --git a/src/oop/exe2/SimpleDate.java b/src/oop/exe3/SimpleDate.java similarity index 97% rename from src/oop/exe2/SimpleDate.java rename to src/oop/exe3/SimpleDate.java index ec5cc47..59bfc74 100644 --- a/src/oop/exe2/SimpleDate.java +++ b/src/oop/exe3/SimpleDate.java @@ -1,4 +1,4 @@ -package oop.exe2; +package oop.exe3; public class SimpleDate {