Defining Classes and Methods

Size: px
Start display at page:

Download "Defining Classes and Methods"

Transcription

1 Defining Classes and Methods Chapter 4 Chapter 4 1

2 Basic Terminology Objects can represent almost anything. A class defines a kind of object. It specifies the kinds of data an object of the class can have. It provides methods specifying the actions an object of the class can take. An object satisfying the class definition instantiates the class and is an instance of the class. Chapter 4 7

3 Basic Terminology, cont. The data items and the methods are referred to as members of the class. We will call the data items associated with an object the instance variables of that object (i.e. that instance of the class). Chapter 4 8

4 A Class as an Outline Chapter 4 9

5 Class Files and Separate Compilation Each Java class definition should be in a file by itself. The name of the file should be the same as the name of the class. The file name should end in.java A Java class can be compiled before it is used in a program The compiled byte code is stored in a file with the same name, but ending in.class Chapter 4 11

6 Class Files and Separate Compilation, cont. If all the classes used in a program are in the same directory as the program file, you do not need to import them. Chapter 4 12

7 Example class SpeciesFirstTry Chapter 4 13

8 Example, contd. The modifier public associated with the instance variables should be replaced with the modifier private, as we will do later in the chapter. Chapter 4 14

9 Example, contd. class SpeciesFirstTryDemo Each object of type SpeciesFirstTry has its own three instance variables Chapter 4 15

10 Using Methods two kinds of methods: methods that return a single value (e.g. nextint) methods that perform some action other than returning a single value (e.g println), called void methods Chapter 4 16

11 Methods That Return a Value example int next = keyboard.nextint(); keyboard is the calling object. You can use the method invocation any place that it is valid to use of value of the type returned by the method. Chapter 4 17

12 Methods That Do Not Return a Value example System.out.println( Enter data: ); System.out is the calling object. The method invocation is a Java statement that produces the action(s) specified in the method definition. It is as if the method invocation were replaced by the statements and declarations in the method definition. Chapter 4 18

13 void Method Definitions example public void writeouput() { } System.out.println( Name: + name); System.out.println( Age: + age); Such methods are called void methods. Chapter 4 19

14 Method Definitions All method definitions belong to some class. All method definitions are given inside the definition of the class to which they belong. If the definition of the method begins with public void, it does not return a value. public indicates that use is unrestricted. void indicates that the method does not return a value. Chapter 4 20

15 Method Definitions, cont. The parentheses following the method name contain any information the method needs. The first part of the method definition is called the heading. The remainder of the method is called the body, and is enclosed in braces {}. Statements or declarations are placed in the body. Chapter 4 21

16 Defining Methods That Return a Value example public int fivefactorial(); { } int factorial = 5*4*3*2*1; return factorial; As before, the method definition consists of the method heading and the method body. The return type replaces void. Chapter 4 23

17 Defining Methods That Return a Value, cont. The body of the method definition must contain return Expression; This is called a return statement. The Expression must produce a value of the type specified in the heading. The body can contain multiple return statements, but a single return statement makes for better code. Chapter 4 24

18 Naming Methods Use a verb to name a void method. void methods typically perform some action(s). (e.g. getage) Use a noun to name a method that returns a value. Methods that return a value are used like a value. (e.g. fivefactorial) Observe the convention of starting the method name with a lowercase letter. Chapter 4 25

19 Accessing Instance Variables Outside the class definition, a public instance variable is accessed with the name of an object of the class a dot (.) the name of the instance variable. Example: mybestfriend.name = Lara ; Inside the definition of the same class only the name of the instance variable is used. Example: name = keyboard.nextline(); Chapter 4 29

20 Accessing Instance Variables, cont. equivalent assignment statements: name = keyboard.nextline(); and this.name = keyboard.nextline(); The keyword this stands for the calling object - the object that invokes the method. Chapter 4 30

21 Local Variables A variable declared within a method is called a local variable. Its meaning is local to (confined to) the method definition. Variables with the same name declared within different methods are different variables. A local variable exists only as long as the method is active. Chapter 4 31

