/** * Title: Chapter 19, "Java Data Structures" * Description: Examples * Copyright: Copyright (c) 2000 * Company: Armstrong Atlantic State University * @author Y. Daniel Liang * @version 1.0 */ package chapter19; import java.util.*; import chapter2.MyInput; public class AssignGradeUsingStack { /**Main method*/ public static void main(String[] args) { Stack scoreStack = new Stack(); // Stack to hold scores double best = 0; // The best score char grade; // The grade // Read scores and find the best score System.out.println("Please enter scores. " + "A negative score terminates input."); do { System.out.print("Please enter a new score: "); double score = MyInput.readDouble(); if (score < 0) break; // Add the score into the Stack scoreStack.push(new Double(score)); // Find the best score if (score > best) best = score; } while (true); System.out.println("There are total " + scoreStack.size() + " students "); int i = scoreStack.size(); // Assign and display grades while (!scoreStack.isEmpty()) { // Retrieve an element from the Stack Double doubleObject = (Double)(scoreStack.pop()); // Get the score double score = doubleObject.doubleValue(); if (score >= best - 10) grade = 'A'; else if (score >= best - 20) grade = 'B'; else if (score >= best - 30) grade = 'C'; else if (score >= best - 40) grade = 'D'; else grade = 'F'; System.out.println("Student " + i-- + " score is " + score + " and grade is " + grade); } } }