Question
/* Chapter 3 Programming Activity 2 Calling class methods Anderson, Franceschi */ // ***** add your import statements here public class PracticeMethods { public static
/* Chapter 3 Programming Activity 2 Calling class methods Anderson, Franceschi */
// ***** add your import statements here
public class PracticeMethods {
public static void main(String [] args) { //***** // 1. a. Create a Scanner object to read from the console // b. Prompt the user for their first name // c. Print a message that says hello to the user // d. Print a message that says how many letters // are in the user's name // Part 1 student code starts here:
// Part 1 student code ends here.
//***** // 2. a. Skip a line, then prompt the user for the year // they were born. // b. Calculate and print the age the user will be this year. // c. Declare a constant for average life expectancy, // set its value to 77.9 // d. Print a message that tells the user the percentage // of their expected life they've lived. // Use the DecimalFormat class to format the percentage // Part 2 student code starts here:
// Part 2 student code ends here.
//***** // 3. a. Generate a random integer between 1 and 20 // b. Pop up an input dialog box and ask the user for a guess. // c. Pop up an output dialog box telling the user the number // and how far from the number the guess was (hint: use Math.abs) // Part 3 student code starts here:
// Part 3 student code ends here. }
}
Programming Activity 3-2 Guidance ================================= Equivalent terms ---------------- For our purposes in this class, the following terms are synonyms: - console - Java console - command line - Command Prompt Each of these represents the standard, text-based (non-graphical), mechanism for user input and output. There is a part 1-d ------------------- The textbook does not show part 1-d, which is supposed to display the number of letters in the user's name. It is shown in the textbook's sample output. It is also included in the framework source code comments. It must be included in your solution. First name ---------- Use the Scanner object's "next" method to get the first name. Birth year ---------- Use the Scanner object's "nextInt" method to get the birth year. Age calculation --------------- To determine the user's age, subtract their birth year from the current year. Simply hard-code the current year; you are not required to find it programmatically. (If you want to find it programmatically, that is OK, but requires research.) Life percentage calculation --------------------------- To calculate the percentage of expected life already lived, divide age by average life expectancy and use "#0.0%" for the DecimalFormat pattern. Random number ------------- Use the Random class to generate a random numer. Random number guess ------------------- To get the random number guess, use the JOptionPane.showInputDialog method. To use the String guess in a calculation, first convert it to an int. Closeness of guess ------------------ To display closeness of guess, use the JOptionPane.showMessageDialog method.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started