Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Algorithm Program help

807607Dec 3 2006 — edited Dec 3 2006
Hey guys! I am wondering if any one of you guys could help me with this program.
// Lab18.java


import java.io.*;
import java.util.*;
import java.text.DecimalFormat;

      
public class Lab18
{
	public static void main(String args[]) throws IOException
	{
		List studentArray = new List(100);
		studentArray.getList();
		studentArray.display("UNSORTED LIST OF STUDENTS");
		studentArray.pause();
		
		studentArray.gpaSort(); 
		studentArray.display("STUDENTS SORTED IN DESCENDING ORDER BY GPA");
		studentArray.pause();
		
		studentArray.ageSort(); 
		studentArray.display("STUDENTS SORTED IN ASCENDING ORDER BY AGE");
		studentArray.pause();	
		
		studentArray.idSort();  
		studentArray.display("STUDENTS SORTED IN ASCENDING ORDER BY ID#");
		studentArray.pause();	
		
//		int studentID = getID();
//		int index = studentArray.search(studentID);
//		
//		if (index == -1)
//		    System.out.println("There is no student with an ID# of "+studentID+".\n");
//		else
//		{
//			studentArray.displayStudent(index);   // displays the information for the found student
//
			studentArray.delete(index);           // remove the same student from the array
//			studentArray.display("STUDENTS SORTED IN ASCENDING ORDER BY ID# WITHOUT STUDENT# "+studentID);
//			studentArray.pause();
//		}    
	}
	
	public static int getID()
	{
		Scanner input = new Scanner(System.in);	 
		System.out.print("\nEnter the 6-digit ID of the student.  { 100000 - 999999 }  -->  ");
		return input.nextInt();
	}
}


class Person
{
	public String name;
	public int id;
	public int age;
	public double gpa;
   
	Person(String n, int ID, int a,double g)
	{
		name = n;
		id = ID;
		age = a;
		gpa = g;
	}
}


class List
{
	private Person student[];	// stores array elements
	private int capacity;		// actual array capacity
	private int size;			// number of elements in the array
   
	public List(int c)
	{
		capacity = c;
		size = 0;
		student = new Person[capacity];            
	}

	public void getList() throws IOException
	{
		FileReader inFile = new FileReader("students2.dat");	
		BufferedReader inStream = new BufferedReader(inFile);	     
		String s1,s2,s3,s4;
		int age, id;
		double gpa;						
		int index = 0;
		while( ((s1 = inStream.readLine()) != null) && 
			   ((s2 = inStream.readLine()) != null) && 
			   ((s3 = inStream.readLine()) != null) &&
			   ((s4 = inStream.readLine()) != null) )	
		{
			String name = s1;
			id = Integer.parseInt(s2);
			age = Integer.parseInt(s3);
			gpa = Double.parseDouble(s4);

			student[index] = new Person(name,id,age,gpa);       
			index++;
		}   
		inStream.close(); 
		size = index;   					
	}
	
	private String spaces(String name)
    {
    	int tab = 24 - name.length();
    	String temp = "";
    	for (int j = 1; j <= tab; j++)
    	    temp += " ";
    	return temp;    
    }
      
	public void display(String listInfo)
	{
		DecimalFormat output = new DecimalFormat("0.000");
		System.out.println("\nDISPLAYING "+ listInfo);
		System.out.println("\nStudent ID#     Student Name            Age         GPA");
		System.out.println("============================================================");
		     
		for (int k = 0; k < size; k++)
			System.out.println(student[k].id + "          "+student[k].name + spaces(student[k].name) + student[k].age + "          " + output.format(student[k].gpa));
	}   
	
	public void pause() 
	{   
		Scanner input = new Scanner(System.in);	 
		String dummy;
		System.out.println("\nPress <Enter> to continue.");						
		dummy = input.nextLine();								
	}

	public void displayStudent(int index)
	{
		
	}   

	private void swap(int x, int y)
	{

	}  

	public void gpaSort()
	{

	}

	public void ageSort()
	{

	}
	
	public void idSort()
	{

	}
	
//	public int search(int studentID)
//	{

//	}		
	
	public void delete(int index)
	{
		// This delete method assumes the desired element has already been found.
		// "index" is the index of the element to be deleted.

	}	
}    
There is a persons2 data file, which consists of...

Bart Reagor
123456
27
2.075
Kristyn Reckner
234567
19
3.225
Paul Reiman
345678
45
4.000
Andy Reitinger
456789
20
3.525
William Reynolds
567890
50
3.375
William Robbins
460064
34
2.875
Brian Roberts
811811
23
3.175
Lauri Robertson
119010
19
2.925
Richardson Robinson
203203
26
3.210
Laurie Rosemberg
976976
45
4.000
Barbara Salsa
777777
18
3.000
Lori Salbury
212121
23
3.455
Ann Seaborn
313131
56
1.785
Diane Simcox
987654
32
2.175
Kevin Sims
876543
67
3.235
Michael Ward
765432
19
3.451
Cheryl Willis
654321
37
3.576
Tracy Springer
334213
41
2.305
Mary Pridgen
567765
19
1.957
Steven Johnson
681968
34
0.785
Nancy Barone
911072
25
2.975
Tom Tooch
751977
26
1.456
Patti Skinner
671983
47
2.743
Diana Rockel
429067
56
3.456
Michelle Ritter
491967
25
4.000
Mali Cozart
720097
16
2.345
Mike Bruun
321456
19
1.783
Todd Deans
654789
22
2.023
Laura Collins
876890
30
3.999
Debbie Mozart
342517
40
2.999
Gordon Collins
545632
25
3.100
Susan Craft
551212
29
3.754
Vance Brawner
662323
31
3.678

First of all, i just need to sort the information 3 times (by ID, gpa, and age). After that I need to do more stuff, but i'd like to go step by step.
SORRY FOR THE LONG POST!
Thanks a lot guys.

Message was edited by:
asim9
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 31 2006
Added on Dec 3 2006
4 comments
706 views