BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Lecture 2 Calling and Defining Methods in Java. Introduction ●Calling and defining methods ●Declaring and defining a class ●Instances of a class ●The.
2/18/2008ITK 1681 Feb. 20, Wed. 8:00 – 9:30 pm STV Closed book, notes, and no computer is allowed. 2.Everyone is allowed to bring a self- prepared.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
ITEC200 – Week03 Inheritance and Class Hierarchies.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Programming Week 2 James King 12 August Creating Instances of Objects Basic Java Language Section.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
BIT 115: Introduction To Programming1 Sit in front of a computer Log in –Username: 230class –password: –domain: student Bring up the course web.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming Instructor: Mike Panitz
1 karel_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Constructors & Garbage Collection Ch. 9 – Head First Java.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 3- Introduction to Object Oriented Programming concepts Topic 3.4 Constructors, Overloading,
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
For Friday Finish reading chapter 2 Complete WebCT quiz.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Lecture 11 Instructor: Craig Duckett Instance Variables.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Monday, October 10 th I will double dog try to have.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.
University of Central Florida COP 3330 Object Oriented Programming
BIT115: Introduction to Programming
BIT115: Introduction to Programming
University of Central Florida COP 3330 Object Oriented Programming
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CS240: Advanced Programming Concepts
BIT115: Introduction to Programming
CS2102: Lecture on Abstract Classes and Inheritance
Packages and Interfaces
Interfaces.
Java – Inheritance.
BIT 115: Introduction To Programming
Presentation transcript:

BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett

BIT 115: Introduction To Programming2 Lecture 3 Announcements By now everyone should be up and running with Java, jGRASP, and the Becker Robots on their home or personal computers. Any Problems? Has everyone had a chance to work with the Java programs and the Becker Robots? Reading Assignment for Today Appendix F.1 – Extending a Class Chapter 2.1, 2.2 – Extending Robot Class Chapter 2.4 – Coding Style

3 What? Tinnitus! That’s What!

BIT 115: Introduction To Programming4 Assignment 1 is Due LECTURE 5 Assignment 1 is Due LECTURE 5 Zipped & Uploaded to StudentTracker by MIDNIGHT NEXT Tuesday, October 13, ONE WEEK FROM TODAY It’s posted on the website under Assignments menu midnight –It will be due by midnight If unsure how to upload to StudentTracker, then bring your work to class, in electronic form, and we will go over how to hand in the homework: Student Tracker How to Use Student Tracker Student Tracker How to Use Student Tracker If you’re stuck, seek help –Talk to the Instructor or a classmate – me FYI: You should have enough information after today’s lecture to successfully complete Assignment 1 HOMEWORKHOMEWORK

5 Assignment 1 (LECTURE 5) Tuesday, October 13 in StudentTracker by midnight Assignment 2 (LECTURE 8) Thursday, October 22 Assignment 1 Revision (LECTURE 10) Tuesday, November 3 Assignment 2 Revision (LECTURE 12) Tuesday, November 10 Assignment 3 (LECTURE 13) Thursday, November 12 Assignment 3 Revision (LECTURE 16) Tuesday, November 24 Assignment 4 (LECTURE 20) Tuesday, December 8 Assignment 4 Revision (LECTURE 21) Thursday, December 10 Assignment Dates (By Due Date)

Going forward, always leave your completed quiz beside your throughout the Lecture (and the Quarter) because your Instructor will use these to learn your names 6 You each get a Quiz hand-out: Put your name on it When you think your drawing is complete, raise your hand –5 minute limit The First Quiz And Now…. The First Quiz!

IMPORTANT ! CityFrame – An older version of becker.jar file contained a class called CityFrame which when used looked something like this: CityFrame City = CityFrame(someCity); Please ignore any reference to this. It will, however, rear its ugly head in the ICEs and Assignments on purpose as an example of old 'legacy' code that should either be " commented out " with // or deleted altogether since it will not allow the program to run correctly! 7 Please Note! CityFrame! // CityFrame City = CityFrame(someCity);

Lecture 3 Lecture 3 Buckle up! This could really 8 Extending a Class : Creating a new type of Robot Style and Java Coding Conventions

Appendix F.1, Chapter 2.1, 2.2 Extending a Class BIT 115: Introduction To Programming9 Extension (B  extends  A) Extending the Robot Class Superclass and Subclass Constructor Adding a Method (Service) turnAround(); turnRight(); The This Keyword (Implicit Parameter) Putting It All Together

Constructors BIT 115: Introduction To Programming10 Here, when we create a new instance (an object) of the Robot class, a ‘hidden’ default constructor works in the background to make sure that Kelsey inherits all the attributes and methods available to Robots, including its placement on a particular Street and Avenue and Direction in a particular City, and that it can use all of the actions (methods) available to the Robot class (including move(), pickThing(), turnLeft(), putThing(), frontIsClear(), etc.)

BIT 115: Introduction To Programming11 Constructors have one purpose in life: to create an instance of a class. This can also be called creating an object, as in: The purpose of a method, by contrast is much more general. The purpose of a method is to execute Java code, to act, to allow the object to do something. Constructors

