Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Lab 5 was due today Program 3 due Monday by 12pm

Similar presentations


Presentation on theme: "Announcements Lab 5 was due today Program 3 due Monday by 12pm"— Presentation transcript:

1 Announcements Lab 5 was due today Program 3 due Monday by 12pm
Sample Midterm due Monday in class

2 Questions? Objects & References

3 Today in COMP 110 Review end of last lecture Constructors
Programming Demo (if time permits) In-Class Exercise

4 Creating Objects Why does this look like a method call?
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

5 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

6 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

7 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

8 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;

9 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 Constructors w/ Parameters
Like methods, constructors can have parameters public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; }

11 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

12 Multiple Constructors
You can define multiple constructors All have the same name, but different parameters Group their definitions together

13 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.setName("Roberto"); myPet.setAge(1); myPet.setWeight(150.0);

14 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; 14

15 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

16 Programming Demo Output Each * == 2 percent
| | | | | | | | | | | ************************************************** **** A ************** B *********C *****D ***F

17 Friday Recitation No new lab will be posted
Get help finishing Program 3 Ask questions from practice midterm If you have submitted the Program and completed the practice midterm, you may: show me your completed practice midterm sign out and enjoy your weekend


Download ppt "Announcements Lab 5 was due today Program 3 due Monday by 12pm"

Similar presentations


Ads by Google