22 Blocks The terms block and compound statement both refer to a set of Java statements enclosed in braces {}. A variable declared within a block is local to the block. When the block ends, the variable disappears. If you intend to use the variable both inside and outside the block, declare it outside the block. Chapter 4 33

23 Parameters of a Primitive Type Often it is convenient to pass one or more values into a method and to have the method perform its actions using those values. The values are passed in as arguments (or actual parameters) associated with the method invocation. Chapter 4 36

24 Parameters of a Primitive Type, cont. The method receives the values and stores them in its formal parameters (or simply parameters). A method invocation assigns the values of the arguments (actual parameters) to the corresponding formal parameters (parameters). This is known as the call-by-value mechanism. Chapter 4 37

25 Parameters of a Primitive Type, cont. The formal parameters exist as long as the method is active. Chapter 4 38

26 Parameters of a Primitive Type, cont. Generally, the type of each argument must be the same as the type of the corresponding formal parameter. Java will perform automatic type conversion for an argument that appears to the left of a formal parameter it needs to match byte --> short --> int --> long --> float --> double Chapter 4 39

27 Parameters of a Primitive Type, cont. An argument in a method invocation can be a literal such as 2 or A a variable any expression that yields a value of the appropriate type. A method invocation can include any number of arguments; the method definition contains a corresponding number of formal parameters, each preceded by its type. Chapter 4 40

28 Parameters of a Primitive Type, cont. anobject.dostuff(42, 100, 9.99, Z ); public void dostuff(int n1, int n2, double d1, char c1); arguments and formal parameters are matched by position Everything said about arguments and formal parameters applies to methods that return a value as well as to void methods. Chapter 4 41

29 Information Hiding Information overload is avoided by suppressing or hiding certain kinds of information, making the programmer s job simpler and the code easier to understand. A programmer can use a method defined by someone else without knowing the details of how it works (e.g. the println method) She or he needs to know what the method does, but not how it does it. Chapter 4 47

30 Information Hiding, cont. What the method contains is not secret, and maybe not even interesting. Viewing the code does not help you use the method and may distract you from the task at hand. Designing a method so that it can be used without knowing how it performs its task is called information hiding or abstraction. Chapter 4 48

31 The public and private Modifiers The instance variables of a class should not be declared public. Typically, instance variables are declared private. An instance variable declared public can be accessed and changed directly, with potentially serious integrity consequences. Declaring an instance variable private protects its integrity. Chapter 4 55

32 The public and private Modifiers, cont. Analogy: An ATM permits deposits and withdrawals, both of which affect the account balance, but it does not permit an account balance to be accessed and changed directly. If an account balance could be accessed and changed directly, a bank would be at the mercy of ignorant and unscrupulous users. Chapter 4 56

33 The private Modifier The private modifier makes an instance variable inaccessible outside the class definition. But within the class definition, the instance variable remains accessible and changeable. This means that the instance variable can be accessed and changed only via the methods accompanying the class. Chapter 4 57

34 The private Modifier, cont. Statements such as System.out.println (secretspecies.population); are no longer valid. Chapter 4 59

35 The private Modifier, cont. Methods in a class also can be private. Methods declared private cannot be invoked outside the class definition, but can be invoked within the definition of any other method in the class. A method invoked only within the definition of other methods in the class should be declared private. Chapter 4 60

36 Accessor and Mutator Methods Appropriate access to an instance variable declared private is provided by an accessor method which is declared public. Typically, accessor methods begin with the word get, as in getname. Mutator methods should be written to guard against inappropriate changes. Chapter 4 61

37 Accessor and Mutator Methods, cont. Appropriate changes to an instance variable declared private are provided by an mutator method which is declared public. Typically, mutator methods begin with the word set, as in setname. Chapter 4 63

38 Encapsulation Encapsulation is the process of hiding details of a class definition that are not needed to use objects of the class. Encapsulation is a form of information hiding. Chapter 4 68

