|
Methods:
Prerequisite: Read Chapter 4 of your Textbook
In this lesson we will look at making methods and passing parameters. We will discuss scope and memory management through examples shown below. You will then take your Craps program and remake it using parameters and NO global variables.
import java.io.*;
public class Ut
public static void main( String args[] ) throws IOException { DataInputStream input = new DataInputStream(System.in); int randomNum = (int) (Math.random()*5); int guess,gcount;
guess = gcount = 1; System.out.println( "Enter your " + gcount + " Guess" );
guess = Integer.parseInt(input.readLine());
if (guess == randomNum) { System.out.println("you got it!"); }
if (guess != randomNum) { System.out.println("No, the answer was " + randomNum); } } // end of main } // end of class Ut
We would like to make this game again, and be able to divide it into parts using methods. How should we divide it up? The BEST senario is that our pseudocode will become our ACTUAL code. Let’s write pseudocode for the task above:
One example would be:
Get Random Number Get Guess If the guess is correct You Win If the guess is not correct You Lose This will be our goal: Can we make main to look like the above pseudocode? ----------------------------------- Program 2 ---------------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount; public static void main( String args[] ) throws IOException { initVars(); System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); if (guess == randomNum) { System.out.println("you got it!"); } if (guess != randomNum) { System.out.println("No, the answer was " + randomNum); } }// end of main
static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*6 + 1); guess = gcount = 1; } } // end of class Ut ------------------------------------ Program 3 ---------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount; public static void main( String args[] ) throws IOException { initVars(); getGuess(); if (guess == randomNum) { System.out.println("you got it!"); } if (guess != randomNum) { System.out.println("No, the answer was " + randomNum + " " + guess); }
}// end of main
static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*6 + 1); guess = gcount = 1; } static void getGuess() throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); } } // end of class Ut -------------------------------------- Program 4 ---------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount; public static void main( String args[] ) throws IOException { initVars(); getGuess(); if ( correct()) { youWin(); } if (!correct()) { youLose(); } }// end of main
static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*6 + 1); guess = gcount = 1; } static void getGuess() throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); }
static boolean correct() { if (guess == randomNum) return true; else return false; } static void youWin() { System.out.println( "You won!"); }
static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); } } // end of class Ut --------------------------- Program 5 -------------------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount; public static void main( String args[] ) throws IOException { initVars(); getGuess(); while (!correct() && (gcount < 5) ) { getGuess(); } if (gcount == 5) youLose(); else youWin(); }// end of main static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*10 + 1); guess = gcount = 1; }
static void getGuess() throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); gcount++; }
static boolean correct() { if (guess == randomNum) return true; else return false; }
static void youWin() { System.out.println( "You won!"); }
static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); }
} // end of class Ut
--------------------------------- Program 6 --------------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount;
public static void main( String args[] ) throws IOException { initVars(); getGuess(); while (!correct() && (gcount < 5) ) { getGuess(); } if (gcount == 5) youLose(); else youWin(); }// end of main static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*10 + 1); guess = gcount = 1; } static void getGuess() throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); gcount++; } static boolean correct() { if (guess == randomNum) {return true; gcount++;} else return false; }
static void youWin() { System.out.println( "You won!"); }
static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); } } // end of class Ut ------------------------------- Program 7 ------------------------------ import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess, gcount; public static void main( String args[] ) throws IOException { initVars(); getGuess(); while (!correct() && (gcount < 5) ) { getGuess(); } if (gcount == 5) youLose(); else youWin(); }// end of main static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*10 + 1); guess = gcount = 1; }
static void getGuess() throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); gcount++; } static boolean correct() { if (guess == randomNum) return true; else {gcount++; return false;} } static void youWin() { System.out.println( "You won!"); }
static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); } } // end of class Ut ------------------------------ Program 8 ----------------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum, guess; public static void main( String args[] ) throws IOException { int gcount = 0; initVars(); getGuess(gcount); while (!correct(gcount) && (gcount < 5) ) { getGuess(gcount); } if (gcount == 5) youLose(); else youWin(); }// end of main static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*10 + 1); guess = 1; }
static void getGuess(int gcount) throws IOException { System.out.println( "Enter your " + gcount + " Guess" ); guess = Integer.parseInt(input.readLine()); gcount++; } static boolean correct(int gcount) { if (guess == randomNum) return true; else {gcount++; return false;} } static void youWin() { System.out.println( "You won!"); } static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); } } // end of class Ut ------------------------------- Program 9 -------------------------- import java.io.*; public class Ut { static DataInputStream input; static int randomNum; public static void main( String args[] ) throws IOException { int g = 0; int gcount = 0; initVars(); g = getGuess(gcount);
while (!correct(g) && (gcount < 5) ) { g = getGuess(gcount); gcount++; } if (gcount == 5) youLose(); else youWin(); }// end of main
static void initVars() { input = new DataInputStream(System.in); randomNum = (int) (Math.random()*10 + 1); }
static int getGuess(int g) throws IOException { System.out.println( "Enter your " + g + " Guess" ); int guess = Integer.parseInt(input.readLine()); return guess; }
static boolean correct(int guess) { if (guess == randomNum) return true; else { return false;} }
static void youWin() { System.out.println( "You won!"); }
static void youLose() { System.out.println( "You lose, the answer was " + randomNum ); } } // end of class Ut
|