Problem of the Day  Why are manhole covers round?

Slides:



Advertisements
Similar presentations
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
Advertisements

Road Map Introduction to object oriented programming. Classes
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
CSC 212 – Data Structures Lecture 3: Fields, Methods, & Constructors.
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.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
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.
Introduction to Programming Writing Java Beginning Java Programs.
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.
CSC 212 Object-Oriented Programming and Java Part 1.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Problem of the Day  Why are manhole covers round?
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Problem of the Day  Why are manhole covers round?
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 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
ECE122 Feb. 22, Any question on Vehicle sample code?
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
Introduction to Programming Writing Java Beginning Java Programs.
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!
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes and Methods. Classes Class Definition Data Fields –Variables to store data items –Differentiate multiple objects of a class –They are called.
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?
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
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!
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
05 Method Calling. 2 What is a Method? Declaring a Method Method Calling Method Call Stack Parameter Passing Pass by Value Outline.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
CSC 212 – Data Structures Lecture 6: Static and non-static members.
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.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Question of the Day  How do you add 5 matches to these 6 & make 9?
Lecture 3: Fields, Methods, & Constructors
Java Programming: Guided Learning with Early Objects
Java Primer 1: Types, Classes and Operators
Variables ICS2O.
Group Status Project Status.
Interfaces.
Building Java Programs
CS2011 Introduction to Programming I Methods (II)
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Building Java Programs
Java Programming Language
In this class, we will cover:
Lecture 8-2: Object Behavior (Methods) and Constructors
Corresponds with Chapter 5
Presentation transcript:

Problem of the Day  Why are manhole covers round?

Problem of the Day  Why are manhole covers round? No matter how circle rotated, radius is constant… …so only shape that cannot fall into the hole

Announcements  Weekly Assignment #1 scores will be ed  Also updated on Angel (“refresh” to get latest scores)  used for first weeks, better solution soon  Similar process for labs (no s for last week)  Weekly assignments goal to help students learn  Checks that you understand & can apply ideas in code  Due on Tuesdays, but problems mapped to lectures  Start early  Start early enough you can get help; use time wisely

Classes vs. Objects  Classes are blueprints describing data type  Classes (usually) cannot do anything on their own  Objects are instances of a class  New objects created (instantiated) using new  Fields describe state of an object  Object’s behavior represented by methods

Methods  Define how objects act, behave, & change  Need unique name & parameters (“signature”)  Can share name or parameter list, but not both  Determines which code used at any method call  Methods also define return type not  Note that return type not part of signature  Unhelpful finding method to call at any line in code

Return Type  Methods must specify their return type  Primitive or reference type if they will return 1 value double squareIt(float x); int[] primes(int n); Professor getFavoriteTeacher();  Use special return type, void, if no value is returned void goodSoldier(); void doSomethingWithX(int x);

return Examples

 Function ends when return is executed  Once we hit return, method immediately stops  Calling function will resume its execution  If code exists after return, method will not compile return Statement

Methods' Parameters

Are Parameters Variables?  At start of method, assigned value of argument  Works like normal assignment for the parameters type  As would expect, primitives copied; references aliased  Parameters “live” only during call & die once over  Parameter placed in memory at top of frame on stack  Variables also found in frame just at lower addresses

Are Parameters Variables?  At start of method, assigned value of argument  Works like normal assignment for the parameters type  As would expect, primitives copied; references aliased  Parameters “live” only during call & die once over  Parameter placed in memory at top of frame on stack  Variables also found in frame just at lower addresses

Locals  Variables declared inside a method  Must be assigned value before using  “Live” only for code block in which declared  Declare local in smallest enclosing block  Local variable better choice than other variable  Helps insure design, algorithm, & code is simple  Reduces opportunities for bugs dramatically  Added bonus, creates fewer comments to write!

B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } }

B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } This is fine. It specifies the class in which the method is located.

B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } This is also fine, since the method is in the same class

B is for Bad public class BDuck { public static void main(String[] args) { int quackVolume; System.out.println(“Volume is ”+BDuck.getVolume()); BDuck duck = new BDuck(); quackVolume = 11; System.out.println(“Volume is ”+ getVolume()); } public static int getVolume() { return quackVolume; } } Compiler error. quackVolume is local to main() & getVolume() is separate method

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

B is for Bad public class BDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BDuck.duckSpeak()); BDuck duck = new BDuck(); System.out.println(duckSpeak()); } } This works. The method is in another class, but we specify which class it is in.

B is for Bad public class BDuck { public static String duckSpeak() { return “quack”; } } public class UseDuck { public void speak() { System.out.println(BDuck.duckSpeak()); BDuck duck = new BDuck(); System.out.println(duckSpeak()); } } Compiler error here, since duckSpeak() is not defined by the UseDuck class

Constructors  Special methods called to instantiate object  Identical name as class; can have 0 or more params  If signatures differ, multiple constructors possible  No return type (not even void ) allowed must  Arguments in new must match 1 constructor  If no constructor defined, implicit one used for class  No parameters included in implicit constructor

Tracing With Methods  At top of stack add frame for every method call  frame is fancy talk for box labeled with method name  Inside frame should include method’s locals & params if  Implicit this in frame if method not static  When method ends, remove frame from stack  Lightly crossing out frame easiest way of doing this  Process is the same no matter how method ends  Goes back to uncrossed out frame at top of stack

Method Trace public class Playa { double static avg(int hits, int pa) { int aB = 3; double retVal = hits/(double)aB; aB = pa; return retVal; } public static void main(String[] args) { Playa h8r = new Playa(); double retVal = avg(10, 100); retVal += avg(0, 10); Playa star = new Playa(); double hits = avg(60, 200); } }

Your Turn  Get into your groups and complete activity

For Next Lecture  Keep reviewing your Java lessons  Should be getting back up-to-speed with Java basics  Friday will discuss how fields in an object work  How to store values between calls to a method?  There are weekly assignment problem on Angel  Due before 5PM on Tuesday as will be the normal case  Should help finish removing rust from lazy summer WILL NOT MEET  Lab WILL NOT MEET tomorrow (sorry!)  Two activities will be posted & are due before next lab