} Each object in a Java program has an identifier (name) } This includes:

Similar documents
Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Announcements - Grades UBLearns grades just updated Monday, March 28 th (11:00am). Please check grades.

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Lecture 02, Fall 2018 Friday September 7

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

CS Problem Solving and Object-Oriented Programming

Creating objects TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

CS 201 Advanced Object-Oriented Programming Lab 6 - Sudoku, Part 2 Due: March 10/11, 11:30 PM

Outline. Turtles. Creating objects. Turtles. Turtles in Java 1/27/2011 TOPIC 3 INTRODUCTION TO PROGRAMMING. Making things to program with.

Using System.out.println()

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 231 Data Structures and Algorithms, Fall 2016

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Chapter 4 Defining Classes I

Basics of Java Programming

Programming Assignment 1: CS11TurtleGraphics

CS 315 Software Design Homework 3 Preconditions, Postconditions, Invariants Due: Sept. 29, 11:30 PM

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Software and Programming 1

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2015

Programming In Java Prof. Debasis Samanta Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Computer Science II Lecture 1 Introduction and Background

Software and Programming 1

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

GRAPHICS & INTERACTIVE PROGRAMMING. Lecture 1 Introduction to Processing

Programming with Java

Initial Coding Guidelines

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Practice Midterm 1. Problem Points Score TOTAL 50

Java Identifiers, Data Types & Variables

CONTENTS: What Is Programming? How a Computer Works Programming Languages Java Basics. COMP-202 Unit 1: Introduction

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Tips from the experts: How to waste a lot of time on this assignment

G52CPP C++ Programming Lecture 9

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Notes from the Boards Set # 5 Page

Write for your audience

Programming in Java Prof. Debasis Samanta Department of Computer Science Engineering Indian Institute of Technology, Kharagpur

CS 1110, LAB 10: ASSERTIONS AND WHILE-LOOPS 1. Preliminaries

AP Computer Science A Summer Assignment 2017

Midterms Save the Dates!

PREPARING FOR PRELIM 1

Software Design and Analysis for Engineers

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

Quizzes Returned. Next two weeks: Transformations 2/14/2018. Warmup 2/(# of chambers in the human Created by Mr. Lischwe.

Programming with Java

CISC 1600 Lecture 3.1 Introduction to Processing

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Java Bytecode (binary file)

CompSci 125 Lecture 02

Announcements. 1. Forms to return today after class:

Principles of Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Lab # 2. For today s lab:

CSCI-1200 Data Structures Fall 2018 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CS 101: Computer Programming and Utilization. Abhiram Ranade

Lab 12: Lijnenspel revisited

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Topic Notes: Java and Objectdraw Basics

Lecture 7. Log into Linux New documents posted to course webpage

Lecture (01) Getting started. Dr. Ahmed ElShafee

BLM2031 Structured Programming. Zeyneb KURT

1 Getting started with Processing

Basic Data Types and Operators CS 8: Introduction to Computer Science, Winter 2019 Lecture #2

Lab 1: Setup 12:00 PM, Sep 10, 2017

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

CS11 Java. Fall Lecture 1

class objects instances Fields Constructors Methods static

Introduction to Java. Java Programs Classes, Methods, and Statements Comments Strings Escape Sequences Identifiers Keywords

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

COMP-202: Foundations of Programming. Lecture 2: Java basics and our first Java program! Jackie Cheung, Winter 2016

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Programming Practice (vs Principles)

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

We will work with Turtles in a World in Java. Seymour Papert at MIT in the 60s. We have to define what we mean by a Turtle to the computer

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

CMSC 150 LECTURE 1 INTRODUCTION TO COURSE COMPUTER SCIENCE HELLO WORLD

AP Computer Science Unit 1. Writing Programs Using BlueJ

Syntax and Variables

QUIZ. What is wrong with this code that uses default arguments?

Programming Project 1

Algorithms and Programming I. Lecture#12 Spring 2015

Bjarne Stroustrup. creator of C++

1) Log on to the computer using your PU net ID and password.

Transcription:

