×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Create a Program to Create Name Recording System in Java Assignment Solution

July 06, 2024
Dr. Hannah Lynch
Dr. Hannah
🇬🇧 United Kingdom
Java
Dr. Hannah, a distinguished Ph.D. holder from the University of York, brings over a decade of expertise to our service. With an impressive track record of completing 1100+ Java assignments, Dr. Hannah's profound knowledge and extensive experience ensure exemplary solutions tailored to meet your specific requirements.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Use modular coding in Verilog assignments by breaking the design into smaller modules. This improves readability, simplifies debugging, and allows for easier testing and reuse of code components in larger designs.
News
In 2024, Visual Studio Code now offers enhanced GitHub Copilot integration for faster, while PyCharm has improved debugging and testing tools, making it ideal for Python students​

Instructions

Objective

Write a program to create name recording system in java language.

Requirements and Specifications

You are to write a java assignment program that accomplishes the following: Name your class: homeWork3 When the program is run, prompt the user in the console to enter records made up of a name and age in two different prompts. After a user enters a name, then prompt them for age. After each name/age record has been entered, prompt the user to enter an additional record by selecting "yes/no". After a user has entered a name and then an age. If the user selects "yes," prompt to enter a name and age again. There should NOT be any limit to the number of records a user can enter. When the user has finished entering their records (by selecting "no" at the enter another record prompt), display the records entered in the console. Now prompt the user whether they would like to write the records to a file with the message: "Would you like to write to a file? yes/no". If the user responds "yes," prompt the user for the filename they would like to use. Once the filename is entered, create the file and write the records to that file, with each record appearing on a separate line. If the user responds "no" at the write to a file prompt, then display the message: "Thanks for playing!". Once you have completed your program upload your homeWork3.java file for your submission. The grading for this homework is BINARY, meaning either your program functions as requested (full points), or it does not (zero points). You may use any resources and programming techniques we have covered in class to date to accomplish this program. The functions defined within this program can be accomplished with the techniques and syntax we have covered in class and the readings.

Source Code

import java.io.BufferedWriter; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class homeWork3 { public static void main(String[] args) throws Exception { // Create Scanner object Scanner sc = new Scanner(System.in); // Create array to store the names entered List names = new ArrayList(); // Create a List to store all the ages entered List ages = new ArrayList(); // The two lists declared above are parallel, which means that the name at index i corresponds to the age at index i /* Begin with main program here. We will create an infinite-loop that will be executed until the user enters the option "no" */ boolean program_running = true; while(program_running) { // First, ask user for name and store in a variable called 'name' String name; System.out.print("Enter name: "); name = sc.nextLine(); names.add(name); // Now, ask user for age int age; age = getAge(sc); ages.add(age); // Ask if he/she wants to enter more names and ages System.out.print("Do you want to enter a new record? "); String input = askYesNo(sc); if(input.compareTo("no") == 0) // user enters no program_running = false; // break the while-loop } // Display records System.out.println(""); System.out.println(String.format("%-10s %10s", "Name", "Age")); System.out.println(String.format("%-10s %10s", "----", "---")); // Now, ask user if he/she wants to save the records to a file for(int i = 0; i < names.size(); i++) { System.out.println(String.format("%-10s %10d", names.get(i), ages.get(i))); } System.out.println(""); // Ask if records must be saved to a file System.out.print("Do you want to save the records to a file? "); String input = askYesNo(sc); if(input.compareTo("yes") == 0) { // ask for file System.out.print("Enter the name of the file: "); String file = sc.nextLine(); // Create Buffered Writer BufferedWriter writer = new BufferedWriter(new FileWriter(file)); // Write lines for(int i = 0; i < names.size(); i++) { writer.write(String.format("%-10s %10d\n", names.get(i), ages.get(i))); } // close file writer.close(); System.out.println("Records successfully saved to " + file + "!"); } // good bye System.out.println("Thanks for playing!"); } public static String askYesNo(Scanner sc) { // This function asks infinitely for user to enter yes/no until he/she enters a valid input String input; while(true) { System.out.print("Enter yes/no: "); input = sc.nextLine().toLowerCase(); // convert to lower case so user can enter Yes, yEs, yeS, YES, etc.. if(input.compareTo("yes") == 0 || input.compareTo("no") == 0) return input; else System.out.println("Invalid input."); } } public static int getAge(Scanner sc) throws Exception { /* This function asks to user for an integer and handles if the user enters an non-numeric value */ while(true) { try { int age; System.out.print("Enter age: "); age = Integer.valueOf(sc.nextLine()); return age; } catch(Exception ex) { System.out.println("Your must enter a valid age."); } } } }

Related Samples

At ProgrammingHomeworkHelp.com, we offer dedicated support for Java assignments with an extensive range of sample codes. Our Java samples cover essential topics such as object-oriented programming, data structures, and design patterns, providing students with clear, practical examples to enhance their learning. Each sample is crafted by experts to help you grasp complex concepts and improve your coding skills. Whether you're working on a simple project or tackling advanced Java challenges, our resources are here to guide you. Explore our Java samples today and excel in your assignments with confidence.