SlideShare a Scribd company logo
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

DOCX
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
PDF
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
PDF
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
PDF
Im getting List Full when I try to add 2nd student.Driver..pdf
PDF
Aj unit2 notesjavadatastructures
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
PDF
For each task, submit your source java code file.(1) Objective Im.pdf
PDF
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
PLEASE MAKE SURE THE PROGRAM IS ASKING FOR INPUT FROM USER TO ADD OR.pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
Im getting List Full when I try to add 2nd student.Driver..pdf
Aj unit2 notesjavadatastructures
Refer to my progress on this assignment belowIn this problem you w.pdf
For each task, submit your source java code file.(1) Objective Im.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf

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

PDF
Write a java class LIST that outputsmainpublic class Ass.pdf
PDF
Hi, Please find my code. I have added comment against each line, ple.pdf
PDF
Sorted number list implementation with linked listsStep 1 Inspec.pdf
PDF
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
DOCX
Array list
DOCX
This class maintains a list of 4 integers. This list .docx
PDF
I need help with this code working Create another project and add yo.pdf
PDF
Code to copy Person.java .pdf
PDF
Please help me to make a programming project I have to sue them today- (1).pdf
DOCX
Please complete all the code as per instructions in Java programming.docx
PDF
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
PDF
DS LAB MANUAL.pdf
PDF
Everything needs to be according to the instructions- thank you! SUPPO.pdf
DOCX
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
PPT
A2003822018_21789_17_2018_09. ArrayList.ppt
PDF
HELP IN JAVACreate a main method and use these input files to tes.pdf
PDF
ReversePoem.java ---------------------------------- public cl.pdf
PDF
please i need help Im writing a program to test the merge sort alg.pdf
PPT
Chap10
PDF
i am trying to add the first four lines of a studenttxt int.pdf
Write a java class LIST that outputsmainpublic class Ass.pdf
Hi, Please find my code. I have added comment against each line, ple.pdf
Sorted number list implementation with linked listsStep 1 Inspec.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Array list
This class maintains a list of 4 integers. This list .docx
I need help with this code working Create another project and add yo.pdf
Code to copy Person.java .pdf
Please help me to make a programming project I have to sue them today- (1).pdf
Please complete all the code as per instructions in Java programming.docx
Not sure why my program wont run.Programmer S.Villegas helper N.pdf
DS LAB MANUAL.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
JAVA Demonstrate the use of your APL in a PartB_Driver class by doing.docx
A2003822018_21789_17_2018_09. ArrayList.ppt
HELP IN JAVACreate a main method and use these input files to tes.pdf
ReversePoem.java ---------------------------------- public cl.pdf
please i need help Im writing a program to test the merge sort alg.pdf
Chap10
i am trying to add the first four lines of a studenttxt int.pdf

More from suresh640714 (20)

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

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
RMMM.pdf make it easy to upload and study
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
102 student loan defaulters named and shamed – Is someone you know on the list?
GDM (1) (1).pptx small presentation for students
Pharmacology of Heart Failure /Pharmacotherapy of CHF
RMMM.pdf make it easy to upload and study
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
A systematic review of self-coping strategies used by university students to ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
human mycosis Human fungal infections are called human mycosis..pptx
Chinmaya Tiranga quiz Grand Finale.pdf
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx

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); } } } }