Question of the Day  How do you add 5 matches to these 6 & make 9?

Slides:



Advertisements
Similar presentations
Lecture 2: Object Oriented Programming I
Advertisements

CSC Programming for Science Lecture 30: Pointers.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 212 – Data Structures Lecture 12: Java Review.
Question of the Day  What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Problem of the Day  Why are manhole covers round?
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Question of the Day  Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
CSI 3125, Preliminaries, page 1 Compiling the Program.
ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Topics Instance variables, set and get methods Encapsulation
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Problem of the Day  Why are manhole covers round?
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Defining Your Own Classes II
Lecture 3: Fields, Methods, & Constructors
CSE 116/504 – Intro. To Computer Science for Majors II
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Inheritance and Encapsulation
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Review Session.
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
CMSC 202 Static Methods.
CSC 143 Inheritance.
Chapter 9 Inheritance and Polymorphism
Lecture 11 C Parameters Richard Gesick.
Pass by Reference, const, readonly, struct
Interface.
Java Programming Language
Building Java Programs
Inherited Classes in Java
Group Status Project Status.
Interfaces.
Classes and Objects 5th Lecture
Object Oriented Programming
Building Java Programs
Object Oriented Programming in java
Reference Parameters.
Java Lesson 36 Mr. Kalmes.
Unit 3 Test: Friday.
Building Java Programs
Classes and Objects Static Methods
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Lecture 8-2: Object Behavior (Methods) and Constructors
ITE “A” GROUP 2 ENCAPSULATION.
CS 240 – Advanced Programming Concepts
CSG2H3 Object Oriented Programming
Presentation transcript:

Question of the Day  How do you add 5 matches to these 6 & make 9?

Question of the Day  How do you add 5 matches to these 6 & make 9?

Announcements  Weekly assignments problems due tomorrow  Helps get rid of rust; last week of reviewing material  Make sure that your coding skills are back up to speed  Next week JUnit tests provided for each problem

Calling Methods  Some methods really need an object  Integer. intValue() is unclear without an object  But other methods independent of an instance  main(String[]) lacks object to use to call it  Why require Integer to call parseInt(String) ?

static Methods  static methods do not need an instance  Behavior defined by class, not an instance  parseInt(String) based only on Integer  All methods similar in how written & executed  May or may not include parameters  Local variables can be declared and used  Must either be void or declare return type  Mix of methods possible for all classes & enums

Making Method static  Include static keyword in method declaration public static void main(String args[]) public static int parseInt(String s) private static int nextInt()  Behaves like any other method (almost)  Use locals & parameters like normal  As with other code, call (nearly all) other methods  Java operators can be used  Perform any I/O operations

static v. Non- static  Methods that are non- static have this  Aliased to instance on which method called  Can directly use fields & call all methods  No this parameter in static methods  Code directly using non- static members illegal…  … using static fields & methods perfectly legal  As always, can use object to access its members

BadDuck public class BadDuck { private int quackVolume; public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume()); } public int getVolume() { return quackVolume; } }

BadDuck public class BadDuck { private int quackVolume; public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume()); } public int getVolume() { return quackVolume; } } Compiler error. Without an object, whose volume are we getting?

BadDuck public class BadDuck { private int quackVolume; public static void printVolume() { System.out.println(“Volume is ” + getVolume()); BadDuck duck = new BadDuck(); duck.quackVolume = 11; System.out.println(“Volume is ”+duck.getVolume()); } public int getVolume() { return quackVolume; } } This is good. There is a duck to get the volume.

BadDuck public class BadDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak()); } }

BadDuck public class BadDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak()); } } This compiles. Static methods do not use an object, so this is preferred way to call them.

BadDuck public class BadDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BadDuck.duckSpeak()); BadDuck duck = new BadDuck(); System.out.println(duck.duckSpeak()); } } Also fine, but since method is static, still cannot access instance.

static Fields  Some data belongs to class, not instance Float.MAX_VALUE Citizen.nationalPopulation Instructor.bestProfessor  static fields used for any class-based data  All of the class instances see same value  Do not need an instance to access this value  Can be updated via any instance or via class

static Code for Tracing public class HHL { static int numTeams; String name; HHL(String newName) { numTeams += 1; name = newName; } public static void main(String[] args) { HHL me = new HHL(“MHz”); HHL you = new HHL(“SFH”); HHL alias = you; alias.name = “Bob”; alias.numTeams = 10; } }

Instance v. Class Review  Class owns members declared as static  Accessing & using member possible without instance  Methods lack implicit this parameter no matter what  To use a non- static member, instance required  Fields’ data instance-specific and not shared by class  Can access all members using implicit this parameter  Other instances’ members accessible with reference

Your Turn  Get into your groups and complete activity

For Next Lecture  Reading about packages & visibility for Wed.  What are packages and what do they do?  What are the different visibility modifiers in Java?  What is javadoc and why is it so amazingly useful?  There is weekly assignment posted on Angel tomorrow  Assignment due tomorrow; working week-to-week  While not due daily, problems still follow lectures