Chapter 2 Java Programming Basics 1262022 Introduction to

Chapter 2 Java Programming Basics 1/26/2022 Introduction to Object-Oriented Programming with Java-Wu Chapter 2 - 1

Chapter 2 Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java programs. Distinguish two types of Java programs-applications and applets. Write simple Java applications and applets. Describe the difference between object declaration and object creation. Describe the process of creating and running Java programs. Use Main. Window and Message. Box classes from the javabook package to write Java applications. Use the Graphics class from the standard Java package. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 2

The First Java Application A program to display a window on the screen. The size of the window is slightly smaller than the screen, and the window is positioned at the center of the screen with a default title Sample Java Application. The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 3

Program My. First. Application /* Program My. First. Application This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook. *; class My. First. Application { public static void main(String[ ] args) { Main. Window main. Window; Declare a name main. Window = new Main. Window(); Create an object main. Window. set. Visible( true ); Make it visible } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 4

Points to Remember Main. Window main. Window; class object (declare) main. Window = new Main. Window(); new object from Main. Window class main. Window. set. Visible( true ); making it visible Main. Window – capitalization of class main. Window – object No spaces Ca. Se Se. Nsi. Ti. Ve 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 5

Object Diagram for My. First. Application main. Window My. First. Application Main. Window main 1/26/2022 true set. Visible Introduction to Object-Oriented Programming with Java--Wu 6

Flow of the My. First. Application Program Main. Window main. Window; main. Window = new Main. Window(); main. Window. set. Visible( true ); main. Window Main. Window See page 44 -45 in text State-of-Memory Diagram 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 7

Object Declaration Class Name This class must be defined before this declaration can be stated. Main. Window More Examples 1/26/2022 Account Student Vehicle Object Name One object is declared here. main. Window; customer; jan, jim, jon; car 1, car 2; Introduction to Object-Oriented Programming with Java--Wu 8

Object Creation Class Name An instance of this class is created. Object Name of the object we are creating here. main. Window More Examples 1/26/2022 = new Main. Window ( Argument No arguments are used here. ); customer = new Customer( ); jon = new Student(“John Java” ); car 1 = new Vehicle( ); Introduction to Object-Oriented Programming with Java--Wu 9

Distinction Between Declaration and Creation Customer customer; customer = new Customer( ); customer Created with the first new. 1/26/2022 Customer Introduction to Object-Oriented Programming with Java--Wu Created with the second new. Reference to the first Customer object is lost. 10

Sending a Message Object Name of the object to which we are sending a message. Method Name The name of the message we are sending. main. Window More Examples 1/26/2022 . set. Visible ( Argument The argument we are passing with the message. true ) ; account. deposit( 200. 0 ); student. set. Name(“john”); car 1. start. Engine( ); Introduction to Object-Oriented Programming with Java--Wu 11

Program Components A Java program is composed of comments, import statements, and class declarations. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 12

Program Component: Comment /* Program My. First. Application This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ import javabook. *; class My. First. Application { public static void main(String[ ] args) { Main. Window Comment main. Window; main. Window = new Main. Window(); main. Window. set. Visible( true ); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 13

Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */ These are part of the comment. /* /* /* This is a comment */ Error: No matching beginning marker. */ 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 14

Three Types of Comments /* This is a comment with three lines of Multiline Comment text. */ // This is a comment // This is another comment Single line Comments // This is a third comment /** * This class provides basic clock functions. In addition * to reading the current time and today’s date, you can javadoc Comments * use this class for stopwatch functions. */ 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 15

