-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSc2010_FunArrays.java
More file actions
159 lines (118 loc) · 5.13 KB
/
CSc2010_FunArrays.java
File metadata and controls
159 lines (118 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//Stephanie Wyckoff
//The program functions as expected. The program requests the user to define length of the array, provide integers for the array and checks if the
//array contains the numbers 7, 13 or 18. If yes, the program is determined to be lucky. The isLucky() method returns true, prints for the user that the
//array is lucky and calls the sum() method which computes the sum of all of the integers in the array. If no, the program is determined to be not lucky.
//The isLucky() method then returns false, prints for the user that the array is not lucky and calls the sumOfEvens() method which computes the sum of all
//of the even integers in the array. Next the user is prompted to enter another array to compute dot product. If the second array is the same size as the
//first, the program proceeds with getting the integers from the user. Otherwise it notes that the arrays must be of equal length and loops until equal
//length is given. Both arrays are printed for the user and the program calls dotProduct() and returns the dot product of the first and second arrays.
//Finally the user is prompted to choose whether or not to start over and and enter another array. The program loops until the user enters 2 which ends the
//program.
import java.util.Arrays;
import java.util.Scanner;
public class FunArrays {
public static boolean isLucky(int[] lucky){ //checks if array is lucky (contains 7, or 13, or 18)
boolean isTrue = false;
for(int i = 0; i < lucky.length; i++){
int charms = lucky[i];
if (charms == 7 || charms == 13 || charms == 18){
isTrue = true; //returns true
break;
}
}
return isTrue; //returns false
}
public static int sum(int[] num){ //if array is lucky, continues to here per instructions
int sum = 0;
for(int i = 0; i < num.length; i++){
sum = sum + num[i];
}
return sum;
}
public static int sumOfEvens(int[] num){ //if array is not lucky, continues to here per instructions
int even = 0;
for(int i = 0; i < num.length; i++){
if(num[i] % 2 == 0){
even = even + num[i];
}
}
return even;
}
public static int dotProduct(int[] first, int[] second){ //computes dot product ((a*b)+(a1*b1)....)
int sum = 0;
for(int i = 0; i < first.length; i++){
int product = first[i] * second[i];
sum = sum + product;
}
return sum; //returns dot product
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
boolean loop = true;
while(loop){ //loops program
System.out.println("How many integers are in your array?");
int num = input.nextInt();
int[] numbers = new int[num];
for (int i = 0; i < numbers.length; i++){
System.out.println("Please enter term " + (i+1) + ":");
numbers[i] = input.nextInt();
}
System.out.println();
System.out.println("Your array is " + Arrays.toString(numbers));
System.out.println();
if(isLucky(numbers) == true){
System.out.println("Your array is lucky!");
System.out.println();
System.out.println("The sum of all integers in your array is " + sum(numbers) + "."); //per the instructions
System.out.println();
}else{
System.out.println("Your array is not lucky.");
System.out.println();
System.out.println("The sum of all even integers in your array is " + sumOfEvens(numbers) + "."); //per the instructions
System.out.println();
}
System.out.println();
System.out.println("To compute dot product, you will need to enter a second array. How many integers are in your second array?");
//requests second array to compute dot product
int num2 = input.nextInt();
int[] numbers2 = new int[num2];
while(numbers2.length != numbers.length){ //checks second array length with first, if not equal cannot compute dot product
System.out.println("To compute dot product, the arrays must be of equal length.");
System.out.println();
System.out.println();
System.out.println("How many integers are in your second array?");
num2 = 0;
num2 = input.nextInt();
numbers2 = new int[num2];
if(numbers2.length == numbers.length){//loop continues until array lengths are equal
break;
}
}
for (int k = 0; k < numbers2.length; k++){
System.out.println("Please enter term " + (k+1) + ":");
numbers2[k] = input.nextInt();
}
System.out.println();
System.out.println("Your first array is " + Arrays.toString(numbers));
System.out.println("Your second array is " + Arrays.toString(numbers2));
System.out.println();
System.out.println("The dot product of your arrays is " + dotProduct(numbers, numbers2) + ".");
System.out.println();
System.out.println();
System.out.println("Would you like to try again?");
System.out.println("1 - Yes");
System.out.println("2 - No");
int answer = input.nextInt();
if(answer == 2){//loops program until user enters 2
loop = false;
break;
}
else if(answer == 1){
loop = true;
}
}
if(input != null){
input.close();
}
}
}