Skip to content

Commit

Permalink
Added more OOP exercises and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
shaqed committed Oct 11, 2018
1 parent 09811fa commit 5f69956
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 76 deletions.
58 changes: 58 additions & 0 deletions src/oop/exe2/DynamicArray.java
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() {

}
}
32 changes: 0 additions & 32 deletions src/oop/exe2/Main.java

This file was deleted.

41 changes: 41 additions & 0 deletions src/oop/exe2/Mainer.java
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


}
}
2 changes: 1 addition & 1 deletion src/oop/exe2/Bank.java → src/oop/exe3/Bank.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oop.exe2;
package oop.exe3;
/**
*
* Object Oriented Programming - Bank
Expand Down
58 changes: 18 additions & 40 deletions src/oop/exe3/Main.java
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();
}

}
2 changes: 1 addition & 1 deletion src/oop/exe3/Computer.java → src/oop/exe4/Computer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oop.exe3;
package oop.exe4;

public class Computer {

Expand Down
2 changes: 1 addition & 1 deletion src/oop/exe3/Game.java → src/oop/exe4/Game.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oop.exe3;
package oop.exe4;

public class Game {

Expand Down
54 changes: 54 additions & 0 deletions src/oop/exe4/Main.java
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());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package oop.exe3;
package oop.exe4;

public class SimpleDate {

Expand Down

0 comments on commit 5f69956

Please sign in to comment.