COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
-
Upload
linda-foster -
Category
Documents
-
view
216 -
download
2
Transcript of COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
![Page 1: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/1.jpg)
COMP 110COMP 110ConstructorsConstructors
Luv KohliOctober 13, 2008
MWF 2-2:50 pmSitterson 014
![Page 2: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsProgram 3 assigned today, due October
31, 2pm
Ivan Sutherland talk today, 4pm, Sitterson Hall 011
2
![Page 3: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/3.jpg)
Questions?Questions?How is Lab 5 going?
3
![Page 4: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/4.jpg)
Today in COMP 110Today in COMP 110A couple of notes
Friday’s in-class exercise
Constructors
4
![Page 5: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/5.jpg)
Running a Java programRunning a Java programMake sure you are running the program
that has a main method in it
Otherwise, you will get this:
java.lang.NoSuchMethodError: main
Exception in thread "main"
5
![Page 6: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/6.jpg)
Local variablesLocal variablesWhat does the greet() method output?
public class Example{ private String str = “hello”; public void doStuff() { String str = “goodbye”; } public void greet() { doStuff(); System.out.println(str); }}
Outputs hello. Why?doStuff() uses local variable str, not instance variable str
6
![Page 7: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/7.jpg)
Local variablesLocal variablesWhat does the greet() method output?
public class Example{ private String str = “hello”; public void doStuff() { str = “goodbye”; } public void greet() { doStuff(); System.out.println(str); }}
Outputs goodbye. Why?Make sure you understand this for step 1 of Lab 5!
7
![Page 8: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/8.jpg)
Creating a new instanceCreating a new instancepublic class AnotherExample
{
private Student jack;
public void myMethod()
{
jack = new Student();
jack.setName(“Jack Smith”);
jack.setAge(19);
}
}
8
![Page 9: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/9.jpg)
Friday’s in-class exerciseFriday’s in-class exercise
9
![Page 10: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/10.jpg)
The perils of incorrectly initialized dataThe perils of incorrectly initialized data
10
![Page 11: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/11.jpg)
Spirit of KansasSpirit of Kansas crash, Feb. 23, 2008 crash, Feb. 23, 2008http://www.youtube.com/watch?v=_ZCp5h1gK2QAfter heavy rains, water affected air-data sensorsThese sensors feed angle of attack and yaw data to
flight-control systemWater distorted preflight readings in 3 of the plane’s 24
sensorsCaused flight-control system to make an erroneous
correction, making the plane stall and crash
11
![Page 12: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/12.jpg)
ConstructorsConstructorsCreate and initialize new objects
Special methods that are called when creating a new object
Student jack = new Student();
12
Calling a constructor
![Page 13: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/13.jpg)
Creating an objectCreating an objectCreate an object jack of class StudentStudent jack = new Student();
Scanner keyboard = new Scanner(System.in);
Create an object keyboard of class Scanner
13
Create an object by calling a constructor
Return memory address of object
Assign memory address of object to variable
![Page 14: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/14.jpg)
ConstructorsConstructorsCan perform any action you write into a
constructor’s definition
Meant to perform initializing actions◦For example, initializing values of instance
variables
14
![Page 15: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/15.jpg)
Similar to mutator methodsSimilar to mutator methodsHowever, constructors create an object
in addition to initializing it
Like methods, constructors can have parameters
15
![Page 16: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/16.jpg)
Example: Pet classExample: Pet classpublic 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; }}
16
Default constructor
![Page 17: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/17.jpg)
Example: Pet class, setPet methodExample: Pet class, setPet methodpublic void setPet(String newName, int newAge,
double newWeight)
{
name = newName;
age = newAge;
weight = newWeight;
}
17
![Page 18: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/18.jpg)
A closer lookA closer look
public Pet(String initName, int initAge, double initWeight)
{
name = initName;
age = initAge;
weight = initWeight;
}
18
Same name as class name
No return type
ParametersBody
![Page 19: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/19.jpg)
Initializing instance variablesInitializing instance variablesConstructors give values to all instance
variables
Even if you do not explicitly give an instance variable a value in your constructor, Java will give it a default value
Normal programming practice to give values to all instance variables
19
![Page 20: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/20.jpg)
Default constructorDefault constructorConstructor that takes no parameters
public Pet(){ name = “No name yet.”; age = 0; weight = 0;}
Java automatically defines a default constructor if you do not define any constructors
20
![Page 21: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/21.jpg)
Default constructorDefault constructorIf you define at least one constructor, a
default constructor will not be created for you
21
![Page 22: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/22.jpg)
Several constructorsSeveral constructorsYou can have several constructors per
class◦They all have the same name, just different
parameters
22
![Page 23: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/23.jpg)
Example: Pet classExample: Pet classpublic 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; }}
23
![Page 24: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/24.jpg)
Calling a constructorCalling a constructorPet myPet;
myPet = new Pet(“Frostillicus”, 3, 121.5);
You cannot use an existing object to call a constructor:
myPet.Pet(“Fang”, 3, 155.5); // invalid!
24
![Page 25: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/25.jpg)
Change an object using mutatorsChange an object using mutatorsmyPet.setPet(“Fang”, 1, 155.5);
25
![Page 26: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/26.jpg)
Calling methods from constructorsCalling methods from constructors
Just like calling methods from methods
public Pet(String initName, int initAge, double initWeight){ setPet(initName, initAge, initWeight);}
public void setPet(String newName, int newAge, double newWeight){ name = newName; age = newAge; weight = newWeight;}
26
![Page 27: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/27.jpg)
Calling methods from constructorsCalling methods from constructors
Can cause problems when calling public methods◦Problem has to do with inheritance, chapter 8◦Another class can alter the behavior of public
methods
Can solve problem by making any method that constructor calls private
27
![Page 28: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/28.jpg)
Example: Pet classExample: Pet classpublic class Pet{ private String name; private int age; private double weight;
public Pet(String initName, int initAge, double initWeight) { set(initName, initAge, initWeight); }
public void setPet(String newName, int newAge, double newWeight) { set(newName, newAge, newWeight); }
private void set(String newName, int newAge, double newWeight) { name = newName; age = newAge; weight = newWeight; }}
28
![Page 29: COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.](https://reader030.fdocuments.us/reader030/viewer/2022032414/56649eec5503460f94bfde7b/html5/thumbnails/29.jpg)
WednesdayWednesdayTalk about Lab 5
More about constructors
Static variables and methods
29