© 2006 Pearson Addison-Wesley. All rights reserved 2.1.1 A DrawingGizmo object can be moved around a window to draw lines. It appears like an arrow. (See.

Slides:



Advertisements
Similar presentations
Introduction to Java.
Advertisements

1 C++ Syntax and Semantics The Development Process.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Dale Roberts Introduction to Java - First Program Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Programming Language Features Syntax Semantics. Syntax Diagram.
The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1.
The Java Programming Language
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Getting Started with Java
Outline Java program structure Basic program elements
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
© 2006 Pearson Education Making Objects 1 of 19 MAKING OBJECTS Views of a Class Defining Your Own Class Declaring Instance Variables Declaring Methods.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Writing Classes (Chapter 4)
INTRO TO OO Object lessons on Objects. Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)
Chapter 2 Overview of C Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
© 2006 Pearson Education 1 Obj: cont 1.3 and 1.4, to become familiar with identifiers and to understand how programming languages work HW: p.51 #1.8 –
Definition A string is a sequence of symbols. Examples “Hi, Mom.” “YAK” “abbababba” Question In what ways do programmers use strings?
The Java Programming Language
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CPS120: Introduction to Computer Science
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
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.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
© 2006 Pearson Addison-Wesley. All rights reserved How to Design a Supplier 1) Determine the public methods. (Don’t forget the constructor(s).) 2)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© 2006 Pearson Addison-Wesley. All rights reserved Enter onoff channel selection volume Ave-+ TV int channel int volume boolean isOperating.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Introduction to Programming Writing Java Beginning Java Programs.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
More about Java Chapter 2 9/8 & 9/9 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
TCU CoSc Introduction to Programming (with Java) Java Language Overview.
© 2006 Pearson Addison-Wesley. All rights reserved DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Conditionals, Block Statements, Style
Chapter 10 Programming Fundamentals with JavaScript
Working with Java.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Chapter 3 GC 101 Java Fundamentals.
How to Design Supplier Classes
The Need for Specifications
Chapter 4 Procedural Methods.
Chapter 10 Programming Fundamentals with JavaScript
Chapter 1: Computer Systems
Units with – James tedder
Chapter 7 Procedural Methods.
Focus of the Course Object-Oriented Software Development
Object Oriented Programming in java
Chap 2. Identifiers, Keywords, and Types
Use a variable to store a value that you want to use at a later time
Instructor: Alexander Stoytchev
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved A DrawingGizmo object can be moved around a window to draw lines. It appears like an arrow. (See the green arrow to the right.) The Class Diagram DrawingGizmo «constructor» DrawingGizmo() «update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()

© 2006 Pearson Addison-Wesley. All rights reserved The method call is the primary instruction in a Java program. Method Call Syntax When a method call is executed, the operation associated with the method is applied to an object. objectReference. methodName() ; where  objectReference refers to some particular object, and  methodName is the name of a parameterless method from the class to which objectName belongs. pen.moveForward(); Examples (assume an object, pen, of type DrawingGizmo) DrawingGizmo «constructor» DrawingGizmo() «update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()

© 2006 Pearson Addison-Wesley. All rights reserved Within a Java program instructions are also called statements. A sequence of statements (instructions) is executed in order from top to bottom. pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.dontDraw(); pen.moveForward(); Sample Java (assume an object, pen, of type DrawingGizmo) DrawingGizmo «constructor» DrawingGizmo() «update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()

© 2006 Pearson Addison-Wesley. All rights reserved Objects don’t exist until they are instantiated. Attempting to call methods on objects before they are instantiated is an error. Syntax to Instantiate & Assign objectName = new constructorName(); where  objectName is the name (i.e., variable) of some particular object, and  constructorName is the name of a parameterless constructor method from the class to which objectName belongs. (Note: In Java constructors have the same name as the class.) Example (assume an object, pen, of type DrawingGizmo)

