COMP 110 Introduction to Programming Tyler Johnson Mar

  • Slides: 33
Download presentation
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11: 00 AM-12:

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

Questions? 2 COMP 110: Spring 2009

Questions? 2 COMP 110: Spring 2009

Program 3 Example solutions 3 COMP 110: Spring 2009

Program 3 Example solutions 3 COMP 110: Spring 2009

Today in COMP 110 Constructors Static Variables & Methods 4 COMP 110: Spring 2009

Today in COMP 110 Constructors Static Variables & Methods 4 COMP 110: Spring 2009

Constructors Section 6. 1 in text 5 COMP 110: Spring 2009

Constructors Section 6. 1 in text 5 COMP 110: Spring 2009

Creating Objects Student jack = new Student(); Why does this look like a method

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 6 COMP 110: Spring 2009

Constructors A constructor is a special method that is called when an object is

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 7 COMP 110: Spring 2009

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

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 8 COMP 110: Spring 2009

Example: Pet class public class Pet { private String name; private int age; private

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; } } 9 public Pet(String init. Name, int init. Age, double init. Weight) { name = init. Name; age = init. Age; weight = init. Weight; } COMP 110: Spring 2009

Constructors A constructor must have the same name as its class The constructor for

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 10 COMP 110: Spring 2009

Constructors The classes you have used up to this point use a constructor created

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 11 COMP 110: Spring 2009

Constructors w/ Parameters Like methods, constructors can have parameters public Pet(String init. Name, int

Constructors w/ Parameters Like methods, constructors can have parameters public Pet(String init. Name, int init. Age, double init. Weight) { name = init. Name; age = init. Age; weight = init. Weight; } 12 COMP 110: Spring 2009

Default Constructor A constructor that takes no arguments is called a default constructor public

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 13 COMP 110: Spring 2009

Multiple Constructors You can define multiple constructors All have the same name, but different

Multiple Constructors You can define multiple constructors All have the same name, but different parameters Group their definitions together 14 COMP 110: Spring 2009

Constructors You cannot call a constructor on an existing object Pet my. Pet =

Constructors You cannot call a constructor on an existing object Pet my. Pet = new Pet(); my. Pet("Roberto", 1, 150. 0); //error Must use mutators on objects that have already been created my. Pet. set. Pet("Roberto", 1, 150. 0); //ok 15 COMP 110: Spring 2009

Calling Methods within Constructors Just like calling methods within methods /*constructor*/ public Pet(String init.

Calling Methods within Constructors Just like calling methods within methods /*constructor*/ public Pet(String init. Name, int init. Age, double init. Weight) { } set. Pet(init. Name, init. Age, init. Weight); //have the mutator perform the set /*mutator*/ public void set. Pet(String new. Name, int new. Age, double new. Weight) { } 16 name = new. Name; age = new. Age; weight = new. Weight; COMP 110: Spring 200916

Static Variables & Methods Section 6. 2 in text 17 COMP 110: Spring 2009

Static Variables & Methods Section 6. 2 in text 17 COMP 110: Spring 2009

The Keyword Static The keyword static is a specifier that can be applied to

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; } 18 public double get. GPA() { return gpa; } COMP 110: Spring 2009

The Keyword Static The keyword static is used to indicate that only ONE copy

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 Units. And. Measures { //static, all objects share the SAME copy of this variable public static final int FEET_PER_YARD = 3; //NOT static, all objects have their OWN copy of this variable private int feet; } 19 COMP 110: Spring 2009

Example: Static Instance Variables A class that counts the number of method calls to

Example: Static Instance Variables A class that counts the number of method calls to ALL of its objects public class Static. Example { //static, all objects share the SAME copy of this variable private static number. Of. Calls = 0; } public void method() { number. Of. Calls++; } public class Static. Example. Tester { public static void main(String[] args) { Static. Example se = new Static. Example(); Static. Example se 2 = new Static. Example(); } 20 } se. method(); //changes number. Of. Calls to 1 se 2. method(); //changes number. Of. Calls to 2 COMP 110: Spring 2009

The Keyword Static The keyword static can also be used with methods The main

The Keyword Static The keyword static can also be used with methods The main method public static void main() { Static. Example se = new Static. Example(); Static. Example se 2 = new Static. Example(); se. method(); //changes number. Of. Calls to 1 se 2. method(); //changes number. Of. Calls to 2 } 21 COMP 110: Spring 2009

Using the Keyword Static When should you use the keyword static with a method?

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; } Does this method access any instance variables? No. Should be declared static return result; } 22 COMP 110: Spring 2009

Example public class Dimension. Converter { public static final int INCHES_PER_FOOT = 12; public

Example public class Dimension. Converter { public static final int INCHES_PER_FOOT = 12; public static final double convert. Feet. To. Inches(double feet) { return feet*INCHES_PER_FOOT; } public static double convert. Inches. To. Feet(double inches) { return inches / INCHES_PER_FOOT; } } 23 COMP 110: Spring 2009

Accessing Static Variables From outside the class, static variables that are declared public can

Accessing Static Variables From outside the class, static variables that are declared public can be accessed using the name of the class No Object is Specified! int inches. Per. Foot = Dimension. Converter. INCHES_PER_FOOT; Class Name 24 Static Variable Name COMP 110: Spring 2009

Calling Static Methods From outside the class, static methods that are declared public can

Calling Static Methods From outside the class, static methods that are declared public can be accessed using the name of the class No Object is Specified! int inches = Dimension. Converter. convert. Feet. To. Inches(12); Class Name 25 Static Variable Name COMP 110: Spring 2009

Restrictions on Static Methods Static methods CANNOT Access non-static instance variables Call non-static methods

Restrictions on Static Methods Static methods CANNOT Access non-static instance variables Call non-static methods Static methods CAN Be called by any method, static or non-static 26 COMP 110: Spring 2009

Restrictions on Static Methods Access Non Access Call Non Call -Static Variables Methods Static

Restrictions on Static Methods Access Non Access Call Non Call -Static Variables Methods Static methods Non-Static Methods 27 X √ √ √ COMP 110: Spring 2009

Example public class Circle { public static final double PI = 3. 14159; private

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 28 COMP 110: Spring 2009

Example public class Circle { public static final double PI = 3. 14159; private

Example public class Circle { public static final double PI = 3. 14159; private void print. Area(double area) { System. out. println(area); } } public static void area(double radius) { print. Area(PI * (radius * radius)); } Will this code compile? No. Cannot call non-static methods inside static methods 29 COMP 110: Spring 2009

Example public class Circle { public static final double PI = 3. 14159; private

Example public class Circle { public static final double PI = 3. 14159; private void print. Area() { 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 30 COMP 110: Spring 2009

Programming Demo Grade Distribution A class to display the distribution of letter grades in

Programming Demo Grade Distribution A class to display the distribution of letter grades in a class Given 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 grade Draw a bar graph of the grade distribution 31 COMP 110: Spring 2009

Programming Demo Output Each * == 2 percent 0 10 20 30 40 50

Programming Demo Output Each * == 2 percent 0 10 20 30 40 50 60 70 80 90 100 | | | ************************* A ******* B *****C *****D ***F 32 COMP 110: Spring 2009

Wednesday Math class Wrapper class Writing & Testing Methods 33 COMP 110: Spring 2009

Wednesday Math class Wrapper class Writing & Testing Methods 33 COMP 110: Spring 2009