Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014."— Presentation transcript:

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

2 COMP 110: Spring 20092 Announcements Lab 5 due tomorrow by midnight Extending Program 3 to Monday by 5pm A sample midterm has been posted

3 COMP 110: Spring 20093 Questions?

4 COMP 110: Spring 20094 Lab 4

5 COMP 110: Spring 20095 Program 3

6 COMP 110: Spring 20096 Today in COMP 110 In-Class Exercise Constructors

7 COMP 110: Spring 20097 In-Class Exercise Work in Groups

8 COMP 110: Spring 20098 Constructors Section 6.1 in text

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

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

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

12 COMP 110: Spring 200912 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; }

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

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

15 COMP 110: Spring 200915 Constructors w/ Parameters Like methods, constructors can have parameters public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; }

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

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

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

19 COMP 110: Spring 200919 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; } 19

20 COMP 110: Spring 200920 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 Fs, compute the percentage of each type of grade e.g. 15% As, 30% Bs, 30% Cs, 15% Ds, 10% Fs Include accessors and mutators for each type of grade Draw a bar graph of the grade distribution

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

22 COMP 110: Spring 200922 Friday Recitation No new lab will be posted Get help finishing Program 3 Ask questions about lecture material for midterm


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

Similar presentations


Ads by Google