CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Fields, Constructors, Methods
Written by: Dr. JJ Shepherd
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
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,
Access to Names Namespaces, Scopes, Access privileges.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
CS102--Object Oriented Programming Review 1: Chapter 1 – Chapter 7 Copyright © 2008 Xiaoyan Li.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Inheritance Part II. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Understanding class definitions Looking inside classes.
Object Oriented Software Development
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 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.
The Java Programming Language
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
CS100A, Fall Lecture 4 1 CS100A, Fall 1997 Lecture, Thursday, 12 September. This lecture continues the discussion of classes. In addition, the following.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Classes Java and other Object-Orientated Programs or OOP are based on the creation and interaction of classes. Every program in Java has at least one class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
OOP Basics Classes & Methods (c) IDMS/SQL News
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
Object-Oriented Concepts
Chapter 4 Repetition Statements (loops)
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
Java Programming Language
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Sit next to someone. Today, we do some work in pairs.
Java Inheritance.
Tyler, Jonah, Abby, Gavin, Zain, Matt
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
(We’ll spend a few lectures on iteration)
Presentation transcript:

CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out

CS100Lecture 62 Today’s Topics Review of Friday Review of topics covered so far Scope of variables Introduction to while

CS100Lecture 63 Review of Friday Inheritance: subclass vs. superclass The method toString() Overriding methods super() public vs. private

CS100Lecture 64 Confusion on Strings String s = new String("Millett"); String t = new String("Millett"); if (s == t) System.out.println("s and t are equal!"); String r = "Millett"; String q = "Millett"; if (r == q) System.out.println("r and q are equal!"); if (s == r) System.out.println("s and r are equal!");

CS100Lecture 65 Today: Definitions of basic entities that make up a Java program Syntax of these entities Memorize these Be able to come up with definitions and examples on a test

CS100Lecture 66 Class A “model” or blueprint for the objects (or instances) of the class; a class defines the fields (or variables) and methods of each object of the class. Analogy: a blueprint for a house is a design for a house, many houses (objects) can be built from the same blueprint, but they may differ in color of rooms, wallpaper, etc. Java syntax: public class {declaration of fields and methods } Example: public class C {int x; String s; public C(String sp); {x= 0; s= sp;} public void addToX(int y) {x= x+y;} }

CS100Lecture 67 Variable A named box that can contain the value of some type or class. For a type like int, the value is an integer. For a class, it is the name of (or reference to) an instance of the class Declaration: a definition of the name of the variable and the type or class of value it can containt Syntax: ; Examples: // a variable x that can contain an integer int x; // variable s that can contain the name of an object of class String String s; // a variable that can contain a boolean value (true or false) boolean b;

CS100Lecture 68 Method A parameterized sequence of statements, whose execution performs some task. In Java, there are three kinds of methods: –procedures, –functions, –constructors. The next few slides describe them

CS100Lecture 69 Notes on methods A method should be accompanied by a comment that says what the method does. This is the specification of the method. The comment has to be precise and clear. A potential user of the method should be able to look only at the comment and the list of parameters to know how to use it; they should not have to look at the list of instructions. Example: When you want to bake a cake, you look at the title of a recipe, a short description, and the list of ingredients to determine whether you want to use that recipe --not the list of instructions to bake it.

CS100Lecture 610 Procedure A method that performs some task and doesn’t return a value Java syntax: // comment that explains what the procedure does public void ( ) { sequence of statements to execute } Example: // Raise the salary by n dollars if the salary is <$20000 public void raiseSal(double n); {if (salary < 20000) salary= salary + n; } Example procedure call: raiseSal(20 * y);

CS100Lecture 611 Function A method that performs some task and returns a value. Instead of the keyword void, the type or class of the return value is used. Statement return is used to terminate execution of a function call and return. Syntax: // Comment that explains what the function does. It should include // something like “Yield …” to describe what the function returns. public ( ) {Sequence of statements to execute} Example: // Yield the maximum of x and y public int max (int x, int y); {if (x>= y) return x; return y; } Example function call (within some statement) z = 1 + max(x, y);

