CS 177 Recitation. Week 8 Methods

Similar documents
We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

CS 177 Week 15 Recitation Slides. Review

CS 101 Spring 2007 Midterm 2 Name: ID:

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

CS 101 Fall 2006 Midterm 1 Name: ID:

CS 112 Introduction to Programming

CS 112 Introduction to Programming

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

class objects instances Fields Constructors Methods static

Declaring and ini,alizing 2D arrays

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Lab # 2. For today s lab:

last time in cs recitations. computer commands. today s topics.

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CISC 1600, Lab 3.2: Interactivity in Processing

Advanced Computer Programming

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

CS 211: Methods, Memory, Equality

CSCI 135 Exam #1 Fundamentals of Computer Science I Fall 2012

Lec 7. for loops and methods

More on inheritance CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2014

COMP 202 Java in one week

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Section 2.2 Your First Program in Java: Printing a Line of Text

AP Computer Science Unit 1. Writing Programs Using BlueJ

Computer Science is...

CS 177 Recitation. Week 1 Intro to Java

CS 177 Week 5 Recitation Slides. Loops

Array. Prepared By - Rifat Shahriyar

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

First Java Program - Output to the Screen

Software and Programming 1

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Full file at

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

CISC 1600 Lecture 3.1 Introduction to Processing

type name(arg1, arg2,...) { body return }

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Question 2. [5 points] Given the following symbolic constant definition

COMP 110 Programming Exercise: Simulation of the Game of Craps

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

2. [20] Suppose we start declaring a Rectangle class as follows:

Chapter 5 Lab Methods

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring What is your name?: (4 points for writing it on your answer sheet)

CISC 1600, Lab 2.2: Interactivity in Processing

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Variables One More (but not the last) Time with feeling

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Lesson 3: Accepting User Input and Using Different Methods for Output

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Midterms Save the Dates!

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Give one example where you might wish to use a three dimensional array

AP Computer Science Unit 1. Programs

CS 302: Introduction to Programming in Java. Lecture 11 Yinggang Huang. CS302 Summer 2012

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CSE 114 Computer Science I

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

CS 112 Introduction to Programming

COS 126 Midterm 1 Written Exam Fall 2011

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

CS 161: Object Oriented Problem Solving

AP Computer Science Unit 1. Writing Programs Using BlueJ

Project #1 rev 2 Computer Science 2334 Fall 2013 This project is individual work. Each student must complete this assignment independently.

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Section 2.2 Your First Program in Java: Printing a Line of Text

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

CS/ENGRD 2110 SPRING 2018

Computational Expression

Controls Structure for Repetition

Java Applets. Last Time. Java Applets. Java Applets. First Java Applet. Java Applets. v We created our first Java application

Starting to Program in C++ (Basics & I/O)

Simple Java Reference

Course Outline. Introduction to java

Chapter 1. Section 1.4 Subprograms or functions. CS 50 - Hathairat Rattanasook

Recitation: Loop Jul 7, 2008

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Simple Java YEAH Hours. Brahm Capoor and Vrinda Vasavada

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Introduction to Classes and Objects

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

Lab 12: Lijnenspel revisited

Lec 3. Compilers, Debugging, Hello World, and Variables

C Programming for Engineers Functions

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS Problem Solving and Object-Oriented Programming

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

1 Getting started with Processing

Transcription:

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 project3m Project3.java Make sure you turn in to project3m Not project3, or project3m, etc.

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.

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

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.

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.

Methods

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

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

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)

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

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.

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

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.

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

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

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

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

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

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.

Questions?