Docsity

Notes on Multiple Constructors - Introduction to Programming in Java | CSIS 110, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 8
Last Time
We looked at the NumberDisplay class:
- Abstraction and modularization
- boolean operations: &&, ||, and !
- Modulo operator: %
ClockDisplay
Now create a ClockDisplay object and play around with it a little bit.
Let’s look at the fields for this class:
private NumberDisplay hours;
private NumberDisplay minutes;
private String displayString;
Important point – two of these fields have the type NumberDisplay, which is the class
we just looked at.
So, where do these two objects come from?
Look at the first constructor (the one with no arguments).
hours = new NumberDisplay(24);
minutes = new NumberDisplay(60);
These statements invoke the NumberDisplay constructor (just like you did by hand from
BlueJ when you select that choice from the class menu. However, you won't see them
displayed in BlueJ.)
So, when we create a new ClockDisplay, its constructor automatically creates the two
NumberDisplay objects for us.
It does this using the new operator, which is used in statements this way:
new ClassName( parameter-list );
The new operation does 2 things:
1) It creates a new object of the named class (for example, NumberDisplay)
2) It invokes that class’s constructor.
If the constructor is defined with formal parameters, then the new operation needs to
specify values for the actual parameters.
pf3
pf4

Partial preview of the text

Download Notes on Multiple Constructors - Introduction to Programming in Java | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 8

Last Time We looked at the NumberDisplay class:

  • Abstraction and modularization
  • boolean operations: &&, ||, and!
  • Modulo operator: % ClockDisplay Now create a ClockDisplay object and play around with it a little bit. Let’s look at the fields for this class: private NumberDisplay hours; private NumberDisplay minutes; private String displayString; Important point – two of these fields have the type NumberDisplay, which is the class we just looked at. So, where do these two objects come from? Look at the first constructor (the one with no arguments). hours = new NumberDisplay(24); minutes = new NumberDisplay(60); These statements invoke the NumberDisplay constructor (just like you did by hand from BlueJ when you select that choice from the class menu. However, you won't see them displayed in BlueJ.) So, when we create a new ClockDisplay, its constructor automatically creates the two NumberDisplay objects for us. It does this using the new operator, which is used in statements this way: new ClassName( parameter-list ); The new operation does 2 things:
  1. It creates a new object of the named class (for example, NumberDisplay)
  2. It invokes that class’s constructor. If the constructor is defined with formal parameters, then the new operation needs to specify values for the actual parameters.

[Draw a picture of what this looks like] KEY POINT: An object can create other objects. (We saw this before with the picture project). Multiple Constructors Let’s look at the menu when we right-click the ClockDisplay class – there are two ways to create a new ClockDisplay. What’s going on? The ClockDisplay class contains two constructors! How do they differ? This allows us to initialize a ClockDisplay in different ways – we can set it to 00:00, or we can set it to any time that we want. [What happens if we try to create a ClockDisplay with invalid time?] This is called overloading the constructor. Internal Method Calls There are some statements in the constructors that look a little different: updateDisplay(); setTime( hour, minute ); These are method calls. If we look at the ClockDisplay class, we see that it includes these methods: public void updateDisplay() public void setTime( int hour, int minute ) The above statements call (or invoke) the method – because the methods are defined in the same class as the call statement, we call these internal method calls. So, when we see this statement, we call that method for the current object. When a method is called, control is transferred to the method. The statements in the method are executed, When the method is done, control is transferred back to the calling statement, and then continues with the statement after that. External Method Calls Let’s look at the timeTick method: It includes statements such as: minutes.increment(); if ( minutes.getValue() == 0 ) { hours.increment();

Lab Exercise: Make a new copy of the clock project. (Do not use the project that you modified in the last lab). Enhance the ClockDisplay so that it works like a 12 hour clock instead of a 24 hour clock. That is, the hours should count from 1 to 12, and the minutes should count from 0 to 59. Do NOT modify the NumberDisplay class – make all of your changes to the ClockDisplay class. Make sure that your names are in the header comment. Email the file ClockDisplay.java to me with the subject "Lab 5". Lab Exercise: Make a new copy of the clock project. (Do not use the project that you modified in the last lab). Enhance the ClockDisplay so that it works like a 12 hour clock instead of a 24 hour clock. That is, the hours should count from 1 to 12, and the minutes should count from 0 to 59. Do NOT modify the NumberDisplay class – make all of your changes to the ClockDisplay class. Make sure that your names are in the header comment. Email the file ClockDisplay.java to me with the subject "Lab 5".