Solved

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

Posted on 2006-10-30
9
226 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

776 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