comp 14 introduction to programming mr. joshua stough february 28, 2005 monday/wednesday 11:00-12:15...

38
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00- 12:15 Peabody Hall 218

Post on 21-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

COMP 14Introduction to Programming

Mr. Joshua StoughFebruary 28, 2005

Monday/Wednesday 11:00-12:15

Peabody Hall 218

Page 2: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Announcements

• Previous year’s test, or practice problems, forthcoming\– Review ppt’s .

Page 3: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Mid-Term Wrap-Up

Common Mistakes• You're not in math class

instead of <=– 2(i+5) instead of 2 * (i+5)– num2 instead of num*num– num^2 instead of num*num

• Changing loop control variable for computation inside loop

• Not testing your code

Page 4: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Mid-Term Wrap-Up

for (int i = 10; i<=50; i+=10) {

System.out.print (i + "\t");

i = i + 5;System.out.print (i + "\t");

i = i * 2;System.out.print (i + "\t");

i = i * i;System.out.println (i);

}

int temp;

temp = i + 5;

temp

temp = temp * 2;

temp

temp = temp * temp;

temp

Page 5: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

COMP 14 So Far...

• Problem Solving• Mathematical Calculations• Output• User Input• File I/O• Selection (if, if-else, switch)• Loops (while, do...while, for)

Page 6: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

COMP 14 Next...

• Object-Oriented Design• Writing Methods

– pieces of code that we give a name

• Writing Classes– organize related pieces of information

• Arrays– access multiple pieces of information with a

single identifier

• Goal: Not sure yet.– Javascript GUI Blackjack game, personal

projects

Page 7: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Object-Oriented Design

Problem Statement

Write a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectanglenouns - objects, data membersverbs - actions, operations (methods)

Page 8: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

class Rectangle Data Members and Operations

Last Step:design and implement an algorithm for each operation

class name

data members

operations(methods)

Page 9: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Classes and Objects

Rectangle

A class(the concept)

length = 15, width = 3

An object(the realization)

length = 20, width = 6

length = 15, width = 15

Multiple objectsfrom the same class

Page 10: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Anatomy of a Class

A class contains data declarations and method declarations

int width;int length;

Data declarations

Method declarations(operations)

Page 11: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Rectangle.javaSo Far

public class Rectangle{

// data membersprivate int width;private int length;

// methods// setLength// setWidth// getLength// ...// computeArea// print

}

Page 12: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

What is a Method?

• Group of programming statements that we give a name

• Statements usually work together to perform a specific task/operation

• Every method belongs to a class

Page 13: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Why Use Methods?

• To divide complex programs into manageable pieces

• Abstraction– provide an operation to be performed on an

object (ex: computeArea)

• Code Re-use– write a small piece of code that can be used

(called) multiple times (saves typing)

Page 14: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Methods

• Pre-defined methods– provided by Java standard library– we've used these before– Math class (Math.pow, Math.sqrt, ...)– Integer class (Integer.parseInt, ...)

• User-defined methods– you write these

Page 15: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

String MethodsMethod Description (pgs. 109-110)

boolean equals(String str)

int length()

String toLowerCase()

char charAt(int index)

the result can be stored in a boolean variable or used in a boolean expression (return type)

the method needs to be given another String (parameter)

method name

method specification, notsyntax of use

Page 16: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Using String Methods

• list of String methods: Chapter 3 (pgs 104-112) and Appendix E (pgs 914-916)

• Steps:– create a String object

– put data into the object

– use the dot operator to "call" the method

String line;

line = keyboard.readLine();String greeting = "Hello";

int len = line.length();greeting = greeting.toUpperCase();char ch = greeting.charAt(3);

Page 17: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Two Types of Methods

• Value-returning methods– "return", "evaluate to", "result in"

some value– like mathematical functions– only returns a single value

• Void methods– performs operations but returns no

value

Page 18: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Value-Returning Methods• Think mathematical function

f(x) = 2x + 5 f(x, y) = 2x + yf(1) = 7 f(1, 5) = 7f(2) = 9 f(2, 3) = 7f(3) = 11 f(3, 1) = 7

• Can have multiple arguments (parameters)

• Only one result of the function

Page 19: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Using MethodsWhat You Need To Know

1. Name of the method2. Number of parameters3. Data type of each parameter4. Data type of value computed

(returned) by the method5. The code required to accomplish

the task

* Only need to know 1-4 for predefined methods

Page 20: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Value-Returning MethodsUses

• Save the value for future calculation• Use the value in a calculation• Print the value

