1 / 27

Java Programming with BlueJ

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

vcummings
Download Presentation

Java Programming with BlueJ

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. Java Programming with BlueJ (Gift from Krysten Hall) Editted by Mr. S. Lupoli

  2. Java Programming with BlueJObjectives 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

  3. Java Programming with BlueJObjectives 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

  4. 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

  5. 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

  6. Opening setup You may need to COMPILE the given files If each class has /’s under them RIGHT click on each class Select Compile

  7. Java Programming with BlueJCreating 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

  8. Java Programming with BlueJInvoking 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( )

  9. 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

  10. Java Programming with BlueJParameters 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

  11. Java Programming with BlueJMethod 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

  12. Java Programming with BlueJViewing the Source Code • The source code for a class can be viewed by opening the editor. Right-click the class, click Open Editor.

  13. Java Programming with BlueJViewing the Source Code The source code for the methods is shown here in the class definition. All methods included in the class are here.

  14. Java Programming with BlueJData Types 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”

  15. Java Programming with BlueJMethod 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.

  16. 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

  17. Programming in Java with BlueJClass 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

  18. Java Programming with BlueJConstructors 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.

  19. Java Programming with BlueJAssignment 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

  20. Java Programming with BlueJAccessor 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

  21. Java Programming with BlueJMutator Methods (Mutators) A mutator method is one that changes the state of an object. Mutator methods

  22. Java Programming with BlueJPrinting 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.

  23. Java Programming with BlueJPrinting Display statements

  24. Java Programming with BlueJMaking 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:

  25. Java Programming with BlueJMaking decisions Selection statement

  26. Java Programming with BlueJLocal 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.

  27. Java Programming with BlueJConclusion and Assignments Work on assignments given by your instructor.

More Related