Skip to content

Commit 7d282c8

Browse files
committed
Chapter 8 exercises
1 parent e9bcbae commit 7d282c8

6 files changed

Lines changed: 177 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Exercise 8-2
2+
3+
4+
1) The method bananna is traversing through an array, and accumulating product of all of the elements. Kiwi is that accumulator and the variable i is the index of each element.
5+
2) The method grapefruit is traversing through an array, and is search for any index which matches the parameter int grape, if it finds one that matches, it returns the index, if not it returns -1.
6+
The variable a is the array that is being searched, and int grape is the integer it is searching for.
7+
3) The method pineapple is traversing through an array a, with an int parameter apple. Pear is the counter. If the value in the current index matches the value apple, pear is incremented. At the end it returns how many matching values it found.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Arrays;
2+
import java.util.Random;
3+
4+
public class Exercise8_1 {
5+
public static void main(String[] args) {
6+
7+
//Printing a string representation of an array
8+
//System.out.print(Array.toString(a));
9+
// System.out.println(Arrays.toString(powArray()));
10+
double[] a = {1,2,3,4};
11+
System.out.println(Arrays.toString(powArray(a, 3)));
12+
13+
//creating a random array and storing it
14+
int[] scoreArray = randomArray(100);
15+
System.out.println("Printing out the number of scores for each possible number:");
16+
System.out.println(Arrays.toString(histogram(scoreArray)));
17+
18+
}
19+
20+
// for(int i = 0; i < array.legth; i++) {
21+
// a[i] = Math.pow(a[i], 2.0);
22+
23+
public static double[] powArray(double[] x, int y){
24+
25+
double[] b = new double[x.length];
26+
27+
for(int i = 0; i < x.length; i++) {
28+
b[i] = Math.pow(x[i], y);
29+
}
30+
return b;
31+
}
32+
33+
//Creating a random array of 100 scores
34+
35+
public static int[] randomArray(int size) {
36+
Random random = new Random();
37+
int[] a = new int[size];
38+
for(int i = 0; i < a.length; i++) {
39+
a[i] = random.nextInt(100);
40+
}
41+
return a;
42+
}
43+
44+
// //Enhanced For Loop Exercise
45+
// int[] counts = new int[100];
46+
// for(int score: scores);
47+
// counts[score]++;
48+
//
49+
public static int[] histogram(int[]array) {
50+
// create a counter for each of the 100 possible scores
51+
int[] counts = new int[100];
52+
for(int score : array) {
53+
counts[score]++;
54+
}
55+
return counts;
56+
}
57+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.Random;
2+
import java.util.Arrays;
3+
4+
public class Exercise8_4 {
5+
public static void main(String[] args) {
6+
int[] arr = randomArray(10);
7+
System.out.print("The array we are using is: ");
8+
System.out.println(Arrays.toString(arr));
9+
System.out.print("The index of the highest element is: ");
10+
System.out.println(indexOfMax(arr));
11+
}
12+
13+
//Create Random Array
14+
public static int[] randomArray(int size){
15+
Random random = new Random();
16+
int array[] = new int[size];
17+
for(int i = 0; i < array.length; i++) {
18+
array[i] = random.nextInt(100);
19+
}
20+
return array;
21+
}
22+
23+
24+
//Search Array for highest element at index
25+
public static int indexOfMax(int[] arr) {
26+
int temp = arr[0];
27+
int tempIndex = 0;
28+
//Find the highest element in the array
29+
//Assign the index to tempIndex
30+
for(int i = 0; i < arr.length; i++) {
31+
if(arr[i] > temp) {
32+
temp = arr[i];
33+
tempIndex = i;
34+
}
35+
}
36+
return tempIndex;
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Have not finished this exercise
2+
import java.util.Arrays;
3+
4+
public class Exercise8_5 {
5+
public static void main(String[] args) {
6+
//int[arr] = {1, 2, 3, 7, 8};
7+
System.out.println(sieve(5));
8+
}
9+
public static boolean[] sieve(int n) {
10+
int[] arr;
11+
//Building the array up to n length;
12+
for(int i =0; i < n; i++) {
13+
arr[i] = i;
14+
}
15+
//Searching for prime numbers
16+
for(int j = 0; j < arr.lenght; j++) {
17+
if(arr[j] % arr[j] == 1 &&
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Exercise8_6 {
2+
public static void main(String[] args) {
3+
int[] arr = {3, 6, 9};
4+
System.out.println(areaFactors(4, arr));
5+
}
6+
7+
public static boolean areaFactors(int n, int[] array) {
8+
boolean flag = true;
9+
for(int i = 0; i < array.length; i++){
10+
if(array[i] % n != 0) {
11+
flag = false;
12+
}
13+
}
14+
return flag;
15+
}
16+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Exercise8_7 {
2+
//Find out if the numbers in the array are prime
3+
public static boolean isPrime(int[] arr) {
4+
//looping through the numbers in the array
5+
boolean isPrimeNumber = true;
6+
for(int i =0; i < arr.length; i++) {
7+
//Testing to see if the numbers are prime
8+
for(int j = 2; j < i; j++) {
9+
if(arr[i] % j == 0) {
10+
isPrimeNumber = false;
11+
break;
12+
}
13+
}
14+
}
15+
return isPrimeNumber;
16+
}
17+
// Find the product of the numbers in an array
18+
public static int product(int[] arr) {
19+
int product = 1;
20+
for(int i = 0; i < arr.length; i++) {
21+
product *= arr[i];
22+
}
23+
//System.out.println(product);
24+
return product;
25+
}
26+
//Check to see if the product of the array matches n and the array is composed of primes numbers
27+
public static boolean arePrimeFactors(int n, int[] arr) {
28+
boolean flag = false;
29+
if(product(arr) == n && isPrime(arr) == true) {
30+
flag = true;
31+
}
32+
return flag;
33+
}
34+
public static void main(String[] args) {
35+
int[] arr = {2, 5, 7, 11};
36+
System.out.println(arePrimeFactors(770, arr));
37+
//product(arr);
38+
//System.out.println(isPrime(arr));
39+
}
40+
}

0 commit comments

Comments
 (0)