ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
ISE 582: Web Technology for Industrial Engineering University of Southern California Department of Industrial and Systems Engineering Lecture 5 JAVA Cup.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems.
Java Classes Introduction and Chapter 1 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Arrays and Objects OO basics. Topics Basic array syntax/use OO Vocabulary review Simple OO example Instance and Static methods Static vs. instance vs.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 2 How to Compile and Execute a Simple Program.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
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.
Classes and Methods Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2014.
Chapter 6: User-Defined Functions
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Classes CS 21a: Introduction to Computing I First Semester,
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.
Java Structure import java_packages; class JavaClass { member variables declarations; void otherMethod( ) { } public static void main(String[]
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Chapter 10 Introduction to Classes
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Classes, Interfaces and Packages
Department of Computer Engineering Methods Computer Programming for International Engineers.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
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.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
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.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Information and Computer Sciences University of Hawaii, Manoa
Topic: Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Object-Oriented Programming
Object Oriented Programming in java
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Classes CS 21a: Introduction to Computing I
In this class, we will cover:
Classes, Objects and Methods
Presentation transcript:

ISE 582: Information Technology for Industrial Engineering Instructor: Elaine Chew University of Southern California Department of Industrial and Systems Engineering Lecture 3 First cup of JAVA Winston & Narasimhan: Chapt

11 September 2003ISE 582: Web Technology for IE 2 The Agenda for Today Review: A simple Java program Example: Quidditch The Math class: Arithmetic Expressions Properties of parameters (variables) Procedure abstraction Class variables Class instances Instance methods Constructors

11 September 2003ISE 582: Web Technology for IE 3 Introduction to Objects Object Instance –Attributes (properties): associated data –Operations: actions that change attributes Methods in JAVA Member functions in C++ Object Class –Category or set of objects –Share same attributes, operations and relationships to other objects

11 September 2003ISE 582: Web Technology for IE 4 Quidditch Quidditch is a game played in mid-air on broomsticks that is a cross between rugby, basketball and the emergency room. Each team consists of seven players; three Chasers, two Beaters, one Seeker and one Keeper. The Keeper acts as a goalie against the other team making a score in one of their three hoops. The Chasers work together to score, using a ball known as the Quaffle. Scoring with the Quaffle is worth ten points. Two other balls known as Bludgers are enchanted to try and knock the players from their brooms. It is up to the Beaters to keep this from happening. Equipped with bats similar to those used in Cricket, Beaters work furiously to keep their teammates from being injured by Bludgers. Last and never least, the Seekers have the important task of catching the Snitch. This ball is the size of a walnut with wings and flies around the Pitch independantly. Capture of the Snitch is worth 150 points, and it automatically ends the game. However, this doesn't mean the team that catches the Snitch automatically wins. The game can also be ended with the mutual consent of both team captains From The Quaffle The Golden Snitch

11 September 2003ISE 582: Web Technology for IE 5 Example: Object Class QuidditchTeam name quaffle snitch totalScore setScore(…) addQuaffle() addSnitch() Class name Attributes Operations

11 September 2003ISE 582: Web Technology for IE 6 Example: Object Instances t1: QuidditchTeam name = “Hufflepuff” quaffles = 5 snitch = 0 totalScore = 50 t2: QuidditchTeam name = “Gryffindor” quaffles = 3 snitch = 1 totalScore = 180

11 September 2003ISE 582: Web Technology for IE 7 Convention: –Attributes/operation names begin with lower-case characters –Class and type names are capitalized Common Operations: –get*, set*, is* Naming Conventions Class name attributes:Type operations Class name

11 September 2003ISE 582: Web Technology for IE 8 Some Variable Types char:2 bytes: characters byte,short,int,long: increasing:integers float,double: 4,8: floating point number OTHERS: boolean, String, arrays etc public class Demonstrate{ public static void main (String argv[]) { int script, acting, direction …} }

11 September 2003ISE 582: Web Technology for IE 9 ARITHMETIC EXPRESSIONS The usual operators: +, -, *, / Modulus: % (remainder in division) The usual precedence rules apply Mixing data types in expressions Casting an expression –Original data type remains unchanged Assignment operates from R to L

11 September 2003ISE 582: Web Technology for IE 10 The Math Class public class Demonstrate { public static void main (String argv[]) { System.out.println(“natural log of 10:” + Math.log(10)); System.out.println(“abs value of -10:” + Math.abs(-10)); System.out.println(“max of 2 and 3:” + Math.max(2,3)); System.out.println(“5th power of 6:” + Math.pow(6,5)); System.out.println(“sqrt of 7:” + Math.sqrt(7)); System.out.println(“sine of 8 rad:” + Math.sin(8)); System.out.println(“Random no.(0,1):” + Math.random()); }

11 September 2003ISE 582: Web Technology for IE 11 Defining a Simple Method public class Demonstrate { public static void main (String argv[]) { int script = 6, acting = 9, direction = 8; System.out.print(“The rating of the movie is”); System.out.println(movieRating(script,acting,direction); } public static int movieRating (int s, int a, int d) { return s + a + d; } ARGUMENTS PARAMETERS

11 September 2003ISE 582: Web Technology for IE 12 Defining Methods in Multiple Files public class Movie { public static int movieRating (int s, int a, int d) { return s + a + d; } public class Demonstrate { public static void main (String argv[]) { int s = 6, a = 9, d = 8; System.out.print(“The rating of the movie is”); System.out.println(Movie.movieRating(s,a,d); }

11 September 2003ISE 582: Web Technology for IE 13 Other Things to Know You can define more than one class in the same file To call a method in a different class, preface the method name with the class name and join with a dot. You can define multiple methods with the same name in the same class as long as they have different signatures (i.e. arrangement of parameter data types)

11 September 2003ISE 582: Web Technology for IE 14 Properties of parameters Call-by-value vs. call-by-reference Java is a CALL-BY-VALUE language: public class PassByValue { public static void main(String[] args){ int i = 5; methodZ(i); // i = 5 (call by value); i = 6 (call by reference) } public static void methodZ(int j){ j = 6; } } “i” will still be equal to 5 in the main method

11 September 2003ISE 582: Web Technology for IE 15 Properties of parameters Variables declared inside a block are said to be local variables A parameter is treated as a local variable inside the method block Parameters and local variables have –local scope –dynamic extent –(they use a temporary chunk of memory)

11 September 2003ISE 582: Web Technology for IE 16 Find the BUGS public class Demonstrate { // define a method called adder: public static int adder () { return s + a + d; } // define a method called movieRating: public static int movieRating (int s,int a,int d) { return adder(); } // define main: public static void main (String argv[]) { int script=6, acting=9, direction=8, result; result=movieRating(script,acting,direction); System.out.println( " The rating of the movie is " ); System.out.println(s + a + d); }

11 September 2003ISE 582: Web Technology for IE 17 Procedure Abstraction What is it? Moving computational details into methods VIRTUES: Makes it easy to reuse programs Pushes details out of sight/mind Makes debugging easier Makes augmenting repetitive computations easier Makes it easy to improve computation Makes it easy to change computation

11 September 2003ISE 582: Web Technology for IE 18 Class Variables Exist throughout program Defined inside class body but not inside any method Marked by keyword static CLASS Class Variables... Class Methods public class Movie { // define class variables: public static int wScript=6, wActing=13, wDirection=11; // define movieRating: public static int movieRating (int s,int a,int d) { return wScript*s + wActing*a + wDirection*d; }

11 September 2003ISE 582: Web Technology for IE 19 Class Variables: Access & Scope To access a variable from a diff class… To differentiate from a parameter or local variable of the same name… Public Class Variables have –Universal scope –Static extent Class variables’ values can be changed, to fix a variable at a constant value…use “final” public static final int lightSpeedms =

11 September 2003ISE 582: Web Technology for IE 20 Class Instances CLASS... INSTANCES Arithmetic types (int, double etc) are like simple atoms, and class instances are like molecules in chemistry.

11 September 2003ISE 582: Web Technology for IE 21 Class Instances public class Movie { // define instance variables: public int script, acting, direction ; } public class Demonstrate { public static void main (String argv[]) { Movie m; m = new Movie(); m.script = 3; m.acting = 8; m.direction = 6; } Declaration Creation Define Instance Variables Assign variables

11 September 2003ISE 582: Web Technology for IE 22 Using Class Instance as Argument public class Demonstrate { public static void main (String argv[]) { Movie m = new Movie (); m.script=3; m.acting=8; m.direction=6; System.out.print( " The rating of the movie is " ); System.out.println(Movie.rating(m)); } public class Movie { // define instance variables: public int script, acting, direction; // define rating: public static int rating (Movie m) { return m.script + m.acting + m.direction; }

11 September 2003ISE 582: Web Technology for IE 23 Memory Allocation for Instances A class instance within a method is a reference-type variable b = new Button() b is NOT an object but only a “reference” for the new object Look here to clarify:

11 September 2003ISE 582: Web Technology for IE 24 Reference type variable Java is still a CALL-BY-VALUE language: public class PassByValue { public static void main(String[] args){ Button b = new Button("AAA"); methodX(b); } public static void methodX(button y){ y.setLabel("BBB");  object referring to b IS affected y = new Button("CCC");  object referring to b is NOT affected } } b.getLabel() will be equal to “BBB” in the main method

11 September 2003ISE 582: Web Technology for IE 25 Reference type variable 1 - Assignment of b 2 - Passing b into a method 3 - Assignment of y b’s object no more affected

11 September 2003ISE 582: Web Technology for IE 26 Memory “management” Reassignment of variables such as: Movie m;  Declaration m = new Movie();  Assignment m = new Movie();  Re-assignment (abandoned memory of first class instance) Java has a built-in mechanism that return abandoned memory to the free-storage list (Garbage collection)

11 September 2003ISE 582: Web Technology for IE 27 Instance Methods public class Movie { // define instance variables: public int script, acting, direction; // define rating: public static int rating (Movie m) { return m.script + m.acting + m.direction; } Movie m = new Movie(); m.script = 3; m.acting = 8; m.direction = 6; System.out.println("The rating of the movie is " + m.rating());

11 September 2003ISE 582: Web Technology for IE 28 What is “This”? “this” is a copy of the address of an instance. Use “this” only when necessary or programs get bulky. public class Movie { // define instance variables: public int script, acting, direction; // define instance method, rating: public int rating () { return this.script + this.acting + this.direction; }

11 September 2003ISE 582: Web Technology for IE 29 Ordinary arg in Instance Methods public class Movie { // define instance variables: public int script, acting, direction; // define rating: public int rating (double scaleFactor) { return (int) (scaleFactor*(script+acting+direction)); } Movie m = new Movie(); m.script = 3; m.acting = 8; m.direction = 6; System.out.println("The rating of the movie is " + m.rating(0.75));

11 September 2003ISE 582: Web Technology for IE 30 Constructors Constructor methods are called whenever a new class instance is created. Constructor method names are the same as the name of the class. Constructor methods return new class instances; no return type is specified.

11 September 2003ISE 582: Web Technology for IE 31 A Constructor Method public class Movie { // define instance variables: public int script, acting, direction; // define constructor method, assigning 5 to all variables: public Movie () { script=5; acting=5; direction=5; } // define rating: public int rating () { return script+acting+direction; } In main... Movie m = new Movie(); System.out.println("The default rating of the movie is " + m.rating());

11 September 2003ISE 582: Web Technology for IE 32 Another Constructor Method In Movie class... public Movie () { script=5; acting=5; direction=5; } public Movie (int s, int a, int d) { script=s; acting=a; direction=d; } In main... Movie m = new Movie(); System.out.println("The default rating of the movie is " + m.rating()); Movie m = new Movie(8,9,6); System.out.println("The new rating of the movie is " + m.rating());

11 September 2003ISE 582: Web Technology for IE 33 Ready, Get / Set, Go! A getter is a method that extracts information from an instance A setter (or mutator) is a method that inserts information into an instance By using getter/setter methods, we can include additional computations or output statements Get/Set methods do not have to deal directly with an actual instance variable

11 September 2003ISE 582: Web Technology for IE 34 Get/Set Methods In Movie class public int getScript () { return script; } public void setScript (int s) { script=s; } In main Movie m = new Movie(8,9,6); System.out.println("The script of the movie is " + m.getScript ()); m.setScript (12); System.out.println("The script of the movie is " + m.getScript ());

11 September 2003ISE 582: Web Technology for IE 35 Notes on Access Methods Constructors, getters and setters are access methods When you move representation detail into a set of access methods, you are –practicing data abstraction, and –hiding details of data representation behind data-abstraction barriers

11 September 2003ISE 582: Web Technology for IE 36 Advantages of Data-Abstraction Easy to reuse your work Push details out of sight / mind Easily augment what the class provides Easily improve way data is stored

11 September 2003ISE 582: Web Technology for IE 37 Second Cup of JAVA Summary We have learnt to define: –Classes –Instances –Class Methods –Class Instances –Instance Constructors –Getter and Setter Methods