Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …

Slides:



Advertisements
Similar presentations
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Cosc 1P02 Week 2 Lecture slides
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
1 Lecture Today’s Topics Classes –Attribute (variables) –Behaviors (methods) Using methods –How to write methods –How to use methods Scope –Private.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Lecture 3. Review (What is Class and Object ?) The world of JAVA contains objects The world of JAVA.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Lecture 5. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
Introduction to Programming Writing Java Beginning Java Programs.
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Introduction to Object-Oriented Programming
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Object Oriented Programming … and other things you need to program in java.
Java Classes Methods Objects. Classes Classes We have been using classes ever since we started programming in Java Whenever we use the keyword class.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
CSE8A Lecture3 TODO: –Finish PSA1 individually (no partner!) and turn it in with the bundlePSA1 command GET AN INTERVIEW for PSA1 from a tutor See tutor.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Copyright © Curt Hill Turtles The beginning of media computation.
Writing Methods Mrs. C. Furman October 9, Drawing a Square World worldObj = new World(); Turtle turtle1 = new Turtle(100, 100, worldObj); turtle1.forward.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Introduction to Java Java Translation Program Structure
CSC 142 Computer Science II Zhen Jiang West Chester University
Introduction to Programming Writing Java Beginning Java Programs.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
EE 422C Day 2 Java, Eclipse. Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Getting Started With Java September 22, Java Bytecode  Bytecode : is a highly optimized set of instructions designed to be executed by the Java.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Turtle Graphics Lesson 2 1. There are 3 homeworks to complete during the six lessons of this unit. Your teacher will let you know when a homework has.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
Foundations of Programming: Java
Functions + Overloading + Scope
3 Introduction to Classes and Objects.
Java Course Review.
Object Oriented Systems Lecture 03 Method
CompSci 230 Software Construction
Introduction to Object-Oriented Programming in Java
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
5 Variables, Data Types.
Defining Classes and Methods
An Introduction to Java – Part II
Defining methods and more arrays
Teaching Java using Turtles part 3
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
More on Creating Classes
Introduction to Object-Oriented Programming
More on Creating Classes part 3
References Revisted (Ch 5)
Presentation transcript:

Lecture 4

Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// … /////// Method /////// … }

Review (Attributes and Methods) Example Turtle.java public class Turtle { ///// Field (Attributes) ///// private String name = “me”; private int height = 15; private Pen pen = new Pen(); /////// Method /////// public void forwrad() { } public void turnRight() { } } … … me 15

Source code Java compiler Java bytecode Java interpreter Bytecode complier Machine code.java file.class file Execute! Review (compile) We can test (in Interaction panel)

Today – Variables – Let’s write a method with arguments

What is a variable A variable is a name for a location It holds a data value In mathematics, X and Y are variable Y = 5X + 3 when X=2, Y= 13 when X=5, Y= 28 XY Replaced!

Variables In Java program –A variable must be declared before using it –We need to declare a variable by specifying the variable's name and the data type that it will hold int length; data typevariable name length int What is data type???

Data type Data type is a type of value that variable holds For example int  integers positive and negative numbers e.g. 3, 21, -18, 0, etc … double  decimal numbers positive and negative numbers with floating point e.g. 2.5, 3.0, -9.2, 0.0, etc … Note: There are 8 other primitive types in Java (we will come back this topic later)

Therefore … What does this line mean??? We declare a variable, named length The type of value should be int Then, initialize the variable with 15 int length = 15; data type variable name length 15 value

Example of drawSquare() public void drawSquare() { int length = 100; forward(50); turnRight(); forward(50); turnRight(); forward(50); turnRight(); forward(50); turnRight(); } Let’s declare a variable!!! Name: length Type: int Value: 100

Example of drawSquare() public void drawSquare() { int length = 100; forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); } Once a variable is declared, we can use the variable Efficient to modify source code !!! Exercise: Draw a square with the size of 125 (pixels)

In order to write different size of square we have to modify source code and compile it again and again and again… Any more efficient way … ?

Arguments What is arguments? A method can receive values as input variables –They are called arguments (or parameters) For example ( do you remember this method? ) forward( 100 ); turn( 31.5 ); …

Example of drawSquare() public void drawSquare(int length) { forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); forward(length); turnRight(); } We can declare a variable inside parenthesis Method will receive the value for length whenever it is called For example t.drawSquare(25);

Method with more arguments A method can receive one or more arguments public void drawRectangle(int base, int height) { forward( height ); turnRight(); forward( base ); turnRight(); forward( height ); turnRight(); forward( base ); turnRight(); }

Exercise 1)Write a method, named drawTriangle which will draw a triangle with the user-defined size hint: you will call t.drawTriangle( 50 ); 2) Write a method to draw a square at the position of (x, y) in a window, where x and y are provided as input variables hint: you will call t.drawSquare(250, 140);

Challenge!!! 3) Write a method, named drawLine which will draw a line with the user-defined length toward the user-defined direction hint: you will call t.drawLine( 25, 45 ); t.drawLine( 50, 200 ); t.drawLine( 30, 100 ); etc…