introduction to programming with java. overview what are the tools we are using – what is java?...

19
Introduction to Programming with Java

Upload: hester-robbins

Post on 29-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

– What is BlueJ?• This is the editor in which you write your program

• Goal for the end of the year is to design and build a program of your own.

What are we doing today?

• Introduce some terms

• Get used to the environment

• Get our hands dirty and write some code to draw a creeper’s head

BlueJ IDE Basics

Class Diagram: Lists the classes available

in your program

Object Bench: Shows which objects exists in the computer’s memory.

A Class: Describes an object and what it

can do.

Compile Button:Translates human readable

code into computer readable code.

Terms

• Objects – – Computer’s representation of a real world

object or thing. It exists in the computer’s memory.– Example: Creeper, Skeleton, Sheep, Bow etc.

Anything you can see in your world.

• Class – – Describes what an object is and what it can do. It is a blueprint

for the object in the same way architectural plans describe a building.

– Example: A creeper will be green, have two black eyes, explode, die, walk, etc. Each object is described by a different class.

Practice

Classes

A Picture Object

Practice Creating an Object1. Right-click on the Picture

class

2. Select new Picture() – FYI this is the constructor for a Picture object.

3. In the dialogue box type in creeperHead (with no spaces)

4. This will create a picture object that will eventually draw a picture of your creeper head. You will see a blank window called BlueJ Shapes Demo appear.

5. On your own: Create a new Quadrilateral object. (You can accept the default name. Once you have completed the task, you will see an new object in your object bench.)

Terms

• Method – Is an action that the object can perform. – The class describes how to perform the action.– Given the example below, the method name is makeVisible.

– Example: void makeVisible() method describes how the object is to make itself visible.

• To perform the action, a programmer must invoke or call the method.

Practice Calling Methods1. Right-click on the

Quadrilateral object in the object bench

2. Select void makeVisible()

3. You will see a red square appear in the BlueJ Shapes Demo window.

4. On your own: move the object either up, down, left, or right. What methods enable you to do this?

Terms• Parameter – Is input to your method so that it can do its job.– A value may or may not be required.– A value is required whenever you see words in

between the parenthesis after the method name.

– Example: If I want to change the color of my quadrilateral, I need to state which color I want to make my shape – in this case “green”.

Practice Calling Methods1. Right-click on the

Quadrilateral object

2. Select void changeColor(String newColor) – (String newColor is the parameter)

3. A dialogue box appears. Type “green” in the dialogue box and click OK. (Note: you must type the quotes.)

4. On your own: change the height, width, or position of your object.

IDE Basics Summary

• So far the tutorial has familiarized you with the interactive components of the BlueJ IDE.– Creating Objects.– Calling methods with and without parameters.

• The problem is the your work is not saved and you are limited to what has been provided in the project.

• Next let’s write some code!

Terms• Variable

– is a named storage container that holds a specific type of data.– It must be declared before you can do anything with it.– Once the variable has been declared, you can place the object within

the variable.– The variables creeperHead and creeperHead can hold only a Square; the creeperHat only holds a Triangle.

Quadrilateral leftCreeperEye

Triangle creeperHat

Objects Variables

Quadrilateral creeperHead

Write a Variable Practice1. Double-click the Picture

class to open the source code editor.

2. Type private Quadrilateral head; after the class declaration1 to declare the variable.

3. Type head = new Quadrilateral(); after the draw() method header2 to initialize the variable3.

4. Click the Compile button on the BlueJ main window.

(2) Declare the variable.

(3) Create the object and store it in the variable.

Invoking Methods

At this point if you create a picture object nothing will happen. 1. In the source code editor type

head.changeHeight(200); head.changeWidth(200); head.makeVisible();

2. Compile your code.

3. Create a Picture object. You will see a large red square appear in the BlueJ Shapes Demo window.

4. On Your Own: Add code to the draw method to change the head object “green”. To view available methods: Open the Quadrilateral source code editor and switch to documentation view by clicking on the dropdown with “Source Code” in it.

Code Writing Summary

• A variable declaration reserves space in the computer’s memory to store an object.– It must be declared directly after the class declaration.

• A variable must be initialized in order to hold the object.

• You can invoke methods of an object by using the following template <variable_name>.<method_name>(<parameter_value>);

• All code must be written within a method definition.

Design Your Project• Before starting any project – plan! Follow:

– Understand: ask questions. – Design: on paper layout what you want to do.– Implement & Test: write the code and then test it.– Consider Alternatives: ask how to fix your code if it failed your test.

• Keep in mind the following: “Fail to plan; plan to fail”

• Create a sketch of what you are doing and/or a list of what you will need. It does not need to be perfect, it is just a starting point and it will change.– We need a head

• which is a green square; • two eyes which are two black squares; • a mouth …

Your First Project – Draw a Creeper’s Head• Design: On a separate sheet of paper, draw a creeper’s

head and face. You have already created its head. You will need additional objects in order to complete the picture. On the picture note:– Approximate size and location of the eyes and mouth.– Solve simple problems first. For example the mouth is complex

so make the mouth just a simple rectangle first.

• Implement: In your code declare the necessary variables and then invoke the required methods to draw the remaining eyes and mouth.

• Remember your drawing and plans do not need to be perfect, it is just a starting point and it will change. There will be a lot of trial and error!

Summary

• This tutorial covered a tremendous amount of ground. You learned:– About the BlueJ IDE.– Basic Terminology that is essential to programming.– Write a simple program that drew the head of a creeper.

• At home you can continue practicing your coding skills by drawing other pictures and experimenting.

• Keep a spare copy of your project for safe keeping, then mess around with the code. If you mess something up – no worries because you have your spare from which you can make a new copy!