Friday, October 26, 2007

FindTwoHighestScore

I am such a code monkey.

It's been, what, a few weeks since I got Y. Daniel Liang's superb Introduction to Java Programming, Sixth Edition (companion site here).

I am so excited to tell the rest of the world that I was able to solve a particularly hard Programming Exercise (for me - I'm still a newbie at this).

The rest of the post will contain the question, and my code answer. If you have questions, comments or any suggestions in making the code more compact, go ahead and post them!

4.9* (Finding the two highest scores) Write a program that prompts the user to enter the number of students and each student's name and score, and finally displays the student with the highest score and the student with the second-highest score.
FindTwoHighestScore.java
==

import javax.swing.*;

public class FindTwoHighestScore {
public static void main(String[] args) {
String numOfStudentsString = JOptionPane.showInputDialog("Enter the number of students:");
int numOfStudents = Integer.parseInt(numOfStudentsString);


switch (numOfStudents) {
case 0: {
String error0String = "You must enter " +
"a number greater than 2 for this to work!";
JOptionPane.showMessageDialog(null, error0String);
System.exit(1);}
case 1: {
String student1 = JOptionPane.showInputDialog("Enter a student name:");
double score1 = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));
System.out.println("Top student:");
System.out.println(student1 + "'s score is " + score1);
System.exit(0);}
case 2: {
String student1 = JOptionPane.showInputDialog("Enter a student name:");
double score1 = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));
String student2 = JOptionPane.showInputDialog("Enter a student name:");
double score2 = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));
if (score1 > score2) {
System.out.println("The highest score is " + score1 +
" by " + student1);
System.out.println("The next highest score is " + score2 +
" by " + student2);} else {
System.out.println("The highest score is " + score2 +
" by " + student2);
System.out.println("The next highest score is " + score1 +
" by " + student1);}
System.exit(0);}
default: {
String student1 = JOptionPane.showInputDialog("Enter a student name:");
double score1 = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));
String student2 = JOptionPane.showInputDialog("Enter a student name:");
double score2 = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));

for (int i = 0; i < numOfStudents - 2; i++) {
String student = JOptionPane.showInputDialog("Enter a student name:");
double score = Double.parseDouble(JOptionPane.showInputDialog("Enter a student score:"));

if (score1 > score) {
if (score1 > score2) {
if (score2 < score) {
score2 = score;
student2 = student;}
}
else {
double scoreAlt = score1;
score1 = score2;
score2 = scoreAlt;
String studentAlt = student1;
student1 = student2;
student2 = studentAlt;}
}
else {
if (score1 > score2) {
score2 = score1;
score1 = score;
student2 = student1;
student1 = student;}
else {
if (score2 > score) {
score1 = score2;
score2 = score;
student1 = student2;
student2 = student;}
else {
score1 = score;
student1 = student;}
}
}
}
System.out.println("The highest score is " + score1 +
" by " + student1);
System.out.println("The next highest score is " + score2 +
" by " + student2);
}
}
}
}

No comments: