Create an array of 5 doubles...

Ads
 

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);
        }
    }

Ads









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 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
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 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 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 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
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
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
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
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
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  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
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
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  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
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 element comparison
:- Assuming size as 5. Therefore ; Array 1 be 10 , 5 , 6 Array 2 be 12 , 6 , 8 Array 3 be 15 , 4 , 9 Array 4 be 8 , 3 , 6 Array 5 be 10 , 8 , 4 Now I want to compare... Array of L , B and H) and to create new array as RESULT. Thank You In Adv
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