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

JAVA JAVA JAVA . I need to convert a string input to int array, for example...
JAVA JAVA JAVA . I need to convert a string input to int array, for example if user enters 12 / 27 / 2020 , I want to store each value in a separate array and add them afterwards.
I need to draw a cylinder in java with user input, and I can't seem to...
I need to draw a cylinder in java with user input, and I can't seem to get my lines to line up with my ovals correctly from the users input... I know I will have to either add or subtract part of the radius or height but I'm just not getting it right, here is how I'm looking to do it.            g.drawOval(80, 110, radius, height);            g.drawLine(?, ?, ?, ?); g.drawLine(?, ?, ?, ?);   ...
JAVA) I need to get 10 integer numbers from the user. Then I need to find...
JAVA) I need to get 10 integer numbers from the user. Then I need to find sum of odd numbers, sum of even numbers, the lowest number of all numbers, the highest number of all numbers, and the average of all numbers( use double, with the two digit decimal) process; loopCount = 1 While LoopCount <= 10 Read number from the keyboard If odd, add to the total of all odd numbers If even, add to the total of all...
In Java, I need a program that will ask a user to input how many tests...
In Java, I need a program that will ask a user to input how many tests they want the average of. For example, It needs to ask the user how many tests would you like the average of? When the user enters the amount of tests they want the average score of, the program needs to give them that many test scores to input. Then with the average, determine what their grade is, if 90-100=A if 80-90 = B etc....
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...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
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...
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<iostream> #include<string> #include<exception> #include <cstdlib> #include<stdio.h> #include<map> #include <cctype> #include<Windows.h> #define INPUT_SIZE 8 using namespace std;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT