Solved

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

Posted on 2006-10-30
9
219 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:kannan_ekanath
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:duta
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
json format text only 4 65
JDeveloper 12c for 32 bit 4 34
micro services vs rest web services 16 45
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now