39 Encapsulation, cont. When done correctly, encapsulation neatly divides a class definition into two parts: the user interface which communicates everything needed to use the class the implementation consisting of all the members of the class. A class defined this way is said to be wellencapsulated. Chapter 4 69

40 The User Interface The user interface consists of the headings for the public methods the defined public constants comments telling the programmer how to use the public methods and the defined public constants. The user interface contains everything needed to use the class. Chapter 4 70

41 The Implementation The implementation consists of the private instance variables the private defined constants the definitions of public and private methods. The Java code contains both the user interface and the implementation. Imagine a wall between the user interface and the implementation. Chapter 4 71

42 Encapsulation Guidelines Precede the class definition with a comment that shapes the programmer s view of the class. Declare all the instance variables in the class private. Provide appropriate public accessor and mutator methods. Chapter 4 73

43 Encapsulation Guidelines, cont. Provide public methods to permit the programmer to use the class appropriately. Precede each public method with a comment specifying how to use the method. Declare methods invoked only by other methods in the class private. Use /*...*/ or /**...*/ for user interface comments and // for implementation comments. Chapter 4 74

44 Encapsulation Characteristics Encapsulation should permit implementation changes (improvements, modifications, simplifications, etc.) without requiring changes in any program or class that uses the class. Encapsulation combines the data and the methods into a single entity, hiding the details of the implementation. Chapter 4 75

45 ADT An abstract data type (ADT) is basically the same as a well-encapsulated class definition. Chapter 4 76

46 Variables Variables of a class type name objects, which is different from how primitive variables store values. All variables are implemented as memory locations. The value of a variable of a primitive type is stored in the assigned location. The value of a variable of a class type is the address where a named object of that class is stored. Chapter 4 82

47 Variables, cont. A value of any particular primitive type always requires the same amount of storage. example: a variable of type int always requires 4 bytes. An object of a class type might be arbitrarily large. An object of type String might be empty, or might contain 1, 120, 5280, or more characters. Chapter 4 83

48 Variables, cont. However, there is always a limit on the size of an address. The memory address where an object is stored is called a reference to the object. Variables of a class type behave differently from variables of a primitive type. Chapter 4 84

49 Allocating Memory for a Reference and an Object A declaration such as SpeciesFourthTry s; creates a variable s that can hold a memory address. A statement such as s = new SpeciesFourthTry(); allocates memory for an object of type SpeciesFourthTry. Chapter 4 89

50 == with Variables of a Class Type, cont. When used with variables of a class type, == tests if the variables are aliases of each other, not if they reference objects with identical data. To test for equality of objects in the intuitive sense, define and use an appropriate equals method. Chapter 4 91

51 Method equals The definition of method equals depends on the circumstances. In some cases, two objects may be equal when the values of only one particular instance variable match. In other cases, two objects may be equal only when the values of all instance variables match. Always name the method equals. Chapter 4 94

52 Boolean-Valued Methods A method that returns a value of type boolean is called a boolean-valued method. Method equals produces and returns a value of type boolean. The invocation of a boolean-valued method can be used as the condition of an if-else statement, a while statement, etc. Chapter 4 98

53 Boolean-Valued Methods, cont. The value returned by a boolean-valued method can be stored in a variable boolean areequal = s1.equals(s2); Any method that returns a boolean value can be used in the same way. Chapter 4 99

54 Class Parameters Recall When the assignment operator is used with objects of a class type, a memory address is copied, creating an alias. When the assignment operator is used with a primitive type, a copy of the primitive type is created. Chapter 4 100

55 Class Parameters, cont. When a parameter in a method invocation is a primitive type, the corresponding formal parameter is a copy of the primitive type. Chapter 4 101

56 Class Parameters, cont. When a parameter in a method invocation is a reference to a class type (i.e. a named object), the corresponding formal parameter is a copy of that reference (i.e. an identically valued reference to the same memory location). Chapter 4 102

