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 10, 2018
1 parent 1d4e498 commit 09811fa
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 78 deletions.
56 changes: 56 additions & 0 deletions src/oop/exe0/Fraction.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions src/oop/exe0/Mainer.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
71 changes: 71 additions & 0 deletions src/oop/exe1/Fraction.java
Original file line number Diff line number Diff line change
@@ -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
}


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

This file was deleted.

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

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

public class Computer {

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

import java.util.Date;
package oop.exe3;

public class Game {

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

public class SimpleDate {

Expand Down

0 comments on commit 09811fa

Please sign in to comment.