Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.

Similar presentations


Presentation on theme: "CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p."— Presentation transcript:

1 CS 177 Recitation Week 8 – Methods

2 Questions?

3 Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p project3m Project3.java  Make sure you turn in to project3m  Not project3, or project3M, etc.

4 Project 3 import statements  You must include these statements at the very top of your Project 3 file, before the class declaration:  import java.io.*; import java.util.*;  This lets the loadPuzzle() method compile correctly.  Import statements- what do they do?  They tell the compiler that you are using classes that are not in the same directory without you having to type in the full path to the class.

5 loadPuzzle()  The loadPuzzle() method is called with 2 arguments  Argument 1 is a 2d int array that represents your Sudoku puzzle  Argument 2 is the name of a file to load  Call like: String filename = …; int[][] puzzle = …; boolean loaded = loadPuzzle(puzzle,filename);

6 loadPuzzle() – Scanner class  The Scanner class used in the loadPuzzle() method is a class included with Java. It allows you to read input just like StdIn does  The Scanner class also allows you to read input directly from files, instead of only from the user while running the program. It also has a lot of methods to take the input in different ways and parse it.

7 Another note: Coding Standards  To receive full credit on your projects, you must follow the coding standards- linked on the course website  Please review these before turning in the next project  There are requirements for:  Variable naming – capitalization, meaningful names  Commenting – for all variable declarations, all methods  Formatting/Indentation – you can use Dr. Java to do this: highlight the code you want to indent correctly and press Tab. Dr. Java will indent consistently and correctly as long as your braces are correct.

8 Methods

9 Why do we use Methods?  Methods allow us to reuse code that is useful in many places, without having to type it in again  For example, you could write a method to print an array, since you do this a lot for your projects!  Methods can organize the code into separate responsibilities  Allows the programmer to think about one step at a time  Allows multiple programmers to work on the same program

10 Return Statements  Methods should always have a return statement, even if the return type is void  Methods can have more than one return statement if different values will be returned in different cases public static int max(int a, int b) { if(a<b) return b; else return a; }

11 Calling Methods  Calling a method in a different class  Class.name(arg1, arg2, …)  For example: StdDraw.point(x, y)  Calling a method in the same class  name(arg1, arg2, …)  For example: loadPuzzle(puzzle, filename)

12 Void Methods  public static void name(arg1, …)  This indicates that the method does not return a value. It is more like a command to do something public static void help(int times) { for(int i = 1; i <= times; i++) System.out.println(“Help!”); return; }  help(100) is basically a command to print “Help!” 100 times

13 Passing Arguments  The method gets copies of the arguments you pass – it cannot change the values of primitive data types like int and char public static int square(int x) { x = x * x; return x; }  Calling square(x) returns the square of x, but does not change its value.  When passing arrays, the method can change the values in the array because it is not a primitive data type.

14 Example 1  Let’s write a function that computes the area of a rectangle.  Inputs are an int for width and an int for height  The return value is an int that is the area of the rectangle public static int calculateArea(int width, int height) { int area; area = width * height; return area; }

15 Example 1  How do we call our calculateArea() method?  Let’s calculate the area of a 5 by 6 rectangle. int x = 5; int y = 6; int area = calculateArea(x, y);  area then has the value 30.

16 Example 2  Let’s write a method that prints a menu and gets a choice  There are no inputs  The return value is an int for the user’s menu selection public static int getChoice() { System.out.println(“1 – Menu choice 1”); System.out.println(“2 – Menu choice 2”); System.out.println(“3 – Exit”); System.out.println(“Which one do you want?”); int choice = StdIn.readInt(); return choice; }

17 Example 3  Let’s write a method that draws a blue circle of radius 0.25 at a specified point.  Inputs are 2 doubles to indicate position  There is no need to return a value public static void drawBlueCircle(double x, double y) { StdDraw.setPenColor(StdDraw.BLUE); StdDraw.filledCircle(x, y, 0.25); return; }

18 Conway’s Game of Life public static void conway (int[][] cell, int m, int n) { int live = 0; for (int i=m-1; i<=m+1; i++) { for (int j=n-1; j<=n+1; j++) { if (cell[i,j]==1) live++; }

19 Conway’s Game of Life cont. if (cell[m,n]==1) { live--; if (live 3) cell[m,n] = 0; } else { if (live == 3) cell [m,n] = 1; } return; }

20 conway() method  As mentioned before, changes that this method makes to cell[m,n] DO affect the actual array that was sent to conway()  However, conway() won’t work if cell[m,n] is an edge so it doesn’t have 8 neighbors  How do we fix this?  Conditionals?  We could fix it by adding extra if statements to check whether cell[m,n] is an edge so that we don’t get out of bounds errors

21 A better way to fix conway()  Instead of using if statements in the conway() method, there is a way to fix this without changing the conway() method at all  If we want 100 rows and 100 columns, instead make cell a 102 by 102 array: int[][] cell = new int[102][102];  We’ll set all the entries in rows 0 and 101 as well as columns 0 and 101, the edges, to 0.  Then, cells in rows 1-100 and columns 1-100 are valid cells, and none of them are on the edge of the array!  This is one of the fun parts of what software developers do – it’s like a puzzle to figure out the best way to solve something while keeping the programming as simple as possible. The solution works perfectly but is very easy to do.

22 Questions?


Download ppt "CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p."

Similar presentations


Ads by Google