Download 1 Objects and classes 2 Data types and variables in Java 3 Anatomy

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COE 318/BME 506(Software Systems)
Lecture Notes Thursday Sept. 10, 2010
Page 1 of 3
Software Systems (coe318/bme506) Lecture Notes
Thursday, September 10, 2010 (2 hours)
Announcements:
•
Lab 1 available on course website (http://www.ee.ryerson.ca/~courses/coe318)
•
My lecture notes available from the website or directly here.
1 Objects and classes
•
A class describes the general characteristics and behaviour of a type of object. It is
like a blueprint.
•
For example, a Dog class might describe a dog in general as having a breed and a
size and being able to “speak”.
•
The characteristics are data and are implemented as instance or state variables.
•
Behaviours are implemented with methods.
•
A constructor is used to create a new specific instance of the general notion
described by the class.
2 Data types and variables in Java
•
Java has two kinds of data types: primitive types and object types.
•
Primitive data types include int, byte, short, long, char, float,
double and boolean.
•
These are similar to the corresponding C types; boolean has two possible
values: true and false.
•
There are thousands of built-in object types. For the moment, we will only use
String.
•
Each class you write, creates a new data type of the class's name. For example, a
class called Dog creates a Dog data type.
3 Anatomy of a class
•
Source code for a class is written in a file whose name is the class name with the
extension .java. (For example, Dog.java.) Note that the class name should begin
with an uppercase letter.
DRAFT
COE 318/BME 506(Software Systems)
•
•
Lecture Notes Thursday Sept. 10, 2010
Page 2 of 3
A class contains zero or more members where a member can be:
•
an instance variable;
•
a constructor;
•
a method
The simplest possible class is:
public class Empty {
}
•
A class member is accessible outside of the class if it is declared public.
•
It is inaccessible if it is declared private.
•
•
Until further notice, instance variables should be private and methods and
constructors should be public. This is the first step to achieving encapsulation.
A class can be compiled outside of an IDE with the javac command.
•
For example, javac Dog.java will compile the Dog class producing the
compiled output file Dog.class.
•
The class file can then be executed (if it contains a main method) with the
command java Dog. (Note: the class extension is not included.)
•
A class file can (should!) also contain javadoc comments which describe the
Application Programming Interface (API) to public members. These kind of
comments are enclosed with
/**
•
javadoc comments
*/
The command javadoc Dog.java can then be used to generate html
formatted documentation in the file Dog.html.
4 A Person who can be male or female (GenderedPerson)
•
Live demo in class
5 Discussion of Lab 1
6 Post lecture study guide
1. Read Chapters 1 and 2 of Head First Java. NOTE: We have not yet covered all of the material
in these chapters (such as conditional statements and loops), but you should be able to follow
most of the material. (Loops and conditionals are done in Java in a way very similar to what
you have previously learned about these topics in you C programming course.)
2. Look at the first chapter of Oracle's Java tutorial. (This excellent tutorial covers all of Java—
much more than is covered in this course—and its many downloadable examples may help.)
DRAFT
COE 318/BME 506(Software Systems)
Lecture Notes Thursday Sept. 10, 2010
Page 3 of 3
3. Alice says, “I like the idea of having a car, but haven't decided if I prefer a Ford Fusion or a
Toyota Prius yet.” Bob says, “That red Ferrari must be doing 250 km/hr and it's headed right
for me. I'm scared!”
Is Alice talking about a class or an object? What about Bob? Briefly explain.
7 Prep for next lecture
•
Start working on lab 1.
Copyright © 2010 Ken Clowes. This work is licensed under the Creative Commons Attribution 3.0 Unported
License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a
letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
DRAFT