©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A First Program in Java: Printing.
Introduction to Java.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
The Web Warrior Guide to Web Design Technologies
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Ch2: Getting Started with Java - Objectives After.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Program development.
IT151: Introduction to Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Getting Started with Java
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components.
 2003 Prentice Hall, Inc. All rights reserved. Customized by Sana Odeh for the use of this class. 1 Introduction to Computers and Programming in JAVA.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java.
Chapter Chapter 2 Getting Started with Java.
Chapter 2 - Introduction to Java Applications
Programming with Java standard classes. Java API Application Programming Interface Provides hundreds of standard classes that can be incorporated into.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
The Java Programming Language
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
Java Programming, Second Edition Chapter One Creating Your First Java Program.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Sample Slides - 1 Sample Animated Slides for Wu, Intro to OOP.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
1 CSE1340 Class 4. 2 Objectives Write a simple computer program in Java Use Swing components to build the GUI Use proper naming conventions for classes.
© 2000 McGraw-Hill Modified by C.W.Pang with author's permission Intro to OOP with Java--Wu Chapter Chapter 1 Introduction to Object-oriented Programming.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Getting Started with Java. Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Creating a Java Application and Applet
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
Introducing Java Chapter 3 Review. Why Program in Java? Java, is an object-oriented programming language. OOP languages evolved out of the need to better.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Chapter 6 JavaScript: Introduction to Scripting
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Intro to OOP with Java, C. Thomas Wu Getting Started with Java
2.5 Another Java Application: Adding Integers
Chapter 2 Java Programming Basics
Defining Your Own Classes
Chapter 2 - Introduction to Java Applications
Chapter 3 Classes and Objects
MSIS 655 Advanced Business Applications Programming
Getting Started with Java
Computer Programming-1 CSC 111
Presentation transcript:

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Objectives After you have read and studied this chapter, you should be able to Identify the basic components of Java programs. Write simple Java programs. Describe the difference between object declaration and object creation.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Objectives, cont. After you have read and studied this chapter, you should be able to Describe the process of creating and running Java programs. Use the Date, SimpleDateFormat, String, and JOptionPane classes from the standard Java packages. Develop Java programs using the incremental development approach.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program This first program displays a window on the screen. The size of the window is 300 x 200 pixels, and the default title is My First Java Program. The fundamental OOP concept illustrated by the program: An object-oriented program uses objects.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample1.java */ import javax.swing.*; class Ch2Sample1 { public static void main(String[ ] args){ JFramemyWindow; myWindow = new JFrame(); myWindow.setSize(300, 200); myWindow.setTitle(My First Java Program); myWindow.setVisible(true); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.1 Result of running the Ch2Sample1 program.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.2 The program diagram for the Ch2Sample1 program.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.3 The program diagram for the Ch2Sample1 program that shows the dependency relationship.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program To use an object in a program, first we declare and create and object. Then we send messages to it.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program When we declare an object, we must give it a name. A Java identifier is a sequence of letters, digits, underscores, and dollar signs used to name a class, object, method, etc. The first character in a Java identifier must be a letter.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program Uppercase and lowercase letters are distinguished. myWindow, mywindow, MYWINDOW No spaces are allowed in an identifier. Java naming conventions dictate the use of an uppercase letter for the first letter of class names, and a lowercase letter for the first letter of object names. class Account object account

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program No objects are created by the declaration. The declaration only assigns an identifier to the object. We create an object by invoking the new operation: = new ( ); myWindow = new JFrame ( );

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.4 Distinction between object declaration and object creation.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.5 Relationship between the state-of- memory diagram and the program diagram notation.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.6 The state after two new commands are executed.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.1 The First Java Program The object that receives a message must possess a corresponding method. The expressions sending a message and calling a method are synonymous.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.7 Correspondence between message sending as represented in the program diagram and in the actual Java statement.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.8 How the beginning and ending comment markers are matched.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components Below are the program components for the program Ch2Sample1: Comments Import Statement Class Declaration Method Declaration

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components Programs contain comments in which we state the purpose of the program, explain the meaning of code, and provide other descriptions to help programmers use and understand the code. A comment is any sequence of text that begins with the marker /* and ends with the marker */.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components In Java, classes are grouped into packages, and the Java system comes with numerous packages. To use a class from a package, we refer to the class in the program by using the following syntax:. This notation is called dot notation.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components Using the fully qualified name of a class can be cumbersome. Using the import statement solves this problem. import javax.swing.*;

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components A Java program is composed of one or more classes. The syntax for declaring a class is class { } A class member is either a data value or a method.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components One of the classes in the program must be designated as the main class. If we designate a class as a main class, we must define a method called main, because when a Java program is executed, the main method of a main class is executed first.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components A sample method declaration and its components.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig. 2.9 A program template for simple Java applications.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.2 Program Components /* Chapter 2 Sample Program: Freehand Drawing File: Ch2FunTime.java The program will allow you to draw a picture by dragging a mouse (move the mouse while holding the left mouse button down; hold the button on Mac). To erase the picture and start over, click the right mouse button (command-click on Mac). */ import javabook.*; class Ch2FunTime { public static void main (String[ ] args) { SketchPad doodleBoard; doodleBoard = new SketchPad( ); doodleBoard.setVisible( true ); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig The window that appears on the screen when the program starts running.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig The same window after a picture is drawn.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig The program diagram for the FunTime program.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig A MiniBrowser object displaying a sample web page.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.3 Edit-Compile-Run Cycle Step One: Edit the program. Type in the program, using a text editor, and save the program to a file. Use the name of the main class and the suffix.java for the filename. This file is called a source file.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.3 Edit-Compile-Run Cycle Step 2: Compile the source file. The process of compiling the source file creates the bytecode file. The name of the compiler-generated bytecode file will have the suffix.class while its prefix is the same as the source files.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.3 Edit-Compile-Run Cycle A sample source file and its bytecode file.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.3 Edit-Compile-Run Cycle Step 3: Execute the bytecode file. A java interpreter will go through the bytecode file and execute the instructions in it. If your program is error-free, a window will appear on the screen. If an error occurs while running the program, the interpreter will catch it and stop its execution.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.3 Edit-Compile-Run Cycle The result after the interpreter executes the instructions in the bytecode file.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.4 Sample Java Standard Classes Using existing Java classes is an important step in becoming a successful and proficient programmer. We will introduce four standard classes here: JOptionPane String Date SimpleDateFormat.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.4 Sample Java Standard Classes Using the JOptionPane class is a simple way to display a result of a computation to the user. JOptionPane.showMessageDialog(null, I Love Java);

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig A simple message dialog created by the showMessageDialog method of the JOptionPane class.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig A dialog with multiple lines of text.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.4 Sample Java Standard Classes The textual values passed to the showMessageDialog method are instances of the String class. A sequence of characters separated by double quotes is String constants. String text; text = Espresso; JOptionPane.showMessageDialog(text.substring(2, 7));

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig A dialog showing the substring of espresso from index position 2 to 6.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig Individual characters in a string are numbered from 0.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig The effect of the substring method is shown. The original string remains intact.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.4 Sample Java Standard Classes Date and SimpleDateFormat The Date class is used to represent a time instance to a millisecond. This class is in the java.util package. SimpleDateFormat can be used to provide an alternative format to the Date class. This class is in the java.text package.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig JOptionPane can also be used for input: Below is the result of calling the showInputDialog method of the JOptionPane class with What is your name? as the methods second argument.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.5 Sample Development: Printing the Initials Problem statement: Write an application that asks for the users first, middle, and last names and replies with their initials. Overall plan: Get the users first, middle, and last names. Extract the initials to form the monogram. Output the monogram.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig The program diagram for Ch2Monogram.

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.5 Sample Development: Printing the Initials /* Chapter 2 Sample Program: Displays the Monogram File: Step1/Ch2Monogram.java */ import javax.swing.*; class Ch2Monogram { public static void main (String [ ] args) { String name; name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):"); JOptionPane.showMessageDialog(null, name); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.5 Sample Development: Printing the Initials /* Chapter 2 Sample Program: Displays the Monogram File: Step 2/Ch2MonogramStep2.java */ import javax.swing.*; class Ch2Monogram { public static void main (String [ ] args) { String name, first, middle, last, space, monogram; space = " "; //Input the full name name = JOptionPane.showInputDialog(null, "Enter your full name (first, middle, last):");

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. 2.5 Sample Development: Printing the Initials //Extract first, middle, and last names first = name.substring(0, name.indexOf(space)); name = name.substring(name.indexOf(space)+1, name.length()); middle = name.substring(0, name.indexOf(space)); last = name.substring(name.indexOf(space)+1, name.length()); //Compute the monogram monogram = first.substring(0, 1) + middle.substring(0, 1) + last.substring(0,1); //Output the result JOptionPane.showMessageDialog(null, "Your monogram is " + monogram); }

©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Fig Apply the two sequences of indexOf and substring methods to extract three substrings from a given string.