Introduction to Programming Writing Java Beginning Java Programs.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
 2005 Pearson Education, Inc. All rights reserved Introduction.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Chapter 2 Using Objects. These slides for CSE 110 Sections are based in part on the textbook-authors ’ slides, which are copyright 2003 by Cay Horstmann.
Objects Interaction and Source Code Week 2. OBJECT ORIENTATION BASICS REVIEW.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2  Using Objects 1 Chapter 2 Using Objects.
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.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
Intro to CS – Honors I Programming Basics GEORGIOS PORTOKALIDIS
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Programming Writing Java Beginning Java Programs.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2: Classes and Objects, using Scanner and String.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
JAVA BASICS: Variables and References SYNTAX, ERRORS, AND DEBUGGING.
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.
Classes CS 21a: Introduction to Computing I First Semester,
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
Chapter 1 Object Orientation: Objects and Classes.
CIT 590 Intro to Programming First lecture on Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java Introduction part 2 CSC1401. Overview In this session, we are going to examine some of the instructions from the previous class in more detail.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 4 Introduction to Classes, Objects, Methods and strings
Copyright Curt Hill Variables What are they? Why do we need them?
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Introduction to Object-Oriented Programming Lesson 2.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Aug 2006 Introduction to Object-Oriented Programming in Alice and Java.
Chapter 2 Using Objects. Chapter Goals To learn about variables To understand the concepts of classes and objects To be able to call methods To be able.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Object Oriented Programming Object and Classes Lecture 3 MBY.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Chapter 1 Object Orientation: Objects and Classes.
Yanal Alahmad Java Workshop Yanal Alahmad
CompSci 230 Software Construction
Introduction to Object-Oriented Programming in Java
Chapter 2 Using Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Introduction to Java part 2
Teaching Java using Turtles part 2
Teaching Java using Turtles part 3
Object Oriented Programming in java
Introduction to Java part 2
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Introduction to Object-Oriented Programming
In this class, we will cover:
Teaching Java using Turtles part 2
Methods Coding in Java Copyright © Curt Hill.
Presentation transcript:

Introduction to Programming Writing Java Beginning Java Programs

Review of Lab 1 ► Built-in Types  Integer types ► No decimal point ► integer math-op integer yields an integer ► byte, short, int, long  Real types ► Have decimal point ► real math-op real yields a real ► float, double

Review of lab 1 Continued ► Java Variables  Attributes ► Name ► Type ► Value  Specifies where data is stored in memory  Java requires that a variable be declared before it can be used.  Declaration Form: ► Type variable-name; ► Can only declare once!

Review of Lab 1 ► What is wrong? int firstNum = 0; int firstNum = firstNum + 1; ► What does the = operator in Java mean? ► What is the type of the result of double total, price; int amt; total = price * amt; ► What is wrong with amt = total/price;

Review of Lab 1 Continued ► Variable Naming Conventions  Start with letter or _ follow with any number of letters, digits, or _  Names are case sensitive  Convention – variable names start with lower case letter.  Names should be meaningful. ► sales ► noItems ► qtyOnHand Camel notation

The Semicolon Rule ► Always end a Java statement with a semicolon!  Be careful! The interaction pane does not require a semicolon after each statement.  In a program typed into the definition pane, Java does.  In the interaction pane, typing a semicolon after a statement does not show the value of the result. Leaving the semicolon off does.

The Semicolon Rule ► Always end a Java statement with a semicolon!  The code pad in BlueJ requires a semicolon after each Java statement.  If you forget the semicolon, you will get an error in code pad reminding you that a semicolon is required.

Object Variables ► Java is an Object-oriented programming language. ► We work mostly with objects. ► However, built-in numeric types are not objects. ie. int, float, double, … ► Strings are objects. ► String myName;  myName is a reference to a string object  Objects must be created before they are assigned.  myName has the initial value of null (it does not reference a string object)  Much more on this later.

Classes and Objects ► Classes are templates that define what objects look like and how they behave. ► An object is an instantiations of a class ► Class names in Java should start with a capital letter. ► Turtle is a class  It defines object methods that let you use a Turtle object.  //create a turtle object from the Turtle class Turtle myTurtle = new Turtle(myWorld); Turtle myTurtle = new Turtle(myWorld); Class name defines the type Makes a new object Constructor: Method that initializes a new obj.

