-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnames.java
More file actions
60 lines (57 loc) · 2.39 KB
/
names.java
File metadata and controls
60 lines (57 loc) · 2.39 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
package com.promineotech;
public class names {
public static void main(String[] args) {
/*Create an array of String called names that contains the following values: “Sam”, “Tommy”, “Tim”, “Sally”, “Buck”, “Bob” */
String[] names = {"Sam", "Tommy", "Tim", "Sally", "Buck", "Bob"};
averageLetters(names);
contcatNames(names);
countNameLength(names);
}
/*Use a loop to iterate through the array and calculate the average number of letters per name. Print the result to the console*/
public static void averageLetters(String [] arr) {
// creating variable to count characters in array
int count = 0;
// creating loop to loop through each individual name
for (int i = 0; i < arr.length; i++) {
// creating loop to go through each individual character of individual name
for (int j = 0; j < arr[i].length(); j++) {
count += 1;
}
}
int result = count/arr.length;
System.out.println(result);
}
/* Use a loop to iterate through the array again and concatenate all the names together, separated by spaces, and print the result to the console.*/
public static void contcatNames(String [] arr) {
// create an empty string to hold concatenated names
String sentence = "";
// create space to add between names
String space = " ";
// loop through array to add names and space to string
for (int i = 0; i < arr.length; i++) {
sentence += arr[i] + space;
}
System.out.println(sentence);
}
/*Create a new array of int called nameLengths. Write a loop to iterate over the previously created names array and add the length of each name to the nameLengths array.*/
public static void countNameLength(String [] arr) {
//creating new array that will hold the same amount of elements as the names array
int[] nameLengths = new int[arr.length];
// creating loop to loop through each individual name
for (int i = 0; i < arr.length; i++) {
// creating variable to count characters in array
int count = 0;
// creating loop to go through each individual character of individual name
for (int j = 0; j < arr[i].length(); j++) {
count += 1;
}
nameLengths[i] = count;
}
/*Write a loop to iterate over the nameLengths array and calculate the sum of all the elements in the array. Print the result to the console. */
int sum = 0;
for (int k = 0; k < nameLengths.length; k++) {
sum += nameLengths[k];
}
System.out.println(sum);
}
}