Homework 5 - Methods


We will use Java to do a few programming tasks. Here is an online Java IDE -- an editor that also lets you run code right in the browser. At the top is the editor, below that is a Run button, and then below that the output. You can edit the code in the editor and run it as many times as you want.

About Java:

About the Java IDE:

About this assignment:


The following example code shows using loops in java.
import java.util.Scanner;
public class Main {

    // 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() {
        // any method that talks to the user needs its own Scanner
        Scanner scan = new Scanner(System.in);
        prettyPrint("Please Enter Words", "I will Echo"); // one method can use another
        String userval = scan.nextLine();
        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 combines them
    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
    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++) {
                System.out.println("What is the number for " + words[i] + "?");
                nums[i]  = scan.nextInt();
          }
          return nums; // returning array
    }
    
    
    
    // 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]);
    } // END OF MAIN
    
        // METHODS CAN BE CREATED HERE BELOW MAIN

}

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 timePrint which takes a String and an int, and prints the String the number of times given in the int, with * between, so timePrint("Hi", 3) would print Hi*Hi*Hi
  3. 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. Print "Hello World" 18 times.
  4. Create an array of 5 words. Using a for loop, print each word in the array the number of times given by the index (so the first word 0 times, the next word 1 time, etc).
  5. 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.
  6. [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)