24
1 COS 260 DAY 2 Tony Gauvin

1 COS 260 DAY 2 Tony Gauvin. 2 Agenda Questions? Class roll call Blackboard Web Resources Objects and classes 1 st Mini quiz on chap1 terms and concepts

Embed Size (px)

Citation preview

1

COS 260 DAY 2

Tony Gauvin

2

Agenda

• Questions? • Class roll call• Blackboard Web Resources• Objects and classes• 1st Mini quiz on chap1 terms and concepts

– September 10 – 30 min., M/C and short answer, open book, open

notes, open computer

• Assignment 1 posted in Blackboard – Chap1 & 2 – Due September 17 prior to class

3

Getting what you need1. Java JDK

– http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

– Pick the Kit for your OS

2. BlueJ – http://www.bluej.org/ – Download BlueJ installer for your OS

3. Projects for Class (and other resources) – http://www.bluej.org/objects-first/

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

5.0

Objects First with JavaA Practical Introduction using

BlueJ

David J. BarnesMichael Kölling

5

Objects and classes• objects

– represent ‘things’ from the real world, or from some problem domain (example: “the red car down there in the car park”)

• classes– represent all objects of a kind (example:

“car”)

• Object Oriented Programmers write class code and invoke objects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

6

Objects and Classes

•http://courses.oreillyschool.com/java1/javaOne06.html

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

7 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Class

Object

8

Methods and parameters

• Objects have operations which can be invoked (Java calls them methods).

• Methods may have parameters to pass additional information needed to execute.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

9

Other observations

• Many instances can be created from a single class.

• An object has attributes: values stored in fields.• Attributes describe the object’s “state”

• The class defines what fields an object has, but each object stores its own set of values (the state of the object).

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

10

State

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

11

Two circle objects

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

12

Java Data Types

• Integers int, byte, short and long– 2 , 25879412547, -56, 111

• Real numbers float and double– 58.45, 3.14159265

• Logic boolean– true, false

• Character char– ‘A’, ‘a’, ‘&’, ‘=‘, ‘@’, ‘1’,

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

13

Source code

• Each class has source code (Java code) associated with it that defines its details (fields and methods).• Code is stored in a “*.java” file

where * is the name of the class

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

14

Java Code

• Person person1 = new person();• person1.makevisible();• person1.moveright();• person1.moveHorizontal(50);

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

15

Methods

Method signature

Return_Value<space>method_name(parameter_type<space>parameter_name);

void moveHorizontal(int distance);

Method Invocation object_name.method_name(parameters);

circle1.moveHorizontal(40);

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

16

Return values

• All the methods in the figures project have void return types; but …

• … methods may return a result via a return value.

• Such methods have a non-void return type.

• More on this in the next chapter.

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

17

Make this picture • Use figures project (hint: use terminal

window with record methods options to see java code being implemented

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

18

Make this animation

• Use moveSlowHorizontal & and moveSlowVertical to make the child walk up the hill to stand beside the parent

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

19

Learning Java Code

• Instead of using the right mouse clicks on object bar we can use BlueJ Code pad to type in Java code directly

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Code Pad

20

Writing Java Code • Using editor

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

21

Modifying Code

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Using editor modify code for picture to add a chimneyin house project

22

Creating A database

• Open lab-classes project• Create students using visual

controls and code pad• Using object bench and code pad

and monitor terminal window– getName()– getCredits()

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

23

Objects AS Parameters

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Student br = new Student( "Barney", "YYY");labClass1.enrollStudent(br);

Do exercise 1.27, 1.28 & 1.29 on page 16

24

Terms for Today (and for mini-quiz)

• Object• Class• Instance• Method• Signature• parameter

• Type• State• Source code • Return value• Compiler

Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling