subject

java programming

9.9 ch 9, part 2: arraylist searching

import java. util. arraylist;

public class arraylistset {

/**
* searches through the arraylist arr, from the first index to the last, returning an arraylist
* containing all the indexes of strings in arr that match string s. for this method, two strings,
* p and q, match when p. equals(q) returns true or if both of the compared references are null.
*
* @param s the string to search for.
* @param arr the arraylist of string to search.
* @return an arraylist of the indexes in arr that match s. the arraylist will be ordered from
* the smallest index to the largest. if arr is null, the method will return null.
*/
public static arraylist findexact(string s, arraylist arr) {
return null;
}

/**
* searches through the arraylist arr, from the first index to the last, returning an arraylist
* containing all the indexes of strings in arr that contains string s. for this method, a
* strings p contains q when p. contains(q) returns true or if both of the compared references are null.
*
* @param s the string to search for.
* @param arr the arraylist of string to search.
* @return an arraylist of the indexes in arr that contains s. the arraylist will be ordered from
* the smallest index to the largest. if arr is null, the method will return null.
*/
public static arraylist findcontains(string s, arraylist arr) {
return null;
}

/**
* adds string s to the end of the arraylist arr if and only if arr does not already have an
* element that matches s.
*
* do not duplicate code! use your findexact method to check if arr contains s.
*
* @param s the string to add (if not already in arr).
* @param arr the arraylist in which to s (if a string with the same contents is not already in
* arr).
* @return the number of times that s is found in arr after attempting to adding s. if arr is null,
* return -1.
*/
public static int adduniquestring(string s, arraylist arr) {
return -42;
}

}

import java. util. arraylist;
import java. util. arrays;

public class testarraylistset {

public static boolean testfindexact() {
system. out. println("starting ");
//test 1
{
arraylist ret =
arraylistset. findexact("foo", new arraylist(arrays. aslist("foo","bar","foo";
arraylist exp = new arraylist(arrays. aslist(0, 2));
if(! ret. equals(exp)) {
system. out. println("test 1 failed! ");
system. out. println("returned: " + ret);
system. out. println("expected: " + exp);
return false;
}
}
system. out. println(" testfindexact");
return true;
}

public static boolean testfindcontains() {
system. out. println("starting ");
//test 1
{
arraylist ret =
arraylistset. findcontains("o", new arraylist(arrays. aslist("foo","bar","foo";
arraylist exp = new arraylist(arrays. aslist(0, 2));
if(! ret. equals(exp)) {
system. out. println("test 1 failed! ");
system. out. println("returned: " + ret);
system. out. println("expected: " + exp);
return false;
}
}
system. out. println(" testfindcontains");
return true;
}

public static boolean testadduniquestring() {
system. out. println("starting ");
//test 1
{
arraylist arr = new arraylist(arrays. aslist("a","b","c"));
arraylist exp = new arraylist(arr);
exp. add("d");
int retindex = arraylistset. adduniquestring("d", arr);
int expindex = 1;
if(! arr. equals(exp) || retindex ! = expindex) {
system. out. println("test 1 failed! ");
system. out. println("returned value: " + retindex + "; expected " + expindex);
system. out. println("list after: " + arr);
system. out. println("expected: " + exp);
return false;
}
}
//test 2
{
arraylist arr = new arraylist(arrays. aslist("a","b","b","c"));
arraylist exp = new arraylist(arr);
int retindex = arraylistset. adduniquestring("b", arr);
int expindex = 2;
if(! arr. equals(exp) || retindex ! = expindex) {
system. out. println("test 2 failed! ");
system. out. println("returned value: " + retindex + "; expected " + expindex);
system. out. println("list after: " + arr);
system. out. println("expected: " + exp);
return false;
}
}
system. out. println(" testadduniquestring");
return true;
}

public static void main(string[] args) {
testfindexact();
testfindcontains();
testadduniquestring();
}

}

ansver
Answers: 2

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 11:40
Design a pos circuit that displays the letters a through j on a seven-segment indicator. the circuit has four inputs w, x, y, and z which represent the last 4 bits of the uppercase ascii code for the letter to be displayed. thus, if wxyz = 0001 then "a" will be displayed. (any answer with 22 or fewer gates and inverters, not counting any for the inputs, is acceptable)
Answers: 2
question
Computers and Technology, 22.06.2019 15:10
Which activity should be part of a long-term plan to positively affect yourhealth? oa. wearing regular clothing when handling toxinsob. not worrying about secondhand smokeoc. avoiding excessive exposure to sunlightod. drinking only well water
Answers: 1
question
Computers and Technology, 23.06.2019 15:00
Barbara is interested in pursuing a career in the science and math pathway. which qualifications will her reach that goal? a.an advanced knowledge of physics and math b.an advanced knowledge of engineering and math c. an advanced knowledge of physics and robotics an d. advanced knowledge of machinery and math
Answers: 2
question
Computers and Technology, 24.06.2019 01:30
Hazel has just finished adding pictures to her holiday newsletter. she decides to crop an image. what is cropping an image?
Answers: 1
You know the right answer?
java programming

9.9 ch 9, part 2: arraylist searching

import java. util. a...
Questions
question
Chemistry, 29.08.2019 23:30
question
Social Studies, 29.08.2019 23:30
question
English, 29.08.2019 23:30
question
Computers and Technology, 29.08.2019 23:30
Questions on the website: 13722359