-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more OOP exercises and examples
- Loading branch information
Showing
9 changed files
with
175 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package oop.exe2; | ||
|
||
public class DynamicArray { | ||
|
||
/*** | ||
* This method retrieves an item from the dynamic array at said index | ||
* | ||
* if the given index is out of the bounds an error message | ||
* will be printed and the method will return -1 | ||
* | ||
* @param index The desired index | ||
* @return The value desired | ||
*/ | ||
public int get(int index) { | ||
return -1; | ||
} | ||
|
||
|
||
/*** | ||
* This method returns the current length of the Dynamic Array | ||
* Note that for every new item added to the dynamic array, | ||
* the length increases | ||
* @return The length of the dynamic array | ||
*/ | ||
public int length() { | ||
return 0; | ||
} | ||
|
||
/*** | ||
* This method adds a new item to the dynamic array | ||
*/ | ||
public void add(int value) { | ||
|
||
} | ||
|
||
/*** | ||
* Attempts to remove an item from the dynamic array based on a given index | ||
* The method will fail if the index is invalid | ||
* | ||
* The method will also re-arrange the array in the following way: | ||
* Let some array be: {5, 6, 7} (where index 0: 5 and index 1: 6 and index 2: 7) | ||
* After a remove(1) (remove the 6) | ||
* The array will be: {5,7} (where index 0: 5 and index 1: 7) | ||
* | ||
* @param index A valid given index | ||
* @return A boolean indicating whether the deletion was successful | ||
*/ | ||
public boolean remove(int index) { | ||
return false; | ||
} | ||
|
||
/*** | ||
* Prints the array in a nice format | ||
*/ | ||
public void print() { | ||
|
||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package oop.exe2; | ||
|
||
/** | ||
* In this exercise you will be creating a Dynamic Array. | ||
* | ||
* As we know, arrays - once declared, cannot change their size. | ||
* We would like to write a class that can do that | ||
* | ||
* Below in the "main" you will see code written by a programmer using your | ||
* class, expected output in the prints are written in the comments. | ||
* | ||
* Complete the DynamicArray class methods to make the class function as desired | ||
* | ||
* Note: You may add as many fields and methods you like - but they must be 'private' | ||
*/ | ||
public class Mainer { | ||
|
||
public static void main(String[] args) { | ||
// Create a new dynamic array object | ||
// The array is currently empty | ||
DynamicArray arr = new DynamicArray(); | ||
|
||
System.out.println(arr.length()); // 0 | ||
System.out.println(arr.get(0)); // -1, there are no items | ||
System.out.println(arr.get(-9)); // -1, -9 is a bad index | ||
|
||
arr.add(65); | ||
arr.add(-100); | ||
arr.add(1337); | ||
|
||
System.out.println(arr.length()); // 3 | ||
System.out.println(arr.get(0)); // 65 | ||
arr.print(); // {65, -100, 1337} | ||
|
||
System.out.println(arr.remove(1)); // true | ||
arr.print(); // {65, 1337} | ||
System.out.println(arr.length()); // 2 | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package oop.exe2; | ||
package oop.exe3; | ||
/** | ||
* | ||
* Object Oriented Programming - Bank | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,32 @@ | ||
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)); | ||
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package oop.exe3; | ||
package oop.exe4; | ||
|
||
public class Computer { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package oop.exe3; | ||
package oop.exe4; | ||
|
||
public class Game { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package oop.exe4; | ||
|
||
|
||
/** | ||
* 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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package oop.exe3; | ||
package oop.exe4; | ||
|
||
public class SimpleDate { | ||
|
||
|