04_Writing_Classes - COMP 1210 Fundamentals of Computing I...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 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 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); Its state includes the source for the Scanner object (e.g., System.in); what input/data will be scanned Its 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(); 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) 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 toString...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Page1 / 50

04_Writing_Classes - COMP 1210 Fundamentals of Computing I...

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

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