Practice Exercises Java - Learn to program performing exercises with Java

Practice Exercises Java

Learn to program performing exercises with Java

12 Lessons Java with the Solutions - 228 Exercises Java with the solutions
For Beginners, Intermediates and Advanceds


App Practice Exercises Java

The human knowledge belongs to the world
¡The infomation should be free!

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

C# Programming Course New

Beginners, Intermediates, Advances

This FREE C# programming course! It covers all the aspects necessary for programming in C# up to the present.

Watch this!

Java Programming Course New

Beginners, Intermediates, Advances

This FREE Java programming course! It covers all the aspects necessary for programming in Java up to the present.

Watch this!

VB.Net Programming Course New

Beginners, Intermediates, Advances

This FREE VB.Net programming course! It covers all the aspects necessary for programming in VB.Net up to the present.

Watch this!

Computer programs - Practice Exercises Java


Lesson 4:

Structures and Strings


Exercise 4.23:

Computer programs


Objetive:

Create a Java program that can store up to 1000 records of computer programs. For each program, you must keep the following data:

* Name
* Category
* Description
* Version (is a set of 3 data: version number -text-, launch month -byte- and launch year -unsigned short-)

The program should allow the user the following operations:
1 - Add data of a new program (the name must not be empty, category must not be more than 30 letters (or should be re-entered), and for the description, it will accept only the first 100 letters entered and skip the rest; the version needs no validation).
2 - Show the names of all the stored programs. Each name must appear on one line. If there are more than 20 programs, you must pause after displaying each block of 20 programs, and wait for the user to press Enter before proceeding. The user should be notified if there is no data.
3 - View all data for a certain program (from part of its name, category or description, case sensitive). If there are several programs that contain that text, the program will display all of them, separated by a blank line. The user should be notified if there are no matches found.
4 - Update a record (asking the user for the number, the program will display the previous value of each field, and the user can press Enter not to modify any of the data). He should be warned (but not asked again) if he enters an incorrect record number. It is not necessary to validate any of the fields.
5 - Delete a record, whose number will be indicated by the user. He should be warned (but not asked again) if he enters an incorrect number.
6 - Sort the data alphabetically by name.
7 - Fix redundant spaces (turn all the sequences of two or more spaces into a single space, only in the name, for all existing records).
X - Exit the application (as we do not store the information, data will be lost).


Source Code:


import java.util.*;
public class Main
{
	private final static class versionType
	{
		public String number;
		public byte month;
		public short year;

		public versionType clone()
		{
			versionType varCopy = new versionType();

			varCopy.number = this.number;
			varCopy.month = this.month;
			varCopy.year = this.year;

			return varCopy;
		}
	}

	private final static class program
	{
		public String name;
		public String category;
		public String description;
		public versionType version = new versionType();

		public program clone()
		{
			program varCopy = new program();

			varCopy.name = this.name;
			varCopy.category = this.category;
			varCopy.description = this.description;
			varCopy.version = this.version.clone();

			return varCopy;
		}
	}

