Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Games and Simulations O-O Programming in Java The Walker School
Program: Little Crab Mr Gano.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Java Basics M Taimoor Khan
Inheritance Inheritance Reserved word protected Reserved word super
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
A Simple Applet.
Color (1) Turtle class contains a method to change the pen color Note: before using Color class, you should add following line in the top of the source.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.3 The Class String.
Games and Simulations O-O Programming in Java The Walker School
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Java: Chapter 1 Computer Systems Computer Programming II Aug
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
© 2006 Pearson Education Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition.
Introduction to Programming Writing Java Beginning Java Programs.
Java: Chapter 1 Computer Systems Computer Programming II.
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
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.
1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.
The Java Programming Language
Input, Output, and Processing
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
1 C++ Programming Basics Chapter 2 Lecture CSIS 10A.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
Introduction to Programming Writing Java Beginning Java Programs.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
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.
Classes, Interfaces and Packages
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 2 – The Little Crab Program:. Little Crab Scenario Inheritance: The Arrows Denote Hierarchy Crab is an Animal Animal is an Actor Therefore, It.
Lecture 4 – Scanner & Style
Functions + Overloading + Scope
Chapter 3 Syntax, Errors, and Debugging
Data types and variables
Java Review Hello..This ppt is to give you an introduction about java and why it is soo special The segments all discussed here are the crucial parts of.
Introduction to the C Language
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Variables ICS2O.
Organizing Memory in Java
CMSC 202 Java Primer 2.
Introduction to Primitive Data types
Applying OO Concepts Using Java
Instructor: Alexander Stoytchev
Introduction to Primitive Data types
Presentation transcript:

Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks or query the state of the object Fields store information about the state of an object In our first attempt at Java we will look at how to use an object when programming Later our goal will evolve into building out own objects

We will learn beginning programming concepts by using software called Greenfoot Download and install from:

If you’ve been successful downloading and installing Greenfoot you should be able to open the TurtleGraphics scenario so we can get started

A programmer is constantly reading documentation to learn how to use existing code Rather than write their own from scratch Greenfoot provides documentation in two way Built in classesCustom classes

Add a Turtle to the World Right click on the Actor or hold the shift key

As a general rule you should never edit code that is not yours unless you’ve been told/paid to or are fixing a bug Instead we can take all of the code and use it as our own without effecting the parent class. In this case the parent class is the Turtle. You will make your own subclass to do your tasks. This is a good design principle. In Java you “inherit” all methods and fields. You cannot access anything declared private though Let’s make our own Turtle to edit

Step 1Step 2

Time to code! I will walk you through a demo of how I can find and test which commands I need to make my initials using the mouse at runtime. I’ll make a note of the code so that I can reconstruct my initials without relying on the mouse at run time. In the end I will have a program that will draw my initials by running it in Greenfoot

public void drawInitials() {mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("penDown()");mrJoyce.addCommand("move(20)");mrJoyce.addC ommand("turnR()"); mrJoyce.addCommand("move(20)");mrJoyce.addCommand("turnR()");mrJoyce.addCom mand("move(20)");mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(10)"); mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(20)");mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(10)"); }

Java does not care how your code is organized BUT YOU SHOULD! Code should be lined up to the block in which is was created { addCommand(… … } Tabbed Block

public void drawInitials() { mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(10)"); mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(10)"); }

Hard to understand what’s going on! Programming languages allow for comments to be added to the source code which is ignored at compilation Java has three mechanisms: Inline: // Multiline: /* … */ Javadoc: /** … */