BIT 115: Introduction To Programming12

BIT 115: Introduction To Programming13 N ow … what if these is an action that you might want Kelsey to do that isn’t found in the Robot class? For instance, instead of invoking the the turnLeft() method three times, you could just call up a turnRight() ? The problem is, the Robot class does not have a turnRight() command (method). The Robot class has been finalized. You cannot add to it. The good news is, you can create a new method like turnRight() that will do what you want the robot to do! But in order to make this happen, you need to extend the Robot class … In other words, you’re going to make a new class from the Robot class so you can add new methods to it like turnRight()

Extending a Class: Where Class B extends Class A 14 In Plain Old English: Where Class B “inherits” all the attributes and actions of Class A and then adds it’s own functionality by creating new methods public class Example extends Object When we’re not interested in extending a class because we’re happy with the methods that come with that class just the way they are, then we declare our class the ‘normal’ default way: However, if we want to add new functionality (methods) to the Robot class (like turnRight) then we need to extend the Robot class (which is itself an extension of Object) public class MrRoboto extends Robot Object is the top class of all class hierarchies. When a new instance of anything is made in Java, then it inherits all the attributes and actions of the Object class. You can’t get a new object without Object. Class Hierarchy Object

Extending a Class: Where ClassB extends ClassA BIT 115: Introduction To Programming15 Instantiation (Instance) vs. Extension? Instantiation creates a new object from a class, but extension extends a new class from a class through inheritance, allowing for an improved class that might offer additional attributes and services (methods) not available in the original class …

16 Think of it as adding an extension to a house. You still get to use all of the original house, but you also get to use the new section you added to the house

Extending the Robot Class BIT 115: Introduction To Programming17 public class MrRoboto extends Robot Robot MrRoboto MrRoboto “inherits” all of the Robot attributes and services and then can have additional attributes and services of its own (i.e., those not shared by Robot). extends inherits

Superclass and Subclass BIT 115: Introduction To Programming18 Robot MrRoboto Superclass Subclass

Constructor BIT 115: Introduction To Programming19 import becker.robots.*; public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } //New service or services go here }

Constructor BIT 115: Introduction To Programming20 import becker.robots.*; public class MrRoboto extends Robot { // This declares the parameters used by Robot “inside” of MrRoboto public MrRoboto(City theCity, int street, int avenue, Direction aDirection) // This passes on information received by the parameters used by Robot ‘inside” of MrRoboto { super(theCity, street, avenue, aDirection); //Instead of Robot here, Java uses the keyword super } //New service or services go here } Constructors fulfill a special roll. They are responsible for ensuring an object is set up properly when it is created, and that it can be immediately used once it is created. This construction process is known as initialization. Two other details about constructors: they must have the same name as the class and they do not have a return type, not even a void. NOTE: We will talk briefly about return types in just a few minutes, and go over them in greater detail in an upcoming lecture.

Constructor BIT 115: Introduction To Programming21 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } Robot MrRoboto

Constructor BIT 115: Introduction To Programming22 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } Robot super MrRoboto

Constructor BIT 115: Introduction To Programming23 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } super MrRoboto  imagine a conduit …

Constructor BIT 115: Introduction To Programming24 public class MrRoboto extends Robot { public MrRoboto(City theCity, int street, int avenue, Direction aDirection) { super(theCity, street, avenue, aDirection); } } super MrRoboto bothell, 3, 2, Direction.SOUTH Since MrRoboto is inheriting the Robot parameters, the Robot still needs those parameters in order for MrRoboto to inherit them. This is why it appears as if there are two sets of parameters: one set to pass through MrRoboto, a second set for Robot to receive them, where Robot sends them back to MrRoboto by extension.

Adding New Methods (Actions) 25 public void turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); }. public void turnRight() { this.turnAround(); this.turnLeft(); }

The this keyword 26 The new Java feature in the new services we created is the use of the this keyword. The keyword this is useful when you need to refer to an instance of the class from its method, but without having to refer to it by a specific name. Why? Because when you create the new method, you don’t know the name of the particular robot that is going to use it, so ‘this’ is a kind of placeholder name. The this keyword helps us to avoid name conflicts, and also creates a shortcut to having to invent a unique name for each field in the different methods. public void turnAround() { this.turnLeft(); this.turnLeft(); }

Putting It All Together 27 Two Ways of Doing the Same Thing, however: THE CLASS THAT CONTAINS MAIN HAS TO BE THE SAME NAME AS THE FILE Version 1: One Class MrRoboto.java Version 2: Two Classes MrRobotoMain.java

Putting It All Together 28 MrRoboto2.java MrRobotoTest2.java MrRoboto.java All on One FileOn Two Separate Files

Chapter 2.4 – Coding Conventions (Style) BIT 115: Introduction To Programming These coding conventions are not only good for Java, but for other languages as well, including C, C++, C#, JavaScript, Perl, Python, etc, to name a few.

Lecture 3 ICE: Creating a New Type of Robot BIT 115: Introduction To Programming30