import java.util.Scanner;
public class MethodHwk {
// methods need public and static in front of them to cooperate with main
// return type void doesn't return
// also no parameters, so either
// method always does same thing or gets input from user
public static void userEcho() {
prettyPrint("Please Enter Words", "I will Echo"); // one method can use another
String userval = promptString("enter:");
System.out.println ("You said:");
prettyPrint(userval, userval);
}
// return type void doesn't return
// two parameters, both are strings
public static void prettyPrint(String input1, String input2) {
System.out.println(encase(input1, "==="));
System.out.println(encase(input2, ":::"));
}
// method that takes two strings and returns one string that
// encases the value string in two copies of the decor string
public static String encase(String value, String decor) {
return decor + " " + value + " " + decor; // returning an expression
}
// method that takes an int as parameter
// and returns a string of that many Xs
public static String numToString(int num) {
String str = ""; // start with empty string
for (int i = 0; i < num; i++) {
str = str + "X";
}
return str; // returning a variable of the correct type
}
// method that takes three ints as parameters
// and returns their average, a double
public static double average( int a, int b, int c) {
// note that in java integer division results in an integer
// so to get a double result, one thing being divided had to be a double
// in this case, the 3.0 (3 would be an int)
return (a + b + c) / 3.0; // returning an expression of the right type
}
// method that takes an array of Strings and returns a parallel array of ints
// with values from the user
public static int[] makeNums( String[] words) {
Scanner scan = new Scanner(System.in);
int[] nums = new int[words.length];
for (int i = 0; i < words.length; i++) {
nums[i] = promptInt("What is the number for " + words[i] + "?");
}
return nums; // returning array
}
// method that takes an array of Strings and adds "s" to each
// notice don't have to return, arrays are references in java
// so changes to array parameters affect original
public static void pluralize( String[] words) {
for (int i = 0; i < words.length; i++) {
words[i] = words[i] + "s";
}
}
// METHODS CAN BE CREATED HERE ABOVE MAIN
// main is the method in java that runs automatically,
// only other methods called by main run
public static void main(String[] args) {
// METHODS CAN NOT BE CREATED HERE INSIDE MAIN
// if method is commented out, it does not run
//userEcho();
// calling method with literals
prettyPrint("APPLE", "ORANGE");
String ap = "APPLE";
// calling method with variable and expressione
prettyPrint(ap, ap + " PIE");
// calling methods with results of methods called with results of methods
prettyPrint(encase("APPLE", "..."), encase(encase("APPLE", "!!!"), "---"));
// silly to call method that returns and not use value
numToString(8); // seems to do nothing because we didn't use the result
String result = numToString(5); // catching result in a variable
System.out.println("Result was " + result);
System.out.println("Next time: " + numToString(6)); // printing result
double avg = average(22, 89, 51); // result in a variable
System.out.println(avg);
System.out.println(average(99, 77, 61)); // printing result
String[] names = {"Abe", "Bob", "Cal", "Dan", "Eve"};
int[] nameNums = makeNums(names); // passing array
System.out.println(names[2] + " has number " + nameNums[2]);
pluralize(names); // changes contents of array
System.out.println("There are several " + names[3]);
} // END OF MAIN
// METHODS CAN BE CREATED HERE BELOW MAIN
// utility for reading in String input
public static String promptString(String promptText) {
System.out.print(promptText + " ");
Scanner scan = new Scanner(System.in);
String response = scan.nextLine();
return response;
}
// utility for reading in integer input
public static int promptInt(String promptText) {
System.out.print(promptText + " ");
Scanner scan = new Scanner(System.in);
int response = scan.nextInt();
return response;
}
}
You will test the user on their knowledge of the squares of the numbers from 1 to 20. Store these numbers in an array. Go through the list of numbers and quiz the user on their squares. For each one, if the user gets it right, they don't need to be tested on it again (think of how you could keep track of this). After quizzing them, print them a list of which ones they got wrong and offer to quiz again. If they choose to quiz again, only quiz them on the ones they got wrong. Keep going until you run out of numbers they got wrong or the user chooses to quit.