



















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















































2
4
import javax.swing.*;
class Ch2Sample1 {
public static void main(String[ ] args) { JFrame myWindow; myWindow = new JFrame( );
myWindow.setSize(300, 200);
myWindow.setTitle(“My First Java Program”); myWindow.setVisible( true ); } }
Declare a name
Create an object
Use an object
5
myWindow : JFrame
setTitle(“My First Java Program”)
7
(^) myWindow Mywindow … are different
(^) end with ;
(^) “words” (^) import new myFrame } ; { (^) Some words have a special meaning in Java: reserved or keywords. Shown in bold orange in the slides (^) Spaces -- also called white spaces (^) Space, tabs, returns (show up as blanks) (^) Multiple, contiguous whitespaces are ignored
8
JFrame myWindow;
Object Name One object is declared here.
Class Name This class must be defined before this declaration can be stated.
10
myWindow = new JFrame ( ) ;
customer = new Customer( ); jon = new Student(“John Java”); car1 = new Vehicle( );
Object Name Name of the object we are creating here.
Class Name An instance of this class is created.
Argument No arguments are used here.
11
Customer customer;
customer = new Customer( );
Customer customer;
customer = new Customer( );
1
2
1
2 : Customer
13
Customer customer;
customer = new Customer( ); customer = new Customer( );
Customer customer;
customer = new Customer( );
customer = new Customer( );
: Customer : Customer
Created with the first new.
Created with the second new. Reference to the first Customer object is lost.
14
myWindow.setVisible ( true ) ;
account.deposit( 200.0 ); student.setName( “john” ); car1.startEngine( );
Object Name Name of the object to which we are sending a message.
Method Name The name of the message we are sending.
Argument The argument we are passing with the message.
16
17
Chapter 2 Sample Program: Displaying a Window
File: Ch2Sample2.java */
import javax.swing.*;
class Ch2Sample1 { public static void main (String[ ] args) { JFrame myWindow; myWindow = new JFrame( );
myWindow.setSize(300, 200 );
myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } }
Comment
19
/* This is a comment with three lines of text. */
Multiline Comment
Single line Comments
// This is a comment // This is another comment // This is a third comment
/**
javadoc Comments
20
/* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.java */
import javax.swing.*;
class Ch2Sample1 { public static void main (String[ ] args) { JFrame myWindow; myWindow = new JFrame( );
myWindow.setSize(300, 200 );
myWindow.setTitle( “My First Java Program” ); myWindow.setVisible( true ); } }