Answered step by step
Verified Expert Solution
Question
1 Approved Answer
X1036: Contains Two The following method takes in a List of generic parameter type T and a value of the same type. This method
X1036: Contains Two The following method takes in a List of generic parameter type T and a value of the same type. This method should return true if there are at least two occurrences of value in the list. For example if list is: [1,2, 3, 4, 5, 5] and value is 5, this method should return true. Your Answer: Feedback 1 public boolean contains 2 (List list, T value) 2|{ 3 int count=0; 4 for(T a:list) 5 { 6 if(a==value) 7|{ 8 count++; 9} 10} 11 if(count== 2) return true; 12 return false; 13 } 14 Check my answer! Reset Next exercise 0.67/ 1.0 Result Behavior two occurences in a longer List of Integers two occurrences in a list of Strings Expected: but was: two occurrences in a list of custom objects Expected: but was: less than two occurrences in a list of Integers less than two occurrences in a list of Strings less than two occurrences in a list of custom objects X1037: Debug Null Pointer Exception The following method takes in a list of generic objects and returns the number of items in the list. As written, however, the method will produce a null pointer exception if null is passed in as the parameter value. Add an if statement to the method that will check for a null parameter value and, if so, return -1 instead of producing a null pointer exception. Your Answer: 1 public int findSize(List list) 2|{ 3 4 if(list==null) return 0; 5 return list.size(); 6} Check my answer! Reset Next exercise Feedback 0.5/ 1.0. Result Behavior Ensures method returns size when param is a List Ensures method returns -1 when param is a null, instead of a List Expected: but was: Ensures method returns size when param is a List Ensures method returns -1 when param is a null, instead of a List Expected: but was: Ensures method returns size 1 public class Person 2 { 3 4 5 6 7 8 9 10 11 12 13 14) 3 8 9 private String name; public Person (String na) { 10 11 12 } The following method takes in a list of Person objects and should return the number of objects who's name starts with 'J. However, some of the values in this list may be null. 13 14} this.name = na; public String getName () { } Write a loop to iterate over each person to count if the first letter of the name (accessed by calling getName ( ) ) starts with the character 'J! But add in a check to make sure the object is not null, so that the method does not produce null pointer exceptions. Your Answer: return name; public int countNames (List list) { int count = 0; for (Person p: list) { if(p != null) { if (p.getName().trim().toUpperCase (Locale.ROOT).start sWith("3")) { } count++; return count; Feedback 0.0/ 1.0 Your answer could not be processed because it contains errors: Line 7: error: cannot find symbol: variable Locale X1039: Find Last Negative Box The following question makes use of the generic Box class discussed in the reading assignment. 1 public static class Box { private T value; public Box(T val) { 4 8 9 10 11 12 13 14} } 4 value = val; public T getValue() { return value; } The method below takes in a list of Box objects, each containing an Integer value, and should return the index of the last box that contains a negative number. You can use a numeric for Loop to iterate over the list in reverse order. The method should return-1 if the value is not found anywhere in the list. For example, consider this list. List arr = new List (); arr.add(new Box (1)); // Box at index 0, contains 1 arr.add(new Box (-2)); arr.add(new Box (-3)); arr.add(new Box (4)); // Box at index 1, contains -2 // Box at index 2, contains -3 // Box at index 3, contains 4 A call to lastNegative Box() on this list should return 2. Your Answer: 1 public int lastNegative Box (List list) { Feedback 0.0/ 1.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
It looks like you have several screenshots of questions from a Java programming exercise and it seems youre looking for correct implementations for th...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started