The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 - 03 Introduction to Programming Adrian Ilie July 13, 2005.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 4: Writing Classes
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 6, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 13, 2005.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 28, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
C++ data types. Structs vs. Classes C++ Classes.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 18, 2005.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 1, 2005.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Methods F Hello World! F Java program compilation F Introducing Methods F Declaring Methods F Calling Methods F Passing Parameters by value F Overloading.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
C# Programming Methods.
Classes - Intermediate
Methods.
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.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
1 More About Derived Classes and Inheritance Chapter 9.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Functions + Overloading + Scope
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 7 User-Defined Methods.
Programming Language Concepts (CIS 635)
Chapter 4: Writing Classes
Methods.
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Group Status Project Status.
Classes, Encapsulation, Methods and Constructors (Continued)
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 4 Writing Classes.
Lecture 11 Parameters CSE /26/2018.
C++ data types.
Presentation transcript:

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP Introduction to Programming Adrian Ilie July 13, 2005

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 2 Today in Comp 14 Review Classes and Methods ♦ Rectangle class ♦ ReverseString class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 3 Data Scope Example public class Rectangle { // variables declared here are class-level // available in all methods in Rectangle class public int computeArea() { // variables declared here are method-level // only available in computeArea() } public void print() { // variables declared here are method-level // only available in print() }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 4 Overloading - the process of using the same method name for multiple methods The signature of each overloaded method must be unique ♦ Number of parameters ♦ Type of the parameters ♦ not the return type of the method, though The compiler determines which version of the method is being invoked by analyzing the parameters Review Overloading Methods

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 5 Constructors public Rectangle (int l, int w) { length = l; width = w; } public class Rectangle { private int length; private int width; Rectangle r2 = new Rectangle (5, 10); public Rectangle () { length = 0; width = 0; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 6 Create an object: Rectangle r; r = new Rectangle(2, 3); OR Rectangle r = new Rectangle(2, 3); Use the object and the dot operator to access methods: r.setLength(5); r.setWidth(10); r Creating and Using Objects r

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 7 Exercise Write a method for the Rectangle class called printBox that will print the rectangle as a box made of % example: length = 3, width = 5 %%% % %%%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 8 public void printBox () { for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); for (int i = 1; i <= length - 2; i++) { System.out.print("%"); for (int j = 1; j <= width - 2; j++) System.out.print(" "); System.out.println("%"); } for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); } %%%% length = 3, width = 8 % % %%%%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 9 public void printBox () { for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); for (int i = 1; i <= length - 2; i++) { System.out.print("%"); for (int j = 1; j <= width - 2; j++) System.out.print(" "); System.out.println("%"); } for (int i = 1; i <= width; i++) System.out.print("%"); System.out.println( ); } %% length = 8, width = 3 % % % % % % % % % % % % %%

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 10 public class RectangleTester { public static void main (String[] args) { Rectangle r1 = new Rectangle(); Rectangle r2 = new Rectangle (20, 30); r1.setWidth(5); r1.setLength(10); r1.print(); r2.print(); System.out.println(r1); System.out.println(r2); } } // end of RectangleTester class What if we just wanted to output r1 and r2 using System.out.println? Testing

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 11 The toString Method Special method in Java classes Produces a String object based on the current object, suitable for printing Mapped to the ' + ' operator Also called when the object is a parameter in a print() or println() method There is a default toString method, but it's better if we write our own System.out.println(r1); System.out.println(r1.toString());

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 12 Rectangle.java public String toString() { String result = ""; result += "length: " + length + "\n"; result += "width: " + width; return (result); } Rectangle r = new Rectangle (2,3); System.out.println (r); length: 2 width: 3

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 13 Rectangle.java What if we wanted to print out the box when we say System.out.println(r1)?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 14 Rectangle.java public class Rectangle { private int length; private int width; public Rectangle (int length, int width) { length = length; width = width; } Which length and which width ??

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 15 The Reference this Reserved word Refers to instance variables and methods of a class Allows you to distinguish between member variables and local variables with the same name

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 16 Rectangle.java public class Rectangle { private int length; private int width; public Rectangle (int length, int width) { this.length = length; this.width = width; }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 17 ReverseString.java Available on course web page ♦ Fully commented ReverseTester.java ♦ Driver class : Tests the ReverseString class ♦ Also available online

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 18 Review Classes and Methods ReverseString.java When do you use dot operator (.) to access methods? ReverseString r1 = new ReverseString( keyboard.readLine() ); // Output the reverse of the entered string System.out.println("The reverse of the string is " + r1.getReverse() ); Inside ReverseTester.java Dot operator to access method getReverse()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 19 Review Classes and Methods ReverseString.java When do you use dot operator (.) to access methods? ReverseString r1 = new ReverseString( keyboard.readLine() ); // Output the reverse of the entered string System.out.println("The reverse of the string is " + r1.getReverse() ); public ReverseString(String s){ original = s; computeReverse(); } Inside ReverseString.java No dot operator to access computeReverse()

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 20 Review Classes and Methods ReverseString.java When do you use dot operator (.) to access methods? ♦ Must use dot operator to access method that is in a different class ♦ To access a method in ReverseString from ReverseTester, use dot operator ♦ To access a method in ReverseString from ReverseString, NO dot operator

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 21 Review Classes and Methods ReverseString.java When do you use public vs. private? private void computeReverse(){ // method body } public boolean isPalindrome(){ // method body } Where are these methods called from? (What class are they accessed in?) Only called from ReverseString class Called from ReverseTester class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 22 Review Classes and Methods ReverseString.java When do you use public vs. private? ♦ Use private: When the method or data member is only going to be accessed inside the same class ♦ Use public: When the method or data member is going to be accessed outside the class Anything that is called, or used, inside ReverseTester class must be public

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 23 Review Classes and Methods ReverseString.java Why did we not use static in ReverseString class? When and why do we use static? How do we access static methods or variables from another class?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 24 Review Classes and Methods ReverseString.java Why did we not use static in ReverseString class? When and why do we use static? How do we access static methods or variables from another class? classname.methodname() Example: Integer.parseInt(“48”); public static int parseInt(String s) This method must be declared static inside class classname Dot operator

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 25 Review Classes and Methods ReverseString.java Why did we not use static in ReverseString class? When and why do we use static? Do not need object to use method/variable Consistent among all objects Shared among all objects of the class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 26 Review Classes and Methods ReverseString.java Why did we not use static in ReverseString class? When and why do we use static? Want all objects of class to share one copy of data Do not need method/variable to be object specific NOTE: Can't call a non-static method from a static method Can't access non-static variables from a static method

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 27 Review Classes and Methods ReverseString.java Why did we not use static in ReverseString class? NOTE: Can't call a non-static method from a static method Can't access non-static variables from a static method public static void main (String[] args) { myMethod(); } main method is static, therefore myMethod() must also be static within same class public static void myMethod ( ) { // method body }

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 28 Review Classes and Methods ReverseString.java Why was there no main method in ReverseString class? ♦ Only a driver class (a class that tests your program) has a main method ♦ General rule: For each program you write, there will only be one main method Here, it was given in ReverseTester class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 29 Review Classes and Methods ReverseString.java Why was there no keyboard input in ReverseString class (keyboard.readLine()) ? ♦ Keyboard input already performed in ReverseTester ♦ How did we give input to ReverseString class??

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 30 Review Classes and Methods ReverseString.java Why was there no keyboard input in ReverseString class (keyboard.readLine()) ? ♦ How did we give input to ReverseString class?? Parameters Specifically, in this example, parameter to the constructor public ReverseString(String s){ original = s; computeReverse(); } Formal Parameter Inside ReverseString class

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 31 Review Classes and Methods ReverseString.java Why was there no keyboard input in ReverseString class (keyboard.readLine()) ? ♦ How did we give input to ReverseString class?? Parameters public ReverseString(String s){ original = s; computeReverse(); } Formal Parameter Inside ReverseString class ReverseString r1 = new ReverseString( keyboard.readLine() ); Inside ReverseTester class Actual Parameter

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 32 Review Classes and Methods ReverseString.java Why didn’t we use System.out.println in the ReverseString class? ♦ Output to the screen already performed in ReverseTester ♦ How did we give information to ReverseTester class for output?

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 33 Review Classes and Methods ReverseString.java Why didn’t we use System.out.println in the ReverseString class? ♦ How did we give information to ReverseTester class for output? Return from Methods Specifically, in this example: System.out.println("The reverse of the string is " + r1.getReverse() ); public String getReverse(){ return reverse; } Inside ReverseString class Inside ReverseTester class return type Call to getReverse() will be replaced with a String

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 34 Review Classes and Methods ReverseString.java Why didn’t we use System.out.println in the ReverseString class? ♦ How did we give information to ReverseTester class for output? Return from Methods Specifically, in this example: System.out.println("The reverse of the string is " + r1.getReverse() ); public String getReverse(){ return reverse; } Inside ReverseString class Inside ReverseTester class Replaced with the value of reverse

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 35 Communication between Classes Methods provide communication between methods ♦ Parameters provide input into class ♦ Return value provides output public returnType methodName ( formal parameters ) Output from method Input to method

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 36 Question Write a program that takes in 5 int values, computes the sum, and then prints the values in reverse order.

The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie 37 int num0, num1, num2, num3, num4; int sum; System.out.println("Enter five integers, one per line "); num0 = Integer.parseInt(keyboard.readLine()); num1 = Integer.parseInt(keyboard.readLine()); num2 = Integer.parseInt(keyboard.readLine()); num3 = Integer.parseInt(keyboard.readLine()); num4 = Integer.parseInt(keyboard.readLine()); sum = num0 + num1 + num2 + num3 + num4; System.out.println("The sum of the numbers = " + sum); System.out.println(num4 + " " + num3 + " " + num2 + " “ + num1 + " " + num0); Solution What if we wanted to add and print 100 values?