Solved

Java5: A simple code to find max score out of the user's inputs

Posted on 2006-10-30
9
228 Views
Last Modified: 2010-03-31
Hi experts:

I am trying to write a simple code to get exam multiple exam scores from a student, and to find the best
score.  The problem is that I do not know how many exam scores the student may enter.
As soon as the student enters a sentinal (-1), the program is to quit and find the best score among the exam scores that the student entered  until  he entered  -1.

Here is my incomplete code which obviously is not working.

Hope you experts may kindly help me again!

_________________ Here is my code ______________________

import javax.swing.JOptionPane;

public class findBestScore
{
 
  public static void main (String[] args)
  {
    FindBestScore duta = new findBestScore ();
   
    duta.findBest ();
   
    System.exit (0);
  }
 
 
  public int[] getScore ()
  {
       
    int [] scores = new int [count];
     
    for (int i; i < count; i++ )  
     {
      while ( scores[i] != - 1) // enter -1 to quit
       {
         scores [i] = getInput ("Enter exam scores " + (i + 1) );
       }
    }
    return scores;
  }
 
 
 
 public int getInput (String msg)
 {
   int num = Integer.parseInt (JOptionPane.showInputDialog (msg) );
   return num;
 }
                                   
  public void display (String msg)
  {
    JOptionPane.showMessageDialog (null, msg);
  }
 
  public int findBest ( )
  {
    int [] scores = getScores ();
    int i = 0;
    int max = 0;
    for ( i = 0; i < scores.length; i ++ )
    {
       max = scores[0];
     
      if ( scores[i] > max )
      {
        max = scores [i];
      }
     
    }
    return max;
    display ("The maximum score is " + max);
  }
   
}                
0
Comment
Question by:duta
  • 6
  • 3
9 Comments
 

Author Comment

by:duta
ID: 17839888
Here is my latest code which is working:


My question is still there:  In my latest code, I asked the student "how many scores" which he is going to enter.

Sometimes, I may need to ask students to enter any number of scores he may feel like entering, and to find the best score among the inputs. In other words, I may not know how many scores students may enter, and I may not ask how mahy scores they are going to enter.

In that situation, I need to modify my current code.  

Thanks a lot for your kind help as always!

duta



______________ Here is my latest code ___________________

import javax.swing.JOptionPane;

public class findMax
{
 
  public static void main (String[] args)
  {
    findMax duta = new findMax ();
   
    duta.findBest ();
   
    System.exit (0);
  }
 
 
  public int[] getScore ()
  {
    int count     = getInput ("How many scores?");    //   ??????????????????????
    int [] scores = new int [count];                         //  ???????????????????????
   
   {
    for (int i=0; i < count; i++ )  
     {
   
       {
         scores[i] = getInput ("Enter exam score " + (i + 1) );
         if (scores[i] == -1)
             break;
       }
    }
   }
    return scores;
  }
 
 
 
 public int getInput (String msg)
 {
   int num = Integer.parseInt (JOptionPane.showInputDialog (msg) );
   return num;
 }
                                   
  public void display (String msg)
  {
    JOptionPane.showMessageDialog (null, msg);
  }
 
  public void findBest ( )
  {
    int [] scores = getScore ();
    int i = 0;
    int best = 0;
    for ( i = 0; i < scores.length; i ++ )
    {
       best = scores[0];
     
      if ( scores[i] > best )
      {
        best = scores [i];
      }
      best = best;
    }
   
    display ("The maximum score is " + best);
  }
   
}                                    
   
     
 
0
 
LVL 5

Expert Comment

by:kannan_ekanath
ID: 17840294
Duta!
You need to close the previous questions :) before I think you are reliable enough to spend time :) or atleast say if it wasnt useful :)

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_22000302.html
0
 

Author Comment

by:duta
ID: 17842234
Dear kannan:
Sorry for my procrastination.  It is my fault. I took your tip as acceptable solution.

Now is your turn to help me again :)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:kannan_ekanath
ID: 17842325
Duta,
Kindly note that I am not *forcing you* into accepting my solution :). However you need to write a while loop to get outside your scores. I am showing you the getScores() method *only* to show you how it would look. I am sure you should be able to take it from there on.

       public List getScore() {
            List scores = new ArrayList();
            int score = 0, index = 1;
            while((score = getInput("Enter exam score [" + (index++ ) + "] (-1 to finish)")) != -1) {
                  scores.add(score);
            }
            return scores;
      }

Now, you have a list, just call
Collections.sort(scores);
//print scores.get(scores.size()-1) ; //this being the last element

The crux is the while loop to terminate until he pressed -1. Since this sounds like homework, kindly dont ask for full code ;)
0
 

Author Comment

by:duta
ID: 17843683
Dear kannan:

Thank you so much for giving me tons of priceless tips and teaching me how to use this site properly :)

Every tip that you have given me  has tremendously helped me to hang on and to continue to learn things that sometimes looked too much for me (for my intelligence).

I will do my best to fix it using your valuable tip.

Thanks again!

duta
0
 

Author Comment

by:duta
ID: 17843720
Dear kannan:

No, it is not a homework.  I am trying to learn myself by trying this code and that.

Thanks a lot for your constant help :)
0
 

Author Comment

by:duta
ID: 17843848
TO: kannan:

I have never used List and have no idea about how to call it.

This is not a homework or anything like that. If you check my history of questions, they cover almost all subjects, and you may know that my questions have little to do with any homework.

By the way,  I just wonder whether you may show me how to use the last part of your tip:

Now, you have a list, just call
Collections.sort(scores);
//print scores.get(scores.size()-1) ; //this being the last element

Without your further help, I may have to spend many more days trying to figure it out :)

Thanks again!
0
 
LVL 5

Accepted Solution

by:
kannan_ekanath earned 500 total points
ID: 17845095
List is a collection. You can read any collection tutorial from the Suns standard website http://java.sun.com/docs/books/tutorial/collections/index.html

The following is a working code,

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class FindMax extends JFrame {
      public static void main(String[] args) {
            FindMax duta = new FindMax();
            duta.findBest();
            System.exit(0);
      }

      public List getScore() {
        List scores = new ArrayList();
        Integer score;
            int index = 1;
        while((score = getInput("Enter exam score [" + (index++ ) + "] (-1 to finish)")).intValue() != -1) {
             scores.add(score);
        }
        return scores;
   }

      public Integer getInput(String msg) {
            return Integer.valueOf(JOptionPane.showInputDialog(msg));
      }

      public void display(String msg) {
            JOptionPane.showMessageDialog(null, msg);
      }

      public void findBest() {
            List list = getScore();
            Collections.sort(list);
            display("The maximum score is " + list.get(list.size() - 1));
      }
}

List is one type of collection. As the name implies, it is ordered :)
0
 

Author Comment

by:duta
ID: 17845637
TO: kannan:

Thank you so much for helping me out with such petience.

It worked great. I am going to try your tip over and again and make it my own.

Thanks a lot. Hope you may come back again to help me with others.

duta
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ForLoop Example 3 52
Oracle SQL syntax check  without executing 6 74
how to install java on RHEL image on EC2 4 39
ejb on wildfly 5 30
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question