Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
!*.java

72 changes: 72 additions & 0 deletions recursion/src/Recursion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import fill_bag.Bag;
import fill_bag.Thing;
import pow.NegativePowException;
import pow.Pow;

public class Recursion {
public static void main(String[] args) {
// Test pow()
System.out.println("------ Test recursion pow ------");
System.out.println();

int x = 3;
int n1 = -3, n2 = 0, n3 = 3;

try {
System.out.println(String.format("pow(%d, %d) = %d", x, n1, Pow.pow(x, n1)));
} catch (NegativePowException e) {
System.out.println(e.getMessage());
}

try {
System.out.println(String.format("pow(%d, %d) = %d", x, n2, Pow.pow(x, n2)));
} catch (NegativePowException e) {
System.out.println(e.getMessage());
}

try {
System.out.println(String.format("pow(%d, %d) = %d", x, n3, Pow.pow(x, n3)));
} catch (NegativePowException e) {
System.out.println(e.getMessage());
}

System.out.println();

// Test placement()
System.out.println("------ Test recursion pow ------");
System.out.println();

Bag bag = new Bag(14);

Thing[] things = new Thing[]{
new Thing(5, 3),
new Thing(10, 5),
new Thing(6, 4),
new Thing(5, 2)
};

bag.fill(things);
System.out.println(bag);


Bag bag1 = new Bag(3);

bag1.fill(things);
System.out.println(bag1);

Bag bag2 = new Bag(5);

bag2.fill(things);
System.out.println(bag2);

Bag bag3 = new Bag(10);

bag3.fill(things);
System.out.println(bag3);

}
}




43 changes: 43 additions & 0 deletions recursion/src/fill_bag/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package fill_bag;

public class Bag {
public int capacity;

public int cost;
public int weight;
public int numThings;

private int sumWeight;
private int sumCost;
private int count;

public Bag(int _cap) {
capacity = _cap;
}

public void fill(Thing[] things) {
combination0(things, 0, things.length, 0, 0, 0);
}

private void combination0(Thing[] arr, int start, int end, int count, int sumWeight, int sumCost) {
if (capacity >= sumWeight) {
if (sumCost > cost) {
cost = sumCost;
weight = sumWeight;
numThings = count;
}
}

if (start == end) {
return;
}

for (int i = start; i < end; i++) {
combination0(arr, i + 1, end, count+1, sumWeight + arr[i].weight, sumCost + arr[i].cost);
}
}

public String toString() {
return "maxCost = " + cost + ", maxWeight = " + weight + ", things = " + numThings;
}
}
15 changes: 15 additions & 0 deletions recursion/src/fill_bag/Thing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fill_bag;

public class Thing {
public int weight = 0;
public int cost = 0;

public Thing(int _weight, int _cost) {
weight = _weight;
cost = _cost;
}

public String toString() {
return "weight: " + weight + ", cost: " + cost;
}
}
7 changes: 7 additions & 0 deletions recursion/src/pow/NegativePowException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pow;

public class NegativePowException extends Exception {
NegativePowException(String msg) {
super(msg);
}
}
18 changes: 18 additions & 0 deletions recursion/src/pow/Pow.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pow;

public class Pow {
public static long pow(int x, int n) throws NegativePowException {
if (n < 0) {
throw new NegativePowException("ERROR: Negative power");
}

if (n == 0) {
return 1;
}
else {
return pow(x, n - 1) * x;
}
}
}