Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Please help with this code
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Rod Jones
Please help with this code
0 Answer(s)      a year and 8 months ago
Posted in : Java Beginners


I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble sort to work. I've spent a long time on this and think my brain is just fried at this point! :(

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))
                {
                    System.out.println(name +
                    " is in the list of Students.");
                } else
                {
                System.out.println(name +
                " is not in the list of Students");
                }
            }

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

    private static boolean result(int grade, Student[] students) {
        for (Student student : students) {
            if (student.getGrade() == grade) {
                return true;
            }
        }
        return false;
    }

    // !Bubble sort goes here!
    private void sort(Student[] students) {

        Student hold; // temporary holding area for swap

        for (int i = 0; i < students.length; i++) { // passes
            Student current = students[i];
            Student next = students[i + 1];
            if (current.getGrade() > next.getGrade()) // one comparison
            {
                hold = students[i]; // one swap
                students[i] = students[i + 1];
                students[i + 1] = hold;
            }
        }
    }
}

Advertisement
View Answers

Related Tutorials/Questions & Answers:
Please help with this code
Please help with this code  I need some help getting the Search method below to work with the menu, and I also cannot figure out how to get my bubble sort to work. I've spent a long time on this and think my brain is just fried
could anyone please help with the code.
could anyone please help with the code.  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException...(request, response); } } could anyone please check the code. If i enter
Advertisements
ajax code please help to solve this........
ajax code please help to solve this.  in this i am trying to get data... null; } please help me when i am running this it show an error...;<a href="help.jsp">help instruction</a></li> <
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
please help me to write a code for this program
please help me to write a code for this program   1 1 1 1 2 2 1 1 3 4 3 1 1 4 7 7 4 1
Not sure whats wrong with my code HELP PLEASE?!?!
Not sure whats wrong with my code HELP PLEASE?!?!  I cant figure out what I am doing wrong in my code can anyone help me out??? Grades function computeGrade( ) { var hw, lab, midt, fin, avg; hw = parseFloat
please help me to give code - Java Beginners
please help me to give code  Write a function with a signature cheerlead(word) that prints a typical cheer as follows. The word robot: Gimme an R Gimme an O Gimme a B Gimme an O Gimme a T What did you give me? ROBOT
please help me to give code - Java Beginners
please help me to give code  Write a function, sliding(word, num)that behaves as follows. It should print out each slice of the original word having length num, aligned vertically as shown below. A call to sliding(examples, 4
Please help me fix this code - MobileApplications
Please help me fix this code   Please help me in this area of code... in the background of the forms in this code i want to sum all expenses amount... expenses)", "Please fill all required field \n \n * This signify required field
please help me to give code - Java Beginners
please help me to give code  Write a program that uses loops to generate an n x n times table. The program should get n from the user. As a model here is a 4 x4 version: | 1 2 3 4
please help me to give code - Java Beginners
please help me to give code  Write a program that prints an n-level stair case made of text. The user should choose the text character and the number of stairs in the stair case * ** *** ****   Hi friend
please help me to give code - Java Beginners
please help me to give code  Write a program that reads a file named famous.txt and prints out the line with the longest length. In the case of a tie, you may print out only one of them. For example in the file: Alan Turing
Please help me to modify my java code from php code
]; } } I tried like this (see below JSP code) ... but this is not giving me the exact result as the above PHP code is giving. So please help me to convert...Modify Java code from PHP Code  i want to covert this php code int
Please help me to modify my java code from php code
Please help me to modify my java code from php code  i want to covert...) ... but this is not giving me the exact result as the above PHP code is giving. So please... same, as they alter it before. Please help me on this topic "how to order rows
I need help on my Java code.... please please help me out!?
I need help on my Java code.... please please help me out!?  Well my code is supposed to ask for an input file and then (ex: input.txt), read... in the file. Also it should be displayed on the screen. However my code doesn't display
please help
please help  please send me the code of dynamic stack in java without using the built in functions
please any one can help me to write a code for this question?
please any one can help me to write a code for this question?  1) Copy one file content to other? 2) Count the number of words in a file
Please help me to write a code for add contacts to groups in a website?
Please help me to write a code for add contacts to groups in a website?   Iam developing a site. In that site I need to write code for Groups... send a code for this? Please make it fast
please any one can help me to write a code for this question?
please any one can help me to write a code for this question?  Q 1) In a class first day 25 students are joined. After two days that total students will increased to 60. We can develop a program by using ArrayList concept
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 ‚?? this is the ouput..;;; i
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 ‚?? this is the ouput..;;; i
please help//
please help//  Number square cube 1 1 1 3 9 27 5 25 125 7 49 343 9 81 729 total 165 1225 ‚?? this is the ouput..;;; i
help please
file.. Or atleast help me with code here.. I tried checking session alive...help please  hi i am done with register application using jsps servlets htmls. But i couldnt imp one thing that.. Wen u login to ur account browser
help please?
help please?  Define a class named Circle with the following properties: ‚?Ę An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both data fields
help please?
help please?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
help please?
help please?  Define a class named Circle with the following properties: List item ‚?Ę An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
help please?
help please?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
help please?
help please?  Define a class named Circle with the following properties: List item An integer data field named radius with protected access modifier, and a String data field named colour with private access modifier. Both
java please please help
java please please help  Dear Friends plz help me to complete this program import java.util.*; public class StringDemo { static...[] to HashMap so that i can seperate key and value using Map.Entry.Please help me!  
Please Help
Please Help  How do I create an attribute that represents the following: A grayscale ‚??color‚?? value, that is, an integer between 0 and 255 inclusive, where 0 corresponds to the darkest black and 255 to the brightest white
Please Help
Please Help  How do I create an attribute that represents the following: A grayscale 'color' value, that is, an integer between 0 and 255 inclusive, where 0 corresponds to the darkest black and 255 to the brightest white
 

 

 

DMCA.com