public void drawInitials() { /* The code below will draw Mr. Joyce’s initials in Turtle Graphics, C J The letters are drawn in black in a 20 pixel block */ mrJoyce.addCommand("turnL()");//starts facing east, need to turn 180 degrees mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("penDown()");//ready to draw the C now mrJoyce.addCommand("move(20)");//bottom edge mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(20)");//left side mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(20)");//top mrJoyce.addCommand("penUp()");//pick up pen to move over for J mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penDown()");//begin drawing J mrJoyce.addCommand("move(20)");//top mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(10)");//move to middle mrJoyce.addCommand("penDown()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("move(20)");//middle mrJoyce.addCommand("turnR()"); mrJoyce.addCommand("move(10)");//bottom }

As programmers we make better software by breaking down tasks into small manageable chunks Methods! In my case I’ll make two methods Draw C Draw J

Methods in java always have the following syntax ( …) { } Example: public void setPenColor(int red, int green, int blue) { … } The name and parameter list defines their signature and must be unique Overloading a method means using the same name but different signatures Ex. setPenColor(Color c) vs setPenColor(int red, int green, int blue)

Not an easy question (in general) I’ll make the C and J independent of whatever else might be happening in the program Need to document what to expect so programmers know how to achieve desired behaviour

/** * Draws a C in a 20 pixel block * Pre Conditions: *-Turtle has the penDown *-Turtle is facing West (180 degrees) *-Turtle begins at the bottom right position of the C * / public void drawC() { addCommand("move(20)"); addCommand("turnR()"); addCommand("move(20)"); addCommand("turnR()"); addCommand("move(20)"); }

/** * Draws a J in a 20 pixel block * Pre Conditions: *-Turtle has the penDown *-Turtle is facing East (0 degrees) *-Turtle begins at the top left position of the J * / public void drawJ() { addCommand("move(20)"); addCommand("penUp()"); addCommand("turnL()"); addCommand("move(10)"); addCommand("penDown()"); addCommand("turnL()"); addCommand("move(20)"); addCommand("turnR()"); addCommand("move(10)"); }

public void drawInitials() { //starts facing east, need to turn 180 degrees mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("penDown()"); //ready to draw the C now mrJoyce.drawC(); //pick up pen to move over for J mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("move(20)"); mrJoyce.addCommand("penDown()"); //begin drawing J mrJoyce.drawJ(); }

Getting there… One other issue with our code is a typical rookie mistake called magic numbers Numbers that appear and reappear in your code rather than using a variable Good: int pennies = 3; int nickels = 2; int dimes = 5; int quarters = 3; int loonies = 2; int twonies = 3; double total = 0.01*pennies *nickels *dimes *quarters + 1*loonies + 2*twonies; Bad: double total = 0.01* * * *3 + 1*2 + 2*3; Calculate the same thing What if number of coins changes? Which 3 do I change? Quarters can be reused in your program, with clarity, how about 3?

= new ; Turtle mrJoyce = new Turtle(); mrJoyce is the variable In Java, could be any name provided that: The first character is {a-z} | _ | $ The remaining characters {a-z} | {0-9} | _ | $ It is not a reserved word (int, class, public, void, etc.) Style 101 Like constants, programmers use a particular style for their variables Do not start with a capital (class names start with capitals) Use an underscore, _, between words or a capital letter mr_joyce or mrJoyce Should be a name that is appropriate to your code xg_10a15$243 could be used but what the heck are you talking about volume, height, age, size, pixels, width, etc. are much better Avoid numbering them a1, a2, a3,

/** * Draws a C in a given pixel block * Pre Conditions: *-Turtle has the penDown *-Turtle begins at the bottom right position of the C blockSize the number of pixels the C fill fit in * / public void drawC(int blockSize) { addCommand("move("+blockSize+")"); addCommand("turnR()"); addCommand("move("+blockSize+")"); addCommand("turnR()"); addCommand("move("+blockSize+")"); }

/** * Draws a J in a given pixel block * Pre Conditions: *-Turtle has the penDown *-Turtle begins at the top left position of the J blockSize the number of pixels the J fill fit in * / public void drawJ(int blockSize) { addCommand("move(" + blockSize + ")"); addCommand("penUp()"); addCommand("turnL()"); addCommand("move("+(blockSize/2)+")"); addCommand("penDown()"); addCommand("turnL()"); addCommand("move("+blockSize+")"); addCommand("turnR()"); addCommand("move("+(blockSize/2)+")"); }

For now, we’ll use Strings mostly for represent text Rather than manipulate text A String is a sequence of characters They are immutable – cannot be changed once created Can reassign a variable to a new value though (different memory) To join strings together you can use the “+” operator This is called concatenation The rules for string concatenation are simple String + = String + =

“1”+”2” = ? “3” + 5 = ? “7” + 9 = ? “5” = ? “3” “5” = ? “1” + ( ) + “5” + 6 = ?

public void drawInitials(int pixels) { //starts facing east, need to turn 180 degrees mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("turnL()"); mrJoyce.addCommand("penDown()"); //ready to draw the C now mrJoyce.drawC(pixels); //pick up pen to move over for J mrJoyce.addCommand("penUp()"); mrJoyce.addCommand("move("+pixels+")"); mrJoyce.addCommand("penDown()"); //begin drawing J mrJoyce.drawJ(pixels); }

Did you try the setPenColor method? It has two forms RGB color Color object Color is defined in java.awt.Color import java.awt.Color Importing libraries allows us to use other programmers code to build our software Let’s look at the Color class documentation to find the available colors

A convention that is widely used if not universal is to capitalize constants in your code. That way a programmer knows (by looking at it) that its value cannot be changed, only accessed Who owns the field? If it is shared by the entire class of objects it should be static. Example: Math.PI Color.BLUE

Adjust your initials to be drawn using a color (or several colors) other than black. Use both methods One with a Color, the other with 3 RGB values Use paint to find RGB values that you like

Objects Classes Method Signature Overloading Parameters Return types Access Fields Constants Importing libraries Variables Constructors Whitespace Blocks Comments Magic Numbers