Write an application that will allow a user to enter any num

Write an application that will allow a user to enter any number of double values up to 10. The user should enter 99999 to quit entering numbers. Display an error message if the user quits without entering any numbers; otherwise, display each entered value and its distance from the average. Save the file as DistanceFromAverage.java. Don\'t forget to create the application/project DistanceFromAverageTest.java Class that has the main method and an object to use the DistanceFromAverage class.

Solution

//include pckages
import java.util.*;

//driver class to trigger DistanceFromAverage
public class DistanceFromAverageTest{

     public static void main(String []args){
         //create object of DistanceFromAverage and calavg
        DistanceFromAverage test=new DistanceFromAverage();
        test.calAvg();
     }
}

//
class DistanceFromAverage
{
    //declare variables instance
    final int MAX_SIZE=10;
    double nos[]=new double[10];
  
    //function which pormpts user for nos
    public boolean read()
    {
        //declare scanner oject
        Scanner read=new Scanner(System.in);
        boolean flag=true;
        int size=-1;
        //loop till user enters 99999
        do
        {
            double tmp;
            //prompt for nos from user
            System.out.print(\"Enter no: \");
            tmp=read.nextDouble();
            //if user enters 99999,quit
            if(tmp==99999)
            {
                flag=false;
                if(size==-1)
                return false;
            }
            //else fetch nos upto 10
            else
            {
                size++;
                nos[size]=tmp;
                if(size==MAX_SIZE-1)
                flag=false;
            }
        }while(flag);
        return true;
    }
    //function to cal average
    public void calAvg()
    {
        //if nos read,display eror message
        if(!read())
        System.out.println(\"No numbers entered\");
        //cal average and distance
        else
        {
        double avg=0;
        //calcualte avergae of nos
        for(int i=0;i<nos.length;i++)
        {
            avg+=nos[i];
        }
        avg/=nos.length;
      
        //display average
        System.out.println(\"Average: \"+avg);
        //calculate distance from avg and display
        System.out.println(\"Number\\tDistanceFromAverage\");
        for(int i=0;i<nos.length;i++)
        {
            System.out.println(nos[i] +\" \\t\\t\"+(avg-nos[i]));
        }
    }
    }
  
}

 Write an application that will allow a user to enter any number of double values up to 10. The user should enter 99999 to quit entering numbers. Display an err
 Write an application that will allow a user to enter any number of double values up to 10. The user should enter 99999 to quit entering numbers. Display an err

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site