/* * Lab 2, Part 2 Practice writing Methods * * name: CS 112 Course Staff */ import java.util.*; public class Insults2 { /* * This method prompts for an age and displays * an insult accordingly */ public static void insultMe() { Scanner scan = new Scanner( System.in ); int age; String keepGreeting; do { System.out.print( "How old are you? " ); age = scan.nextInt(); System.out.println( insult( age ) ); System.out.print( "Would you like to issue another insult (yes/no)? " ); keepGreeting = scan.next(); // Need to use this to read throg the newline character left on the input buffer // from the prior call to next() // Recall that each call to nextLine will stop at the first newline encountered scan.nextLine(); } while ( keepGreeting.equals("yes") ); // Note the use of calling the equals method on the String object } /* * This method forms a string based on the value * of the age argument. */ public static String insult( int age ) { String insult; if ( age <= 0 ) insult = "You are an idiot who does not pay attention to directions."; else if ( age < 20 ) insult = "You're such a dweeb!"; else if ( age < 30 ) insult = (30 - age) + " more years until 30!"; else if ( age < 50 ) { insult = "Being an adult is rough."; } else { insult = "Yikes."; } return( insult ); } /* * This method is the entry point of our program. * * This method should be used to call the individual methods * that have been written as specified in lab. * */ public static void main( String [] args ) { //the main method is just the entry point now //and acts as a driver to run your program. //by calling the appropriate method. insultMe(); } }