Homework 5 - Methods

Part A

Answer the following in a Word or .txt document. Attach this to the same submission as for part B
  1. Suppose you will be writing a program that will act as a traveller's guide to visiting a large city. Identify 5 methods that could be involved in this program and write the top line of each method including return type, name, and parameters (you do not need to write method bodies, but what kind of thing the methods do should be clear from the names and types you choose in your first line).
  2. Suppose you will be writing a program that will train a player to become better at chess. Identify 5 methods that would be involved in this program and write the first line of each including return type, name, and parameters.

Part B

The following example code shows using methods in java.

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;
    }

}

Add the following methods to a class with a main (they can be above main or below, but not inside main's {}
  1. Create a method min that takes three ints and returns the lowest
  2. Create a method tempSurvey, which takes an array of doubles representing temperatures, and returns a parallel array of booleans. For each temperature in the array of doubles, it asks the user whether they would consider that temperature hot, and based on their yes or no answer, fills up the array of booleans.
In main, do the following inside main (in the {}) to test your methods (hint: USE THE METHODS):
  1. Find the lowest of three numbers (you choose the numbers) and put the result in a variable, then print it
  2. In one line of code, find and print the lowest out of seven numbers
  3. Create an array of 5 temperatures, get the user's opinions on them, and then go through and print only the temperatures that the user considered hot.
  4. [EC +10] Create an extra method yesNoPrint which takes parallel arrays of booleans and doubles to do the printing part of #5, so that in main you can do all of #5 in two lines of code (one to create the array of temperatures, the other to do everything else)

  5. Create another java class with a main and do the following, creating at least three methods in addition to main to do parts of the task.

    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.