import java.util.Scanner; /* * Lab 2, Practice writing Methods * * name: CS 112 Course Staff */ public class Methods { /* * print3Times - takes a string s and prints it 3 times */ public static void print3Times(String s) { for (int i = 0; i < 3; i++) { System.out.println(s); } } /* * printNTimes - takes a takes an integer n and a string s, * and prints the string n times */ public static void printNTimes(int n, String s) { for (int i = 0; i < n; i++) { System.out.println(s); } } /* * For Fall 2019 this method was moved to the String practice * task of the lab * * replaceStart - takes s1 and s2 and creates a new string in which * the first s1Len characters of s2 are replaced by s1, * where s1Len is the length of s1. */ public static String replaceStart(String s1, String s2) { if ( s1 == null || s2 == null ) throw new IllegalArgumentException(); int s1Len = s1.length(); if (s1Len < s2.length()) { String s2End = s2.substring(s1Len); // in Python: s2[s1Len: ] s1 += s2End; } return( s1 ); } /* * This method prompts for a name and age and displays * a welcome message accordinglty */ public static void greetMe() { Scanner scan = new Scanner( System.in ); int age; String keepGreeting; do { System.out.print( "\nPlease enter your name: " ); String name = scan.nextLine(); System.out.print( "Hello " + name + ", Welcome to CS112!!!\nHow old are you " + name + "? " ); age = scan.nextInt(); System.out.println( insult( name, age ) ); System.out.print( "\nWould you like to issue another greeting (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( String name, int age ) { // Can also check that the parameter name is not passed a null value, // but not explicitly necessary as this method is not invoking any methods on name. // String insult = "Wow " + name + ", you "; if ( age <= 0 ) insult += "are an idiot who does not pay attention to directions"; else if ( age < 11 ) insult += "are still sweet, for the moment"; else if ( age < 18 ) insult += "are such a dweeb"; else if ( age < 21 ) { int diff = 21-age; insult += "have " + diff + " year"; if (diff > 1) insult += "s"; insult += " to go"; } else if ( age == 21 ) insult += "just made it"; else if ( age < 30 ) { int diff = 30-age; insult += "have only " + diff + " good year"; if (diff > 1) insult += "s"; insult += " left"; } else if ( age < 40 ) insult += "are in a sorry state"; else if ( age < 50 ) insult += "must be miserable"; else insult = "yikes"; insult += "!!!"; 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 ) { greetMe(); } }