CH04 - Chapter 4 Java: an Introduction to Computer Science...

Info iconThis preview shows pages 1–8. 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: Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Chapter 4 ● Class and Method Definitions ● Information Hiding and Encapsulation ● Objects and Reference ● Parameter passing Classes, Objects, and Methods Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Learn by Doing ● All programs in book are available on the CD ● Run the programs as you read about them ● You need SavitchIn.java file for keyboard input Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Classes ● Class — definition of a kind of object ● Example: an Automobile class » Object that satisfies the Automobile definition instantiates the Automobile class ● Class specifies what kind of data objects of that class have » Each object has the same data items but can have different values ● Class specifies what methods each object will have » All objects of the same class have the exact same methods Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Class as an Outline Class Name: Automobile Data: amount of fuel ________ speed ________ license plate ________ Methods (actions): increaseSpeed: How: Press on gas pedal. stop: How: Press on brake pedal. First Instantiation: Object name: patsCar amount of fuel: 10 gallons speed: 55 miles per hour license plate: “135 XJK” Second Instantiation: Object name: suesCar amount of fuel: 14 gallons speed: 0 miles per hour license plate: “SUES CAR” Third Instantiation: Object name: ronsCar amount of fuel: 2 gallons speed: 75 miles per hour license plate: “351 WLF” Class Definition Objects that are instantiations of the class Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Objects ● Objects » variables that are named instances of a class » class is their type ● Objects have member: » data (or fields or instance variables) » methods – Invoking a method means to call ( i.e. execute) method, – Syntax for invoking an object's method: object_Variable_Name . method() Calling object Dot Operator Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Example: String Class ● String is a class » stores a sequence of characters » length method returns the number of characters ● Example: // read characters typed in by user from keyboard // and output the number of characters entered String userInput; userInput = SavitchIn.readLine(); System.out.println(userInput.length()); Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Class Files ● Each Java class definition » in separate file ● Use the same name for the class and the file, » add ".java" to the file name ● Good programming practice: » Start the class (and file) name a capital letter » capitalize inner words upper case » e.g. MyClass.java for the class MyClass ● For now » put all the classes you need to run a program in the same directory Chapter 4...
View Full Document

This note was uploaded on 04/17/2008 for the course COMP SCI 201 taught by Professor Durga during the Spring '08 term at Wentworth Institute of Technology.

Page1 / 45

CH04 - Chapter 4 Java: an Introduction to Computer Science...

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