Question

[Java] I'm not sure how to implement the code. Please check my code at the bottom....

[Java] I'm not sure how to implement the code. Please check my code at the bottom.

In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester.

public static int min(int[] array) gets the minimum value in the array

public static boolean contains(String[] array, String letter)determines if the array contains a word that starts with the given letter. Returns true if it does, otherwise returns false. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter

public static ArrayList<String> contains(ArrayList<String> list, char letter)gets an ArrayList of all the strings in the given ArrayList that contain with the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter

Use the following file:

UtilTester.java

import java.util.ArrayList;

public class UtilTester
{

   public static void main(String[] args)
   {
      //testing min method
      int[] numbers = {5, 8, 4, 6, 2, 1, 7, 3};
      System.out.println(Util.min(numbers));
      System.out.println("Expected: 1");

      int[] numbers2 = {2, 9, 4, 6, 5, 8, 7, 3};
      System.out.println(Util.min(numbers2));
      System.out.println("Expected: 2");

      int[] numbers3 = {10, 9, 4, 6, 5, 8, 7, 3};
      System.out.println(Util.min(numbers3));
      System.out.println("Expected: 3");

      //testing first contains
      String[] javaIdentifiers = {"Integer", "Double", "Float", "Char",
            "boolean", "long", "short", "byte"};
      System.out.println(Util.contains(javaIdentifiers,"c"));
      System.out.println("Expected: true");
      System.out.println(Util.contains(javaIdentifiers, "x"));
      System.out.println("Expected: false");
      System.out.println(Util.contains(javaIdentifiers,"B"));
      System.out.println("Expected: true");
      
      //testing second contains
      ArrayList<String> words = new ArrayList<String>();
      System.out.println(Util.contains(words, 'b'));
      System.out.println("Expected: []");
      words.add("Big");
      words.add("Java");
      words.add("is");
      words.add("best");
      words.add("Be");
      words.add("the");
      words.add("computer");
      words.add("CS46A/B");
      System.out.println(Util.contains(words, 'e'));
      System.out.println("Expected: [best, Be, the, computer]");
      System.out.println(Util.contains(words, 'B'));
      System.out.println("Expected: [Big, best, Be, CS46A/B]");
      System.out.println(Util.contains(words, 'a'));
      System.out.println("Expected: [Java, CS46A/B]");
      System.out.println(Util.contains(words, 'k'));
      System.out.println("Expected: []");

   }

}

[Here's my code]

import java.lang.reflect.Array;
import java.util.ArrayList;

public class Util
{
   public static int min(int[] array)
   {
       int min = array[0];
       for(int i = 0; i < array.length; i++)
       {
           if(min > array[i])
           {
               min = array[i];
           }
       }
       return min;
   }
   public static boolean contains(String[] array, String letter)
   {
       boolean returnValue = false;
       for(int i = 0; i < array.length; i++)
       {
           if(array[i].contains(letter.toLowerCase()))
           {
               returnValue = true;
           }
           else
           {
               returnValue = false;
           }
       }
       return returnValue;
   }
   public static ArrayList<String> contains(ArrayList<String> list, char letter)
   {
       for(int i = 0; i < list.size(); i++)
       {
           if(list.get(i).toString().substring(i, i + 1).equals(letter))
           {
               return ;
           }
       }
   }
  
}

Homework Answers

Answer #1

package as3;

import java.util.ArrayList;

public class UtilTester
{

public static void main(String[] args)
{
//testing min method
int[] numbers = {5, 8, 4, 6, 2, 1, 7, 3};
System.out.println(Util.min(numbers));
System.out.println("Expected: 1");

int[] numbers2 = {2, 9, 4, 6, 5, 8, 7, 3};
System.out.println(Util.min(numbers2));
System.out.println("Expected: 2");

int[] numbers3 = {10, 9, 4, 6, 5, 8, 7, 3};
System.out.println(Util.min(numbers3));
System.out.println("Expected: 3");

//testing first contains
String[] javaIdentifiers = {"Integer", "Double", "Float", "Char",
"boolean", "long", "short", "byte"};
System.out.println(Util.contains(javaIdentifiers,"c"));
System.out.println("Expected: true");
System.out.println(Util.contains(javaIdentifiers, "x"));
System.out.println("Expected: false");
System.out.println(Util.contains(javaIdentifiers,"B"));
System.out.println("Expected: true");
  
//testing second contains
ArrayList<String> words = new ArrayList<String>();
System.out.println(Util.contains(words, 'b'));
System.out.println("Expected: []");
words.add("Big");
words.add("Java");
words.add("is");
words.add("best");
words.add("Be");
words.add("the");
words.add("computer");
words.add("CS46A/B");
System.out.println(Util.contains(words, 'e'));
System.out.println("Expected: [best, Be, the, computer]");
System.out.println(Util.contains(words, 'B'));
System.out.println("Expected: [Big, best, Be, CS46A/B]");
System.out.println(Util.contains(words, 'a'));
System.out.println("Expected: [Java, CS46A/B]");
System.out.println(Util.contains(words, 'k'));
System.out.println("Expected: []");

}

}


class Util
{
public static int min(int[] array)
{
int min = array[0];
for(int i = 0; i < array.length; i++)
{
if(min > array[i])
{
min = array[i];
}
}
return min;
}
public static boolean contains(String[] array, String letter)
{
boolean returnValue = false;
for(int i = 0; i < array.length; i++)
{
if(array[i].contains(letter.toLowerCase()))
{
returnValue = true;
}
else
{
returnValue = false;
}
}
return returnValue;
}
public static ArrayList<String> contains(ArrayList<String> list, char letter)
{
   ArrayList<String> words = new ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
  
// this is where you have made mistake you just return but you need to return list so i have defined one arraylist when in the list match withh the letter i will put that word into the new list and just return list
   if(list.get(i).toString().contains(Character.toString(letter)))
      
   words.add(list.get(i));
  
}
return words;
}
  
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 3.1 Implement the stack ADT using array 3 TASKS public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1)...
This is the java code that I have, but i cannot get the output that I...
This is the java code that I have, but i cannot get the output that I want out of it. i want my output to print the full String Form i stead of just the first letter, and and also print what character is at the specific index instead of leaving it empty. and at the end for Replaced String i want to print both string form one and two with the replaced letters instead if just printing the first...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
I'm generating a random number every 10 second and Storing and Updating the number every 10...
I'm generating a random number every 10 second and Storing and Updating the number every 10 second when I generate new number in UpdateInt method. However, In my main method when I called UpdateInt method it generate every 10 second new number but it doesn't update and store in the arraylist. ***I want the Runnable to be in the arraylist method. Not in main method. my goal is to send the arraylist that has the update number*** import java.util.ArrayList; import...