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.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Draft timetable has the same times as this semester: - ◦ Monday 9:00 am to 12:00 noon, 1:00 pm to 3:00 pm. ◦ Tuesday 9:00 am to 12:00 noon, 1:00 pm.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Week 9: Methods 1.  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.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
1 CS 177 Week 4 Recitation Slides Conditionals. 2 Announcements Project 1 is due on Feb. 7th 9pm (extended) Project 2 will be posted today (Feb. 5th)
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 10: 1 TEST!!
Classes, methods, and conditional statements We’re past the basics. These are the roots.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
1 CS 177 Week 15 Recitation Slides Review. Announcements Final Exam on Sat. May 8th  PHY 112 from 8-10 AM Complete your online review of your classes.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
CS 201 Functions Debzani Deb.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Java Unit 9: Arrays Declaring and Processing Arrays.
Hello AP Computer Science!. What are some of the things that you have used computers for?
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
The Java Programming Language
Programming With C.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Lecture 2: Classes and Objects, using Scanner and String.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
Week 10 - Friday.  What did we talk about last time?  References and primitive types  Started review.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CS 177 Recitation Week 1 – Intro to Java. Questions?
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
CS305j Introduction to Computing Parameters and Methods 1 Topic 8 Parameters and Methods "We're flooding people with information. We need to feed it through.
Chapter 9 Introduction to Arrays Fundamentals of Java.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Chapter VII: Arrays.
User-Written Functions
3 Introduction to Classes and Objects.
Introduction to Computer Science / Procedural – 67130
Yanal Alahmad Java Workshop Yanal Alahmad
Creating and Modifying Text part 2
Java Software Structures: John Lewis & Joseph Chase
INPUT STATEMENTS GC 201.
An Introduction to Java – Part I, language basics
Peer Instruction 4 Control Loops.
CSC 1051 – Data Structures and Algorithms I
Unit 3: Variables in Java
Presentation transcript:

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 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 and columns 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?