	public static void main()
	{
		int capacity = 1000;
		program[] data = new program[capacity];
		boolean repeat = true;
		String option;
		int counter = 0;

		do
		{
			System.out.println();
			System.out.println("Computer programas database");
			System.out.println();
			System.out.println("1.- Add data.");
			System.out.println("2.- View names of the programs.");
			System.out.println("3.- Search programs.");
			System.out.println("4.- Modify program.");
			System.out.println("5.- Delete Program.");
			System.out.println("6.- Sort alphabetically");
			System.out.println("7.- Fix redundant spaces");
			System.out.println("0.-Exit.");
			System.out.print("Option: ");
			option = new Scanner(System.in).nextLine();

			switch (option)
			{
				case "1": //add
					if (counter > capacity - 1)
					{
						System.out.println("Database full!");
						break;
					}

					do
					{
						System.out.print("Enter Name: ");
						data[counter].name = new Scanner(System.in).nextLine();
						if (data[counter].name.length() == 0)
						{
							System.out.print("Cannot be empty");
						}
					} while (data[counter].name.length() == 0);

					do
					{
						System.out.print("Enter category: ");
						data[counter].category = new Scanner(System.in).nextLine();
						if (data[counter].category.length() > 30)
						{
							System.out.print("Too long. 30 letters max, please");
						}
					} while (data[counter].category.length() > 30);

					System.out.print("Enter Description: ");
					data[counter].description = new Scanner(System.in).nextLine();
					if (data[counter].description.length() > 100)
					{
						data[counter].description = data[counter].description.substring(0, 100);
					}

					System.out.print("Enter the version number: ");
					data[counter].version.number = new Scanner(System.in).nextLine();
					System.out.print("Enter the version month: ");

					data[counter].version.month = Byte.parseByte(new Scanner(System.in).nextLine());
					System.out.print("Enter the version year: ");
					data[counter].version.year = Short.parseShort(new Scanner(System.in).nextLine());

					counter++;
					break;

				case "2": //view
					if (counter == 0)
					{
						System.out.println("No data!");
					}
					else
					{
						for (int i = 0; i < counter; i++)
						{
							System.out.printf("%1$s: %2$s" + "\r\n", i + 1, data[i].name);
							if (i % 20 == 19)
							{
								System.out.print("Press Enter...");
								new Scanner(System.in).nextLine();
							}
						}
					}
					break;

				case "3": //search
					System.out.print("Enter part of the name, description, etc... (case sensitive): ");
					String search = new Scanner(System.in).nextLine();
					boolean found = false;
					for (int i = 0; i < counter; i++)
					{
						if (data[i].name.contains(search) || data[i].description.contains(search) || data[i].category.contains(search))
						{
							System.out.printf("%1$s: %2$s" + "\r\n", i + 1, data[i].name);
							found = true;
						}
					}
					if (!found)
					{
						System.out.println("Not found!");
					}
					break;

				case "4":
					System.out.print("Enter the program number: ");
					int progNumber = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;

					if ((progNumber > counter) || (progNumber < 0))
					{
						System.out.print("Out of range!");
						break;
					}

					System.out.printf("Program name (was %1$s; hit ENTER to leave as is): ", data[progNumber].name);
					String newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].name = newText;
					}

					System.out.printf("Program category (was %1$s; hit ENTER to leave as is): ", data[progNumber].category);
					newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].category = newText;
					}

					System.out.printf("Program description (was %1$s; hit ENTER to leave as is): ", data[progNumber].description);
					newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].description = newText;
					}

					System.out.printf("Program version (number) (was %1$s; hit ENTER to leave as is): ", data[progNumber].version.number);
					newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].version.number = newText;
					}

					System.out.printf("Program version (month) (was %1$s; hit ENTER to leave as is): ", data[progNumber].version.month);
					newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].version.month = Byte.parseByte(newText);
					}

					System.out.printf("Program version (year) (was %1$s; hit ENTER to leave as is): ", data[progNumber].version.year);
					newText = new Scanner(System.in).nextLine();
					if (!newText.equals(""))
					{
						data[progNumber].version.year = Short.parseShort(newText);
					}


					break;

				case "5": //delete
					int position = 0;
					System.out.print("Enter the position number to delete: ");
					position = Integer.parseInt(new Scanner(System.in).nextLine()) - 1;
					if (position > counter)
					{
						System.out.println("Error: out of range");
					}
					else
					{
						for (int i = position; i < counter; i++)
						{
							data[i] = data[i + 1].clone();
						}
						counter--;
					}
					break;

				case "6": // Sort
					program aux = new program();
					for (int i = 0; i < counter - 1; i++)
					{
						for (int j = i + 1; j < counter; j++)
						{
							if (data[i].name.compareTo(data[j].name) > 0)
							{
								aux = data[i].clone();
								data[i] = data[j].clone();
								data[j] = aux.clone();
							}
						}
					}
					break;

				case "7": //replace "  " x " "
					for (int i = 0; i < counter; i++)
					{
						while (data[i].name.contains("  "))
						{
							data[i].name = data[i].name.replace("  ", " ");
						}
					}
					break;

				case "0":
					repeat = false;
					break;

				default:
					System.out.println("Wrong option!");
					break;
			}
		} while (repeat != false);
	}
}
Exercisey 4.23





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.