Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA LANGUAGE Your program is tasked with obtaining the top three scores from a

ID: 3916165 • Letter: J

Question

JAVA LANGUAGE

Your program is tasked with obtaining the top three scores from a race (console IO), and manipulating this data. The scores provided will not necessarily be in order, but your program will order the race statistics, describe the overlap of the race scores, and finally report on the average and range of the racers.

Work Items (1) Submit only your java program (“.java” text file) via the website’s dropbox. (2) Prepare for the first Midterm by reviewing the topics listed in the syllabus for the first test.

Introduction – 3 Inputs, 1 Output Your task is to write a program that collects three pieces of user input and provides a summary (output) to the console window. Your program will ask the user for three individual race times that correspond the first, second, and third top race scores. Note that these won’t be in order…this is the first thing your program should do. If you decompose your tasks into smaller chunks, attacking a larger program becomes more manageable. If you mentally divided up the tasks before you, would you have decided on the following “work items”, “chunks”, or “modules”?

Data Input

a. Ask the user for three race scores (in no particular order)

Ordering Data

b. Order the race scores by first, second, and third place.

Data Analysis & Output

c. Determine how many racers tied for first, second or third place(overlap). d. Calculate the range of the race scores (slowest-fastest (abs value of this)) e. Calculate the average of the race times.

Note that if two racers tied for first, you should describe this in console output in step 3c above. Here is a sample execution from the console:

Enter the race scores (in seconds) 85.6 120.5 90.2

First place : 85.6 Second place : 90.2 Third place : 120.5

None of these racers shared first, second, or third place. (overlap) The range of the race scores : 34.9 The average : 98.76

For added challenge, try using JOptionPane.showInputDialog or JOptionPane.showMessageDialog() instead of the Scanner class.

Program Requirements There exists many ways to build this code, but the requirements stay the same. Your software should:

(1) Prompt the user for three numbers (ints? doubles?), which represent the three racer’s times. (2) Reorder the three scores into ascending order (least to greatest). a. Remember that, to swap two variables requires a third, temporary variable (see text for more information) b. See the Math Classes set of operations (min() and max() may help here) (3) Output the race scores in order (4) Describe the overlap: a. All tied for first b. Some tied for first c. None tied for first (5) Do step (4) for second and third places (6) Output the range of the race scores (7) Output the average of the race scores

Hints

• Don’t wait till the last minute to get started or get help from the instructor. • Use comments in your code. • Consider using Boolean variables such as “doTwoMatch” or “allThreeMatch” • Which data type should you use for the scores? • Be sure you know how to swap two variables values using a third temporary variable • Be sure your software doesn’t have logic pitfalls. o You can identify these by testing your code and sampling its input, execution, and output. o Once located you can correct these problems by restructuring your branching logic

Explanation / Answer

I've written the code using scanner class. However, you can use JOptionPane.showInputDialog by using the following block of code:

String score1 = JOptionPane.showInputDialog("First score ");
String score2 = JOptionPane.showInputDialog("Second score ");
String score3 = JOptionPane.showInputDialog("Third score ");
int s1 = Integer.parseInt( score1 );
int s2 = Integer.parseInt( score2 );
int s3 = Integer.parseInt( score3 );

The final code is :


import java.lang.Math; // headers MUST be above the first class
import java.util.Scanner;

public class race
{
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the scores");
int s1 = sc.nextInt();
int s2 = sc.nextInt();
int s3 = sc.nextInt();
int max, min;
int f1=0; //flag for sharing first place
int f2=0; //flag for sharing second place
  
if(s1>=s2 && s1>=s3)
{
max=s1;
if(s1==s2 || s1==s3)
{
f1=1;
System.out.println("First place:"+s1);
if(s1==s2)
min=s3;
else
min=s2;
}
  
else
{
System.out.println("First place:" + s1);
if(s2>=s3)
{
if(s2==s3)
f2=1;
System.out.println("Second place:" + s2);
min=s3;

}
else
{
System.out.println("Second place:" + s3);
min=s2;
}
}
}
else if(s2>=s3)
{
max=s2;
if(s2==s1 || s2==s3)
{
f1=1;
System.out.println("First place:"+s2);
if(s2==s1)
min=s3;
else
min=s1;
}
  
else
{
System.out.println("First place:" + s2);
if(s1>=s3)
{
if(s1==s3)
f2=1;
System.out.println("Second place:" + s1);
min=s3;

}
else
{
System.out.println("Second place:" + s3);
min=s1;
}
}
}
else
{
max=s3;
System.out.println("First place:" + s3);
if(s1>=s2)
{
if(s1==s2)
f2=1;
System.out.println("Second place:" + s1);
min=s2;

}
else
{
System.out.println("Second place:" + s3);
min=s1;
}
  
}
  
if(f2==0)
{
if(f1==0)
System.out.println("Third place:" + min);
else
System.out.println("Second place:" + min);
}

System.out.println("Range=" + (max-min));
if(f1==1)
System.out.println("Tied at first place");
if(f2==1)
System.out.println("Tied at second place");
if(f1==0 && f2==0)
System.out.println("None of these racers shared first, second, or third place");

System.out.println("Average=" + ((s1+s2+s3)/3));
  
}
}