SlideShare a Scribd company logo
1 of 7
Download to read offline
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
*
*/
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)
{
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)
{
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)
{
// 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++)
{
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);
}
}
}
}

More Related Content

Similar to JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf

Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfxlynettalampleyxc
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfmalavshah9013
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxVictorzH8Bondx
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfezonesolutions
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdfdeepakangel
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxcgraciela1
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfStewart29UReesa
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docxKomlin1
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdffashiongallery1
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfAugstore
 
Step 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdfStep 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdfformaxekochi
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfarpaqindia
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhvasavim9
 
(Sort ArrayList) Write the following method that sorts an ArrayList.pdf
(Sort ArrayList) Write the following method that sorts an ArrayList.pdf(Sort ArrayList) Write the following method that sorts an ArrayList.pdf
(Sort ArrayList) Write the following method that sorts an ArrayList.pdfalokkesh
 

Similar to JAVA helpNeed bolded lines fixed for it to compile. Thank you!pu.pdf (20)

Linked list1
Linked list1Linked list1
Linked list1
 
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdfClass DiagramIn the Assignment #10, you are given three files Ass.pdf
Class DiagramIn the Assignment #10, you are given three files Ass.pdf
 
Chapter14
Chapter14Chapter14
Chapter14
 
The LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdfThe LinkedList1 class implements a Linked list. class.pdf
The LinkedList1 class implements a Linked list. class.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
Note- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docxNote- Can someone help me with the private E get(int index- int curren (1).docx
Note- Can someone help me with the private E get(int index- int curren (1).docx
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdf
 
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
  import java.util.Iterator; import java.util.NoSuchElementException; .pdf  import java.util.Iterator; import java.util.NoSuchElementException; .pdf
import java.util.Iterator; import java.util.NoSuchElementException; .pdf
 
Please complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docxPlease complete all the code as per instructions in Java programming.docx
Please complete all the code as per instructions in Java programming.docx
 
Note- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdfNote- Can someone help me with the Public boolean add(E value) method.pdf
Note- Can someone help me with the Public boolean add(E value) method.pdf
 
강의자료6
강의자료6강의자료6
강의자료6
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docx
 
For this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdfFor this lab you will complete the class MyArrayList by implementing.pdf
For this lab you will complete the class MyArrayList by implementing.pdf
 
Adt of lists
Adt of listsAdt of lists
Adt of lists
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
 
Step 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdfStep 1 The Pair Class Many times in writing software we come across p.pdf
Step 1 The Pair Class Many times in writing software we come across p.pdf
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
(Sort ArrayList) Write the following method that sorts an ArrayList.pdf
(Sort ArrayList) Write the following method that sorts an ArrayList.pdf(Sort ArrayList) Write the following method that sorts an ArrayList.pdf
(Sort ArrayList) Write the following method that sorts an ArrayList.pdf
 
강의자료9
강의자료9강의자료9
강의자료9
 

More from suresh640714

In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfsuresh640714
 
Find the slope of the line passing through each pair of points or st.pdf
Find the slope of the line passing through each pair of points or st.pdfFind the slope of the line passing through each pair of points or st.pdf
Find the slope of the line passing through each pair of points or st.pdfsuresh640714
 
How does packet switching combine signals from different sourcesa.pdf
How does packet switching combine signals from different sourcesa.pdfHow does packet switching combine signals from different sourcesa.pdf
How does packet switching combine signals from different sourcesa.pdfsuresh640714
 
For the circuit and current waveform given in example 2 of lecture vi.pdf
For the circuit and current waveform given in example 2 of lecture vi.pdfFor the circuit and current waveform given in example 2 of lecture vi.pdf
For the circuit and current waveform given in example 2 of lecture vi.pdfsuresh640714
 
Epidemiology Scavenger HuntFind an example infectious disease for .pdf
Epidemiology Scavenger HuntFind an example infectious disease for .pdfEpidemiology Scavenger HuntFind an example infectious disease for .pdf
Epidemiology Scavenger HuntFind an example infectious disease for .pdfsuresh640714
 
Discuss the concept of breeder reactors. How do they breed fuel What.pdf
Discuss the concept of breeder reactors. How do they breed fuel What.pdfDiscuss the concept of breeder reactors. How do they breed fuel What.pdf
Discuss the concept of breeder reactors. How do they breed fuel What.pdfsuresh640714
 
Discuss botulism and the different types of toxins produced by the d.pdf
Discuss botulism and the different types of toxins produced by the d.pdfDiscuss botulism and the different types of toxins produced by the d.pdf
Discuss botulism and the different types of toxins produced by the d.pdfsuresh640714
 
Could please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfCould please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfsuresh640714
 
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdfConsider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdfsuresh640714
 
CommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfCommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfsuresh640714
 
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfchoose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfsuresh640714
 
Based on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfBased on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfsuresh640714
 
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfB Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfsuresh640714
 
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfarMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfsuresh640714
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfsuresh640714
 
Andy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfAndy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfsuresh640714
 
A die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfA die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfsuresh640714
 
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfA 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfsuresh640714
 
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdfsuresh640714
 
2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdfsuresh640714
 

More from suresh640714 (20)

In JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdfIn JavaWrite a program that reads a file and counts how many line.pdf
In JavaWrite a program that reads a file and counts how many line.pdf
 
Find the slope of the line passing through each pair of points or st.pdf
Find the slope of the line passing through each pair of points or st.pdfFind the slope of the line passing through each pair of points or st.pdf
Find the slope of the line passing through each pair of points or st.pdf
 
How does packet switching combine signals from different sourcesa.pdf
How does packet switching combine signals from different sourcesa.pdfHow does packet switching combine signals from different sourcesa.pdf
How does packet switching combine signals from different sourcesa.pdf
 
For the circuit and current waveform given in example 2 of lecture vi.pdf
For the circuit and current waveform given in example 2 of lecture vi.pdfFor the circuit and current waveform given in example 2 of lecture vi.pdf
For the circuit and current waveform given in example 2 of lecture vi.pdf
 
Epidemiology Scavenger HuntFind an example infectious disease for .pdf
Epidemiology Scavenger HuntFind an example infectious disease for .pdfEpidemiology Scavenger HuntFind an example infectious disease for .pdf
Epidemiology Scavenger HuntFind an example infectious disease for .pdf
 
Discuss the concept of breeder reactors. How do they breed fuel What.pdf
Discuss the concept of breeder reactors. How do they breed fuel What.pdfDiscuss the concept of breeder reactors. How do they breed fuel What.pdf
Discuss the concept of breeder reactors. How do they breed fuel What.pdf
 
Discuss botulism and the different types of toxins produced by the d.pdf
Discuss botulism and the different types of toxins produced by the d.pdfDiscuss botulism and the different types of toxins produced by the d.pdf
Discuss botulism and the different types of toxins produced by the d.pdf
 
Could please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdfCould please answer those questions about North country movieHere.pdf
Could please answer those questions about North country movieHere.pdf
 
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdfConsider the equation 1 - 2x = sin x.  Use the Intermediate Value The.pdf
Consider the equation 1 - 2x = sin x. Use the Intermediate Value The.pdf
 
CommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdfCommunicationsEvery organization has its own unique “organization.pdf
CommunicationsEvery organization has its own unique “organization.pdf
 
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdfchoose the word that BEST fits each statement.A. stomachB. Esoph.pdf
choose the word that BEST fits each statement.A. stomachB. Esoph.pdf
 
Based on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdfBased on your new understanding of Master Data Management and how or.pdf
Based on your new understanding of Master Data Management and how or.pdf
 
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdfB Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
B Ann baked more pans of lasagna. Each pan was shared with a differen.pdf
 
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdfarMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
arMathAp11 6.3.021.Ask Your TeacherMy NotesHow much must be de.pdf
 
Answer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdfAnswer using basic programming beginner knowledge pls...........Othe.pdf
Answer using basic programming beginner knowledge pls...........Othe.pdf
 
Andy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdfAndy and Ben are going to play a final round of game 5 to determine .pdf
Andy and Ben are going to play a final round of game 5 to determine .pdf
 
A die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdfA die is rolled repeatedly until two consecutive rolls have the same .pdf
A die is rolled repeatedly until two consecutive rolls have the same .pdf
 
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdfA 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
A 70-kg individual drinks 2 Lday from a pond outside his house. Wha.pdf
 
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
4.2.4. Suppose we assume that X1, X2, . . . , Xn is a random sample .pdf
 
2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf2. If you have a nonlinear relationship between an independent varia.pdf
2. If you have a nonlinear relationship between an independent varia.pdf
 

Recently uploaded

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
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); } } } }