COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

46
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014

Transcript of COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Page 1: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110:Introduction to Programming

Tyler JohnsonFeb 16, 2009

MWF 11:00AM-12:15PMSitterson 014

Page 2: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20092

Announcements

Program 2 due Wednesday by midnight

Lab 4Part 2 is now extra credit

Page 3: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20093

Lab 2

Page 4: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20094

Questions?

Page 5: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20095

Today in COMP 110

Classes & MethodsRead Section 5.1 in text

Page 6: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20096

Classes

Java programs consists of objects of various class types interacting with one another

You have already been using classes

System, String, Scanner, Math

Page 7: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20097

Classes

A class is a definition of a kind of object

It describes a general template or blueprint for creating objects of that class

A class “Car” is a definition of what a “Car” is and what it can do

Page 8: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20098

Classes & Objects

Objects of the class Car might have the following attributes

MakeModelYearOwnerLocation

All objects of the class Car have these attributes, but with potentially different values

Page 9: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 20099

Classes & Objects

Class CarMakeModelYearOwnerLocation

focus objectMake = FordModel = FocusYear = 2001Owner = Samantha SmartLocation = School

camry objectMake = ToyotaModel = CamryYear = 2000Owner = John DoeLocation = Garage

Page 10: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200910

Classes & Objects

Important: classes do not have data; individual objects have data

Classes specify what kind of data objects have

10

Class CarMakeModelYearOwnerLocation

camry objectMake = ToyotaModel = CamryYear = 2000Owner = John DoeLocation = Garage

Page 11: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200911

Classes & Objects

Both the “camry” and “ford” objects satisfy the definition of the class “Car”

They are said to be instances of the “Car” class

When we create an object of a certain class, we are said to instantiate it

Page 12: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200912

Methods

Classes also define the actions that objects of its type can performThese are called methods

All objects of the same class have the same methods

The class “Car” might have the following methods

accelerate()brake()sell()start()

Page 13: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200913

String Methods

string.length()string.charAt(index)string.substring(index1, index2)string.toLowerCase()string.equals(A_String)

Page 14: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200914

UML

A UML class diagram can be useful in describing classes

UML stands for Universal Modeling Language

Page 15: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200915

UML

Car

- make: String- model: String- year: int- owner: String- location: String

+ accelerate(double pedalPressure): void+ brake(double pedalPressure): void+ sell(String newOwner): void+ start(): void

Class name

Attributes

Methods (actions)

A UML class diagram for the class “Car”

Page 16: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200916

Defining Classes

You have already been defining your own classes!

Every program you have written defines a class

//define the class TipCalculatorpublic class TipCalculator {

…}

Page 17: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200917

Class Files & Separate Compilation

Each Java class definition goes in its own, SEPARATE .java file

ClassName ClassName.java

The class “Car” must be saved in a file called “Car.java”

Page 18: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200918

Class Files & Separate Compilation

What happens when you compile a .java file?

.java file gets compiled into a .class file• .class file contains Java bytecode• Car.java is compiled into Car.class

You can compile a Java class before you have a program that uses it

Page 19: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200919

The Student Class

Student

- name: String- year: int- gpa: double- major: String- credits: int- gpaSum: double

+ getName(): String+ getMajor(): String+ printData(): void+ increaseYear(): void+ calculateGPA(double grade): void

Page 20: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200920

Defining the Class Student

public class Student {

public String name; public int year; public double GPA; public String major; // ...

public String getMajor() { return major; }

public void increaseYear() { year++;

}// …

}

Class name

Data(instance variables)

Methods

Page 21: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200921

Instance Variables

The data members of a class are called instance variables

public String name;public int year;public double gpa; public String major;

public No restrictions on how these instance variables are used (more details later)

Page 22: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200922

Creating Objects

Declaring/Initializing Primitives

int i = 0;double area = 10.5;

Declaring/Creating Objects

Scanner keyboard = new Scanner(System.in);

Page 23: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200923

Creating Objects

Create an object called “jack” of class “Student”

Student jack = new Student();

Create an objectAssign memory address of object to variable

Page 24: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200924

Using Instance Variables: Inside the Class Definition

public class Student {

public String name; public int year; public double GPA; public String major; // ...

public String getMajor() { return major; }

public void increaseYear() { year++;

}// …

}

Page 25: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200925

Using public Instance Variables Outside a Class

