X1036: Contains Two The following method takes in a List of generic parameter type T and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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: <0> 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: <0> 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 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: <0> 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: <0> 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
Expert Answer:
Answer rating: 100% (QA)
It looks like you have several screenshots of questions from a Java programming exercise and it seems youre looking for correct implementations for th... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Give Correct ANSWERS Human-Computer Interaction (a) If you had been one of the original inventors of the WIMP interface, and engineers on the technical team had been sceptical about the advantages...
-
Write an alternative definition that is tail-recursive (iterative) and makes use of accumulator variables. [10 marks] Explain why your alternative definition executes more efficiently. [3 marks] 1...
-
Transactions related to revenue and cash receipts completed by Acheville Architects Co. during the period September 2-30, 2014, are as follows: Sept. 2. Issued Invoice No. 793 to Nickle Co., $5,200....
-
What is the lifetime unified credit for 2016 and its related exclusion equivalent?
-
The following information is related to the properties owned by Mr Chan: Property W Tai Koo Shing, Hong Kong Mr Chan let Property W to Ms Novack on 1 October 2013 for a term of 4 years. The relevant...
-
Let \(V_{k s}\) be the value of an adjustable-rate loan initiated at period \(k\) and state \(s\) with initial principal of 100 . The loan is to be fully paid at period \(n\). The interest rate...
-
Three years ago American Insulation Corporation issued 10 percent, $800,000, 10-year bonds for $770,000. Debt issue costs were $3,000. American Insulation exercised its call privilege and retired the...
-
Review Apple's and Microsoft's 10-K for the fiscal year 2023. Use the information to answer the following: 1. Explain at least two reasons for the difference in ROE between the two companies. What...
-
A 2.500-g sample of a mixture of sodium hydrogen carbonate and potassium chloride is dissolved in 25.00 mL of 0.437 M H2SO4. Some acid remains after treatment of the sample. a. Write both the net...
-
Terminal cash flow-Replacement decision Russell Industries is considering replacing a fully depreciated machine that has a remaining useful life of 10 years with a newer, more sophisticated machine....
-
U.S. Steel and a few cigarette companies were all engaged in ____________to attain their economic ends.
-
Even though the fault current is not symmetrical and not strictly periodic, the rms asymmetrical fault current is computed as the rms ac fault current times an "asymmetry factor," which is a function...
-
Generally, in power-system short-circuit studies, for calculating subtransient fault currents, transformers are represented by their ____________ transmission lines by their equivalent ____________...
-
For a power system, modeled by its positive-sequence network, both busadmittance matrix and bus-impedance matrix are symmetric. (a) True (b) False
-
Game theory predicts that in a market controlled by four fi rms producing an identical service, each will ultimately charge ___________ price. a) a high b) an intermediate c) a low
-
(b) Write a friend function to a class that will return true if a number x is a factor of a number y. (6 Marks) (c) Write a program that defines a shape class with a constructor that gives value to...
-
A 20-cm-square vertical plate is heated to a temperature of 30oC and submerged in glycerin at 10oC. Calculate the heat lost from both sides of the plate.
-
A step tracer input was used on a real reactor with the following results: a. What is the mean residence time tm? b. What is the variance 2 ? For t 10 min, then C = 0 For 10 st 30 min, then C+= 10...
-
How do you think the rate of reaction, r A , will depend on species concentrations and on temperature?
-
The catalytic dehydration of methanol (ME) to form dimethyl ether (DME) and water was carried out over an ion exchange catalyst (K. Klusacek, Collection Czech. Chem. Commun., 49, 170 (1984)). The...
-
Study Figure 13.6. Write a short report on Bayers management of its cash flows over the period reported. Figure 13.6 Bayer Group's consolidated statement of cash flows Income after taxes Income taxes...
-
Expenses and revenues are subjective; cash flows are facts. Therefore cash flow statements cannot mislead. Discuss.
-
Repeat Exercise 13.3, but this time work on the assumption that non-current assets that had originally cost :30,000, with accumulated depreciation of :12,000, had been sold during the year ended 31...
Study smarter with the SolutionInn App