forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDemo.java
More file actions
122 lines (107 loc) · 3 KB
/
ArrayDemo.java
File metadata and controls
122 lines (107 loc) · 3 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
public class ArrayDemo
{
public static void main (String [] args)
{
int [] numbers={1,5,9};
printArray(numbers);
System.out.println();
int [] numbers2={5,7,13};
int sum = arrayTotal(numbers2);
int sum2= arrayTotal(numbers);
printArray(numbers2);
System.out.println("The total of these numbers is: "+sum);
System.out.println();
printArray(numbers);
System.out.println("The total of these numbers is: "+sum2);
System.out.println();
int [] numbers3= {5,8,21,19,2};
int max=(arrayMax(numbers3));
printArray(numbers3);
System.out.println("The max of these numbers is: "+max);
System.out.println();
int [] numbers4= {78,23,9,34};
int max2=(arrayMax(numbers4));
printArray(numbers4);
System.out.println("The max of these numbers is: "+max2);
int maxIndex=(arrayMaxIndex(numbers4));
System.out.println(maxIndex);
int maxIndex2= arrayMaxIndex(numbers3);
System.out.println(maxIndex2);
System.out.println();
double [] numbers5= {34.2,18.0,12.5,13.1};
double avg= arrayAverage(numbers5);
System.out.println("The average of these numbers is "+avg);
System.out.println();
double [] numbers6= {10.0,15.0,20.0};
double avg2=(arrayAverage(numbers6));
System.out.println("The average of these numbers is " +avg2);
int [] keyword=new int [10];
keyword [0]=4;
keyword [3]=2;
keyword [9]=4;
printArray(keyword);
String [] keyword2= new String [10];
keyword2 [0]= "Hi";
keyword2 [3]= "Hello";
keyword2 [9]= "Bye";
printStrings(keyword2);
}
private static void printArray(int [] values)
{
for (int value:values)
{
System.out.println(value);
}
}
private static int arrayTotal(int[] values)
{
int sum=0;
for(int value: values)
{
sum=sum+value;
}
return sum;
}
private static int arrayMax(int [] values)
{
int max=0;
for(int value:values)
{
if(value>max)
max = value;
}
return max;
}
private static int arrayMaxIndex( int [] values)
{
int max=0;
int maxIndex=0;
for (int x = 0; x < values.length; x++)
{
if(values[x]>max)
{
max=values[x];
maxIndex=x;
}
}
return maxIndex;
}
private static double arrayAverage(double []values)
{
double sum=0;
double length= values.length;
for (double value:values)
{
sum= sum+value;
}
double avg= sum/length;
return avg;
}
private static void printStrings (String [] keywords)
{
for (String stringTest:keywords)
{
System.out.println(stringTest);
}
}
}