/* Chapter 3 Programming Activity 2 Calling class methods Anderson, Franceschi */ // ***** add your import statements here import java.util.Scanner; import java.util.Calendar; import java.text.NumberFormat; import java.text.DecimalFormat; 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 // Your code goes here Scanner scan = new Scanner(System.in); System.out.println ("Please enter your first name"); String first = scan.next(); System.out.println ("Hello " + first); System.out.println (first.length()); //***** // 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 76.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 System.out.println(); System.out.println("Please enter the year of your birth"); Calendar now = Calendar.getInstance(); int year = scan.nextInt(); int age = now.get(now.YEAR) - year; System.out.println ("Your age is " + age ); final double lifeExpectancy = 76.9; double used = age/lifeExpectancy; NumberFormat pct = NumberFormat.getPercentInstance(); System.out.println("You've used up " + pct.format(used) + " of your life"); DecimalFormat percnt = new DecimalFormat ("##0.00%"); System.out.println("or more precisely " + percnt.format(used) + " to 2 places"); //***** // 3. a. Generate a random integer between 1 and 20 // b. Skip a line, then print a message that you're // thinking of a number between 1 and 20 // and that you'll tell them the number in 5 seconds // c. Wait five seconds by calling the // wait method in the prewritten Pause class. // The wait method has this API: // public static void wait( int secondsToWait ) // The authors have written this class and included in // the same directory as this file, so an import // statement is not needed. // d. Print a message that tells the user the number. final int last = 20; final int start = 1; double Rand = Math.random()*(last - start) + start; int R = (int) Rand; System.out.println("I'm thinking of a number between 1 and 20. Wait 5 seconds"); Pause.wait(5); System.out.println(R); } }