CH04 - Chapter 4 Classes Objects and Methods Class and...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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());
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
Chapter 4 Java: an Introduction to Computer Science & Programming - Walter Savitch Instance Variables
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern