1 Lecture 13. 2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

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.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS102--Object Oriented Programming Discussion 2: (programming strategy in java) – Two types of tasks – The use of arrays Copyright © 2008 Xiaoyan Li.
CIT 590 Intro to Programming Java lecture 3. Hashmaps The equivalent of python dictionaries. With both ArrayLists and Hashmaps, the syntax only allows.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Lecture 2. Review To play with Turtle, we need to download and install followings: 1.JDK 6 2.Dr. Java 3.Sample program (e.g. BookClass)
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Lecture 6. What is the difference in pictures ???
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
Chapter 7: User-Defined Methods
Lecture 5. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
COMP More About Classes Yi Hong May 22, 2015.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Introduction to Programming Writing Java Beginning Java Programs.
Introduction to Object-Oriented Programming
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1 Software Construction Lab 4 Classes and Objects in Java Basics of Classes in Java.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
CSE8A Lecture3 TODO: –Finish PSA1 individually (no partner!) and turn it in with the bundlePSA1 command GET AN INTERVIEW for PSA1 from a tutor See tutor.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Introduction to Programming Writing Java Beginning Java Programs.
Methods We write methods in our programs for many reasons:
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
Introduction to Methods Shirley Moore CS 1401 Spring 2013 cs1401spring2013.pbworks.com April 1, 2013.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Building java programs, chapter 3 Parameters, Methods and Objects.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Announcements Final Exam: TBD. Static Variables and Methods static means “in class” methods and variables static variable: one per class (not one per.
Methods.
OOP Basics Classes & Methods (c) IDMS/SQL News
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Topic: Classes and Objects
Chapter 7 User-Defined Methods.
Class Structure 15-Jun-18.
Some Eclipse shortcuts
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Class Structure 16-Nov-18.
Class Structure 7-Dec-18.
Class Structure 2-Jan-19.
Methods and Data Passing
Class Structure 25-Feb-19.
More on Creating Classes
Introduction to Object-Oriented Programming
Chapter 2: Java Fundamentals cont’d
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

1 Lecture 13

2 Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private and Public

3 Introduction to Classes (review) A class contains –Attributes (fields) Attributes are variables declared in the class –name, size, ID, etc… –Methods (behavior) Methods are behaviors of the class –turn, forward, drawLine, etc…

4 Example of a Class Definition Before writing code, we can draw a diagram of a class to outline its features such as its name, attributes, and methods Turtle - color : Color - name : String... + forward (int): + setPenColor (Color):... Class Name List of its Variables List of its Methods

5 public class ClassName { // attributes or variables variable1 variable2 ….. // methods or behaviors method1 { … } method2 { … } ….. }

6 Method Write your own methods for classes!!! –So far, we use only methods of classes which are already provided in Java standard library sqrt(double), pow(double, double) from Math class forward(), turnRight() from Turtle class –We can add (write) our own methods in a class For example drawSquare(), drawRobot(), etc…

7 How to write method Syntax (rule) [scope] [return type] [method name] ( [arguments] ) { [method body] } Scope: public, private, protected, … Return type: type of output values as a result of method body (if there is no output values, we put void) Method name: Arguments: 0 or more input variables to the method Method body: if return type is specified, return a value at the end of method body by using reserved word return Method requires input and output Arguments and Return value So, you can write a method like function in mathematics F(x) = 2x + 1; For example, F(3) = 7, F(-2) = -3 Method inputoutput

8 Example of method public class MyClass { private int total; public static void main(String[] args) { MyClass myClass = new MyClass(); int sum = myClass.addTwoNum( 2, 3 ); System.out.println( “sum is “ + sum ); } public int addTwoNum( int a, int b ); { int result = a + b; return result; } Print as sum is 5 Program starts from main method Go to the method 5 Create object of MyClass 2 + 3

Example of method public class MyClass { public static void main(String[] args) { World w = new World(); Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w); t1.forward(50); t1.turnRight(); t1.forward(50); t1.turnRight(); t1.forward(50); t1.turnRight(); t1.forward(50); t1.turnRight(); t2.forward(100); t2.turnRight(); t2.forward(100); t2.turnRight(); t2.forward(100); t2.turnRight(); t2.forward(100); t2.turnRight(); } inefficient

Example of method public class Turtle { public void drawSquare(int len) { forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); forward( len ); turnRight(); } public class MyClass { public static void main(String[] args) { World w = new World(); Turtle t1 = new Turtle(w); Turtle t2 = new Turtle(w); t1.drawSquare( 50 ); t2.drawSquare( 100 ); } Efficient

11 Scope: Public & Private (1) public class Student { private String name; private char grade; public char getGrade (int score) { if( score > 70 ) grade = ‘A’; else grade = ‘B’; return grade; }

12 Scope: Public & Private (2) We use the Java reserved word private to prevent access to a variable or method from the other classes We use the Java reserved word public to allow access to a variable or method from the other classes

13 Scope: Public & Private (3) Normally, we declare variables to be private Normally, we declare methods to be public Class1 (e.g. Turtle) - color : Color + forward(int) : int Class2 (e.g. Test) public private public We can create class1 object in class2 For example, we create Turtle object in Test class We cannot access variables in class1 from class2 We can use methods of class1 in the class2 For example, t.forward() Accessible!!! because of own class

Example: Public & Private (4) public class Turtle { private String name; private Color penColor; public void drawLine( int len ) { forward( len ); setPenColor(Color.GREEN); } private void setPenColor(Color c) { penColor = c; } public class MyClass { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.drawLine( 100 ); t.name = “chonho”; t.penColor = Color.BLUE; t.setPenColor( Color.BLUE ); } ? ? ? ? ?

15 Exercise 1. ExerciseMethod1.java - write a method to return the sum of two numbers - write a method to return the difference of two numbers 2. ExerciseMethod2.java - now, you have doCalculation method - in the main method, create an object of ExerciseMethod2 and use the method

16 Exercise 3. ExerciseMethod3.java and MyCalculator3.java - Now, all methods are in ExerciseMethod3 - and MyCalculator3 contains a main method - in the main method, create an object of ExerciseMethod3 and call doCalculation method

17 Announcement In homework 3 - I have found some students whose answers are completely same… - Same font, same program, same output, even same wrong answers, etc … - I don’t want to say “they cheated” because I recommend you help your classmates each other. - Also, I know you spend much time for doing project, and your Java programming skill is improving. - But, you cannot learn anything from just copying.

18 Announcement For the rest of course - Let’s finish Project 1 and make our Gallery - Please try to do homework 3 again. - We have homework 4 and 5 - Additional homework (for extra credit) if you want - Project 2 - You can use Dr.Java to check if your answer is correct