COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.

Slides:



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

COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Exceptions Session 21. Memory Upload Creating Exceptions Using exceptions to control object creation and validation.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Overloading. Overloading Basics  We have seen that you can give the same name to methods in different classes.  You can also give the same name to methods.
Written by: Dr. JJ Shepherd
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
Chapter 3 Classes and Methods. 2 Knowledge Goals Appreciate the difference between a class in the abstract sense and a class as a Java construct Know.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
COMP 110 Errors, Strings, and Review Tabitha Peck M.S. January 28, 2008 MWF 3-3:50 pm Philips
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
Writing Classes (Chapter 4)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
COMP 110 Objects and references Luv Kohli October 8, 2008 MWF 2-2:50 pm Sitterson 014.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
COMP 110 Worksheet review, debugger Luv Kohli September 29, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
COMP 110 Static variables and methods Luv Kohli October 29, 2008 MWF 2-2:50 pm Sitterson 014.
Written by: Dr. JJ Shepherd
COMP 110 Branching Statements and Boolean Expressions Luv Kohli September 8, 2008 MWF 2-2:50 pm Sitterson
COMP 110 Designing and overloading methods Luv Kohli November 3, 2008 MWF 2-2:50 pm Sitterson 014.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
COMP 110 More about classes Luv Kohli October 3, 2008 MWF 2-2:50 pm Sitterson 014.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
COMP 110 Some more about objects and references Luv Kohli October 10, 2008 MWF 2-2:50 pm Sitterson 014.
Written by: Dr. JJ Shepherd
OBJECT ORIENTED PROGRAMMING
Arrays in Classes and Methods
Haidong Xue Summer 2011, at GSU
Intro To Classes Review
More Object Oriented Programming
Implementing Classes Chapter 3.
Questions? Math Class Wrapper Classes Writing / Testing Methods.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
Announcements Lab 6 was due today Lab 7 assigned this Friday
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Dr. R Z Khan Handout-3 Classes
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Announcements Lab 5 due Wednesday at noon.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014

Announcements Program 3 assigned today, due October 31, 2pm Ivan Sutherland talk today, 4pm, Sitterson Hall 011 2

Questions? How is Lab 5 going? 3

Today in COMP 110 A couple of notes Friday’s in-class exercise Constructors 4

Running a Java program Make 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

Local variables What 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

Local variables What 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

Creating a new instance public class AnotherExample { private Student jack; public void myMethod() { jack = new Student(); jack.setName(“Jack Smith”); jack.setAge(19); } 8

Friday’s in-class exercise 9

The perils of incorrectly initialized data 10

Spirit of Kansas crash, Feb. 23, After heavy rains, water affected air-data sensors These sensors feed angle of attack and yaw data to flight-control system Water distorted preflight readings in 3 of the plane’s 24 sensors Caused flight-control system to make an erroneous correction, making the plane stall and crash 11

Constructors Create and initialize new objects Special methods that are called when creating a new object Student jack = new Student(); 12 Calling a constructor

Creating an object Create an object jack of class Student Student 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

Constructors Can perform any action you write into a constructor’s definition Meant to perform initializing actions ◦ For example, initializing values of instance variables 14

Similar to mutator methods However, constructors create an object in addition to initializing it Like methods, constructors can have parameters 15

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; } 16 Default constructor

Example: Pet class, setPet method public void setPet(String newName, int newAge, double newWeight) { name = newName; age = newAge; weight = newWeight; } 17

A 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 Parameters Body

Initializing instance variables Constructors 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

Default constructor Constructor 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

Default constructor If you define at least one constructor, a default constructor will not be created for you 21

Several constructors You can have several constructors per class ◦ They all have the same name, just different parameters 22

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; } 23

Calling a constructor Pet 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

Change an object using mutators myPet.setPet(“Fang”, 1, 155.5); 25

Calling 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

Calling 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

Example: Pet class public 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

Wednesday Talk about Lab 5 More about constructors Static variables and methods 29