Object (Instance) Methods ► The behavior of an object is defined by the methods that belong to the method. ► Each instance of an object has it own copy of all the methods defined in the object’s class that are not Class Methods. ► Turtle turtle1 = new Turtle(myWorld); Turtle turtle2 = new Turtle(myWorld); turtle1.turnRight(); turtle2.turnRight(); Note: both turtles have a turnRight method

Class Methods ► Some classes have methods that belong to the class and not an instance of the class. ► java.Math  Class that defines math functions to use  Would not want multiple math objects that each implement an abs method.  Class methods are methods that belong to only the class and hence there is only one copy of the method for all objects of the class.  int myVal = Math.abs(-12);

A Basic Java Program // if you have to add import statements put //them here public class CS1Test { public static void main(String [] args) { public static void main(String [] args) { // put you Java program statements here // put you Java program statements here }} ► You will see what this is for in a minute.

Working With Turtles Example of using classes ► Get a world for our turtle to live in  World csExWorld = new World(); ► Put a turtle in our world  Turtle ourTurtle = new Turtle(csExWorld); ► Check the state of our turtle  System.out.println(ourTurtle);  System.out.println(ourTurtle.toString()); Parameter that specifies the world to put the turtle in.

Talking to a Turtle ► When we want an object to do something we send it a message. ► Make the turtle wander around  ourTurtle.forward(20);  ourTurtle.turnLeft();  ourTurtle.forward(40);  ourTurtle.turn(45);  ourTurtle.forward(65);

How Do I Know What Turtles Can Do? ► The set of methods that define what a class can do is called an API (Application Program Interface). ► Java defines a special format called JavaDoc for presenting APIs. ► You can find a link to the doc on the desktop of the lab machines and at C:\JavaDocumentation\Intro2CS\doc\index. html.

Problem: Draw a square ► Describe how the turtle will travel in a square. ► Program Code: //draw side one ourTurtle.forward(100); // turn 90 deg counterclockwise ourTurtle.turnLeft(); // draw side two ourTurtle.forward(100); // turn 90 deg counterclockwise ourTurtle.turnLeft(); // draw side three ourTurtle.forward(100); //turn 90 degrees counterclockwise ourTurtle.turnLeft(); // draw side four ourTurtle.forward(100);

Reusing Code ► What if we wanted to draw a second square?  We would have to retype the code a second time.  Not good! Is not there a better way? ► Define a method that draws a square. ► We can invoke it every time we need to draw a square.

The drawSquare Method public void drawSquare() { //draw side one this.forward(100); // turn 90 deg counterclockwise this.turnLeft(); // draw side two this.forward(100); // turn 90 deg counterclockwise this.turnLeft(); // draw side three this.forward(100); //turn 90 degrees counterclockwise this.turnLeft(); // draw side four this.forward(100);} } Body of the method Parentheses required Opening and Closing Brace Required 1.Open the Turtle.java file found in C:\intro-prog- java\bookClasses 2.Put the method definition at the bottom of the class where the documentation directs. 3.Create a World, a Turtle, and draw a square.

How do we draw two squares? ► Call drawSquare() twice. ► What happened? ► Cannot call drawSquare again it will draw over top the square we have drawn. ► Need to move the Turtle before we draw again. ► How? Look at documentation  Pen up  Move to new position  Pen down

Parameters ► How do we draw squares of different sizes? ► Observation: Each time we draw a square we need to change the 100 to the size we want. ► This could generate a lot of almost identical methods. ► Use a parameter to specify the width.  public void drawSquare(int width) { … }  Change 100 to width ► Call as:  ourTurtle.drawSquare(50);

Draw the squares in Different Colors ► Back to the documentation  public void setColor(java.awt.Color color)  Need a Color object found in java.awt package  Must import this package to use it  Find information in Java 1.5 API Documentation ► Invoke  ourTurtle.setColor(Color.BLUE);  Draw a blue square.

How do you …? ► Save the program?  Put it in a CSTurtleTest class ► Hide the turtle? ► Draw the squares at an angle to the window?