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 =...
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
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...
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)...
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...
Hi, I'm writing a Java program that prints a grid with circles and squares that have...
Hi, I'm writing a Java program that prints a grid with circles and squares that have letters in them and it is also supposed to print the toString() function to the console window each time the application runs. This toString() function is supposed to show the tile's shape, letter, and color component (or components). Could someone please review and debug my code to help me figure out why my toString() function is not working? import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton;...