Skip to content

Commit

Permalink
review ch08..ch11
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisMayfield committed Apr 29, 2016
1 parent ae811e0 commit a60b1d1
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 135 deletions.
56 changes: 0 additions & 56 deletions ch08/ArrayExamples.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.util.Arrays;
import java.util.Random;

/**
* Demonstrates uses of arrays.
Expand Down Expand Up @@ -62,8 +61,6 @@ public static void main(String[] args) {
// reduce
double total = sum(a);
System.out.println("total = " + total);

makeHistogram();
}

/**
Expand Down Expand Up @@ -100,57 +97,4 @@ public static double sum(double[] a) {
return total;
}

/**
* 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;
}

/**
* Example code related to histograms.
*/
public static void makeHistogram() {
int numValues = 8;
int[] array = randomArray(numValues);
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]++;
}
}
}
35 changes: 35 additions & 0 deletions ch08/Fruit.java
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;
}

}
59 changes: 59 additions & 0 deletions ch08/Histogram.java
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]++;
}
}

}
34 changes: 34 additions & 0 deletions ch08/MakeDubMus.java
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));
}

}
22 changes: 22 additions & 0 deletions ch09/Exercise.java
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);
}

}
5 changes: 5 additions & 0 deletions ch09/Max.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
public class Max {

/**
* Converts args to integers and prints the max.
*/
public static void main(String[] args) {
System.out.println(Arrays.toString(args));

Expand All @@ -15,5 +18,7 @@ public static void main(String[] args) {
max = value;
}
}
System.out.println("The max is " + max);
}

}
34 changes: 34 additions & 0 deletions ch09/Recurse.java
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();
}

}
86 changes: 66 additions & 20 deletions ch09/StringsThings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@
*/
public class StringsThings {

/**
* Reverses a string, returns a new String.
*/
public static String reverse(String s) {
String r = "";
for (int i = s.length() - 1; i >= 0; i--) {
r = r + s.charAt(i);
}
return r;
}

public static void main(String[] args) {

// Characters

String fruit = "banana";
char letter0 = fruit.charAt(0);

if (letter0 == 'a') {
System.out.println('?');
}

System.out.print("Roman alphabet: ");
for (char c = 'A'; c <= 'Z'; c++) {
System.out.print(c);
Expand All @@ -28,6 +26,47 @@ public static void main(String[] args) {
}
System.out.println();

// Strings are immutable

String name = "Alan Turing";
String upperName = name.toUpperCase();

String text = "Computer Science is fun!";
text = text.replace("Computer Science", "CS");

// String traversal

for (int i = 0; i < fruit.length(); i++) {
char letter = fruit.charAt(i);
System.out.println(letter);
}

for (char letter : fruit.toCharArray()) {
System.out.println(letter);
}

int length = fruit.length();
char last = fruit.charAt(length - 1); // correct

System.out.println(reverse(fruit));

// Substrings

System.out.println(fruit.substring(0));
System.out.println(fruit.substring(2));
System.out.println(fruit.substring(6));

System.out.println(fruit.substring(0, 3));
System.out.println(fruit.substring(2, 5));
System.out.println(fruit.substring(6, 6));

// The indexOf method

int index = fruit.indexOf('a');
int index2 = fruit.indexOf('a', 2);

// String comparison

String name1 = "Alan Turing";
String name2 = "Ada Lovelace";
if (name1.equals(name2)) {
Expand All @@ -43,17 +82,24 @@ public static void main(String[] args) {
System.out.println("name2 comes before name1.");
}

String fruit = "banana";
// Wrapper classes

for (int i = 0; i < fruit.length(); i++) {
char letter = fruit.charAt(i);
System.out.println(letter);
}
String str = "12345";
int num = Integer.parseInt(str);

for (char letter : fruit.toCharArray()) {
System.out.println(letter);
}
num = 12345;
str = Integer.toString(num);
}

System.out.println(reverse(fruit));
/**
* Reverses a string, returns a new String.
*/
public static String reverse(String s) {
String r = "";
for (int i = s.length() - 1; i >= 0; i--) {
r = r + s.charAt(i);
}
return r;
}

}
Loading

0 comments on commit a60b1d1

Please sign in to comment.