Skip to content

Commit

Permalink
Added more strings exercises as well as one more advanced OOP one
Browse files Browse the repository at this point in the history
  • Loading branch information
shaqed committed Oct 6, 2018
1 parent 23e740f commit bcc5ef8
Show file tree
Hide file tree
Showing 10 changed files with 491 additions and 0 deletions.
113 changes: 113 additions & 0 deletions src/oop/exe1/Bank.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
32 changes: 32 additions & 0 deletions src/oop/exe1/Main.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
70 changes: 70 additions & 0 deletions src/oop/exe2/Computer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 43 additions & 0 deletions src/oop/exe2/Game.java
Original file line number Diff line number Diff line change
@@ -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 "";
}
}
54 changes: 54 additions & 0 deletions src/oop/exe2/Main.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
30 changes: 30 additions & 0 deletions src/oop/exe2/SimpleDate.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 24 additions & 0 deletions src/strings/out/Exe1.java
Original file line number Diff line number Diff line change
@@ -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
}

}
Loading

0 comments on commit bcc5ef8

Please sign in to comment.