1 / 23

CHAPTER 2 – Part A

CHAPTER 2 – Part A. AN INTRODUCTION TO OBJECTS AND CLASSES. Where to get the software:. Download Java 2 SKD 1.4.2 (J2SE) [offline installation] at http://java.sun.com/j2se/1.4.2/download.html To download BlueJ version 1.3.5 at http://www.bluej.org. Objects and Classes.

tekla
Télécharger la présentation

CHAPTER 2 – Part A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 – Part A AN INTRODUCTION TO OBJECTS AND CLASSES

  2. Where to get the software: • Download Java 2 SKD 1.4.2 (J2SE) [offline installation] athttp://java.sun.com/j2se/1.4.2/download.html • To download BlueJ version 1.3.5 athttp://www.bluej.org

  3. Objects and Classes • Object: entity that you can manipulate in your programs (by invoking methods) • Each object belongs to a class • Class: Set of objects with the same behavior • Class determines legal methods"Hello".println() // Error"Hello".length() // OK

  4. Rectangle Class • Construct a rectangle:new Rectangle(5, 10, 20, 30)new Rectangle() • Use the constructed objectSystem.out.println(new Rectangle(5, 10, 20, 30));prints java.awt.Rectangle[x=5,y=10,width=20,height=30]

  5. Rectangle Shapes

  6. Syntax 2.1: Object Constructionnew ClassName(parameters) Examples: • new Rectangle(5, 10, 20, 30) Purpose: To construct a new object, initialize it with the construction parameters, and return a reference to the constructed object.

  7. A Rectangle Object

  8. Object Variables • Declare and optionally initialize:Rectangle cerealBox = new Rectangle(5, 10, 20, 30);Rectangle cerealBox; • Apply methods:cerealBox.translate(15, 25); • Share objects:r = cerealBox;

  9. Uninitialized and Initialized Variables Uninitialized Initialized

  10. Two Object Variables Referring to the Same Object

  11. Syntax 2.2: Variable DefinitionTypeName variableName;TypeName variableName = expression; • Examples: Rectangle cerealBox; String name ="Dave"; int n = 5; • Purpose: To define a new variable of a particular type and optionally supply an initial value

  12. Writing a Test Program for Imported Library Classes • Invent a new class, say MoveTest • Supply a main method • Place instructions inside the main method • Import library classes by specifying the package and class name:import java.awt.Rectangle; • You don't need to import classes in the java.lang package such as String and System

  13. Syntax 2.3 : Importing a Class from a PackageimportpackageName.ClassName ; • Example: • import java.awt.Rectangle; • Purpose: • To import a class from a package for use in a program

  14. File MoveRect.java 1 import java.awt.Rectangle; 2 3 public class MoveTest 4 { 5 public static void main(String[] args) 6 { 7 Rectangle cerealBox = new Rectangle(5, 10, 20, 30); 8 // print the original rectangle 9 System.out.println(cerealBox); 8 // move the rectangle 10 cerealBox.translate(15, 25); 11 // print the moved rectangle 12 System.out.println(cerealBox); 13 }

  15. A Simple Class public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } }

  16. Method Definition • access specifier (such as public) • return type (such as String or void) • method name (such as sayHello) • list of parameters (empty for sayHello) • method body in{ }

  17. Method Parameters public class Rectangle{ . . . public void translate(int x, int y) {method body} . . .}

  18. Syntax 2.4: Method Implementation • public class ClassName{ ... accessSpecifier returnType methodName(parameterType parameterName,...) { method body } ... } …Continue

  19. …Continue Example: • public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } } Purpose: • To define the behavior of a method A method definition specifies the method name, parameters, and the statements for carrying out the method's actions

  20. Syntax 2.5: The return Statementreturn expression; OR return; • Example: return message; • Purpose: To specify the value that a method returns, and exit the method immediately. The return value becomes the value of the method call expression.

  21. Testing a Class • Test class: a class with a main method that contains statements to test another class. • Typically carries out the following steps: • Construct one or more objects of the class that is being tested. • Invoke one or more methods. • Print out one or more results

  22. A Test Class for the Greeter Class public class GreeterTest { public static void main(String [] args)) { Greeter worldGreeter = new Greeter(); System.out.println(worldGreeter.sayHello()); } }

  23. Building a Test Program 1. Make a new subfolder for your program. 2. Make two files, one for each class. 3. Compile both files. 4. Run the test program.

More Related