© 2006 Pearson Addison-Wesley. All rights reserved Programming languages, like Java, are extremely particular about notation (syntax). MethodCall (abridged version) Notes  ObjectReference is a variable name or other acceptable object reference  MethodName is the name of a parameterless method from the class to which ObjectReference belongs.  No white space is permitted before or after the period (. ). Syntax diagrams are a pictorial way to define permissible syntax, ObjectReferenceMethodName.();

© 2006 Pearson Addison-Wesley. All rights reserved AssignmentInstruction Notes  Variable is a variable name  Expression must have a type that conforms to the type of Variable. VariableExpression =; Expression (abridged version) Note  Constructor names a valid parameterless constructor. Variable Constructor ()new StatementSequence OneStatement OneStatement (abridged version) MethodCall AssignmentInstruction

© 2006 Pearson Addison-Wesley. All rights reserved InstanceVarDecls StatementSequence ImportDecls PrivateMethod publicclassDriver{ publicDriver(){ } } DriverClass (abridged version of Class ) Semantics Instantiating a Driver object causes StatementSequence from Driver to execute. Note  ImportDecls and PrivateMethod are explained later.

© 2006 Pearson Addison-Wesley. All rights reserved InstanceVarDecls Notes  The scope of each Variable is the class in which it is declared.  Each Variable must be named by an identifier that is unique for the class/ OneVarDecl PrivateVarDecl (a version of OneVarDecl ) ClassName private Variable ;, Example private DrawingGizmo pen; private DrawingGizmo pencil; Alternative

© 2006 Pearson Addison-Wesley. All rights reserved public class Driver { private DrawingGizmo pen; public Driver() { pen = new DrawingGizmo(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); } public class Driver { private DrawingGizmo pen; public Driver() { pen = new DrawingGizmo(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); } When this program executes, it draws a square. DrawingGizmo «constructor» DrawingGizmo() «update» void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()

© 2006 Pearson Addison-Wesley. All rights reserved Identifiers Program entities are named by identifiers. Syntax  begin with one alphabetic character (a-z or A-Z)  followed by zero or more consecutive alphabetic and/or numeric (0-9) characters. (Underscores and dollars sign are reserved for special usage.) Notes  Take care to use different names for different entities.  Java identifiers are case sensitive (i.e., capital letters are different than small letters).  Identifiers should be meaningful to enhance software readability. Identifiers name... each class - - like DrawingGizmo each variable - - like pen each method - - like moveForward and other things (stay tuned)

© 2006 Pearson Addison-Wesley. All rights reserved Programmers frequently include notes within their programs. Such notes are called comments. Comment Comments can assist you and others in understanding a program. Notes  anythingOnOneLine is any sequence of characters up to the end of the text line.  anythingButCommentEnd is any sequence of characters up to the first */.  Comment is permitted anywhere white space is allowed. anythingOnOneLine // /* anythingButCommentEnd */ Semantics Executing a comment has no effect at run-time. Why Comment?

© 2006 Pearson Addison-Wesley. All rights reserved /** Program to draw two 30 degree angles David Riley August, 2005 */ public class Driver { private DrawingGizmo pen, pencil; public Driver() { pen = new DrawingGizmo(); pen.draw(); pen.moveForward(); pencil = new DrawingGizmo(); pencil.turnClockwise(); pencil.dontDraw(); pencil.moveForward(); pen.turnClockwise(); pen.moveForward(); pencil.draw(); pencil.moveForward(); pencil.turnClockwise(); pencil.moveForward(); } /** Program to draw two 30 degree angles David Riley August, 2005 */ public class Driver { private DrawingGizmo pen, pencil; public Driver() { pen = new DrawingGizmo(); pen.draw(); pen.moveForward(); pencil = new DrawingGizmo(); pencil.turnClockwise(); pencil.dontDraw(); pencil.moveForward(); pen.turnClockwise(); pen.moveForward(); pencil.draw(); pencil.moveForward(); pencil.turnClockwise(); pencil.moveForward(); } DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise() void turnCounterclockwise() void dontDraw() void draw() void delay2Sec()