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

Programming part Write a Java program that will read a sequence of positive real

ID: 3546296 • Letter: P

Question

Programming part

Write a Java program that will read a sequence of positive real numbers entered by the user and then:

a) your program will print the same numbers in sorted order from smallest to largest. The user will input a zero to mark the end of the input. You will need to use an array to store the positive real numbers entered by the user. Don't include zero in your array. Your program needs to verify whether the user input is valid or not. Only positive real numbers are valid numbers for this program.

Next, your program will need to complete the following two tasks:
     b) your program will print the sum and average of all the positive numbers entered by the user;  
     c) your program will reverse the order of the positive numbers entered by the user in the array and print out the numbers in the revsered array.Assume that at most 20 positive numbers will be entered.

Explanation / Answer

import java.util.Scanner;

public class store {
    int arr[] = new int[100];
    int count;
    int sum;
   
    void printarray(){
        for(int i=0;i<count;i++){
            for(int j=i+1;j<count;j++){
                if(arr[i]>arr[j]){
                    int x = arr[i];
                    arr[i]=arr[j];
                    arr[j]=x;
                }
            }
        }
       
        System.out.println("Printing array in sorted order");
        for(int i=0;i<count;i++){
            System.out.print(arr[i] +" ");
            sum =sum + arr[i];
        }
       
        System.out.println(" sum = "+sum + "and Avg=" + sum/count);
       
        System.out.println("Printing array in reverse order order");
        for(int i=count-1;i>=0;i--){
            System.out.print(arr[i] +" ");
        }
    }
    public static void main(String[] args) {
        store obj = new store();
        Scanner in = new Scanner(System.in);
        while(true){
            int i = in.nextInt();
            if(i>0){
                obj.arr[obj.count] = i;
                obj.count++;
            }else if(i == 0){
                break ;
            }
        }
       
        obj.printarray();

    }

}