Create an array of 5 doubles...

Create an array of 5 doubles...

Part one In a Java program, create an array of 5 doubles. Have the user input values into these array items inside a loop using JOptionPane. Using the loop, calculate the sum and average of the 5 numbers and output this using JOptionPane.

Part two Same as above, only have a second loop (enhanced for loop) do the summing to calculate the average.

Part three Write a simple program that will have an integer division inside a try block followed by a catch block with an appropriate argument type. Inside the catch block, output a message saying that division by zero is not allowed. Run this code with and without the denominator being zero. When the denominator is not zero, output a message displaying the calculated ratio.

View Answers

May 22, 2012 at 7:33 AM

import javax.swing.JOptionPane;

public class FiveDoubles 
{
    /**
     * @param args
     */
    public static void main(String[] args) 
    {
            String input;
            double data[] = new double[5];  
            double sum = 0, average;
            for ( int i = 0; i < data.length; i++ )
            {
                input = JOptionPane.showInputDialog("INPUT A NUMBER");
                data[i] = Double.parseDouble(input);
            }
                for ( int j = 0; j < data.length; j++)
                    sum += data[j];
                average = sum/data.length;
                String output = "Value/n";
                for (int k = 0; k < data.length; k++)
                    output += data[k] + "\n";
                JTextArea outputArea = new JTextArea();
                outputArea.setText(output);
                JOptionPane.showmessageDialog(null, outputArea, "SumAverage",
                        JOptionPane.INFORMATION_MESSAGE);
                System.exit(0);
        }
    }









