Question

In: Computer Science

Java Programming I need an application that collects the user input numbers into an array and...

Java Programming

I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user.

The user decides when to stop inputting the numbers.

Thanks for your help!

Solutions

Expert Solution

Source Code in text format (See below images of code for indentation):

import java.util.*;

/*class definition*/

public class ArraySum

{

    /*main method*/

    public static void main(String[] args)

    {

        /*Scanner class to read input from the user*/

        Scanner read=new Scanner(System.in);

        /*variables*/

        int n,sum=0,i;

        /*read size of the array from user*/

        System.out.print("Enter the size of the array: ");

        n=read.nextInt();

        /*declaration of array*/

        int[] a=new int[n];

        /*read elements from the user*/

        System.out.println("Enter elements of array:");

        for(i=0;i<n;i++)

            a[i]=read.nextInt();

        /*display array*/

        System.out.print("The array is: ");

        for(i=0;i<n;i++)

            System.out.print(a[i]+" ");

        /*method call*/

        sum=sumArray(a);

        /*print sum*/

        System.out.print("\nSum of array elements is: "+sum);

    }

    /*method definition*/

    public static int sumArray(int[] a)

    {

        /*calculate sum*/

        int sum=0,j;

        for(j=0;j<a.length;j++)

            sum+=a[j];

        /*return sum*/

        return sum;

    }

}

Source Code:

Output:



Related Solutions

Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
*Java* Write an application that declares an array of three Listings whose contents are input by...
*Java* Write an application that declares an array of three Listings whose contents are input by the user (User inputs name and age for each listing). After the input, the listings should output in reverse order.
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write...
Java Code: Write an application that takes in user input. (Name, Age, and Salary) ------ Write an application that includes a constructor, user input, and operators.
I am creating a crop watering "simulator" in Python. I have the user input an array...
I am creating a crop watering "simulator" in Python. I have the user input an array and then must compare the placement of water and crops to determine if all the crops in the array are watered. The user will either input a "w" for water or "c" for crop when creating the array. A w cell can water 8 cells around it, including itself. My end result must determine if all the crops will be watered or not. How...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once...
JAVA Programming Implement the class DataProcess and prompt a user to enter 5 integer numbers. Once The program should output the average, largest, and smallest of 5 numbers. You must implement the methods listed below in your program. static float getAverage(int[] data) {...} static int getLargest(int[] data) {...} static int getSmallest(int[] data) {...}
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based...
java Programming Problem 1: Reverse Characters Create an application ReverseCharacters in which you use an array-based stack to read a sentence from the keyboard, and reverse the order of the letters in each word. Print the initial sentence, as well as the result of the reversal operation. Your Tasks: Using the information given in your textbook, create a class named ArrayBasedStack, that will help you solve the problem given above by providing the stack operations you need for this application....
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT