mirror of
https://github.com/Rushilwiz/APCS.git
synced 2025-04-04 20:40:20 -04:00
104 lines
3.7 KiB
Java
104 lines
3.7 KiB
Java
// Name: B6-24
|
|
// Date: 12/11/19
|
|
// use for-each loops or iterators, not regular for-loops
|
|
import java.io.*;
|
|
import java.util.*;
|
|
public class IteratorLab
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
System.out.println("Iterator Lab\n");
|
|
int[] rawNumbers = {-9, 4, 2, 5, -10, 6, -4, 24, 20, -28};
|
|
for(int n : rawNumbers )
|
|
System.out.print(n + " ");
|
|
ArrayList<Integer> numbers = createNumbers(rawNumbers);
|
|
System.out.println("\nArrayList: "+ numbers); //Implicit Iterator!
|
|
System.out.println("Count negative numbers: " + countNeg(numbers));
|
|
System.out.println("Average: " + average(numbers));
|
|
System.out.println("Replace negative numbers: " + replaceNeg(numbers));
|
|
System.out.println("Delete zeros: " + deleteZero(numbers));
|
|
String[] rawMovies = {"High_Noon", "High_Noon", "Star_Wars", "Tron", "Mary_Poppins",
|
|
"Dr_No", "Dr_No", "Mary_Poppins", "High_Noon", "Tron"};
|
|
ArrayList<String> movies = createMovies(rawMovies);
|
|
System.out.println("Movies: " + movies);
|
|
System.out.println("Movies: " + removeDupes(movies));
|
|
}
|
|
// pre: an array of just int values
|
|
// post: return an ArrayList containing all the values
|
|
public static ArrayList<Integer> createNumbers(int[] rawNumbers)
|
|
{
|
|
ArrayList<Integer> list = new ArrayList<Integer>();
|
|
for (int i = 0; i < rawNumbers.length; i++)
|
|
list.add(rawNumbers[i]);
|
|
return list;
|
|
}
|
|
// pre: an array of just Strings
|
|
// post: return an ArrayList containing all the Strings
|
|
public static ArrayList<String> createMovies(String[] rawWords)
|
|
{
|
|
ArrayList<String> myList = new ArrayList<String>();
|
|
for ( String str : rawWords )
|
|
myList.add( str );
|
|
return myList;
|
|
}
|
|
|
|
// pre: ArrayList a is not empty and contains only Integer objects
|
|
// post: return the number of negative values in the ArrayList a
|
|
public static int countNeg(ArrayList<Integer> a)
|
|
{
|
|
Iterator<Integer> it = a.iterator(); int count = 0;
|
|
while(it.hasNext())
|
|
if (it.next() < 0)
|
|
count++;
|
|
return count;
|
|
}
|
|
// pre: ArrayList a is not empty and contains only Integer objects
|
|
// post: return the average of all values in the ArrayList a
|
|
public static double average(ArrayList<Integer> a)
|
|
{
|
|
Iterator<Integer> it = a.iterator(); double avg = 0;
|
|
while(it.hasNext())
|
|
avg += it.next();
|
|
|
|
avg /= a.size();
|
|
return avg;
|
|
}
|
|
// pre: ArrayList a is not empty and contains only Integer objects
|
|
// post: replaces all negative values with 0
|
|
public static ArrayList<Integer> replaceNeg(ArrayList<Integer> a)
|
|
{
|
|
ListIterator<Integer> it = a.listIterator();
|
|
while (it.hasNext())
|
|
if (it.next() < 0)
|
|
it.set(0);
|
|
|
|
return a;
|
|
}
|
|
// pre: ArrayList a is not empty and contains only Integer objects
|
|
// post: deletes all zeros in the ArrayList a
|
|
public static ArrayList<Integer> deleteZero(ArrayList<Integer> a)
|
|
{
|
|
ListIterator<Integer> it = a.listIterator();
|
|
while(it.hasNext())
|
|
if (it.next() == 0)
|
|
it.remove();
|
|
|
|
return a;
|
|
}
|
|
// pre: ArrayList a is not empty and contains only String objects
|
|
// post: return ArrayList without duplicate movie titles
|
|
// strategy: start with an empty array and add movies as needed
|
|
public static ArrayList<String> removeDupes(ArrayList<String> a)
|
|
{
|
|
ArrayList<String> newList = new ArrayList<>(); ListIterator<String> it = a.listIterator();
|
|
while(it.hasNext()) {
|
|
String movie = it.next();
|
|
if (!newList.contains(movie))
|
|
newList.add(movie);
|
|
}
|
|
return newList;
|
|
}
|
|
|
|
}
|
|
|