Program Component: Import Statement /* Program My. First. Application This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ Import Statement import javabook. *; class My. First. Application { public static void main(String[ ] args) { Main. Window main. Window; main. Window = new Main. Window(); main. Window. set. Visible( true ); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 16

Use Comments to… Create a program header Explain blocks of code Comment on specific lines Help with debugging 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 17

Import Statement Syntax and Semantics Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name of the package that contains the classes we want to use. More Examples 1/26/2022 <package name> . e. g. . javabook import <class name> ; Input. Box; javabook. *; java. awt. image. Color. Model; com. drcaffeine. galapagos. *; Introduction to Object-Oriented Programming with Java--Wu 18

Program Component: Class Declaration /* Program My. First. Application This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. Class Declaration */ import javabook. *; class My. First. Application { public static void main(String[ ] args) { Main. Window main. Window; main. Window = new Main. Window(); main. Window. set. Visible( true ); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 19

Program Component: Method Declaration /* Program My. First. Application This program displays a window on the screen. The window is positioned at the center of the screen, and the size of the window is almost as big as the screen. */ Method Declaration import javabook. *; class My. First. Application { public static void main(String[ ] args) { Main. Window main. Window; main. Window = new Main. Window(); main. Window. set. Visible( true ); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 20

Method Declaration Elements Modifier public ) Modifier static Return Type void Method Name main ( Parameter String[ ] args { Main. Window main. Window; Method Body main. Window = new Main. Window(); main. Window. set. Visible( true ); } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 21

Template for Simple Java Applications center of the screen, and the size of the window is almost as big as the screen. */ import javabook. *; class My. First. Application { Comment Import Statements Class Name public static void main(String[ ] args) { Main. Window main. Window; Method Body main. Window = new Main. Window(); main. Window. set. Visible( true ); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 22

Steps in Executing Java Applications Step 1 Edit Type in the program using an editor and save the program to a file. Step 2 Compile the source file. Step 3 Run Execute the compiled source file called bytecode file. Click this image to read step-by-step instructions on how to edit, compile, and run Java programs. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 23

The javabook Package To become a good object-oriented programmer, one must first learn how to use predefined classes. We used predefined classes from the javabook package. To download the package or get its detailed documentation, please visit Dr. Caffeine's web site. Advantages of using javabook: Gives you a taste of how real-world programs are developed. Minimizes the impact of programming language syntax and semantics. Allows you to write practical programs without learning too many details. Serves as good example of how to design classes. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 24

Sample Program: Displaying Messages Problem Statement Write an application that displays the message Love Java. I Design Alternative 1: Set the title of the Main. Window to the designated message. Alternative 2: Use a Message. Box object. This object is intended for displaying a single line of short text to grab the enduser’s attention. The Message. Box class is available from the javabook package. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 25

Sample Program: Design Document: Class Display. Message 1/26/2022 Display. Message Purpose The main class of theprogram. Main. Window The main frame window of the program. The title is set to Display Message. This class is from javabook. Message. Box The dialog for displaying the required message. This class is from javabook. Introduction to Object-Oriented Programming with Java--Wu 26

Sample Program: Object Diagram main. Window Main. Window Display. Message true set. Visible main “I L ov e. J message. Box av a” Message. Box show 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 27

Sample Program: Source Code /* Program Display. Message The program displays the text "I Love Java". The program uses a Message. Box object from the javabook package to display the text. */ import javabook. *; class Display. Message { public static void main(String[] args) { Main. Window main. Window; //declare two objects Message. Box message. Box; //create two objects main. Window = new Main. Window("Display Message"); message. Box = new Message. Box(main. Window); main. Window. set. Visible( true ); message. Box. show("I Love Java"); //display two objects: first the frame //and then the dialog } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 28

Sample Program: Testing Run the program, and you will see… 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 29

Program My. First. Applet /* Program My. First. Applet An applet that displays the text "I Love Java" and a rectangle around the text. */ import java. applet. *; import java. awt. *; public class My. First. Applet extends Applet { public void paint( Graphics graphic) { graphic. draw. String("I Love Java", 70); graphic. draw. Rect(50, 100, 30); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 30

Three Components of Program My. First. Applet /* Program My. First. Applet Header Comment An applet that displays the text "I Love Java" and a rectangle around the text. */ Import Statements Class Declaration import java. applet. *; import java. awt. *; public class My. First. Applet extends Applet { public void paint( Graphics graphic) { graphic. draw. String("I Love Java", 70); graphic. draw. Rect(50, 100, 30); } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 31

Object Diagram for My. First. Applet. Viewer My. First. Applet main 1/26/2022 graphic paint Introduction to Object-Oriented Programming with Java--Wu 32

Drawing Graphics inside the paint Method public void paint( Graphics graphic) { graphic. draw. String("I Love Java", 70); graphic. draw. Rect(50, 100, 30); } Drawing This is where we draw on an applet window by using the Graphics methods. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 33

Drawing Methods draw. Line( x 1, y 1, x 2, y 2) draws a line from (x 1, y 1) to (x 2, y 2) draw. Rect(x, y, w, h) draws a rectangle w pixels wide and h pixels high at (x, y). draw. Oval( x, y, w, h) draws an oval w pixels wide and h pixels high at (x, y). See java. awt. Graphics for information on these and other drawing methods. 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 34

Template for Simple Java Applets /* Program My. First. Applet Comment An applet that displays the text "I Love Java" and a rectangle around the text. */ import java. applet. *; import java. awt. *; public class My. First. Applet extends Applet { public void paint( Graphics graphic) { graphic. draw. String("I Love Java", 70); graphic. draw. Rect(50, 100, 30); Import Statements Class Name Method Body } } 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 35

Executing Java Applets Basic steps for Java applications apply for applets as well. The main difference is that you need to define an HTML file. A Web browser or the Applet. Viewer needs this HTML file to execute an applet. An HTML file for the sample applet looks like this: <HTML> <BODY> <APPLET CODE="My. First. Applet. class" WIDTH=300 HEIGHT=190> </APPLET> </BODY> </HTML> 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 36

Edit-Compile-Run Cycle for Applets My. First. Applet. java My. First. Applet. class My. First. Applet. html (bytecode file) (HTML file) Compiler Editor (source file) Applet. Viewer 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 37

Let’s Try One… Page 62 -66 Place name, Unified ID, Exercise 1, and description in header Display your name and Unified ID instead of “I Love Java” When done, print the code, sign it, and turn it in 1/26/2022 Introduction to Object-Oriented Programming with Java--Wu 38
- Slides: 38