public static void main(String[] args) {

Student jack = new Student(); jack.name = "Jack Smith"; jack.major = "Computer Science";

System.out.println(jack.name + " is majoring in " + jack.major);

Student sam = new Student(); sam.name = "Samantha Smart"; sam.major = "Biology";

System.out.println(sam.name + " is majoring in " + sam.major);}

“jack.name” and “sam.name” are two different variables because they belong to different objects

Page 26: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200926

Methods

Two kinds of methods

Methods that return a value• Examples

– string.substring()– string.charAt()

Methods that return nothing• Example

– System.out.println()

Page 27: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200927

Methods

public String getMajor() { return major;}

public void increaseYear() { year++;}

returns a String

returns nothing

return type

Page 28: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200928

Defining Methods that Return Nothing

Method heading: keywordspublic: no restriction on how to use the method (more details later)void: the method returns nothing

Method body: statements executed when the method is called (invoked)

Must be inside a pair of braces

public void increaseYear() { year++;}

Page 29: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200929

Method printData

As usual, inside a block (defined by braces), you can have multiple statements

public void printData() { System.out.println("Name: " + name); System.out.println("Major: " + major); System.out.println("GPA: " + gpa);}

Page 30: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200930

Methods

All method definitions must appear inside the definition of the class to which they belong!

public class Student {

// … public String getMajor() { return major; }

public void increaseYear() { year++;

}// …

}

Page 31: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200931

Methods

When you use a method such as

keyboard.nextInt()

you are said to call or invoke the method

Page 32: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200932

Calling Methods that Return Nothing

Syntaxobject.method();

Use them like Java statementsStudent jack = new Student();jack.year = 1;

jack.increaseYear(); //year = 2jack.increaseYear(); //year = 3

System.out.println("Jack’s class year is " + jack.year);

Jack’s class year is 3

Output

Page 33: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200933

Defining Methods that Return a Value

Method heading: keywordspublic: no restriction on how to use the method (more details later)Type: the type of value the method returns

Method body: statements executedMust be inside a pair of bracesMust have a return statement

public String getMajor() { return major;}

Page 34: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200934

The Return Statement

A method that returns a value must have at least one return statement

Terminates the method, and returns a value to the caller

Syntax:return Expression;

Expression can be any expression that produces a value of the return type

34

Page 35: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200935

Methods that Return a Value

public String getClassYear() {

if(year == 1) return "Freshman"; else if(year == 2) return "Sophomore"; else if ...}

Page 36: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200936

Calling Methods that Return a Value

Syntaxobject.method();

Use as a variable of the method’s return type

Student jack = new Student();jack.name = "Jack Smith";jack.major = "Computer Science";

String m = jack.getMajor();

System.out.println("Jack’s full name is " + jack.getName());System.out.println("Jack’s major is " + m);

36

Page 37: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200937

The Return Statement

Can also be used in methods that return nothing

Terminates the method

Syntax:return;

public void increaseYear() {

if(year >= 4) return; //exit the method

year++;}

37

Page 38: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200938

The Return Statement

Typically, you want to write your methods to contain only one return statement

It will make your programs easier to read

Page 39: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200939

The Main Method

The main method is a method like any other

public static void main(String[] args) {// …

}

The main method is not invoked by you, but invoked automatically by the system when you run a program

Page 40: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200940

The Main Method

Classes that have a main method can be run as a program

Not all classes have a main methodSome are used inside the main method of other classes

Page 41: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200941

Local Variables

Variables declared inside a method are called local variables

As opposed to instance variables, which are declared in the class definition

public class Student {

public double gpa; //an instance variable

public static void main(String[] args) { int size; //a local variable // …

}}

Page 42: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200942

Local/Instance variables

Instance variablesDeclared in a class• Can be used anywhere in the class that declares the

variable, including inside the class’ methods

Local variablesDeclared in a method• Can only be used inside the method that declares the

variable

Page 43: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200943

Programming Demo

Counter classA class used to count things

Define a class that records a count as a non-negative whole numberInclude methods for the following

Reset counter to 0Increment the countDecrement the countGet the current countDisplay the current count

Page 44: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200944

Counter Class

No methods should allow the count to become negative

Write a program to test the class

Page 45: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200945

Programming Demo

Programming

Page 46: COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

COMP 110: Spring 200946

Wednesday

More about Classes