Related Tutorials/Questions & Answers:
Create an array of 5 doubles...
Create an array of 5 doubles...  Part one In a Java program, create an array of 5 doubles. Have the user input values into these array items inside a loop using JOptionPane. Using the loop, calculate the sum and average of the 5
Create Array ??
Create Array ??   Question in e-clips program: Part 1. Array Implementation -Create a class called â??PhoneEntryâ?? which can be used to store the name and number of ONE client. -Create a class called â??MyArrayListâ?? which
Advertisements
how to create array in r
how to create array in r  Hi, In R Programming how array is created? how to create array in r? Explain me the process of array creation using.... You can create an array with the help of array keyword. Following is an example
create dynamic array in javascript
create dynamic array in javascript  How to create dynamic array in javascript
create associative array from array javascript
create associative array from array javascript  How to create associative array from an array in JavaScript? Please suggest. Thanks
how to create array in python
how to create array in python  Hi, how to create array in python? I want to create an array in Python and add some values to it. Thanks   Hi, You can create a new array with the code myArray = [] and after
declare a variable that will store an array of 5 objects
declare a variable that will store an array of 5 objects  Q7. Give a declaration for a variable that will 'store' an array of 5 Card objects... you   Declare Array of 5 objects in the following way: int list[]=new
How to create dynamic array in C?
How to create dynamic array in C?  How to create dynamic array in c programming language?   Dynamic Array in C Tutorial
How to create and use Array in Java?
How to create and use Array in Java?  Hi, How to create and use Array in Java? Write program to construct and use the array. Thanks   Hi, You can initialize the int array in following way: int num[] = new int[10
Create list from array in Java
Create list from array in Java  Hi, I have an array with some data. How to Create list from array in Java? Thanks   Hi, Here is example of creating of creating an array: String[] names = new String[]{"Java", "JSP
Java create new array of strings.
Java create new array of strings.  Java create new array of strings...); } } } Description: Array is container which holds fix length of any kind of data type...] name is an String type array can hold 7 values. You can also
create python array of size n
create python array of size n  Hi, I want code to create python array of size n? For example if n=10 then it should create an empty array... for you: n=10 myList = [0] * n print(myList) Above code create list with 10
ModuleNotFoundError: No module named 'doubles'
ModuleNotFoundError: No module named 'doubles'  Hi, My Python... 'doubles' How to remove the ModuleNotFoundError: No module named 'doubles... to install padas library. You can install doubles python with following command
Create array of random size
Create array of random size In this tutorial, you will learn how to create a randomly sized integer array. For this, we have declared an array and using... have create a text field to enter any index of array and another textfield
create and use an array variable in a java program
create and use an array variable in a java program  how do i write a program that will prompt the user for a list of 5 prices, once the user has entered all values , your program should compute and display the following: The sum
create a string from an array using JavaScript
create a string from an array using JavaScript  How to use "join()" to create a string from an array using JavaScript
What is the best way to create a string array in python
What is the best way to create a string array in python  Hi, I have to create string array in Python. I can do it crude way but I want to know the best of creating string array in Python? What is the best way to create a string
how to create a zip by using byte array
how to create a zip by using byte array  hi, How to convert byte array to zip by using java program.can u plz provide it...... Thanks, krishna
Python create array of size n with value 0
Python create array of size n with value 0  Hi, In python I want to create an array with n size and initialize it with 0 value. What is Python code to create array of size n with value 0? Thanks   Hi
Python create array of size n with value
Python create array of size n with value  Hi, I have to a requirement to create an array with n size and fill it with value 10. How it's possible in Python? Share me code to create array of size n with value in Python. Thanks
How to create binary search tree using an array?
How to create binary search tree using an array?  hello people, pls guide me on the topic above. i have an string array, i want to make a binary search tree based on data inside this array. the array contains names of people
Create a counter in mySQL 5 database through Java - SQL
Create a counter in mySQL 5 database through Java  Dear Editor... question regarding Java & mySQL 5 database... for 12 times. May i know how to create a counter (some sort like word search
Create a int array with the help of int buffer.
Create a int array with the help of int buffer.  In this tutorial, we will see how to create a int array with the help of int buffer. IntBuffer API...; float[] array() The array() method returns int array based
How to create a long array with the help of long buffer.
How to create a long array with the help of long buffer.  In this tutorial, we will see how to create a long array with the help of long buffer....  long[] array() The array() method returns long
Create a int buffer by wrapping an int array into a buffer.
Create a int buffer by wrapping an int array into a buffer.  In this tutorial, we will see how to create a int buffer by wrapping an int array...[] array)  The wrap(....) method wrap an existing int array and create
Create a long buffer by wrapping an long array.
Create a long buffer by wrapping an long array.  In this tutorial, we will see how to create a long buffer by wrapping an long array into a buffer...)  The wrap(....) method wrap an existing long array and create long
Create a short array with the help of short buffer.
Create a short array with the help of short buffer.  In this tutorial, we will see how to create a short array with the help of short buffer... capacity.  short[] array() The array() method returns
How to Create a ByteBuffer using Byte Array in java.
How to create a ByteBuffer using Byte Array in java.      ..., we will discuss how to creates a buffer using byte array. The ByteBuffer ... Description static ByteBuffer wrap(byte array
returning doubles with 2 decimals JAVA
returning doubles with 2 decimals JAVA  Hi all. I'm writing a program where users input the cost of an item (in the constructor), so for example, Item (string name, double cost, int qty) { ... } I have a method public double
Array
Scanner(System.in); int array[]=new int[5]; System.out.println("Enter...Array  What if i will not declare the limit index of an array, how will I declare an array and store values with it using loop?   Hi Friend
Array
Array  How do i insert elements into an array up to a limit from command prompt using SOP statement.like Enter range:3 Enter elements: 1 5 3  ...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size
Array
Array  can we create an array of size 1 lakh in java programming
in_array
in_array  in_array in php
is _array()
is _array()  is_array() in php   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
is _array()
is _array()  is _array()   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php $yes = array('Hello', 'World'); echo is_array($yes) ? 'Array
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  Hi i have array like {1,2,3,4,,5,5,6,6} then how can i display 1 time , 5 - 2times, 3- 2times thank kalins naik   Here is an example that store some integers into an array and find the occurrence of each
JavaScript array functions
JavaScript array functions We are going to describe How to create JavaScript... object hold the different element defined by array index from [0] -[5].The for loop...;Apricot" array[5] = "Carrot" document.write("<b>Element
Array's
Array's  I have to finish an "order page". that checks 2 dropdown boxes and a set of radio buttons and put the results into a set of textboxes, 1 line at a time, then calculate a total. I know I need to create an array, but I'm
Hibernate 5 Hello World
Hibernate 5 Hello World Example In this section we will see how to create... and understand how to create session factory in this version of Hibernate. In Hibernate 5...-factory> </hibernate-configuration> Step 5: Create the utility class
Array
1 to 5. If the user types 2, your application should assign a Normal Seat... reserved. Use a one-dimensional array of primitive type Boolean to represent the seating chart of the cinema theater. Initialize all the elements of the array
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
Array
Array  is it possible to define array like this..? int[] intArray = new int[] {4,5,6,7,8}; Explain...?   Yes, you can. Java Initialize Array
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  WAP in java to store 6 element in array P and 4 element in array Q. Produce the third arra y R containing all element from p & q
array
array  array memory allocation is dynamic or static in java   Java Arrays have dynamic memory allocation
Array
Array  i having problem how to make algorithm for [a][5] [a][10] [b][15] [b][10] [c][5] [c][5] . . . . sum of [a] = 15 [b] = 25 [c] = 10

Ads