Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class 1 Lecture Topic Concepts, Definitions and Examples.

Similar presentations


Presentation on theme: "1 Class 1 Lecture Topic Concepts, Definitions and Examples."— Presentation transcript:

1 1 Class 1 Lecture Topic Concepts, Definitions and Examples

2 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 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 4 What are these is-a relationships?

5 5 Usefulness of Classes Reusable Objects inherit attributes and methods from classes

6 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 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 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 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 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. Instance Variable

11 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 12 A Java class Course class Course { private int courseNo; public void setCourseNo(int cNo) { courseNo = cNo; } public int getCourseNo() { return courseNo; } Instance Variable Methods

13 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 as Course someCourse; Notify the compiler that an integer someValue exists and reserve computer memory When declaring someCourse not setting aside memory yet!

14 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 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 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 17 The class Course class 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!

18 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 operator

19 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 20 Improving the class Course What attributes can you add to the class Course? What methods can you add?

21 21 Moving On - Using Forte 1.Pictorially 2.Demonstrate Forte 3.You try it…

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

23 23 Forte for Java helpful hints window – close if appears tabs – select Editing tab indicates if a program is running

24 24 New Project Select Project/New Project Create New Project Window, name project – YourInitialsFirstProj select OK

25 25 Choose New If see above window – select Yes

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

27 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 28 Mount Directory make directory – if needed File Name refers to directory NOT a file. Mount – mounts working directory Created New Folder (you can name it), click on New Folder ONCE, Select Mount. New Folder appears as File Name.

29 29 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 project.

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

31 31 Creating Program File highlight (left click on mounted directory) right click to bring up menu select new,classes,empty

32 32 Selecting Name – Must be IDENTICAL to public class VERY IMPORTANT – MUST MATCH CASE – Programming Convention – all Class Names – Capitalize each word Type Name Select Finish

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

34 34 File Created and Ready to Code Your code goes here

35 35 Right Click in Source Editor will bring up menu to Save, Compile, Execute, etc. Reformat code – indents code for you – nice! Save Often – only lets you save if you’ve made a change to code Compile – checks for compilation error Execute – runs program

36 36 Type Code Here Save, Compile, Execute…

37 37 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

38 38 When You’re Finished Unmount file system exit Forte

39 39 Live Example

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

41 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.


Download ppt "1 Class 1 Lecture Topic Concepts, Definitions and Examples."

Similar presentations


Ads by Google