-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae811e0
commit a60b1d1
Showing
14 changed files
with
370 additions
and
135 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
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,35 @@ | ||
/** | ||
* Fruit exercise. | ||
*/ | ||
public class Fruit { | ||
|
||
public static int banana(int[] a) { | ||
int kiwi = 1; | ||
int i = 0; | ||
while (i < a.length) { | ||
kiwi = kiwi * a[i]; | ||
i++; | ||
} | ||
return kiwi; | ||
} | ||
|
||
public static int grapefruit(int[] a, int grape) { | ||
for (int i = 0; i < a.length; i++) { | ||
if (a[i] == grape) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
public static int pineapple(int[] a, int apple) { | ||
int pear = 0; | ||
for (int pine: a) { | ||
if (pine == apple) { | ||
pear++; | ||
} | ||
} | ||
return pear; | ||
} | ||
|
||
} |
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,59 @@ | ||
import java.util.Random; | ||
|
||
/** | ||
* Example code related to histograms. | ||
*/ | ||
public class Histogram { | ||
|
||
/** | ||
* Returns an array of random integers. | ||
*/ | ||
public static int[] randomArray(int size) { | ||
Random random = new Random(); | ||
int[] a = new int[size]; | ||
for (int i = 0; i < a.length; i++) { | ||
a[i] = random.nextInt(100); | ||
} | ||
return a; | ||
} | ||
|
||
/** | ||
* Computes the number of array elements in [low, high). | ||
*/ | ||
public static int inRange(int[] a, int low, int high) { | ||
int count = 0; | ||
for (int i = 0; i < a.length; i++) { | ||
if (a[i] >= low && a[i] < high) { | ||
count++; | ||
} | ||
} | ||
return count; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int numValues = 8; | ||
int[] array = randomArray(numValues); | ||
ArrayExamples.printArray(array); | ||
|
||
int[] scores = randomArray(30); | ||
int a = inRange(scores, 90, 100); | ||
int b = inRange(scores, 80, 90); | ||
int c = inRange(scores, 70, 80); | ||
int d = inRange(scores, 60, 70); | ||
int f = inRange(scores, 0, 60); | ||
|
||
// making a histogram | ||
int[] counts = new int[100]; | ||
for (int i = 0; i < scores.length; i++) { | ||
int index = scores[i]; | ||
counts[index]++; | ||
} | ||
|
||
// histogram with enhanced for loop | ||
counts = new int[100]; | ||
for (int score : scores) { | ||
counts[score]++; | ||
} | ||
} | ||
|
||
} |
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,34 @@ | ||
/** | ||
* Stack diagram exercise. | ||
*/ | ||
public class MakeDubMus { | ||
|
||
public static int[] make(int n) { | ||
int[] a = new int[n]; | ||
for (int i = 0; i < n; i++) { | ||
a[i] = i + 1; | ||
} | ||
return a; | ||
} | ||
|
||
public static void dub(int[] jub) { | ||
for (int i = 0; i < jub.length; i++) { | ||
jub[i] *= 2; | ||
} | ||
} | ||
|
||
public static int mus(int[] zoo) { | ||
int fus = 0; | ||
for (int i = 0; i < zoo.length; i++) { | ||
fus += zoo[i]; | ||
} | ||
return fus; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int[] bob = make(5); | ||
dub(bob); | ||
System.out.println(mus(bob)); | ||
} | ||
|
||
} |
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,22 @@ | ||
/** | ||
* Exercise on encapsulation and generalization. | ||
*/ | ||
public class Exercise { | ||
|
||
public static void main(String[] args) { | ||
String s = "((3 + 7) * 2)"; | ||
int count = 0; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
char c = s.charAt(i); | ||
if (c == '(') { | ||
count++; | ||
} else if (c == ')') { | ||
count--; | ||
} | ||
} | ||
|
||
System.out.println(count); | ||
} | ||
|
||
} |
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
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,34 @@ | ||
/** | ||
* Recursion exercise. | ||
*/ | ||
public class Recurse { | ||
|
||
/** | ||
* Returns the first character of the given String. | ||
*/ | ||
public static char first(String s) { | ||
return s.charAt(0); | ||
} | ||
|
||
/** | ||
* Returns all but the first letter of the given String. | ||
*/ | ||
public static String rest(String s) { | ||
return s.substring(1); | ||
} | ||
|
||
/** | ||
* Returns all but the first and last letter of the String. | ||
*/ | ||
public static String middle(String s) { | ||
return s.substring(1, s.length() - 1); | ||
} | ||
|
||
/** | ||
* Returns the length of the given String. | ||
*/ | ||
public static int length(String s) { | ||
return s.length(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.