04_LectureNotes - (Adapted from Lewis and Loftus...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Slide 4 - 1 COMP 1210 – Fundamentals of Computing I 4. Writing Classes (Adapted from Lewis and Loftus text/slides) We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on: ± class definitions ± instance data ± encapsulation and Java modifiers ± method declaration and parameter passing ± constructors ± graphical objects (skip) ± events and listeners ± buttons and text fields
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 4 - 2 COMP 1210 – Fundamentals of Computing I Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects (skip) Graphical User Interfaces Buttons and Text Fields
Background image of page 2
Slide 4 - 3 COMP 1210 – Fundamentals of Computing I Writing Classes The single class programs we’ve written in previous examples have used classes defined in the Java standard class library Now we will begin to design programs that rely on classes that we write ourselves The class that contains the main method is just the starting point of a program True object-oriented programming is based on defining classes that represent objects with well-defined characteristics and functionality
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 4 - 4 COMP 1210 – Fundamentals of Computing I Classes and Objects Recall from our overview of objects in Chapter 1 that an object has state and behavior Consider a six-sided die (singular of dice) ± It’s state can be defined as which face is showing ± It’s primary behavior is that it can be rolled We can represent a die in software by designing a class called Die that models this state and behavior ± The class serves as the blueprint for a die object We can then instantiate as many die objects as we need for any particular program
Background image of page 4
Slide 4 - 5 COMP 1210 – Fundamentals of Computing I Classes A class Die might contain data declarations and method declarations int MAX_VALUE = 6; int faceValue; Data declarations Method declarations roll(); toString(); . . .
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 4 - 6 COMP 1210 – Fundamentals of Computing I Classes A class Product might contain data declarations and method declarations String desc; int size, weight; char category; Data declarations Method declarations toString(); . . . setSize();
Background image of page 6
Slide 4 - 7 COMP 1210 – Fundamentals of Computing I Classes The values of the data define the state of an object created from the class The functionality of the methods define the behaviors of the object For our Die class, we might declare an integer that represents the current value showing on the face One of the methods would “roll” the die by setting that value to a random number between one and six
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Slide 4 - 8 COMP 1210 – Fundamentals of Computing I Classes We’ll want to design the Die class with other data and methods to make it a versatile and reusable resource Any given program will not necessarily use all aspects of a given class See RollingDice.java (page 163) See Die.java (page 164)
Background image of page 8
Slide 4 - 9 COMP 1210 – Fundamentals of Computing I
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 61

04_LectureNotes - (Adapted from Lewis and Loftus...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online