Lecture14 - Classes, Encapsulation, Methods and...

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

View Full Document Right Arrow Icon
1 Classes, Encapsulation, Methods and Constructors Class definitions Instance data Encapsulation and Java modifiers Method declaration and parameter passing Constructors Reading for this Lecture: L&L, 4.1-4.4
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Writing Classes True object-oriented programming is based on classes that represent objects with well- defined characteristics and functionality The 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
Background image of page 2
3 Classes and Objects An object has state and behavior Consider a six-sided die (singular of dice) It’s state can be defined as the face 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 3

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

View Full DocumentRight Arrow Icon
4 Classes A class has a name and can contain data declarations and/or method declarations Data declarations Method declarations Class Name Die - faceValue: integer + roll() : integer
Background image of page 4
5 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 allow us to “roll” the die by setting its face value to a random number between one and six
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 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 157) See Die.java (page 158)
Background image of page 6
7 The Die Class
Background image of page 7

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

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

Page1 / 22

Lecture14 - Classes, Encapsulation, Methods and...

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

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