Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Statistics - Practice Exercises Java


Lesson 4:

Structures and Strings


Exercise 4.7:

Statistics


Objetive:

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.


Source 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);
	}
}
Exercisey 4.7

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.