what am i missing? from #4b, 4c, 4d and all of 5.... import java.util.Scanner; i
ID: 440875 • Letter: W
Question
what am i missing? from #4b, 4c, 4d and all of 5.... import java.util.Scanner; import java.text.*; import java.util.Random; public class PracticeMethods { public static void main( String [] args ) { //***** // 2. a. Create a Scanner object to read from the keyboard // b. Prompt the user for his/her 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.print("What is your first name > "); String firstName = scan.next(); System.out.println("Hello " + firstName); int len = firstName.length(); System.out.println("There are " + len + " characters in your name "); //***** // 3. Skip a line // Your code goes here System.out.println(); //***** // 4. a. Using previously created Scanner object prompt the user for the year (s)he was 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 // Your code goes here System.out.print("What year were you born?> "); int year = scan.nextLine(); int age; age = 2013 - year; System.out.println("You will be " + age + " years old this year "); //***** // 5. a. Create a Random object // and generate a random integer between 1 and 20 // b. Using JOptionPane pop up an input dialog box and ask the user for a guess. // c. Using JOptionPane pop up an output dialog box telling the user the number // and how far from the number the guess was (hint: use Math.abs) // Your code goes here Random random = new Random(); int start = 1, end = 20; String guess = JOptionPane.showInputDialog(null, "What is your guess of the number? "); int number = random.nextInt(end - start + 1) + start; int difference;////?????? } }Explanation / Answer
import java.util.Scanner; import java.text.*; import java.util.Random; import java.util.Calendar; import java.text.DecimalFormat; public class PracticeMethods { public static void main( String [] args ) { Scanner scan = new Scanner(System.in); System.out.print("What is your first name > "); String firstName = scan.next(); System.out.println("Hello " + firstName); int len = firstName.length(); System.out.println("There are " + len + " characters in your name "); System.out.println(); System.out.print("What year were you born?> "); int year = scan.nextLine(); int age; Calender cal = Calender.getInstance(); age = cal.clear(Calendar.YEAR) - year; System.out.println("You will be " + age + " years old this year "); public static final expectancy = 77.9; ((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true); f=(age/expectacy)*100; f.applypattern(); System.out.println("You have lived " + f + " percentage of the expected life. "); Random random = new Random(); int start = 1, end = 20; int n_number; int number = random.nextInt(end - start + 1) + start; String guess = JOptionPane.showInputDialog(null, "What is your guess of the number? "); n_number = Integer.parseInt(guess); int difference; difference = number - n_number; System.out.println("the number is " + number + " and you are "+Math.abs((n_number/number)*100)+" far from the number."); } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.