Java Programming with BlueJ (Gift from Krysten Hall) Editted by Mr. S. Lupoli
Java Programming with BlueJ Objectives By the end of this presentation you should be able to: Open a BlueJ project Create an instance of a class Invoke methods of a class Toggle between the project window and the output window Identify parameters to a method
Java Programming with BlueJ Objectives View the source code Give examples of different data types Identify the 4 parts of a method signature Identify comments Identify accessor and mutator methods Use the printing methods Use selection statements Identify local variables
Opening a preexisting BlueJ Project Will need to do this: For THIS presentation And whenever you SAVE and return to a project Directions Open BlueJ Project -> Open Project -> C:\BlueJ\examples\shapes
Java Programming with BlueJ We will examine the features of BlueJ by working with the shapes project. The shapes project initially looks like this: The shapes project
Opening setup You may need to COMPILE the given files If each class has /’s under them RIGHT click on each class Select Compile
Java Programming with BlueJ Creating Instances of Classes To create an instance of the Circle class in the shapes project Right click the Circle class Click new Circle( ) Name the instance and click OK Note the naming conventions of lowercase for objects and uppercase for classes Class Object on the Object Bench
Java Programming with BlueJ Invoking Methods To invoke the methods of each class Right-click the object on the object bench Click the method to invoke from the menu, try makeVisible( )
Java Programming with BlueJ You should see a blue ball appear in a separate window. makeVisible( ) from the menu creates the blue ball in another window
Java Programming with BlueJ Parameters Some methods require a parameter be passed to it. For example the moveVertical method requires a distance and changeColor requires a color Parameters to methods
Java Programming with BlueJ Method Signatures When a method requires a parameter you will get a second window to enter that parameter. Note that the method signature indicates the type of data to be entered. Method signature
Java Programming with BlueJ Viewing the Source Code The source code for a class can be viewed by opening the editor. Right-click the class, click Open Editor.
Java Programming with BlueJ Viewing the Source Code The source code for the methods is shown here in the class definition. All methods included in the class are here.
Java Programming with BlueJ Data Types Some methods require parameters, these parameters have a type int – numeric values without decimals -5, 100, 303 -45 float – numeric values with decimals -0.456, 9.65, 100.1 String – a combination of alphanumeric symbols grouped with quotation marks “ “ “red”, “101 College Parkway”, “123”
Java Programming with BlueJ Method Signatures The method signature has 4 parts. public void moveVertical(int distance) 1) Access modifier 2) Return type 3) Name 4) Parameter list NOTE: If the parameter list is empty the parentheses MUST remain. All other parts are required.
Java Programming with BlueJ Comments Comments are included in a program to provide information to the reader about how the source code performs or was created. A single line comment begins with // (no spaces) Multi-line comments for more detailed explanations start with /* and end with */ A multi-line comment
Programming in Java with BlueJ Class definition A class definition contains: Instance variables – the data items for the class. The computer instructions in the methods act on these data items. Also called fields. Methods – the actions taken on the data items. Methods are just sequences of computer instructions. Instance variables Method acting on the instance variable
Java Programming with BlueJ Constructors Note class name is the same as the constructor A constructor is a method that has the same name as the class and provides the initial values for an object. A constructor is executed EACH time a class is instantiated.
Java Programming with BlueJ Assignment Statements The general format of an assignment statement is: variable = expression The expression is evaluated first and the value of the expression is stored in the variable. An assignment statement always terminates with a semi-colon (;) in Java. An assignment statement
Java Programming with BlueJ Accessor methods An accessor method is one that returns information to the caller about the state of an object. Remember that the state of an object is defined by the instance variables. Often an accessor method will contain a return statement in order to pass data back to the caller. Some display functions are considered accessor methods, too. An accessor method
Java Programming with BlueJ Mutator Methods (Mutators) A mutator method is one that changes the state of an object. Mutator methods
Java Programming with BlueJ Printing To display data and text to the screen the println method is used from the System.out object. Various forms of the print statement: System.out.println( ); Sends a linefeed to the output window. System.out.println(“text here”); Displays the text within the “ “ to the output window. System.out.println(“text “ + variable); Creates one string parameter consisting of the text in “ “ and the value of the variable.
Java Programming with BlueJ Printing Display statements
Java Programming with BlueJ Making Choices – Selection Statements The general form of a selection statement: if (condition that evaluates to true or false) { perform these steps if the condition evaluates to true } else{ perform these steps if the condition evaluates to false Some examples: if (length < 0){ System.out.println(“length can’t be negative”); } if (width < 0){ System.out.println(“width can’t be negative”); else { System.out.println(“width accepted”);
Java Programming with BlueJ Making decisions Selection statement
Java Programming with BlueJ Local variables A local variable A local variable is declared inside of a method or constructor and is only used during the execution of the method or constructor. Local variables are temporary. Local variables must be initialized before they are used. A local variable never has private or public associated with it.
Java Programming with BlueJ Conclusion and Assignments Work on assignments given by your instructor.