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

Write program that finds the largest, second largest and third largest of the nu

ID: 3836085 • Letter: W

Question

Write program that finds the largest, second
largest and third largest of the numbers from a given file named Data.txt. A skeleton of the program, as
well as the code for defining a scanner object that reads from Data.txt, is provided in the file named
Top3.java. Download Top3.java and Data.txt to the same folder.
Write a program that finds the largest, second largest and third largest of the numbers contained in the
file Data.txt. Provide your code after the comment “Your code here” in Top3.java. If your code is
correct, when executed it should output
The largest is 99
The second largest is 96
The third largest is 93

must be written in java programming. the software that im using is dr. java

Explanation / Answer

Java Program :

import java.util.*;
import java.io.*;

class Top3{

   public static void main(String args[]) throws Exception{
       //Initializing scanner obj to read from file
       Scanner obj = new Scanner(new File("Data.txt"));
       //Initializing array list to store the elements
       ArrayList<Integer> al = new ArrayList();
       //Read the integers from file and store in a list
       while(obj.hasNextInt()){
           al.add(obj.nextInt());
       }
       //Using the sort method present in the Collections class to sort the data present in the list
       Collections.sort(al);
       //Printing first 3 integers in the sorted list
       System.out.println(al.get(0));
       System.out.println(al.get(1));
       System.out.println(al.get(2));
   }
}

OUTPUT :

3

5

7

Data.txt

4 9 3 11 17 7 22 5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote