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:
- This is java, not our pseudocode, so some things we use in class, like prompt, are not available.
Between the previous homeworks and this one, I have provided examples of all the syntax you should need to be using.
About the Java IDE:
- You have to click Run again each time you want it to run your code to test changes.
- At top left there is a save (floppy) button! This page is pretty good but not perfect, so save often and don't lose your work!
- It WILL NOT SAVE your code if you close the browser, so you need to save!!!
- As you type, the editor tries to help by providing suggestions, but these pull from all of Java instead of
just from the tools we are using, so its suggestions may not be useful! You can turn this off using the settings (gears) button
at top right and unchecking Autocomplete at the bottom of the list, if it annoys you.
- If your output has the word error in it, java is telling you that you have a syntax error you need to fix.
It will try to tell you the line number, but sometimes an error in an earlier line can cause a problem that
doesn't show up til a later line.
About this assignment:
- The editor page starts off with example code in it. REPLACE the example code with the code in the assignment. This means to
DELETE ALL of the code in the editor, and then paste in my example code. ALL.
- When you do a different part, you will need to again REPLACE ALL of the code (you can open more tabs with copies of the IDE in your browser) so make sure you SAVE.
- Attach all your java files to a single submission in blackboard, not multiple submissions.
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 {}
-
Create a method min that takes three ints and returns the lowest
-
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
-
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):
- Find the lowest of three numbers (you choose the numbers) and put the result in a variable, then print it
- In one line of code, find and print the lowest out of seven numbers
- Print "Hello World" 18 times.
- 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).
- 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.
- [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)