57 Class Parameters, cont. Example if (s1.equals(s2)) public boolean equals (Species otherobject) causes otherobject to become an alias of s2, referring to the same memory location, which is equivalent to otherobject = s2; Chapter 4 103

58 Class Parameters, cont. Any changes made to the object named otherobject will be done to the object named s2, and vice versa, because they are the same object. If otherobject is a formal parameter of a method, the otherobject name exists only as long as the method is active. Chapter 4 104

59 Comparing Class Parameters and Primitive-Type Parameters A method cannot change the value of a variable of a primitive type passed into the method. A method can change the value(s) of the instance variable(s) of a class type passed into the method. Chapter 4 105

60 The Graphics Class An object of the class Graphics represents an area of the screen. The class Graphics also has methods that allow it do draw figures and text in the area of the screen it represents. Chapter 4 109

61 The Graphics Class, cont. Chapter 4 110

62 The Graphics Class, cont. A Graphics object has instance variables that specify an area of the screen In examples seen previously, the Graphics object represented the area corresponding to the inside of an applet. Chapter 4 111

63 The Graphics Class, cont. When an applet is run, a suitable Graphics object is created automatically and is used as an argument to the applet s paint method when the paint method is (automatically) invoked. The applet library code does all this for us. To add this library code to an applet definition, use extends JApplet Chapter 4 112

64 Programming Example class MultipleFaces Chapter 4 113

65 The init Method An init method can be defined when an applet is written. The method init (like the method paint) is called automatically when the applet is run. The paint method is used only for things like drawing. All other actions in an applet (adding labels, buttons, etc.) either occur or start in the init method. Chapter 4 117

66 Adding Labels to an Applet A label is another way to add text to an applet. Chapter 4 118

67 Adding Labels to an Applet, class LabelDemo cont. Chapter 4 119

68 The Content Pane Think of the content pane as inside of the applet. Container contentpane = getcontentpane(); When components are added to an applet, they are added to its content pane. The content pane is an object of type Container. Chapter 4 121

69 The Content Pane, cont. A named content pane can do things such as setting color contentpane.setbackground(color.white); or specifying how the components are arranged contentpane.setlayout (new FlowLayout()); Chapter 4 122

70 The Content Pane, cont. or adding labels JLabel label1 = new JLabel( Hello ); contentpane.add(label1); Chapter 4 123

Recitation 02/02/07 Defining Classes and Methods. Chapter 4

Recitation 02/02/07 Defining Classes and Methods. Chapter 4 Recitation 02/02/07 Defining Classes and Methods 1 Miscellany Project 2 due last night Exam 1 (Ch 1-4) Thursday, Feb. 8, 8:30-9:30pm PHYS 112 Sample Exam posted Project 3 due Feb. 15 10:00pm check newsgroup!

More information

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09 Objectives Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters

More information

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5 Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public,

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 ISBN 0136091113 2009 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved Objectives Describe concepts of class, class

More information

Defining Classes and Methods

Defining Classes and Methods Walter Savitch Frank M. Carrano Defining Classes and Methods Chapter 5 Class and Method Definitions: Outline Class Files and Separate Compilation Instance Variables Methods The Keyword this Local Variables

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! Describe and define concepts of class and object! Describe use

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Methods and Data (Savitch, Chapter 5)

