Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Introduction to Programming with Java

2 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.

3 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

4 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. Compile Button: Translates human readable code into computer readable code.

5 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.

6 Practice Classes A Picture Object

7 Practice Creating an Object 1.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.)

8 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.

9 Practice Calling Methods 1.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?

10 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”.

11 Practice Calling Methods 1.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.

12 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!

13 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

14 Write a Variable Practice 1.Double-click the Picture class to open the source code editor. 2.Type private Quadrilateral head; after the class declaration 1 to declare the variable. 3.Type head = new Quadrilateral(); after the draw() method header 2 to initialize the variable 3. 4.Click the Compile button on the BlueJ main window. (2) Declare the variable. (3) Create the object and store it in the variable.

15 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.

16 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. ( ); All code must be written within a method definition.

17 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 …

18 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!

19 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!


Download ppt "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."

Similar presentations


Ads by Google