How do I write this code from while loop to for loop? (this is the whole code)
NOTE: do not add any import other than:
import java.util.ArrayList;
import java.util.Collection;(collection not collections)
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.HashSet;
code:
public static int frequency(Collection values, int
target) {
var iter = values.iterator();
int app = 0;
while (iter.hasNext()) {
if(iter.next() == target) {
app++;
}
}
return app;
}
public static boolean isSorted(List list) {
var iter = list.listIterator();
int prev = 0;
if (iter.hasNext()) {
prev = iter.next();
}
while(iter.hasNext()) {
int curr = iter.next();
if (prev > curr) {
return false;
}
prev = curr;
}
return true;
}
public static int frequency(Collection values, int target) {
int app = 0;
for(var iter = values.iterator();iter.hasNext();) {
if((int)iter.next() == target) {
app++;
}
}
return app;
}
public static boolean isSorted(List list) {
var iter = list.listIterator();
int prev = 0, curr;
if (iter.hasNext()) {
prev = (int)iter.next();
}
for(;iter.hasNext();prev = curr) {
curr = (int) iter.next();
if (prev > curr) {
return false;
}
}
return true;
}
How do I write this code from while loop to for loop? (this is the whole...
How can i rewrite this code?( such as changing the statement) NOTE: do not add any import other than : import java.util.ArrayList; import java.util.Collection;(collection not collections) import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.HashSet; code: public static long sum(Collection values) { if(values==null || values.size()<1) return 0; int sum = 0; for (Integer i: values) sum = sum+i; return sum; }
How do i rewrite this code only using the list below? import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Set; import java.util.HashSet; public class RotationCheck { /** * Rotates the elements in the specified list by the specified distance. After calling this * method, the element at index {@code i} will be the element previously at index * {@code (i - distance) % list.size()}, for all values of {@code i} between {@code 0} and * {@code...
how do i change this code from for loop and if to while and break? public static boolean disjoint(Collection<Integer> coll1, Collection<Integer> coll2) { var set1 = new HashSet<>(coll1); var set2 = new HashSet<>(coll2); for (Integer s : set1) { if (set2.contains(s)){ return false; } } return true; }
The JCF provides numerous classes that implement the List interface, including LinkedList, ArrayList, and Vector. Write code to show how the JCF class ArrayList and LinkedList are used to maintain a grocery list. Verify List methods such as "add()", get(), "remove()", "isEmpty()" and "size()" by implementing a test program. import java.util.ArrayList; import java.util.Iterator; public class GroceryList { static public void main(String[] args){ ArrayList<String> groceryList = new ArrayList<String>(); Iterator<String> iter; groceryList.add("apples"); groceryList.add("bread"); groceryList.add("juice"); groceryList.add("carrots"); groceryList.add("ice cream"); System.out.println("Number of items...
Analyze the code to determine what the code does, how the data is structured, and why it is the appropriate data structure (i.e. linked list, stack or queue). Note that these are examples of classes and methods and do not include the "main" or "test" code needed to execute. When you are done with your analysis, add a header describing the purpose of the program (include a description of each class and method), the results of your analysis, and add...
How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...
I've previously completed a Java assignment where I wrote a program that reads a given text file and creates an index that stores the line numbers for where individual words occur. I've been given a new assignment where I need to modify some of my old code. I need to replace the indexer in my Index class with a NavigableMap<String, Word> and update my Word class with NavigableSet<Integer> lines. The instantiated objects should be TreeMap() and TreeSet(). I have below...
I am almost done with this code, but for some reason I am still getting an error message: Some of the instructions were: Using a loop, prompt for a currency to find. Inside the loop that manages the input, call the lookUpCurrency() method can pass the inputted currency code value. The lookUpCurrency() method needs to loop through the list of currencies and compare the passed currency code with the code in the array. If found, return true - otherwise return...
How to complete this methods: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.time.LocalDate; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Scanner; import java.util.Set; /** * Utility class that deals with all the other classes. * * @author EECS2030 Summer 2019 * */ public final class Registrar { public static final String COURSES_FILE = "Courses.csv"; public static final String STUDENTS_FILE = "Students.csv"; public static final String PATH = System.getProperty("java.class.path"); /** * Hash table to store the list of students using their...
I need help with my Java code. A user enters a sentence and the program will tell you what words were duplicated and how many times. If the same word is in the sentence twice, but one is capitalized, it will not count it as a duplicate. How can I make the program read the same word as the same word, even if one is capitalized and the other is not? For example, "the" and "The" should be counted as...