diff --git a/src/oop/exe1/Bank.java b/src/oop/exe1/Bank.java new file mode 100644 index 0000000..f18e552 --- /dev/null +++ b/src/oop/exe1/Bank.java @@ -0,0 +1,113 @@ +package oop.exe1; +/** + * + * Object Oriented Programming - Bank + * + * In this exercise you will have to create a Bank, by implementing the empty methods listed below. + * The bank will store its clients by name and the amount of currency they have, + * it will store the clients using an array of Strings, and array of doubles + * to represent the amount of currency. + * + * In the constructor, an initial value for CAPACITY is given, there can be no more than + * this number of clients to this bank + * + * + * Implement the empty methods, be sure to check every possible input and return the correct results: + * 1. No prints inside the methods + * 2. Account for bad inputs (non existing name, invalid amount of currency) + * 3. The bank DOES NOT ALLOW anyone to be in debt, so no client should have a negative amount of currency + * 4. Each deletion / addition to the clients list requires to modify the array + * so that there will be no empty spaces in the array + * 5. The total number of clients (size) will never exceed CAPACITY + * + * + * Extra: + * 1. The bank now does allow anyone to be in debt, associate maximum possible debt for each person + * and modify the methods accordingly + * */ +public class Bank { + private double[] currency; + private String[] persons; + private int size; + + private final int CAPACITY; + + public Bank(int capacity) { + this.CAPACITY = 0; // Change this + } + + + /** + * Adds a new person to the bank + * + * Returns whether the operation was successful or not + * */ + public boolean addPerson(String name, double currency) { + // Your code here + return false; + } + + /** + * Tries to remove a person from the bank + * + * Returns whether the operation was successful or not + * */ + public boolean removePerson(String name) { + // Your code here + return false; + } + + + /** + * Returns the amount of currency a person has + * + * returns -1.0 if operation was not successful + * returns a positive decimal number if the operation was successful + * */ + public double getCurrencyOf(String name) { + return -1.0; + } + + /** + * Attempts to update the currency of a person, given his name + * + * Returns whether the operation was successful or not + * */ + public boolean deposit(String person, double amount) { + // Your code here + return false; + } + + /** + * Attempts to withdraw an amount of money from the person's account + * + * Returns -1.0 if the operation was unsuccessful + * Returns any positive decimal number if the operation was successful + * */ + public double withdraw(String person, double amount) { + // Your code here + return -1.0; + } + + + /** + * Attempts to transfer the amount of + * currency from personFrom to personTo + * + * Returns whether the operation was successful + */ + public boolean transfer(String personFrom, String personTo, double amount) { + // Your code here + return false; + } + + + + + public void print() { + System.out.println("Printing content of the bank:"); + for (int i = 0; i < size; i++) { + System.out.print("Person: " + persons[i] + " has " + currency[i] + "\n"); + } + } +} diff --git a/src/oop/exe1/Main.java b/src/oop/exe1/Main.java new file mode 100644 index 0000000..9d6e5af --- /dev/null +++ b/src/oop/exe1/Main.java @@ -0,0 +1,32 @@ +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/exe2/Computer.java b/src/oop/exe2/Computer.java new file mode 100644 index 0000000..4fa0ea1 --- /dev/null +++ b/src/oop/exe2/Computer.java @@ -0,0 +1,70 @@ +package oop.exe2; + +public class Computer { + + public Computer(int memoryInstalled) { + } + + /** + * This method will check if the game is already installed on the computer + * + * Hint: use the unique identifier for each game to compare between games + * */ + public boolean isGameInstalled(Game game) { + return false; + } + + /** + * This method will attempt to install a game on the computer + * A game will be installed on the computer ONLY if the following conditions are met: + * 1. The computer has enough free memory to accommodate the game + * 2. The game is already available (the release date has passed) + * 3. The game is not already installed on the computer + * + * In case of failure, the method will print an error message describing why it failed + * + * The method will return true or false to indicate successful installation + * */ + public boolean installGame(Game game) { + return false; + } + + + /** + * Attempts to remove a game from this computer + * Removing a game will free the memory it required. + * + * This method will fail if the game is not already installed + * The method will return true or false to indicate successful removal of the desired game + * */ + public boolean removeGame(Game game) { + return false; + } + + + /** + * This method returns an array the contains all of the games that are installed on the machine + * + * If there are no games installed on the computer, the array is empty. + * Note that the returned value is an array of Game, not an array of Strings + */ + public Game[] getInstalledGames() { + return new Game[0]; + } + + /** + * This method prints the names of all games installed on the computer + * */ + public void printInstalledGames() { + + } + + + /** + * This method picks a random game from the installed games in the computer. + * If there are no installed games, the method returns "null" + * */ + public Game pickRandomGame() { + return null; + } +} diff --git a/src/oop/exe2/Game.java b/src/oop/exe2/Game.java new file mode 100644 index 0000000..beaab76 --- /dev/null +++ b/src/oop/exe2/Game.java @@ -0,0 +1,43 @@ +package oop.exe2; + +import java.util.Date; + +public class Game { + + public Game(String gameUID, String name, int requiredMB, SimpleDate releaseDate) { + + } + + /** + * Returns the amount of memory in Mega-bytes that this game require + * */ + public int getRequiredMemory() { + return 0; + } + + /** + * Checks if the current date (the current time which this program is running) + * is after the release date of this game. + * For example: If this game's release date is 1/1/2000 + * And this program is being run at 20/12/1999 - the answer is false + * + * Hint: To implement this method it would be helpful to first implement the SimpleDate class + * */ + public boolean isAvailable() { + return false; + } + + /** + * Returns the unique identifier of the game + * */ + public String getUID() { + return ""; + } + + /** + * Returns the name of the game + * */ + public String getName() { + return ""; + } +} diff --git a/src/oop/exe2/Main.java b/src/oop/exe2/Main.java new file mode 100644 index 0000000..a891be9 --- /dev/null +++ b/src/oop/exe2/Main.java @@ -0,0 +1,54 @@ +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)); + + + 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/exe2/SimpleDate.java new file mode 100644 index 0000000..ec5cc47 --- /dev/null +++ b/src/oop/exe2/SimpleDate.java @@ -0,0 +1,30 @@ +package oop.exe2; + +public class SimpleDate { + + public SimpleDate(int day, int month, int year) { + } + + public int getDay() { + return 0; + } + + public int getMonth() { + return 0; + } + + public int getYear() { + return 0; + } + + + /** + * This method will compare this SimpleDate to another SimpleDate and will return one of the following + * 1. A positive integer if anotherDate is greater than the current one + * 2. 0 if they are equal + * 3. A negative integer if the current date is greater than anotherDate + * */ + public int compareTo(SimpleDate anotherDate) { + return 0; + } +} diff --git a/src/strings/out/Exe1.java b/src/strings/out/Exe1.java new file mode 100644 index 0000000..280d09d --- /dev/null +++ b/src/strings/out/Exe1.java @@ -0,0 +1,24 @@ +package strings.out; + +public class Exe1 { + + static int spaces(String s) { + // YOUR CODE HERE + return 0; + } + + + /* + * Complete the function to count the number of spaces in a string + * */ + public static void main(String[] args) { + String str1 = "Hello world"; + String str2 = "The Red Fox"; + String str3 = ""; + + System.out.println(spaces(str1)); // 1 + System.out.println(spaces(str2)); // 2 + System.out.println(spaces(str3)); // 0 + } + +} diff --git a/src/strings/out/Exe2.java b/src/strings/out/Exe2.java new file mode 100644 index 0000000..f6ac60e --- /dev/null +++ b/src/strings/out/Exe2.java @@ -0,0 +1,39 @@ +package strings.out; + +public class Exe2 { + static boolean compare(String s1, String s2) { + // YOUR CODE HERE + return false; + } + + /* + * Complete the function above to compare 2 strings together and return the answer + * as a boolean. + * + * EXTRA: Overload the function to receive an extra boolean as an argument: + * "boolean caseSensitive" + * If the boolean is false, the comparison ignores the cases of letter + * and will treat "Hello" and "HELLO" as equals + * + * */ + + public static void main(String[] args) { + String a1 = "Hello world"; + String b1 = "HelloWorld"; + + String a2 = "Hi, my name is"; + String b2 = "Hi, my name is\n"; + + String a3 = ""; + String b3 = ""; + + String a4 = "Wazap"; + String b4 = "Wazap"; + + System.out.println(compare(a1, b1)); // false + System.out.println(compare(a2, b2)); // false + System.out.println(compare(a3, b3)); // false + System.out.println(compare(a4, b4)); // true + + } +} diff --git a/src/strings/sol/Exe1.java b/src/strings/sol/Exe1.java new file mode 100644 index 0000000..1b29852 --- /dev/null +++ b/src/strings/sol/Exe1.java @@ -0,0 +1,31 @@ +package strings.sol; + +public class Exe1 { + + static int spaces(String s) { + int ans = 0; + for (int i = 0; i < s.length(); i++) { + char tempChar = s.charAt(i); + + if (tempChar == ' ') { + ans++; + } + } + return ans; + } + + + /* + * Complete the function to count the number of spaces in a string + * */ + public static void main(String[] args) { + String str1 = "Hello world"; + String str2 = "The Red Fox"; + String str3 = ""; + + System.out.println(spaces(str1)); // 1 + System.out.println(spaces(str2)); // 2 + System.out.println(spaces(str3)); // 0 + } + +} diff --git a/src/strings/sol/Exe2.java b/src/strings/sol/Exe2.java new file mode 100644 index 0000000..ee5d3b6 --- /dev/null +++ b/src/strings/sol/Exe2.java @@ -0,0 +1,55 @@ +package strings.sol; + +public class Exe2 { + static boolean compare(String s1, String s2) { + + // If the lengths do no match, we don't need to even compare them + if (s1.length() != s2.length()) { + return false; + } + + boolean equals = true; + int n = s1.length(); // doesn't matter that I took s1 and not s2 + // because at this point s1 and s2 have the same length + + for (int i = 0; i < n; i++) { + if (s1.charAt(i) != s2.charAt(i)) { + equals = false; + break; // stop the loop, there's no need to keep looking + } + } + + return equals; + } + + /* + * Complete the function above to compare 2 strings together and return the answer + * as a boolean. + * + * EXTRA: Overload the function to receive an extra boolean as an argument: + * "boolean caseSensitive" + * If the boolean is false, the comparison ignores the cases of letter + * and will treat "Hello" and "HELLO" as equals + * + * */ + + public static void main(String[] args) { + String a1 = "Hello world"; + String b1 = "HelloWorld"; + + String a2 = "Hi, my name is"; + String b2 = "Hi, my name is\n"; + + String a3 = ""; + String b3 = ""; + + String a4 = "Wazap"; + String b4 = "Wazap"; + + System.out.println(compare(a1, b1)); // false + System.out.println(compare(a2, b2)); // false + System.out.println(compare(a3, b3)); // false + System.out.println(compare(a4, b4)); // true + + } +}