Methods and Data (Savitch, Chapter 5) Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private 2 public class Temperature { public static void main(string[] args) {

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

CS112 Lecture: Defining Instantiable Classes

CS112 Lecture: Defining Instantiable Classes CS112 Lecture: Defining Instantiable Classes Last revised 2/3/05 Objectives: 1. To describe the process of defining an instantiable class 2. To discuss public and private visibility modifiers. Materials:

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

EECS168 Exam 3 Review

EECS168 Exam 3 Review EECS168 Exam 3 Review Exam 3 Time: 2pm-2:50pm Monday Nov 5 Closed book, closed notes. Calculators or other electronic devices are not permitted or required. If you are unable to attend an exam for any

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object-oriented programming (OOP) possible Programming in Java consists of defining a number of classes

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Methods. Methods. Mysteries Revealed

Methods. Methods. Mysteries Revealed Methods Methods and Data (Savitch, Chapter 5) TOPICS Invoking Methods Return Values Local Variables Method Parameters Public versus Private A method (a.k.a. func2on, procedure, rou2ne) is a piece of code

More information

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4 Assignments Lecture 6 Complete for Project 1 Reading: 3.1, 3.2, 3.3, 3.4 Summary Program Parts Summary - Variables Class Header (class name matches the file name prefix) Class Body Because this is a program,

More information

Anatomy of a Class Encapsulation Anatomy of a Method

Anatomy of a Class Encapsulation Anatomy of a Method Writing Classes Writing Classes We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: class definitions instance data encapsulation and

More information

STUDENT LESSON A5 Designing and Using Classes

STUDENT LESSON A5 Designing and Using Classes STUDENT LESSON A5 Designing and Using Classes 1 STUDENT LESSON A5 Designing and Using Classes INTRODUCTION: This lesson discusses how to design your own classes. This can be the most challenging part of

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods. Objectives. Outline. Chapter 6 More About Objects and Methods Chapter 6 Objectives learn to define constructor methods learn about static methods and static variables learn about packages and import statements learn about top-down design

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Chapter 6 Objectives learn to define constructor methods learn about static methods and static variables learn about packages and import statements learn about top-down design

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. Before you can drive a car, someone has to design it and build it. A car typically begins as engineering

More information

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Java application programming } Use tools from the JDK to compile and run programs. } Videos at www.deitel.com/books/jhtp9/ Help you get started

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers and letters. Think of them

More information

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA 1 TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson, and instructor materials prepared

More information

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview Introduction to Visual Basic and Visual C++ Introduction to Java Lesson 13 Overview I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Overview JDK Editions Before you can write and run the simple

More information

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7) Array Basics: Outline Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Objects and References: Outline Variables of a Class Type Defining an equals Method for a Class Boolean-Valued Methods Parameters of a Class Type Variables of a Class

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object oriented programming (OOP) possible Programming in Java consists of dfii defining a number of

More information

More About Objects and Methods

More About Objects and Methods More About Objects and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! learn more techniques for programming with classes and objects!

More information

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich. More About Objects and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives! learn more techniques for programming with classes and objects!

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba (C) 2010 Pearson Education, Inc. All rights reserved. Java application A computer program that executes when you use the java command to launch the Java Virtual Machine

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

APCS Semester #1 Final Exam Practice Problems

APCS Semester #1 Final Exam Practice Problems Name: Date: Per: AP Computer Science, Mr. Ferraro APCS Semester #1 Final Exam Practice Problems The problems here are to get you thinking about topics we ve visited thus far in preparation for the semester

More information

9 Working with the Java Class Library

9 Working with the Java Class Library 9 Working with the Java Class Library 1 Objectives At the end of the lesson, the student should be able to: Explain object-oriented programming and some of its concepts Differentiate between classes and

More information

Computational Expression

Computational Expression Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

CPS122 Lecture: Defining a Class

CPS122 Lecture: Defining a Class Objectives: CPS122 Lecture: Defining a Class last revised January 14, 2016 1. To introduce structure of a Java class 2. To introduce the different kinds of Java variables (instance, class, parameter, local)

More information

A variable is a name for a location in memory A variable must be declared

A variable is a name for a location in memory A variable must be declared Variables A variable is a name for a location in memory A variable must be declared, specifying the variable's name and the type of information that will be held in it data type variable name int total;

More information

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations. Data Structures 1 Data structures What is a data structure? Simple answer: a collection of data equipped with some operations. Examples Lists Strings... 2 Data structures In this course, we will learn

More information

Key Differences Between Python and Java

Key Differences Between Python and Java Python Python supports many (but not all) aspects of object-oriented programming; but it is possible to write a Python program without making any use of OO concepts. Python is designed to be used interpretively.