Class #05: More about Objects and Methods Software Design I (CS 120): M. Allen, 11 Sept. 2018 Important Java Syntax I: Identifiers Each object in a Java program has an identifier (name) This includes: any class, like DrawingGizmo any variable, like pen or pencil any method, like moveforward() anything else Each identifier in a Java program: will contain one or more alphanumeric characters: a z, A Z, 0 9 can also contain the underscore or dollar symbol: _, $ must not begin with a number (e.g., a3 is legal, but 3a is not) Other rules: Names must be unique within same scope This means that, inside any block defined by brackets, we cannot have two different elements with the same identifier Names are case sensitive Names should be meaningful Tuesday, 11 Sep. 2018 Software Design I (CS 120) 2 Important Java Syntax, II: Classes Important Java Syntax, III: main() Every working Java program must define at least one class of object. Here, our program begins by defining the Driver class. We choose this name, so it could be called something else, like Program, or PictureMaker, or whatever we like, as long as it is not some name that already exists. For instance, we cannot call it DrawingGizmo, since that is the name that already belongs to another piece of code we are using in this program. The format of the class definition we will use in most cases is: public class ClassName Every working Java program must begin with a special method called main(), that always looks exactly the same on its first line. This method uses constructors like DrawingGizmo() to actually create all the other objects we need and run their methods for us. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 3 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 4 1

