Download class1-Lecture

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
Class 1
Lecture Topic
Concepts, Definitions and
Examples
1
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.
2
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.
3
What are these is-a relationships?
4
Usefulness of Classes
• Reusable
• Objects inherit attributes and methods from
classes
5
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?
6
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?
7
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.
8
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.
9
Instance Variable
Course
Creating a Class
courseNo:Integer
setCourseNo(Integer) : void
getCourseNo(): Integer
• 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.
10
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.
11
Course
courseNo:Integer
A Java class Course
class Course
setCourseNo(Integer) : void
getCourseNo(): Integer
{
private int courseNo;
Instance
Variable
public void setCourseNo(int cNo)
{
courseNo = cNo;
}
public int getCourseNo()
{
return courseNo;
}
}
Methods
12
Using Classes
• Declaring a class does not create actual
object
• Class is an abstract description
Notify the compiler
• You might define an integer as
that an integer
int someValue;
• Define an object of Course as
Course someCourse;
someValue exists and
reserve computer
memory
When declaring
someCourse not
setting aside memory
13
yet!
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.
14
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
15
The class CourseApplicationAWFirst 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)
16
The class Course
class Course {
private int courseNo;
public void setCourseNo(int cNo)
{
courseNo = cNo;
}
}
The client class
does not call
these methods
yet. Next Slide!
public boolean isFourThousandLevel()
{
if ((courseNo >=4000)&&(courseNo<5000))
Course
return true;
courseNo:Integer
else return false;
setCourseNo(Integer) : void
isFourThousandLevel(): Boolean
}
17
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 operator
CourseApplicationAW
main(String) : void
18
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…
19
Improving the class Course
• What attributes can you add to the class
Course?
• What methods can you add?
Course
courseNo:Integer
setCourseNo(Integer) : void
isFourThousandLevel(): Boolean
20
Moving On - Using Forte
1. Pictorially
2. Demonstrate Forte
3. You try it…
21
Starting
• Select/Open Forte for Java CE
• (slow)
• check the version number for Java
22
Forte for Java
tabs –
select
Editing
tab
indicates
if a
program
is running
helpful
hints
window –
close if
appears
23
New Project
Select Project/New Project
Create New Project Window, name project – YourInitialsFirstProj
select OK
24
Choose New
If see above window – select Yes
25
Full Screen Image – working in window that says: Mount Directory
26
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.
27
Mount Directory
make directory – if needed
Mount – mounts working
directory
File Name refers to directory NOT a file.
Created New Folder
(you can name it), click on
New Folder ONCE,
Select Mount.
New Folder appears as File
Name.
28
Explorer Window
Shows mounted directory, you can
mount any number of directories this
way.
This Tab shows the project you
created, any files you create go into
29
project.
Java File
•
•
•
•
all java program files end in .java
done automatically
compiled files are .class
Need to create an empty java file.
30
Creating Program File
• highlight (left click on
mounted directory)
• right click to bring up
menu
• select
new,classes,empty
31
Selecting Name – Must be
IDENTICAL to public class
Type Name
Select Finish
VERY IMPORTANT – MUST MATCH CASE –
Programming Convention – all Class Names – Capitalize each word
32
Respond yes
This places the file within the
project you created.
If select Project tab will see:
33
File Created and Ready to Code
Your code goes here
34
Right Click in Source Editor will
bring up menu to Save, Compile,
Execute, etc.
Compile –
checks for
compilation
error
Reformat code – indents
code for you – nice!
Save Often – only lets you save
if you’ve made a change to code
Execute – runs
program
35
Type Code Here
Save, Compile, Execute…
36
System.out.println – prints output
to this window (usually at bottom
of screen).
true is the boolean value of the response to
whether the courseNo is >=4000 and less than 5000
37
When You’re Finished
• Unmount file system
• exit Forte
38
Live Example
39
Lab Work
•Familiarization with Forte for Java
•Writing first program
•Improving program
•Writing second program
40
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.
41