Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose of this assignment is to practice using while and for loops, if / else statements, Scanner objects, and the char data type to

The purpose of this assignment is to practice using while and for loops, if/else statements, Scanner objects, and the char data type to solve a problem.IMPORTANT NOTE 1: You are asked to solve the problem described below with little or no assistance from built-in Java methods. For this assignment, the following methods can and should be used:close and nextLine from the Scanner classcharAt, equals, and length from the String classparseInt from the Integer classSystem.out.print and System.out.printlnNo other Java methods may be used.SetupTo get started, follow these instructions:Create a new package called assign02. See Assignment 1 for instructions if needed.In the assign02 package, add a new class called LuckyNumberGenerator. Be sure to check the box to automatically make a main method. Remember that exact spelling and capitalization of package and class names is necessary for grading.RequirementsYou are to write a program that consists of a single class called LuckyNumberGenerator, which contains a single main method. Do not introduce any additional classes or methods.The program prompts the user to enter their name, birth month, and birth day (of the month) in the console and then prints their lucky number. To make the task of constructing this program manageable, develop it in the stages outlined here, checking your work along the way.Stage 1- InputThe program must first prompt a user to enter their name in the console. The prompt should be one line of text asking the user to enter their name. Use System.out.println for this prompt. Entering a name means typing a name of any length and possibly including spaces, such as Carlos or Patricia Augustine Overmeir or k.d. lang. After typing the name, the user will press enter. This type of input represents a line of text. Your program should read this input using a Scanner object, which is a tool for working with text input. Detailed documentation can be found hereLinks to an external site., but we need only one of Scanner's methods, nextLine. As the name suggests, this method scans for the next complete line of text and returns it as a String.Similar to System.out, which we use to print output to the console, System.in can be used to take input from the console. Creating your scanner with the following constructor sets it up to scan input from System.in:Scanner input = new Scanner(System.in);Note that Eclipse may give you a red squiggle error message stating "Scanner cannot be resolved to a type." indicating that you have not told Java where to find the code for Scanner. Do so by importing the Scanner library with the following line. Place this line near the top of your file just below the package name:import java.util.Scanner;Now you have the tools set up for output and input. Try out your code by printing out your prompt, then using input.nextLine() to read some typed input. You can then print that input to make sure it is what you are expecting.When you have correct code for getting the user's name, write similar code to get the birth month as a number (1 to 12). Note that you need an additional method to covert the String to an int. This conversion is achieved with the parseInt method of the Integer classLinks to an external site., like so:int month = Integer.parseInt(inputStringScanned);IMPORTANT NOTE 2: We are scanning the input as a string and then converting to an integer, rather than scanning the integer directly. This makes the work of Stage 4 easier.When you have correct code for getting the user's birth month, write similar code to get the birth day (as a number 1 to 31). In all, your program must prompt and scan input for three values from the user: name, birth month, birth day. Do this one at a time, with a separate prompt for each.You may assume that the user's name contains at least one character and that the first character is a letter. (Do not assume anything else about the number of characters in the name or the value of other characters.) You may also assume that the user enters valid integers for the birth month and day.Stage 2- Generating the lucky numberAfter completing stage 1, your program should contain a String holding the user's name, an int holding the user's birth month (as a number), and an int holding the user's birth day. The user's lucky number is generated using this information. Compute the sum of some or all ASCII character codes in the name (more details below).Get the remainder when dividing this sum by the birth day.Add the birth month to this remainder.This result is the lucky number.How the ASCII character codes in the name are summed depends on the first character in the name.For names in which the first letter is a vowel, sum every character in the name.For names in which the first letter is a consonant B-L or b-l, sum every other character in the name (even positions 0,2,...).For names in which the first letter is a consonant M-Z or m-z, sum every other character in the name (odd positions 1,3,...).Make good use of if and else and the char data type to determine which case applies. For each case, create a loop that accumulates the appropriate character values into a sum. Helpful methods provided by String are length()Links to an external site. and charAt(int index)Links to an external site.. Recall that a char can be interpreted as a number that has integer values. Because of this, we can use regular arithmetic operators, such as +,-,*,/, and % with char-type variables. (See Lab 2, as needed.)Stage 3- OutputAfter entering their name, birth month, and birth day, the user should receive their lucky number in a message similar to the following example run of the program. (Text in red is the input typed by the user.)What is your name?CarlosIn what month were you born? (Enter the month as a number; i.e.,1 for January.)3On what day of the month were you born? 9For Carlos born on March 9, the lucky number is 7.Your prompts for input may differ, but should be user-friendly. The final output containing the lucky number must be exactly the same, filling in the user's name, birth month (as a word), birth day, and lucky number to match the input. Be careful of spelling, capitalization, and spaces. As with all other lines of output, the final line should end with a newline (i.e., use println rather than print). This is typically expected when a line of output text is required, unless specified otherwise.Be sure to check your program with several different names to make sure it is behaving the way you intend. Consider names with and without spaces, as well as mixed letter cases.Stage 4- RepetitionOnce all of the stages above are working, you must modify your program to repeat the process of prompting the user to enter their name, birth month, and birth day and then printing their lucky number. In other words, after printing the lucky number, your program should again prompt for information and generate another lucky number. This repetition should continue until the user enters a String equal to "exit". At that point, your program should print a single line saying "Good luck!" and end.Note that the user may type "exit" at any of the three prompts (for name, birth month, or birth day) to quit.Below is an example run of the program, with repetition:What is your name?Patricia Augustine OvermeirIn what month were you born? (Enter the month as a number; i.e.,1 for January.)10On what day of the month were you born?30For Patricia Augustine Overmeir born on October 30, the lucky number is 10.What is your name?k.d. langIn what month were you born? (Enter the month as a number; i.e.,1 for January.)11On what day of the month were you born?2For k.d. lang born on November 2, the lucky number is 12.What is your name?Prof. ParkerIn what month were you born? (Enter the month as a number; i.e.,1 for January.)exitGood luck!Preparing to submit As you finish your program, add and/or check the following to ensure a complete, correct, and professional-looking submission:The top of the program file must have a comment with the course name, assignment title, author name, and date.Check that the formatting of your source code is consistent and makes the code readable for humans. Use indentation and spaces as we have in class. Make sure that any variables you have created have good and meaningful names that use the camel-case convention for Java.Make sure that you have not used any built-in Java methods except those listed at the top of this assignment.Check that you are making use of recently-learned CS 1420 concepts and avoiding use of concepts that have yet to be covered. This ensures you get the expected practice with new concepts and that you do not use other concepts before their proper usage is presented in class.Finally, run your program again to make certain that the documentation and formatting changes you made did not alter the source code.SubmissionFollow the instructions for Assignment 1 on preparing to submit, exporting, and submitting your code file to Gradescope. Do not forget to include a comment at the top of your program with your name and the date.Your solution for this assignment consists of one file: LuckyNumberGenerator.java.Unlike assignment 1, you will only see some of the autograder test results. 35 of the points for the autograder are visible before the assignment deadline. The remaining 35 points are based on a set of post-tests that will become visible after the assignment is fully graded. To increase your success of earning all of those points, be sure to try your program for as many different scenarios as you can think of, and make sure you have followed all of the instructions on this page carefully. You can resubmit up to the due date and see new results. Your most recent submission is graded. Start all CS 1420 assignment early to take advantage of this feature!There are also staff-graded elements in Gradescope. These are things that look at code formatting, comments, variable naming, and other aspects of good code. These scores are published about one week after the assignment deadline.This tool needs to be loaded in a new browser window

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle RMAN For Absolute Beginners

Authors: Darl Kuhn

1st Edition

1484207637, 9781484207635

More Books

Students also viewed these Databases questions

Question

Examine how IPV 6 deals with the scalability problem in routing.

Answered: 1 week ago

Question

=+14. We are doing better in both overall sales and in profits.

Answered: 1 week ago