CS180 Chapter 4 2/1/08. Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Written by: Dr. JJ Shepherd
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Road Map Introduction to object oriented programming. Classes
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 4 l Class and Method Definitions l Information Hiding and Encapsulation.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Introduction to Programming with Java, for Beginners Scope.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS 101E – Exam 2 Review Spring 2007 Michele Co. Announcements Review Session Tonight, 7/7:30 p.m., OLS 009 Will be announced via In-class Exam Wednesday.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
Chapter 5Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 5 l Programming with Methods l Polymorphism l Constructors.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
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.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
CS180 Recitation Chapter 7: Defining Your Own Classes II.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
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
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
Information and Computer Sciences University of Hawaii, Manoa
Classes and Objects Introduced
Chapter 7 User-Defined Methods.
Suppose we want to print out the word MISSISSIPPI in big letters.
Instance Method Review - CIS 1068 Program Design and Abstraction
CSC240 Computer Science III
Building Java Programs
Group Status Project Status.
Building Java Programs
Building Java Programs
CS2011 Introduction to Programming I Methods (II)
Defining Classes and Methods
Building Java Programs
Building Java Programs
Java Basics II Minseok Kwon
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Building Java Programs
Presentation transcript:

CS180 Chapter 4 2/1/08

Announcements Project 3 is out –2 weeks due to the exam Exam –Thursday, February 7, 8:30 – 9:30 pm, WTHR 104 –Covers chapters 1-4 –Study Session: Tuesday, February 5, 6-8 pm, LWSN B131 Project 1 handed back

Classes Ways to organize data –Use to create objects containing data Primitives Objects –Contain methods to perform certain operations Accessors Mutators

Terminology Class – a blueprint for an object Object – an instance of a class Method/function – means to perform operations with the class Instance variables – data held within a class

Methods Used to provide an interface to the class Helper methods – break up your code into understandable chunks Types of methods –Void –Returning a value

Methods public int factorial(int n) { int fact = 1; for (int k=n; k>1; k--) fact *= k; return fact; }

Methods public void stringCount(String str, char c) { for (int k=0; k<str.length(); k++) { if (str.charAt(k) == c) count++; } System.out.println(c + “ occurs “ + count + “ times in “ + str); }

Scope Can only use variables within the current scope –Loop/conditional scope: if a variable is declared within an if/else block or any type of loop, it is not accessible outside the block Includes any declarations in the for loop declaration –Method scope: local variables declared within a method are not accessible outside the method Includes argument list –Class scope: manageable with public/private modifiers When given the choice, Java will always choose the variable with closest scope –If you have a class variable x and a local variable x declared, x refers to the local variable, this.x refers to the class variable

Constructors (Next Week) To demonstrate some basics, we need to introduce non-trivial constructors Constructors are used to initialize objects to contain certain values Very similar to methods, but… –Are always non-static (next week…) –Have no return type public class Point { private int x, y, z; public Point(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } … }

this keyword this is a pointer to an object’s self Always used implicitly, but sometimes useful to be used explicitly In case of ambiguity resulting from using the same variable name twice, use this to refer to the class’s variable and not the local variable class A { private int a; public int add(int a) { return this.a + a; } A more clear version avoids ambiguity class A { private int a; public int add(int b) { return a + b; }

this keyword Use this keyword to call another constructor (introduced in Chapter 7) public class A { private int x; public A() { this(0); } public A(int x) { this.x = x; } … } public class A { private int x; public A() { x=0; } public A(int x) { this.x = x; } … }

Equality Comparison For primitives, compare for equality with == For objects, when to use ==, when to use equals() method? –All object variables are simply addresses to memory locations, addresses are simply integers –When using ==, you are comparing addresses True: if they are aliases False: if they are not aliases (even if they hold the same data) –Write your own equals method to compare actual data within the class

toString Write a toString to convert object into a reasonable String representation Java printing statements automatically call toString for you (rarely do you have to call it yourself)

Pass-By-Value public class A { private int x; public A() { x=0; } public A(int x) { set(x); } public A(A a) { x = a.x; } public void copy(A a) { a = new A(x); } public void set(int x) { this.x = x; } public int get() { return x; } } A a1 = new A(); A a2 = new A(); a1.set(4); a2.copy(a1); a1.get()? Parameters in Java are passed by value (as opposed to pass by reference).

Quiz public class Quiz { private int x; public Quiz() { x = 0; } public void doStuff(Quiz q, int x) { q = new Quiz(); q.x = x; } public String toString() { return “x = “ + x; } public static void main(String[] args) { int x = 5; Quiz q = new Quiz(); q.doStuff(q, x); System.out.println(q); }

Review Complete the method below, which counts the number of vowels within a given String. Then, using this method, write a single expression which computes the number of consonants within the given String. public int numVowels(String str) { }

Review Complete the method below, which counts the number of vowels within a given String. Then, using this method, write a single expression which computes the number of consonants within the given String. public int numVowels(String str) { int count = 0; str = str.toLowerCase(); for (int k=0; k<str.length(); k++) { switch (str.charAt(k)) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’:count++; break; } return count; }

Review Complete the method below, which prints out all factors of a given number. public void printFactors(int x) { }

Review Complete the method below, which prints out all factors of a given number. public void printFactors(int x) { for (int k=1; k <= x/2; k++) { if (x % k == 0) System.out.println(k); } System.out.println(x); }