Question

Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to...

Using NetBeans, create a Java program called Average.java. The program should use the Scanner class to get 4 integers from the user and store them in variables. The program should calculate the average of the 6 numbers as a floating point. Output all of the original input and the calculated average in the Command window. The code is expected to be commented and user-friendly.

Homework Answers

Answer #1

The average of the numbers can be calculated as the sum of all numbers divided by the number of elements.

In the question, it is mentioned that the program should find an average of 4 numbers in one place whereas in the third line it is written to find an average of 6 numbers.

I have taken an array of 6 elements to store values and a variable to store the average value of the 6 numbers.

The program outputs the average values as a floating-point number.

If you want to find the average of 4 elements you can change the array size to 4 and for loop to run for 4 times.

Code:-

package average;

import java.util.Scanner; //Scanner class

public class Average {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); //Scanner class object to take input
        double arr[] = new double[6]; //array of double variables to store value
        double average = 0; //variable to store average value
        
        for(int i=0;i<6;i++){ //loop to take input
            arr[i] = sc.nextDouble(); // taking input
        }
        
        for(int i=0;i<6;i++){ // loop to calculate sum
            average = average + arr[i]; //calculating sum of 6 variables
        }
        average /= 6; //dividing by 6 to get average
        
        System.out.println(average); // printing the result
    }
}

Output:-

The code is well commented and user-friendly.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
5) Create a Java program using Scanner: Write a program where you will enter the grade...
5) Create a Java program using Scanner: Write a program where you will enter the grade score for 5 classes, then it will display total points and average and it will display the grade based on percentage. Steps: 1) Declare variable integer for English, Chemistry, Java Programming, Physics and Math 2) Declare variable total and percentage 3) Create Scanner object and prompt the user to enter grades for each class and input grades after each class 4) Calculate total of...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
JAVA IDE NETBEANS Create a program that will prompt the user to enter 20 numbers using...
JAVA IDE NETBEANS Create a program that will prompt the user to enter 20 numbers using any looping statement. Store it in a 2-dimensional array. Display all the numbers in a table format. Determine which numbers are odd and which numbers are even (include their array index) need some help with codes
Create a simple addition calculator in Java. The program should prompt the user to enter 2...
Create a simple addition calculator in Java. The program should prompt the user to enter 2 integers, then adds the numbers and prints the result. Make sure the program includes appropriate exception handling in case the user does not enter appropriate integer values.
JAVA PROGRAMMING: Create a program which will calculate and output as formatted output the kilometers or...
JAVA PROGRAMMING: Create a program which will calculate and output as formatted output the kilometers or miles, given as keyboard input the miles or kilometers, as inputted as a double number. That is, the user has a choice what to calculate. The original value inputted should be displayed along with the calculated miles or kilometers. Demonstrate escape sequences, a while loop or a do…while loop, and named constants as needed,  should be implemented. Finally, input, processing, and output must be implemented...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
In java : Write a program that will provide important statistics for the grades in a...
In java : Write a program that will provide important statistics for the grades in a class. The program will utilize a for-loop to read ten floating-point grades from user input. Include code to prevent an endless loop. Ask the user to enter the values, then print the following data: Average Maximum Minimum
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
Create a text file called “Trivia.txt” that contains the contents of a trivia game, where the...
Create a text file called “Trivia.txt” that contains the contents of a trivia game, where the questions and answers are on separate lines. Example: How many players are on a Baseball team? Nine What is the name of Batmans butler? Alfred Hg is the chemical symbol for what element? Mercury Create a program that reads the trivia questions from this text file, prints them out to netbeans and asks the user to input an answer. Compare the users answer to...
4) Write a Java program using Conditions: Write a program where it will ask user to...
4) Write a Java program using Conditions: Write a program where it will ask user to enter a number and after that it will give you answer how many digits that number has. Steps: 1) Create Scanner object and prompt user to enter the number and declare variable integer for input 2) Use if else condition with && condition where you will specify the digits of numbers by writing yourself the digit number that should display: Example(num<100 && num>=1), and...