Statistics V2 Java Exercise - Java Programming Course

 Exercise

Statistics V2

 Objetive

Write a java statistical program which will allow the user to:

- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program

These options must appear as a menu. Each option will be chosen by a number or a letter.

Create a statistical program which will allow the user to:

- Add new data
- See all data entered
- Find an item, to see whether it has been entered or not
- View a summary of statistics: amount of data, sum, average, maximum, minimum
- Exit the program

These options must appear as a menu. Each option will be chosen by a number or a letter.

The program must reserve space for a maximum of 1000 data, but keep count of how many data actually exist.

 Example Code

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		float[] numbers = new float[1000];
		int count = 0;

		float max = 0.0f, min = 0.0f, total = 0.0f, searchNumber = 0.0f;
		boolean found;

		int option = 0;
		do
		{
			System.out.println("1. Add");
			System.out.println("2. Show");
			System.out.println("3. Search");
			System.out.println("4. Statistics");
			System.out.println("5. Exit");

			option = Integer.parseInt(new Scanner(System.in).nextLine());
			if (option != 5)
			{
				switch (option)
				{
					case 1: // Add

						System.out.println("Enter a number: ");
						numbers[count] = Float.parseFloat(new Scanner(System.in).nextLine());

						max = numbers[count];
						min = numbers[count];

						total += numbers[count];

						count++;

						if (max < numbers[count])
						{
							max = numbers[count];
						}

						if (min > numbers[count])
						{
							min = numbers[count];
						}

						break;
					case 2: // Show

						for (int i = 0; i < count; i++)
						{
							System.out.printf("%1$s " + "\r\n", numbers[i]);
						}

						break;
					case 3: // Search

						System.out.println("Enter a number for search: ");
						searchNumber = Float.parseFloat(new Scanner(System.in).nextLine());

						for (int i = 0; i < count; i++)
						{
							if (numbers[i] == searchNumber)
							{
								found = true;
							}
						}

						if (found)
						{
							System.out.printf("Number %1$s found a amount of %2$s " + "\r\n", numbers[i]);
						}
						else
						{
							System.out.println("Not found");
							found = false;
						}

						break;
					case 4: // Statistics

						System.out.printf("Total data: %1$s" + "\r\n", count + 1);
						System.out.printf("Sum: %1$s" + "\r\n", total);
						System.out.printf("Average: %1$s" + "\r\n", total / (count + 1));
						System.out.printf("Min number: %1$s" + "\r\n", min);
						System.out.printf("Max number: %1$s" + "\r\n", max);

						break;
					default:

						System.out.println("Error, option 1-5");
						break;
				}
			}
		} while (option != 5);
	}
}

More Java Exercises of Arrays, Structures and Strings

 Reverse array
Write a Java program to ask the user for 5 numbers, store them in an array and show them in reverse order....
 Search in array
Write a Java program that says if a data belongs in a list that was previously created. The steps to take are: - Ask the user how many data will h...
 Array of even numbers
Write a Java program to ask the user for 10 integer numbers and display the even ones....
 Array of positive and negative numbers
Write a Java program to ask the user for 10 real numbers and display the average of the positive ones and the average of the negative ones....
 Many numbers and sum
Write a Java program which asks the user for several numbers (until he enters "end" and displays their sum). When the execution is going to end, it mu...
 Two dimensional array
Write a Java program to ask the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and display the average for each group....
 Struct
Write a Java Struct to store data of 2D points. The fields for each point will be: x coordinate (short) y coordinate (short) r (red colour, byte)...
 Array of struct
Write a Java program that expand the previous exercise (struct point), so that up to 1.000 points can be stored, using an "array of struct". Ask the u...
 Array of struct and menu
Write a Java program that expand the previous exercise (array of points), so that it displays a menu, in which the user can choose to: - Add data f...
 Books database
Create a small database, which will be used to store data about books. For a certain book, we want to keep the following information: Title Author...
 Triangle V2
Write a Java program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length:...
 Rectangle V3
Write a Java program to ask the user for his/her name and a size, and display a hollow rectangle with it: Enter your name: Yo Enter size: 4 YoYoY...
 Centered triangle
Write a Java program that Display a centered triangle from a string entered by the user: __a__ _uan_ Juan...
 Cities database
Create a database to store information about cities. In a first approach, we will store only the name of each city and the number of inhabitants, a...
 Banner
Write a Java program to imitate the basic Unix SysV "banner" utility, able to display big texts....
 Triangle right side
Write a Java program that asks the user for a string and displays a right-aligned triangle: ____n ___an __uan Juan...
 Strings manipulation
Write a Java program that asks the user for a string and: - Replace all lowercase A by uppercase A, except if they are preceded with a space - Dis...
 Nested structs
Write a Java Struct to store two data for a person: name and date of birth. The date of birth must be another struct consisting on day, month ...
 Sort data
Write a Java program to ask the user for 10 integer numbers (from -1000 to 1000), sort them and display them sorted....
 Two dimensional array as buffer for screen
Write a Java program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions and displays t...
 Two dimensional array 2: circunference on screen
Write a Java program that declares creates a 70x20 two-dimensional array of characters, "draws" a circumference or radius 8 inside it, and displays it...
 Computer programs
Write a Java program that can store up to 1000 records of computer programs. For each program, you must keep the following data: * Name * Category...
 Exercise tasks
Write a Java program that can store up to 2000 "to-do tasks". For each task, it must keep the following data: • Date (a set of 3 data: day, month a...
 Household accounts
Write a Java program in Java that can store up to 10000 costs and revenues, to create a small domestic accounting system. For each expense (or income)...


Juan A. Ripoll - Programming Tutorials and Courses © 2024 All rights reserved.  Legal Conditions.