So, often used in expressions:x = Math.pow (y, 2);z = a + Math.pow (y, 2) + x;System.out.println (Math.pow (y, 2));

Page 21: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Control Flow

• Program control flow– execution always begins with the first

statement in the method main– other methods execute only when called

• Method control flow– when a method is invoked, the flow of

control jumps to the method and the computer executes its code

– when complete, the flow of control returns to the place where the method was called and the computer continues executing code

Page 22: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Method Declaration

• Specifies the code that will be executed when the method is invoked (or called)

• Located inside a class definition

• Contains

– method header

– method body

Page 23: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Method Header

public static int countCharInWord (char ch, String word)

methodnamereturn

type

formal parameter list

The parameter list specifies the typeand name of each parameter

The name of a parameter in the methoddeclaration is called a formal argument

A method declaration begins with a method header

visibilitymodifiers

Page 24: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

int number = countCharInWord ('e', "Heels");

2

Return Type

• Indicates the type of value that the method evaluates to (or, sends back to the calling location)– primitive data type– class name– void

• reserved word indicating that nothing is returned

• When a value is returned, the method call is replaced with the returned value

Page 25: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Method Body

The method header is followed by the method body

{int count = 0;for (int i = 0; i<word.length(); i++) {

if (word.charAt(i) == ch) {count++;

}} return count;

}

The return expression must beconsistent with the return type

ch and word are local data

They are created each time the method is called, and are destroyed when it finishes executing

public static int countCharInWord (char ch, String word)

Page 26: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

ParametersEach time a method is called, the actual parameters in the call are copied into the formal parameters

int num = countCharInWord ('e', "Heels");

{int count = 0;for (int i = 0; i<word.length(); i++) {

if (word.charAt(i) == ch) {count++;

}} return count;

}

public static int countCharInWord (char ch, String word)

Page 27: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The return Statement

• Tells the computer to "return" back to where the call was originally made.

• Specifies the value that will be returnedreturn expression;

• The data type of expression must match the method's return type

• Methods with return types of void usually don't (but can) have a return statement

• Value-returning methods must have at least one return statement

Page 28: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Using return

public static double larger (double x, double y){ double max; if(x >= y) max = x; else max = y; return max;}

public static double larger (double x, double y){ if(x >= y) return x; else return y;}

These are equivalentmethods.

Page 29: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Void Methods

• Does not return a value• Has a return type of void• Similar in structure to value-

returning methods• Call to method is always a stand-

alone statement• Can use return statement to exit

method early

Page 30: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

The main Method

The main method looks just like all other methods

public static void main (String[] args)

modifiers returntype

methodname

parameterlist

Page 31: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Code Re-Use

• Methods can also be used to save typing• If you have a section of code that does one

task and it needs to be done multiple times, you can write it as a method

• Example: you want to print the following heading to start your program

********************************************************************************** Blackjack **********************************************************************************

Page 32: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

public class MyProgram{

public static void main (String[] args){

printStars();printStars();System.out.print ("************");System.out.print (" Blackjack ");

System.out.println ("************");printStars();printStars();

}

public static void printStars(){

System.out.print ("***************");System.out.print ("***************");System.out.println ("*****");

}}

Page 33: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

printStars

What if we wanted to change the number of stars printed on the line?

give the number of stars as a parameter

Page 34: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

public class MyProgram{

public static void main (String[] args){

printStars(35);printStars(35);System.out.print ("************");System.out.print (" Blackjack ");

System.out.println ("************");printStars(35);printStars(35);

}

public static void printStars(int numStars){

for (int i = 0; i<numStars; i++) {System.out.print ("*");

}System.out.println();

}}

Page 35: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

printStars

What if we wanted to change the character printed (something besides stars?)

change the name of the method (because it may not print stars) --> printChars

give the character as a parameter

Page 36: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

public class MyProgram{

public static void main (String[] args){

printChars(35, '#');printChars(35, '#');System.out.print ("###############");System.out.print (" Blackjack ");

System.out.println ("###############");printChars(35, '#');printChars(35, '#');

}

public static void printChars(int num, char ch){

for (int i = 0; i<num; i++) {System.out.print (ch);

}System.out.println();

}}

Page 37: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

printStars (35);

printStars (30 + 5);

int num = 35;printStars (num);

Parameters

• Formal parameters– variable declarations in the method header– automatic local variables for the method

• Actual parameters– actual values that are passed to the method– can be variables, literals, or expressions

Page 38: COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218

Next Time in COMP 14

• More methods– more parameter passing– method overloading– practice writing methods

• Reading: Ch 7 (pgs. 347-349, 359-376),