Docsity

Java Programming: Object Declaration, Creation, and Use - Prof. Sunil K. Prabhakar, Study notes of Computer Science

Partial preview of the text

Download Java Programming: Object Declaration, Creation, and Use - Prof. Sunil K. Prabhakar and more Study notes Computer Science in PDF only on Docsity!

Chapter 2

Getting Started with Java

CS 180

Sunil Prabhakar

Department of Computer Science

Purdue University

2

Objectives

This week we will study

 simple Java programs

 the difference between object declaration and

creation

 some useful classes

 the incremental development approach

4

Program Ch2Sample

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

Program Diagram for Ch2Sample

myWindow : JFrame

Ch2Sample1^ setSize(300, 200)

setTitle(“My First Java Program”)

setVisible(true)

7

Java basics

 Java is Case-Sensitive

 (^) myWindow Mywindow … are different

 A program is made up of statements

 (^) end with ;

 Statements are composed of

 (^) “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

More

Examples

Object Declaration

JFrame myWindow;

Account customer;

Student jan, jim, jon;

Vehicle car1, car2;

Object Name One object is declared here.

Class Name This class must be defined before this declaration can be stated.

10

Object Creation

myWindow = new JFrame ( ) ;

More

Examples

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

Declaration vs. Creation

Customer customer;

customer = new Customer( );

Customer customer;

customer = new Customer( );

  1. The identifier customer is declared and space is allocated in memory.
  2. A Customer object is created and the identifier customer is set to refer to it.

1

2

customer

1

2 : Customer

13

Name vs. Objects

Customer customer;

customer = new Customer( ); customer = new Customer( );

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

Sending a Message

myWindow.setVisible ( true ) ;

More

Examples

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

Program Components

 A Java program is composed of

 comments,

 import statements, and

 class declarations.

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 ); } }

Comments

Comment

19

Three Types of Comments

/* 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

/**

  • This class provides basic clock functions. In addition
  • to reading the current time and today’s date, you can
  • use this class for stopwatch functions. */

javadoc Comments

20

Import Statement

/* 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 ); } }

Import

Statement