More information

Course Outline. Introduction to java

Course Outline. Introduction to java Course Outline 1. Introduction to OO programming 2. Language Basics Syntax and Semantics 3. Algorithms, stepwise refinements. 4. Quiz/Assignment ( 5. Repetitions (for loops) 6. Writing simple classes 7.

More information

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading COMP 202 CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading More on OO COMP 202 Objects 3 1 Static member variables So far: Member variables

More information

C++ & Object Oriented Programming Concepts The procedural programming is the standard approach used in many traditional computer languages such as BASIC, C, FORTRAN and PASCAL. The procedural programming

More information

Basic Computation. Chapter 2

Basic Computation. Chapter 2 Walter Savitch Frank M. Carrano Basic Computation Chapter 2 Outline Variables and Expressions The Class String Keyboard and Screen I/O Documentation and Style Variables Variables store data such as numbers

More information

Section 2: Introduction to Java. Historical note

Section 2: Introduction to Java. Historical note The only way to learn a new programming language is by writing programs in it. - B. Kernighan & D. Ritchie Section 2: Introduction to Java Objectives: Data Types Characters and Strings Operators and Precedence

More information

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others COMP-202 Objects, Part III Lecture Outline Static Member Variables Parameter Passing Scopes Encapsulation Overloaded Methods Foundations of Object-Orientation 2 Static Member Variables So far, member variables

More information

Software and Programming 1

Software and Programming 1 Software and Programming 1 Lab 1: Introduction, HelloWorld Program and use of the Debugger 17 January 2019 SP1-Lab1-2018-19.pptx Tobi Brodie (tobi@dcs.bbk.ac.uk) 1 Module Information Lectures: Afternoon

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Objects and Classes -- Introduction

Objects and Classes -- Introduction Objects and Classes -- Introduction Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on: the concept of objects the use of classes

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5b Information Hiding Programmer using a class method need not know details of implementation Only needs to know what the method does Information hiding: Designing

More information

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Object oriented programming. Instructor: Masoud Asghari Web page:   Ch: 3 Object oriented programming Instructor: Masoud Asghari Web page: http://www.masses.ir/lectures/oops2017sut Ch: 3 1 In this slide We follow: https://docs.oracle.com/javase/tutorial/index.html Trail: Learning

More information

Chapter. Let's explore some other fundamental programming concepts

Chapter. Let's explore some other fundamental programming concepts Data and Expressions 2 Chapter 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design 2007 Pearson Addison-Wesley. All rights reserved Data and Expressions Let's explore some

More information

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

CS 152: Data Structures with Java Hello World with the IntelliJ IDE CS 152: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Electrical and Computer Engineering building

More information

Inheritance. Chapter 7. Chapter 7 1

Inheritance. Chapter 7. Chapter 7 1 Inheritance Chapter 7 Chapter 7 1 Introduction to Inheritance Inheritance allows us to define a general class and then define more specialized classes simply by adding new details to the more general class

More information

ITP 342 Mobile App Dev. Fundamentals

ITP 342 Mobile App Dev. Fundamentals ITP 342 Mobile App Dev Fundamentals Object-oriented Programming Object-oriented programming (OOP) is a programming paradigm based on the concept of objects. Classes A class can have attributes & actions

More information

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space. Chapter 2: Problem Solving Using C++ TRUE/FALSE 1. Modular programs are easier to develop, correct, and modify than programs constructed in some other manner. ANS: T PTS: 1 REF: 45 2. One important requirement

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 1 2 Introduction to Classes and Objects You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation

COMP-202 Unit 8: Defining Your Own Classes. CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation COMP-202 Unit 8: Defining Your Own Classes CONTENTS: Class Definitions Attributes Methods and Constructors Access Modifiers and Encapsulation Defining Our Own Classes (1) So far, we have been creating

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

2 rd class Department of Programming. OOP with Java Programming

2 rd class Department of Programming. OOP with Java Programming 1. Structured Programming and Object-Oriented Programming During the 1970s and into the 80s, the primary software engineering methodology was structured programming. The structured programming approach

More information

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class CS112 Lecture: Defining Classes Last revised 2/3/06 Objectives: 1. To describe the process of defining an instantiable class Materials: 1. BlueJ SavingsAccount example project 2. Handout of code for SavingsAccount

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

COMP 250 Winter 2011 Reading: Java background January 5, 2011

COMP 250 Winter 2011 Reading: Java background January 5, 2011 Almost all of you have taken COMP 202 or equivalent, so I am assuming that you are familiar with the basic techniques and definitions of Java covered in that course. Those of you who have not taken a COMP

More information

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name: CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, 2017 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : In mathematics,

More information

Introduction to Computers and Java

Introduction to Computers and Java Introduction to Computers and Java Chapter 1 Chapter 1 1 Objectives overview computer hardware and software introduce program design and object-oriented programming overview the Java programming language

More information

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language Chapter 1 Getting Started Introduction To Java Most people are familiar with Java as a language for Internet applications We will study Java as a general purpose programming language The syntax of expressions

More information

A Short Summary of Javali

A Short Summary of Javali A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler

More information

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while CSCI 150 Fall 2007 Java Syntax The following notes are meant to be a quick cheat sheet for Java. It is not meant to be a means on its own to learn Java or this course. For that you should look at your

More information

Logic & program control part 2: Simple selection structures

Logic & program control part 2: Simple selection structures Logic & program control part 2: Simple selection structures Summary of logical expressions in Java boolean expression means an expression whose value is true or false An expression is any valid combination

More information

Flow of Control. Chapter 3. Chapter 3 1

Flow of Control. Chapter 3. Chapter 3 1 Flow of Control Chapter 3 Chapter 3 1 Flow of Control Flow of control is the order in which a program performs actions. Up to this point, the order has been sequential. A branching statement chooses between

More information

Fundamental Concepts and Definitions

Fundamental Concepts and Definitions Fundamental Concepts and Definitions Identifier / Symbol / Name These terms are synonymous: they refer to the name given to a programming component. Classes, variables, functions, and methods are the most

More information

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method

Methods. Contents Anatomy of a Method How to design a Method Static methods Additional Reading. Anatomy of a Method Methods Objectives: 1. create a method with arguments 2. create a method with return value 3. use method arguments 4. use the return keyword 5. use the static keyword 6. write and invoke static methods

More information

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007 Java We will be programming in Java in this course. Partly because it is a reasonable language, and partly because you

More information

3 ADT Implementation in Java

3 ADT Implementation in Java Object-Oriented Design Lecture 3 CS 3500 Spring 2010 (Pucella) Tuesday, Jan 19, 2010 3 ADT Implementation in Java Last time, we defined an ADT via a signature and a specification. We noted that the job

More information

egrapher Language Reference Manual

egrapher Language Reference Manual egrapher Language Reference Manual Long Long: ll3078@columbia.edu Xinli Jia: xj2191@columbia.edu Jiefu Ying: jy2799@columbia.edu Linnan Wang: lw2645@columbia.edu Darren Chen: dsc2155@columbia.edu 1. Introduction

More information

Java+- Language Reference Manual

Java+- Language Reference Manual Fall 2016 COMS4115 Programming Languages & Translators Java+- Language Reference Manual Authors Ashley Daguanno (ad3079) - Manager Anna Wen (aw2802) - Tester Tin Nilar Hlaing (th2520) - Systems Architect

More information

Chapter 5: Enhancing Classes

Chapter 5: Enhancing Classes Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition by John Lewis, William Loftus, and Cara Cocking Java Software Solutions is published by

More information

Week 7 - More Java! this stands for the calling object:

Week 7 - More Java! this stands for the calling object: Week 7 - More Java! Variable Scoping, Revisited this Parameter Encapsulation & Principles of Information Hiding: Use of public and private within class API, ADT javadoc Variables of class Type Wrapper

More information