?
Solved

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

Posted on 2006-10-30
9
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

800 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