JAVA help
Need bolded lines fixed for it to compile. Thank you!
public class PersonSort
{
// Test file. Format is \"STRING int int int\"
static final String PERSON_FILE = \".\\\\src\\\\Persons.txt\";
public static void main(String[] args) throws FileNotFoundException
{
// Create new ArrayList and populate from test file
ArrayList list1 = new ArrayList();
populate(list1);
// Create new array with same people
ArrayList list2 = new ArrayList(list1);
insertionSort(list1);
// Print result of sort
System.out.println(\"INSERTION SORT\");
// *** foreach Person p in list1
{
System.out.println(p.toString());
}
selectionSort(list2);
System.out.println();
System.out.println(\"SELECTION SORT\");
// *** foreach Person p in list2
{
System.out.println(p.toString());
}
}
/*
* populate - this method reads the Persons.txt file and creates an array list
*
*/
public static ArrayList populate(ArrayList list)
throws IOException
{
// Scan in the file
File people = new File(PERSON_FILE);
Scanner ppl = new Scanner(people);
// While we have a next line, create a new Person and add it to the list
while (ppl.hasNextLine())
{
String name = ppl.next();
int month = ppl.nextInt();
int day = ppl.nextInt();
int year = ppl.nextInt();
list.add(new Person(name, month, day, year));
}
ppl.close();
return list;
}
/**
*
* Sorts an ArrayList based on the insertion sort algorithm. Modified code based
* on insertion sort from Sort.java in Lesson3SourceCode.
*
*/
// *** change double[] to ArrayList
public static void insertionSort (double[] list)
{
// Temporary variable for the next item to be inserted
// *** change double to Person
double valueToInsert;
int insertPos = 0;
// Iterate through the array taking each array element in turn
// as the next one to be inserted in its correct position.
// This element is placed in its correct position in the array of
// previously sorted elements contained in the lower array indices.
for (int i = 1; i < list.size(); i++)
{
// Hold the next element to be inserted,
// until we find the correct spot
valueToInsert = list.get(i);
insertPos = i;
// Find the correct place to insert this element
// in the lower array indices of already sorted elements
while ((insertPos > 0) && (list.get(insertPos - 1).compareTo(valueToInsert) > 0))
{
// Move elements up the array
// and insert position down
list.set(insertPos, list.get(insertPos - 1));
insertPos--;
}
// We are at the correct position, so insert the element
list.set(insertPos, valueToInsert);
}
}
/**
*
* Sorts an ArrayList based on the selection sort algorithm. Modified code based
* on selection sort from Sort.java in Lesson3SourceCode.
*
*/
// *** change double[] to ArrayList
public static void selectionSort (double[] list)
{
for (int i = 0; i < list.size(); i++)
{
// Find the minimum in the ArrayList through [i..list.length-1]
// *** change double to Person
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < myList.size(); j++)
{
if (currentMin.compareTo(list.get(j)) > 0)
{
curr.
Interactive Powerpoint_How to Master effective communication
JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf
1. JAVA help
Need bolded lines fixed for it to compile. Thank you!
public class PersonSort
{
// Test file. Format is "STRING int int int"
static final String PERSON_FILE = ".srcPersons.txt";
public static void main(String[] args) throws FileNotFoundException
{
// Create new ArrayList and populate from test file
ArrayList list1 = new ArrayList();
populate(list1);
// Create new array with same people
ArrayList list2 = new ArrayList(list1);
insertionSort(list1);
// Print result of sort
System.out.println("INSERTION SORT");
// *** foreach Person p in list1
{
System.out.println(p.toString());
}
selectionSort(list2);
System.out.println();
System.out.println("SELECTION SORT");
// *** foreach Person p in list2
{
System.out.println(p.toString());
}
}
/*
* populate - this method reads the Persons.txt file and creates an array list
*
2. */
public static ArrayList populate(ArrayList list)
throws IOException
{
// Scan in the file
File people = new File(PERSON_FILE);
Scanner ppl = new Scanner(people);
// While we have a next line, create a new Person and add it to the list
while (ppl.hasNextLine())
{
String name = ppl.next();
int month = ppl.nextInt();
int day = ppl.nextInt();
int year = ppl.nextInt();
list.add(new Person(name, month, day, year));
}
ppl.close();
return list;
}
/**
*
* Sorts an ArrayList based on the insertion sort algorithm. Modified code based
* on insertion sort from Sort.java in Lesson3SourceCode.
*
*/
// *** change double[] to ArrayList
public static void insertionSort (double[] list)
{
// Temporary variable for the next item to be inserted
// *** change double to Person
double valueToInsert;
int insertPos = 0;
// Iterate through the array taking each array element in turn
// as the next one to be inserted in its correct position.
// This element is placed in its correct position in the array of
// previously sorted elements contained in the lower array indices.
3. for (int i = 1; i < list.size(); i++)
{
// Hold the next element to be inserted,
// until we find the correct spot
valueToInsert = list.get(i);
insertPos = i;
// Find the correct place to insert this element
// in the lower array indices of already sorted elements
while ((insertPos > 0) && (list.get(insertPos - 1).compareTo(valueToInsert) > 0))
{
// Move elements up the array
// and insert position down
list.set(insertPos, list.get(insertPos - 1));
insertPos--;
}
// We are at the correct position, so insert the element
list.set(insertPos, valueToInsert);
}
}
/**
*
* Sorts an ArrayList based on the selection sort algorithm. Modified code based
* on selection sort from Sort.java in Lesson3SourceCode.
*
*/
// *** change double[] to ArrayList
public static void selectionSort (double[] list)
{
for (int i = 0; i < list.size(); i++)
{
// Find the minimum in the ArrayList through [i..list.length-1]
// *** change double to Person
double currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < myList.size(); j++)
4. {
if (currentMin.compareTo(list.get(j)) > 0)
{
currentMin = list.get(j);
currentMinIndex = j;
}
}
// Swap myList at i with myList at currentMinIndex if necessary;
if (currentMinIndex != i)
{
list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}
}
}
Solution
Hi, Please find my fixed code:
################
public class PersonSort
{
// Test file. Format is "STRING int int int"
static final String PERSON_FILE = ".srcPersons.txt";
public static void main(String[] args) throws FileNotFoundException
{
// Create new ArrayList and populate from test file
ArrayList list1 = new ArrayList();
populate(list1);
// Create new array with same people
ArrayList list2 = new ArrayList(list1);
insertionSort(list1);
// Print result of sort
System.out.println("INSERTION SORT");
for(Person p : list1)
5. {
System.out.println(p.toString());
}
selectionSort(list2);
System.out.println();
System.out.println("SELECTION SORT");
for(Person p : list2)
{
System.out.println(p.toString());
}
}
/*
* populate - this method reads the Persons.txt file and creates an array list
*
*/
public static ArrayList populate(ArrayList list)
throws IOException
{
// Scan in the file
File people = new File(PERSON_FILE);
Scanner ppl = new Scanner(people);
// While we have a next line, create a new Person and add it to the list
while (ppl.hasNextLine())
{
String name = ppl.next();
int month = ppl.nextInt();
int day = ppl.nextInt();
int year = ppl.nextInt();
list.add(new Person(name, month, day, year));
}
ppl.close();
return list;
}
##########################
public static void insertionSort (ArrayList list)
{
6. // Temporary variable for the next item to be inserted
Person valueToInsert;
int insertPos = 0;
// Iterate through the array taking each array element in turn
// as the next one to be inserted in its correct position.
// This element is placed in its correct position in the array of
// previously sorted elements contained in the lower array indices.
for (int i = 1; i < list.size(); i++)
{
// Hold the next element to be inserted,
// until we find the correct spot
valueToInsert = list.get(i);
insertPos = i;
// Find the correct place to insert this element
// in the lower array indices of already sorted elements
while ((insertPos > 0) && (list.get(insertPos - 1).compareTo(valueToInsert) > 0))
{
// Move elements up the array
// and insert position down
list.set(insertPos, list.get(insertPos - 1));
insertPos--;
}
// We are at the correct position, so insert the element
list.set(insertPos, valueToInsert);
}
}
#####################
public static void selectionSort (ArrayList list)
{
for (int i = 0; i < list.size(); i++)
{
// Find the minimum in the ArrayList through [i..list.length-1]
Person currentMin = list[i];
int currentMinIndex = i;
for (int j = i + 1; j < list.size(); j++)
7. {
if (currentMin.compareTo(list.get(j)) > 0)
{
currentMin = list.get(j);
currentMinIndex = j;
}
}
// Swap list at i with list at currentMinIndex if necessary;
if (currentMinIndex != i)
{
list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}
}
}