230 likes | 341 Views
Learn how to work with objects and classes in Java programming. Understand the concepts of object construction, variables, methods, and testing. Practice importing classes and writing test programs to enhance your skills.
E N D
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] athttp://java.sun.com/j2se/1.4.2/download.html • To download BlueJ version 1.3.5 athttp://www.bluej.org
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
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]
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.
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;
Uninitialized and Initialized Variables Uninitialized Initialized
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
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
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
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 }
A Simple Class public class Greeter { public String sayHello() { String message ="Hello,World!"; return message; } }
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{ }
Method Parameters public class Rectangle{ . . . public void translate(int x, int y) {method body} . . .}
Syntax 2.4: Method Implementation • public class ClassName{ ... accessSpecifier returnType methodName(parameterType parameterName,...) { method body } ... } …Continue
…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
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.
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
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()); } }
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.