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

Write a program that reads 4 numbers from a user, save the numbers in a single-d

ID: 3592505 • Letter: W

Question

Write a program that reads 4 numbers from a user, save the numbers in a single-dimensional array, and sort the saved numbers in a “descending” order.

Important Notes:

Assumed that the user makes mistakes at most once for the weight and once for the price while entering the data, i.e. he/she enters the correct value the next time right after being warned.

In Eclipse IDE, inside the newly created project HOMEWORK_3, students should create a new program, i.e. a class, named: Program_4 under a new package named PROBLEM_4 for his/her work on this problem.

Explanation / Answer

//Please see the code below:

import java.util.Scanner;


public class Program_4

{  

    public static void main(String[] args)

    {          

       int temp;
       Scanner scan = new Scanner(System.in);
        /* Creating object of linkedList */

        int []arr=new int[4];
        System.out.println("Please Enter 4 numbers");        
        for(int i=0;i<4;i++)
        {
          
           arr[i] = scan.nextInt();
        }
      
        // sort the saved numbers in a “descending” order
      
        for(int i=0 ;i<4-1;i++)
        {
           for(int j=0 ;j<4-i-1;j++)
              
           {
               if(arr[j]<arr[j+1])
               {
                   temp=arr[j];
                   arr[j]=arr[j+1];
                   arr[j+1]=temp;
               }
              
           }
          
        }
      
        System.out.println("Sorted the saved numbers in a “descending” order");
        for(int i=0;i<4;i++)
        {
           System.out.print(arr[i]+" ");
        }

      
      
    }

}

OUTPUT:

Please Enter 4 numbers
2 5 4 10
Sorted the saved numbers in a “descending” order
10 5 4 2

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