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 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;
}
}
}
}
View Answers
Ads
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>
<
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 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
please help
please help
please send me the
code of dynamic stack in java without using the built in functions
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?
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
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
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 Problem: Write a program that does addition, subtraction, multiplication and division operation on real numbers. The operation started with a user entered 2 numbers and click one of the operation buttons
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
Please help
Please help Problem: Write a program that does addition, subtraction, multiplication and division operation on real numbers. The operation started with a user entered 2 numbers and click one of the operation buttons
urgent...pleAse help me.....please!
urgent...
pleAse help me.....
please!
please help me urgent! how can i do dictionary with the use of array
code in java, where i will type the word then the corresponding meaning for that word will appear...thanks
please help
please help public class AllContact extends javax.swing.JFrame {
/** Creates new form AllContact */
public AllContact() {
initComponents();
try{
Connection1 con =new Connection1();
Connection conobj
Can any one please help me in this ,,,,,,, need java code for this ,,,,please anyone
Can any one
please help me in this ,,,,,,, need java
code for this ,,,,
please anyone The Airport Valet Parking Company (AVP) is a company which provides a convenient medium cost parking solution to users of a local airport. Key
please help me.
please help me.
Please send me a
code of template in opencms and its procedure.so i can implement the
code.
Thanks
trinath