41
1 Class 1 Lecture Topic Concepts, Definitions and Examples

1 Class 1 Lecture Topic Concepts, Definitions and Examples

Embed Size (px)

DESCRIPTION

3 Instantiation An object is an instantiation of a class or a tangible example of a class. This object car is an example of the class car.

Citation preview

Page 1: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

1

Class 1Lecture Topic

Concepts, Definitions and Examples

Page 2: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

2

Introduction to Java

• Object-oriented programming• Representation of reality• Everything is an object and every

object is a member of a class.• What are the objects in this picture?• What class does each object belong

to?• is-a relationship because you can say

My dog Toby is a dog.

Page 3: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

3

Instantiation

• An object is an instantiation of a class or a tangible example of a class.

• This object car is an example of the class car.

Page 4: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

4

What are these is-a relationships?

Page 5: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

5

Usefulness of Classes

• Reusable• Objects inherit attributes and methods from

classes

Page 6: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

6

Sample Program Design

• When you were planning your spring schedule, what things did automatically know about a course?

• What is it that all course have in common?

Page 7: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

7

Course Attributes

• Understand what a course entails from previous knowledge, these are the attributes.– attributes – these are your class variables

• a name, a number, etc.

• What things won’t you know? These are unique to the course?

Page 8: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

8

Classes also have Methods associated with them.

• methods – actions• get, set, doSomething

• You must set the date and time for a course.– setDate()– setTime()– getDate– getTime().

• What you called functions in C are called methods in Java.

Page 9: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

9

Two Parts to Object Oriented Programming

1. Create a class from which objects will be instantiated.

2. Write other classes to use the objects.

• a class client.• CourseApplication is the client class of

course.

Page 10: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

10

Creating a Class

• Assign a name to the class.• Determine attributes and methods that are

part of class.• For a class called Course, an instance

variable (attribute) might be: – courseNumber

• Two methods might be to set the course number and get the course number.

Course

courseNo:Integer

setCourseNo(Integer) : voidgetCourseNo(): Integer

Instance Variable

Page 11: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

11

First Step: Create a class header public class CourseApplicationAW

• An optional access modifier (public/abstract/final)

• The keyword class• Legal identifier that you choose.• After the above line is the opening {• Closing } goes after all Instance

Variables and Methods.

Page 12: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

12

A Java class Courseclass Course {

private int courseNo;

public void setCourseNo(int cNo){ courseNo = cNo;}

public int getCourseNo(){ return courseNo;}

}

Instance Variable

Methods

Course

courseNo:Integer

setCourseNo(Integer) : voidgetCourseNo(): Integer

Page 13: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

13

Using Classes

• Declaring a class does not create actual object

• Class is an abstract description• You might define an integer as

int someValue;

• Define an object of Course asCourse someCourse;

Notify the compiler that an integer someValue exists and reserve computer memory

When declaring someCourse not setting aside memory yet!

Page 14: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

14

Creating Objects

• Allocating memory:– someCourse = new Course();

• Or you could do in one step:Course someCourse = new Course();

– The keyword new indicates that someCourse is allocated memory.

– Course() is the method that constructs the Course object.

Page 15: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

15

Writing first program

• Need two classes• First is called the driver class,

– you will always have this class in your programs, – it will contain the main– Always call this class

SomethingApplicationYourInitials, replace Something with term relevant to program, CourseApplicationAW

• Second class will be Course

Page 16: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

16

The class CourseApplicationAW-First Attempt

public class CourseApplicationAW {

public static void main(String args[]){ Course someCourse = new Course();}

}•Allocates memory for someCourse, but no action takes place yet.

•CourseApplicationAW is the client class of course – (Has-A)

Page 17: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

17

The class Courseclass Course { private int courseNo;

public void setCourseNo(int cNo){courseNo = cNo;}

public boolean isFourThousandLevel(){if ((courseNo >=4000)&&(courseNo<5000))return true;else return false;}

}

The client class does not call these methods yet. Next Slide!

Course

courseNo:Integer

setCourseNo(Integer) : voidisFourThousandLevel(): Boolean

Page 18: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

18

New class CourseApplicationAW – uses methods of Course

public class CourseApplicationAW {public static void main(String args[]){Course someCourse = new Course();someCourse.setCourseNo(4567);System.out.println(someCourse.isFourThousandLevel());

}}

dot operatorCourseApplicationAW

main(String) : void

Page 19: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

19

Public and not Public

• All our classes are placed in same file.• Only one public class allowed• Other classes are private (not accessible

from outside this program).• Many other ways to approach this…

Page 20: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

20

Improving the class Course

• What attributes can you add to the class Course?

• What methods can you add?

Course

courseNo:Integer

setCourseNo(Integer) : voidisFourThousandLevel(): Boolean

Page 21: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

21

Moving On - Using Forte

1. Pictorially2. Demonstrate Forte3. You try it…

Page 22: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

22

Starting• Select/Open Forte for Java CE• (slow)• check the version number for Java

Page 23: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

23

Forte for Java

helpful hints window – close if appears

tabs – select Editing tab

indicates if a program is running

Page 24: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

24

New ProjectSelect Project/New Project

Create New Project Window, name project – YourInitialsFirstProjselect OK

Page 25: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

25

Choose New

If see above window – select Yes

Page 26: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

26

Full Screen Image – working in window that says: Mount Directory

Page 27: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

27

Mount Directory

• This is your working directory • your files are saved here• MUST be a directory• Best to be a directory on floppy• No directory on floppy, can make one at

this stage.

Page 28: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

28

Mount Directory

make directory – if needed

File Name refers to directory NOT a file.

Mount – mounts workingdirectory

Created New Folder (you can name it), click on New Folder ONCE,Select Mount.New Folder appears as FileName.

Page 29: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

29

Explorer Window

Shows mounted directory, you canmount any number of directories thisway.

This Tab shows the project youcreated, any files you create go intoproject.

Page 30: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

30

Java File

• all java program files end in .java• done automatically• compiled files are .class• Need to create an empty java file.

Page 31: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

31

Creating Program File• highlight (left click on

mounted directory)• right click to bring up

menu• select

new,classes,empty

Page 32: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

32

Selecting Name – Must be IDENTICAL to public class

VERY IMPORTANT – MUST MATCH CASE – Programming Convention – all Class Names – Capitalize each word

Type NameSelect Finish

Page 33: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

33

Respond yes

This places the file within the project you created.If select Project tab will see:

Page 34: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

34

File Created and Ready to Code

Your code goes here

Page 35: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

35

Right Click in Source Editor will bring up menu to Save, Compile,

Execute, etc.

Reformat code – indentscode for you – nice!

Save Often – only lets you saveif you’ve made a change to code

Compile – checks forcompilation error

Execute – runsprogram

Page 36: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

36

Type Code Here

Save, Compile, Execute…

Page 37: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

37

System.out.println – prints outputto this window (usually at bottom

of screen).

true is the boolean value of the response towhether the courseNo is >=4000 and less than 5000

Page 38: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

38

When You’re Finished

• Unmount file system• exit Forte

Page 39: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

39

Live Example

Page 40: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

40

Lab Work•Familiarization with Forte for Java•Writing first program•Improving program•Writing second program

Page 41: 1 Class 1 Lecture Topic Concepts, Definitions and Examples

41

Next Week

• Email Survey• Discussion List Assignment• Quiz – see syllabus – (practice using Forte)• write some small programs using two

classes.• Try the example programs pointed to from

syllabus.