CS100Lecture 612 Constructor A method that initializes the fields of an instance of a class when the instance is created. Syntax: // Comment that explains what the constructor does. public ( ) {Sequence of statements to execute} Example (within class Employee): // Constructor: an Employee with name n,salary 0, and year hired d public Employee Employee (String n, int d); {name= n; salary= 0; yearHired= d; } Example use (when creating a new instance): d = new Employee(“Millett”, 1999);

CS100Lecture 613 Execution of Assignment Execution of an assignment statement stores a value in a variable Syntax: = ; Examples: b = 2 + c; s = “Millett” + “ ” + yearHired;

CS100Lecture 614 Block A block is used to unify a sequence of statements into a single statement. Syntax: { sequence of statements } Example: Here is a sequence of statements: a = 10; if (a < c) then a = c; Here is a single statement which is a block: { a = 10; if (a < c) then a = c; }

CS100Lecture 615 Conditional Statement Execution of a conditional statement allows a choice of execution Syntax: if ( ) or if ( ) else

CS100Lecture 616 Subclass A subclass B (say) is a class that extends another class A (say). This means that an instance of B has all the fields and methods that an instance of A has, in addition to the ones declared in B. Syntax: public class extends { declarations of fields and methods } Example: public class VIP extends Employee {private double bonus; // Constructor: an VIP with name n, salary s, bonus b, and year hired d public VIP (String n, double s, double b, int d) {super(n,d); salary= s; bonus= b;} } Note the use of super to call a constructor of the superclass Employee

CS100Lecture 617 Access modifiers Suppose d is an instance of Employee, where class Employee is declared as: public class Employee { int x; … } If the is: –public, then field d.x can be referenced anywhere that d can be referenced. –private, then field d.x can be referenced anywhere within class Employee that d can be referenced –protected, then field d.x can be referenced anywhere within the package in which Employee is declared (packages are discussed later).

CS100Lecture 618 Scope of Variables public class Class1 {public int x; public int y; // Constructor: … public Class1 (int z) {z= z; y= 2*z;} // Set y to the maximum of p and -p public void sety(int p) { int x; x= p; if (p < -p) x= -p; y= x;}} A field of Class1 parameters A local variable of method sety

CS100Lecture 619 More on Scope The scope of a variable is the set of places in which it can be referenced. A variable can be declared only once within a method. We call this a local variable. The scope of a local variable of a method is the sequence of statements following it Example: // Set y to the maximum of p and -p public void sety(int p) {int x; x= p; if (p > -p) x= -p; y= p; } Scope of x

CS100Lecture 620 Another example //... public sety(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; y= -y; } Scope of x x cannot be referenced here

CS100Lecture 621 Scope of parameters The scope of a parameter of a method is the method body. A parameter may not be redeclared within the body. //... public test(int p) {if (y= p); {int x; x= p; if (p > -p) x= -p; } y= p; } Scope of p

CS100Lecture 622 Scope of fields The scope of a field of a class consists of: (1) the bodies of all methods declared in the class and (2) all declarations of fields that follow the declaration of the field. public class Text {int x= 5; int y= x+15; public void test(int p) {if (x= p); {int x= 35; x= p; if (p > -p) x= -p;} x= p; }

CS100Lecture 623 The while loop -- briefly Computer programs are very good at repetition (and don’t get bored) Sometimes it is necessary to repeat a statement or block many times One way to do this is with a while loop A while statement controls how many times another statement/block is executed

CS100Lecture 624 While syntax while ( condition ) while(B) statement;S; Similar to if, but at the end of the statement, the condition is checked again Evaluate B Execute S true false

CS100Lecture 625 Example // use while to print first 10 powers of 2 final static int LIMIT = 10; int count = 1, power_of_two = 1; while (count <= LIMIT) { power_of_two = power_of_two * 2; System.out.println(power_of_two); count = count + 1; }