How Does main() Work? Important Java Syntax, IV The main() method that runs everything has a complex form: public static void main( String[] args ) Over the semester, we will learn what this all means! Every complete block of code must be enclosed by braces Blocks include things like a class or a complete method like main() here. Every complete line of code must end with a semi-colon. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 5 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 6 Important Java Syntax, V These are Javadoc comments. Start at opening marker (/**) and end at close (*/). Usually appear right before methods, to explain their operation. Each method you write should have at least one line of these kind of comments. Java Coding Style The name of a class, like Driver or DrawingGizmo, should start with a Capital Letter. The name of a variable, like pencil, or a method, like main() or moveforward(), should start with a lower-case letter. Java allows us to place comments in code. Completely ignored by the computer, and are meant only for ourselves and other programmers to read. These are inline comments. Each line must start with the comment marker (//). Generally used inside body of a class or method (and should be used relatively little except in highly complex code). For multi-word names, we use capitals or underscore symbols, or both, to separate words: multiplewordmethodname() multiple_word_method_name() ComplexClassName Complex_Class_Name Tuesday, 11 Sep. 2018 Software Design I (CS 120) 7 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 8 2

Class Specifications A class diagram only gives a sketch of that class More detail is given by the full specification of a class Every useful Java class should have such a specification When you write code, it s up to you to create this document, so others can make proper use of your code The DrawingGizmo Specification The invariants give us the basic details about the objects, in terms of properties that must always hold: Invariants A DrawingGizmo object appears appears as an arrow within a window entitled Drawing Canvas, and is either in drawing mode (in which case it is green) or moving mode (in which case it is red). Two main parts: 1. Invariants: things that are always true of class objects 2. Methods: all the operations involving the class, with: Pre-conditions: Anything that needs to be true before the method will work properly Post-conditions: Anything that will be true after the method is done Tuesday, 11 Sep. 2018 Software Design I (CS 120) 9 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 10 Basic Methods Update Methods public void draw() post: The DrawingGizmo is set to drawing mode (and is colored green). public void dontdraw() post: The DrawingGizmo is set to moving mode (and is colored red). public void turnclockwise() post: The DrawingGizmo is rotated 30 degrees clockwise from its current heading. public void turncounterclockwise() post: The DrawingGizmo is rotated 30 degrees counterclockwise from its current heading. Some methods have post-conditions only Nothing special is required for them to work Only need DrawingGizmo object instance to call them Tuesday, 11 Sep. 2018 Software Design I (CS 120) 11 Methods with Both Pre- and Post-conditions public void moveforward() pre: The DrawingGizmo should be at least 20 pixels from the edge of the screen towards which it is pointing; if it is not, then the object and part of the line it draws will appear o -screen. post: TheDrawingGizmo moves in the direction of its arrow by 20 pixels. If it is is drawing mode, a line segment will be drawn between its current and prior position. The preconditions on the moveforward() method tell us what must be true before the method can properly run. (If these are not satisfied, then the method may not necessarily work properly, or even work at all.) The postconditions tell us what will be true after the method has finished running properly. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 12 3

More Complex Methods DrawingGizmo object has simple methods, each of which does the same thing, the same way, every time But it also has some more complex methods, which: Take input parameters Change what they do based on those parameters DrawingGizmo <<constructor>> DrawingGizmo() <<update>> void moveforward() void turnclockwise() void turncounterclockwise() void dontdraw() void draw() void turnby( int ) void moveby( int ) void delayby( int ) A Parameterized Method public void turnby( int n ) post: This object is rotated n degrees clockwise from its previous direction. The turnby( int ) method wants us to insert an integer value ( int in Java) as an input parameter We must give it an integer when we call it, or it won t work! pen.turnby( 9000 ); pen.turnby( -180 ); pen.turnby( 3.6 ); pen.turnby(); All of these are OK. These are both wrong. Tuesday, 11 Sep. 2018 Software Design I (CS 120) 13 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 14 Parameterized Methods vs. Non-parameterized Methods The parentheses () in a method specification serve two purposes: 1. They indicate that it is a method, so compiler and coder know what it is 2. They show whether or not the method has any input parameters If the method is not parameterized, then nothing is shown in the parentheses, and when we use it, we cannot put anything there, or our code won t compile Similarly, if the method is parameterized, then we have to put actual parameters into the code when we use it In this description, the diagram shows the type of the input parameter It is up to us to actually choose what particular input value we will put in there when we call the method DrawingGizmo <<constructor>> DrawingGizmo() <<update>> void moveforward() void turnclockwise() void turncounterclockwise() void dontdraw() void draw() void turnby( int ) void moveby( int ) void delayby( int ) Two More Parameterized Methods public void setbackground( java.awt.color ) public void setforeground( java.awt.color ) These new DrawingGizmo methods change the color of the background window and drawing line E.g., for a yellow line on a blue background: pen.setbackground( java.awt.color.blue ); pen.setforeground( java.awt.color.yellow ); Tuesday, 11 Sep. 2018 Software Design I (CS 120) 15 Tuesday, 11 Sep. 2018 Software Design I (CS 120) 16 4

The Java Color Class Address java.awt.color.green library group library package class class member Supported Colors java.awt.color.black java.awt.color.blue java.awt.color.cyan java.awt.color.darkgray java.awt.color.gray java.awt.color.green java.awt.color.lightgray java.awt.color.magenta java.awt.color.orange java.awt.color.pink java.awt.color.red java.awt.color.white java.awt.color.yellow Tuesday, 11 Sep. 2018 Software Design I (CS 120) 17 Using Import Statements Instead Rather than write the entire Color address each time, we can import the Color class, using information found in the list of standard Java classes Inside the Color class specification, we find the library path for importing: Class package and name One or more lines of a class hierarchy http://docs.oracle.com/javase/8/docs/api/ The last line gives the path we need to import and use the class Tuesday, 11 Sep. 2018 Software Design I (CS 120) 18 public class Driver public static void main( String[] args ) DrawingGizmo pen; pen = new DrawingGizmo(); Using the Imported Code pen.setbackground( java.awt.color.blue ); pen.setforeground( java.awt.color.yellow ); import java.awt.color; public class Driver public static void main( String[] args ) DrawingGizmo pen; pen = new DrawingGizmo(); This adds the import command to the class, and now we don t have to use the full address for our colors These programs produce exactly the same result on-screen when they run Tuesday, 11 Sep. 2018 Software Design I (CS 120) 19 pen.setbackground( Color.blue ); pen.setforeground( Color.yellow ); What About the DrawingGizmo? If we want to use built-in Java classes like Color, we need to either refer to their full class address or import For classes that aren t built in to the language, there is another simple option: put all the class files into the same source directory (folder) When we run our program, the JVM will allow us to use any class of object that we address, import, or that it can find along-side the source code for the main() method Tuesday, 11 Sep. 2018 Software Design I (CS 120) 20 5

For This Week Meetings this week: Tuesday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Homework 01: Colonel Sanders image Friday, 14 Sept., 5:00 PM, D2L Reading assignment: Chapters 1 2, due today Chapter 3, sections 1 9: Next Tuesday, 18 Sept. (start of day) Quiz 01: Covers lectures 01 04 (all will be on Notes page) Tuesday, 18 Sept. (end of class) Office Hours: Wing 210 Monday/Wednesday/Friday, 12:00 PM 1:00 PM Tuesday/Thursday, 1:30 PM 3:00 PM Tuesday, 11 Sep. 2018 Software Design I (CS 120) 21 6