Upload
jeffry-chapman
View
225
Download
1
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
1
Class 1Lecture 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.
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.
4
What are these is-a relationships?
5
Usefulness of Classes
• Reusable• Objects inherit attributes and methods from
classes
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?
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?
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.
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.
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
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.
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
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!
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.
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
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)
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
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
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…
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
21
Moving On - Using Forte
1. Pictorially2. Demonstrate Forte3. You try it…
22
Starting• Select/Open Forte for Java CE• (slow)• check the version number for Java
23
Forte for Java
helpful hints window – close if appears
tabs – select Editing tab
indicates if a program is running
24
New ProjectSelect Project/New Project
Create New Project Window, name project – YourInitialsFirstProjselect OK
25
Choose New
If see above window – select Yes
26
Full Screen Image – working in window that says: Mount Directory
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.
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.
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.
30
Java File
• all java program files end in .java• done automatically• compiled files are .class• Need to create an empty java file.
31
Creating Program File• highlight (left click on
mounted directory)• right click to bring up
menu• select
new,classes,empty
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
33
Respond yes
This places the file within the project you created.If select Project tab will see:
34
File Created and Ready to Code
Your code goes here
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
36
Type Code Here
Save, Compile, Execute…
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
38
When You’re Finished
• Unmount file system• exit Forte
39
Live Example
40
Lab Work•Familiarization with Forte for Java•Writing first program•Improving program•Writing second program
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.