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

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

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

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

COMP 110:Introduction to Programming

Tyler JohnsonMar 16, 2009

MWF 11:00AM-12:15PMSitterson 014

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

COMP 110: Spring 20092

Questions?

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

COMP 110: Spring 20093

Program 3

Example solutions

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

COMP 110: Spring 20094

Today in COMP 110

Constructors

Static Variables & Methods

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

COMP 110: Spring 20095

Constructors

Section 6.1 in text

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

COMP 110: Spring 20096

Creating Objects

Student jack = new Student();

Why does this look like a method call?

Because it is

This is a call to a special method called a constructor

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

COMP 110: Spring 20097

Constructors

A constructor is a special method that is called when an object is created using new

The purpose of a constructor is to perform initializing actions

e.g. initialize the instance variables of an object

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

COMP 110: Spring 20098

Constructors

The purpose of a constructor is similar to that of a mutator (setter)

Used to set the value of variable(s)

However, constructors create an object in addition to initializing it

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

COMP 110: Spring 20099

Example: Pet class

public class Pet {

private String name; private int age; private double weight;

public Pet() { name = “No name yet.”; age = 0; weight = 0; }

public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; }}

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

COMP 110: Spring 200910

Constructors

A constructor must have the same name as its class

The constructor for the class Pet is Pet()The constructor for the class Student is Student()

Constructors do NOT specify a return type

Not even void

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

COMP 110: Spring 200911

Constructors

The classes you have used up to this point use a constructor created automatically by Java

Gives default values to instance variables• May not be what you want

You can specify how instance variables should be initialized by creating your own constructors

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

COMP 110: Spring 200912

Constructors w/ Parameters

Like methods, constructors can have parameters

public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; }

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

COMP 110: Spring 200913

Default Constructor

A constructor that takes no arguments is called a default constructor

public Pet() {name = “No name yet.”;age = 0;weight = 0;

}

Java automatically defines a default constructor if you do not define any constructors

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

COMP 110: Spring 200914

Multiple Constructors

You can define multiple constructors

All have the same name, but different parameters

Group their definitions together

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

COMP 110: Spring 200915

Constructors

You cannot call a constructor on an existing object

Pet myPet = new Pet();myPet.Pet("Roberto", 1, 150.0); //error

Must use mutators on objects that have already been created

myPet.setPet("Roberto", 1, 150.0); //ok

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

COMP 110: Spring 200916

Calling Methods within Constructors

Just like calling methods within methods

/*constructor*/public Pet(String initName, int initAge, double initWeight) {

setPet(initName, initAge, initWeight); //have the mutator perform the set}

/*mutator*/public void setPet(String newName, int newAge, double newWeight) {

name = newName; age = newAge; weight = newWeight;}

16

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

COMP 110: Spring 200917

Static Variables & Methods

Section 6.2 in text

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

COMP 110: Spring 200918

The Keyword Static

The keyword static is a specifier that can be applied to instance variables and methods in Java

You are already familiar with some other specifiers such as public, private, final

public class Student {

private double gpa;

public double getGPA() {return gpa;

}}

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

COMP 110: Spring 200919

The Keyword Static

The keyword static is used to indicate that only ONE copy of the instance variable or method should exist for the entire class

public class UnitsAndMeasures {

//static, all objects share the SAME copy of this variablepublic static final int FEET_PER_YARD = 3;

//NOT static, all objects have their OWN copy of this variableprivate int feet;

}

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

COMP 110: Spring 200920

Example: Static Instance Variables

A class that counts the number of method calls to ALL of its objects

public class StaticExample {

//static, all objects share the SAME copy of this variableprivate static numberOfCalls = 0;

public void method() { numberOfCalls++;}

}

public class StaticExampleTester {

public static void main(String[] args) { StaticExample se = new StaticExample(); StaticExample se2 = new StaticExample();

se.method(); //changes numberOfCalls to 1 se2.method(); //changes numberOfCalls to 2}

}

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

COMP 110: Spring 200921

The Keyword Static

The keyword static can also be used with methods

The main method

public static void main() { StaticExample se = new StaticExample(); StaticExample se2 = new StaticExample();

se.method(); //changes numberOfCalls to 1 se2.method(); //changes numberOfCalls to 2

}

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

COMP 110: Spring 200922

Using the Keyword Static

When should you use the keyword static with a method?

When a method does not access instance variables

public int pow(int x, int y) {

int result = 1;for(int i = 0; i < y; i++) {

result *= x;}

return result;}

Does this method access any instance variables?

No. Should be declared static

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

COMP 110: Spring 200923

Example

public class DimensionConverter {

public static final int INCHES_PER_FOOT = 12;

public static final double convertFeetToInches(double feet) {

return feet*INCHES_PER_FOOT;}

public static double convertInchesToFeet(double inches) {return inches / INCHES_PER_FOOT;

}}

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

COMP 110: Spring 200924

Accessing Static Variables

From outside the class, static variables that are declared public can be accessed using the name of the class

int inchesPerFoot = DimensionConverter.INCHES_PER_FOOT;

Class Name Static Variable

Name

No Object is

Specified!

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

COMP 110: Spring 200925

Calling Static Methods

From outside the class, static methods that are declared public can be accessed using the name of the class

int inches = DimensionConverter.convertFeetToInches(12);

Class Name Static Variable

Name

No Object is

Specified!

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

COMP 110: Spring 200926

Restrictions on Static Methods

Static methods CANNOTAccess non-static instance variablesCall non-static methods

Static methods CANBe called by any method, static or non-static

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

COMP 110: Spring 200927

Restrictions on Static Methods

Access Non-Static

Variables

Access Static Variabl

es

Call Non-Static

Methods

Call Static Method

s

Static methods X √ X √

Non-StaticMethods √ √ √ √

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

COMP 110: Spring 200928

Example

public class Circle {

public static final double PI = 3.14159;private double area;

public static double area(double radius) { area = PI * (radius * radius); return area;}

} Will this code compile?

No.Cannot access non-static instance

variables in static methods

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

COMP 110: Spring 200929

Example

public class Circle {

public static final double PI = 3.14159;

private void printArea(double area) {System.out.println(area);

}

public static void area(double radius) {printArea(PI * (radius * radius));

}}

Will this code compile?

No.Cannot call non-static methods

inside static methods

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

COMP 110: Spring 200930

Example

public class Circle {

public static final double PI = 3.14159;

private void printArea() {System.out.println(area(3.0));

}

public static double area(double radius) {return PI * (radius * radius);

}}

Will this code compile?

Yes.CAN call static methods inside non-static methods

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

COMP 110: Spring 200931

Programming Demo

Grade DistributionA class to display the distribution of letter grades in a classGiven the number of A,B,C,D, and F’s, compute the percentage of each type of grade• e.g. 15% A’s, 30% B’s, 30% C’s, 15% D’s, 10% F’s

Include accessors and mutators for each type of gradeDraw a bar graph of the grade distribution

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

COMP 110: Spring 200932

Programming Demo

OutputEach * == 2 percent

0 10 20 30 40 50 60 70 80 90 100| | | | | | | | | | |****************************************************** A************** B*********C*****D***F

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

COMP 110: Spring 200933

Wednesday

Math class

Wrapper class

Writing & Testing Methods