04_Writing_Classes_2_per_page

04_Writing_Classes_2_per_page - 4. Writing Classes...

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

View Full Document Right Arrow Icon
1 COMP 1210 – Fundamentals of Computing I Slide 4 - 1 4. Writing Classes • Objectives - when we have completed this set of notes, you should be familiar with: Anatomy of a class: state and behaviors Constructors UML class diagrams Encapsulation Anatomy of a method: Parameters, Local data Constant fields (public and private) Invoking methods in the same class Building a class incrementally Testing a class Writing a driver program COMP 1210 – Fundamentals of Computing I Slide 4 - 2 Writing Classes • Thus far you have written programs that use classes defined in the Java standard class library • The driver program (programs with a main method) should not contain all of your code • Object-oriented programming: Classes define sets of objects that will hold data and have specified behavior Each class should be contained a separate file Separate files facilitate testing
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 COMP 1210 – Fundamentals of Computing I Slide 4 - 3 Classes and Objects • An object has a state and a behaviors You have used the Scanner class, which was written for the Java API Scanner input = new Scanner(System.in); It’s state includes the “source” for the Scanner object (e.g., System.in); what input/data will be “scanned” It’s behaviors include reading the next line, reading the next integer, etc. input.nextLine(); COMP 1210 – Fundamentals of Computing I Slide 4 - 4 Classes and Objects • Consider a six-sided die (singular of dice) • Its state might include a face value (the value 1-6 that is currently showing) • Its behaviors might include… roll (roll the die to a random value 1-6) setFaceValue (set the die to a specified value 1-6) getFaceValue (get the face value) • Example of how the Die class could be used: Die dieObj = new Die(); dieObj.roll(); int rollResult = dieObj.getFaceValue();
Background image of page 2
3 COMP 1210 – Fundamentals of Computing I Slide 4 - 5 Classes • You would have to create a Die class with instance data for the state and methods for the behaviors int MAX_VALUE = 6; int faceValue; Instance data (state) Method declarations (behaviors) roll(); toString(); . . . COMP 1210 – Fundamentals of Computing I Slide 4 - 6 Classes • You can now create multiple dice in one program • A program will not necessarily use all aspects of a given class • See RollingDice.java (page 162) • See Die.java (page 165)
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 COMP 1210 – Fundamentals of Computing I Slide 4 - 7 The Die Class •The Die class contains two data values a constant MAX that represents the maximum face value an integer faceValue that represents the current face value •The roll method uses the random method of the Math class to determine a new face value • There are also methods to explicitly set and retrieve the current face value at any time COMP 1210 – Fundamentals of Computing I Slide 4 - 8 The toString Method • All classes that represent objects should define a toString method •The
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

04_Writing_Classes_2_per_page - 4. Writing Classes...

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

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