q3: Write a public static method named q3 that takes an Arraylist of type String
ID: 3706849 • Letter: Q
Question
q3: Write a public static method named q3 that takes an Arraylist of type String and * returns an int. This method returns the number of times* a most significant value appears *in the data where significant is defined as the String that comes first *alphabetically/lexicographically (case sensitive ordering) from the values of the input (hint: Use the compareTo method in the String class) q4: Write a public static method named q4 that takes a HashMap of type String to Character and returns a String. This method returns the "key mapping to* the char that comes last * alphabetically/lexicographically (case sensitive ordering) from the values of the input * (hint: In java, chars are represented as ints that preserve this ordering. You can use) kx *q5: Write a public static method named q5 that takes an Arraylist of Doubles as a parameter and returns an int. This method returns the index of the first value in the input that is in the range -5.55, -2.13) and returns -1 if the input contains no values *in this rangeExplanation / Answer
Below is your code: -
public class Questions {
public static int q3(ArrayList<String> strings) {
String mostSignificant = strings.get(0);
int count = 0;
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).compareTo(mostSignificant) < 0) {
mostSignificant = strings.get(i);
count = 1;
} else if (strings.get(i).compareTo(mostSignificant) == 0) {
count++;
}
}
return count;
}
public static String q4(HashMap<String, Character> mp) {
Iterator<Entry<String, Character>> it = mp.entrySet().iterator();
if (it.hasNext()) {
Map.Entry<String, Character> pair = (Map.Entry<String, Character>) it.next();
String lowes = pair.getKey();
Character highChar = pair.getValue();
while (it.hasNext()) {
pair = (Map.Entry<String, Character>) it.next();
if (pair.getValue() > highChar) {
highChar = pair.getValue();
lowes = pair.getKey();
}
}
return lowes;
} else {
return null;
}
}
public static int q5(ArrayList<Double> doubles) {
for (int i = 0; i < doubles.size(); i++) {
if ((doubles.get(i) > -5.55) && (doubles.get(i) < -2.13)) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
ArrayList<String> strings = new ArrayList<>();
strings.add("B");
strings.add("B");
strings.add("B");
strings.add("C");
strings.add("D");
strings.add("A");
strings.add("A");
System.out.println("q3 output: "+q3(strings));
HashMap<String, Character> map = new HashMap<>();
map.put("B", 'B');
map.put("B", 'B');
map.put("C", 'C');
map.put("A1", 'A');
map.put("D", 'D');
System.out.println("q4 output: "+q4(map));
ArrayList<Double> doubles = new ArrayList<>();
doubles.add(-9.2);
doubles.add(9.2);
doubles.add(-5.2);
doubles.add(-6.2);
doubles.add(-4.2);
doubles.add(-1.2);
System.out.println("q5 output: "+q5(doubles));
}
}
Output
q3 output: 2
q4 output: D
q5 output: 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.