Results 1 to 5 of 5
  1. #1
    riddy is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Question Please help me to initialise my bubble sort

    So my code is pretty solid now (I think), and I know I'm missing something fairly simple here but I cannot figure out how to get the option 7 of my menu to use my sort method to sort the students array and then printed the sorted grades to the screen. Can I get some help please?

    Java Code:
    import java.util.*;
    
    public class StudentProcessor2 {
    
    	public static void main(String[] args) {
    
    		Scanner input = new Scanner(System.in);
    
    		// read in how many students to process
    		System.out.print("How many students?");
    		int numStudents = input.nextInt();
    
    		Student[] students = new Student[numStudents];
    
    		for (int i = 0; i < numStudents; i++) {
    			// read in student data from Scanner
    			System.out.println("What is the students name?");
    			String name = input.next();
    
    			System.out.println("What is the students grade?");
    //			System.out.println("***Type -1 if no more grades***");
    			int grade = input.nextInt();
    			Student student = new Student();
    			student.setName(name);
    			student.setGrade(grade);
    			students[i] = student;
    		}
    		UserSelection(input, numStudents, students);
    	}
    
    	// show menu with options
    	public static void UserSelection(Scanner input, int numStudents,
    			Student[] students) {
    		// Repeatedly process menu selections by the user.
    		int choice;
    		do {
    			System.out.println();
    			System.out.println("*** Student Exam Results Menu ***");
    			System.out.println("(1) Display the results");
    			System.out.println("(2) Display the average result");
    			System.out.println("(3) Display the highest grade");
    			System.out.println("(4) Display the lowest grade");
    			System.out.println("(5) Search for specific result");
    			System.out.println("(6) Search for student grade by name");
    			System.out.println("(7) Sort the results in ascending order");
    			System.out.println("(8) quit");
    			choice = input.nextInt();
    
    			if (choice == 1)
    			{
    				System.out.println(Arrays.toString(students));
    			} else if (choice == 2)
    
    			{
    				double average = getAverage(students);
    				System.out.println("Average Grade = " + average);
    			} else if (choice == 3)
    
    			{
    				int high = getHighest(students);
    				System.out.println("high grade = " + high);
    			} else if (choice == 4)
    
    			{
    				int low = getLowest(students);
    				System.out.println("low grade = " + low);
    
    			} else if (choice == 5)
    
    			{
    				System.out.print("Result to look for: ");
    				int grade = input.nextInt();
    				 if (result(grade, students)) {
    				 System.out.println(grade +
    				 " is in the collection of grades.");
    				 } else
    
    				 {
    				 System.out.println(grade +
    				 " is not in the collection of grades.");
    				 }
    
    			} else if (choice == 6)
    
    			{
    				System.out.print("Student to search for: ");
    				String name = input.next();
    				if (search(name, students) !=null)
    				{
    					System.out.println(name +
    					" is in the list of Students.");
    				} else
    				{
    				System.out.println(name +
    				" is not in the list of Students");
    				}
    			}else if (choice == 7)
    
    			{
    				int order = sort(students);
    				System.out.println("The results in ascending order are: " + sort);
    			}
    
    		} while (choice != 8);
    	}
    
    	// get Lower Grade
    	private static int getLowest(Student[] students) {
    		int lowest = 100;
    		Student result = null;
    		for (Student student : students) {
    			if (student.getGrade() < lowest) {
    				lowest = student.getGrade();
    				result = student;
    			}
    		}
    		return result.getGrade();
    	}
    
    	// get Highest grade
    	private static int getHighest(Student[] students) {
    		int highest = 0;
    		Student result = null;
    		for (Student student : students) {
    			if (student.getGrade() > highest) {
    				highest = student.getGrade();
    				result = student;
    			}
    		}
    		return result.getGrade();
    	}
    
    	// get Average grade
    	private static double getAverage(Student[] students) {
    		int total = 0;
    		for (Student student : students) {
    			total += student.getGrade();
    		}
    		return total / students.length;
    	}
    
    	// Search for student
    	private static Student search(String name, Student[] students) {
    		Student result = null;
    		for (Student student : students) {
    			if (student.getName().equalsIgnoreCase(name)) {
    				result = student;
    				break;
    			}
    		}
    		return result;
    	}
    	// Search for grade
    	private static boolean result(int grade, Student[] students) {
    		for (Student student : students) {
    			if (student.getGrade() == grade) {
    				return true;
    			}
    		}
    		return false;
    	}
    
    	// !Bubble sort goes here!
    	 public static void sort(Student[] students)
    	  {
    	    int j;
    	    boolean flag = true; // set flag to true to begin first pass
    	    Student temp;  //holding variable
    
    	    while (flag)
    	    {
    	      flag = false; //set flag to false awaiting a possible swap
    	      for (j = 0; j < students.length -1; j++)
    	      {
    	        if (students[j].compareTo(students[j+1]) > 0) // change to > for ascending sort
    	        {
    	          temp = students[j];  //swap elements
    	          students[j] = students[j+1];
    	          students[j+1] = temp;
    	          flag = true;  //shows a swap occurred
    	        }
    	      }
    	    }
      }
    		}

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Please help me to initialise my bubble sort

    Your code assumes that your sort( ... ) method returns an int value; it doesn't, it returns a void (nothing).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Please help me to initialise my bubble sort

    Java Code:
                    int order = sort(students);
                    System.out.println("The results in ascending order are: " + sort);
    sort(String[] students) has a void return type. int order = ... doesn't make sense.
    What is "sort" in the println statement? You'll want a function (or some other mechanism hint: for(Student s: students) ) to print the Array; one element at a time.

  4. #4
    riddy is offline Member
    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Default Re: Please help me to initialise my bubble sort

    Quote Originally Posted by SJF View Post
    Java Code:
                    int order = sort(students);
                    System.out.println("The results in ascending order are: " + sort);
    sort(String[] students) has a void return type. int order = ... doesn't make sense.
    What is "sort" in the println statement? You'll want a function (or some other mechanism hint: for(Student s: students) ) to print the Array; one element at a time.
    I've added this and it is compiling now:

    Java Code:
    	}else if (choice == 7)
    
    			{
    				for (Student student : students) {
    				System.out.println("The results in ascending order are: " + students);
    			}
    			}
    but when I try to sort, this is what I'm getting.

    The results in ascending order are: [LStudent;@1b67f74
    The results in ascending order are: [LStudent;@1b67f74

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,760
    Rep Power
    5

    Default Re: Please help me to initialise my bubble sort

    Change students to student in your print statement.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Bubble sort Algorithm
    By Hotspotmar in forum New To Java
    Replies: 1
    Last Post: 06-06-2012, 07:38 AM
  2. Bubble-sort method
    By Mapisto in forum New To Java
    Replies: 10
    Last Post: 12-27-2011, 09:43 PM
  3. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  4. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •