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.

Slides:



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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
©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.
Dialogs. Displaying Text in a Dialog Box Windows and dialog boxes –Up to this our output has been to the screen –Many Java applications use these to display.
Using JOptionPanes for graphical communication with our programs. Pages Horstmann 139.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes Part 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Getting Started with Java
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2: Getting Started with Java *Components.
©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.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
CS 180 Recitation 8 / {30, 31} / Announcements Project 1 is out –Due 10:00pm –Start early! –Use the newsgroup for your questions –LWSN B146.
Intro to GUIs (Graphical User Interfaces) Section 2.5Intro. to GUIs: a GUI Greeter Section 3.7Graphical/Internet Java: Einstein's Equation.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
Object-Oriented Programming (OOP). Implementing an OOD in Java Each class is stored in a separate file. All files must be stored in the same package.
Jaeki Song ISQS6337 JAVA Lecture 03 Introduction to Java -The First Java Application-
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.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
©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.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
SE-1010 Dr. Mark L. Hornick 1 Java Programming Basics.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
 2005 Pearson Education, Inc. All rights reserved. 1 A class A class is the blueprint from which objects are generated. In other words, if we have six.
1 Training Lecture 1. 2 Primitive Data Declaration and Assignments Code State of Memory int firstNumber, secondNumber; firstNumber = 234; secondNumber.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Using classes. One step instantiation, Composition I JFrame myWindow = new JFrame( ); Two step Instantiation, Composition II private JFrame myWindow;
 2005 Pearson Education, Inc. All rights reserved. 1 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.
Strings and I/O. Lotsa String Stuff… There are close to 50 methods defined in the String class. We will introduce several of them here: charAt, substring,
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 7 ( Book Chapter 14) GUI and Event-Driven Programming.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
3 Introduction to Classes and Objects.
Intro to OOP with Java, C. Thomas Wu
Introduction to Classes and Objects
An Introduction to Programming Languages
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
Introduction to Java Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
DON’T PANIC!! Lots of new notions coming in these slides
Getting Started with Java
Presentation transcript:

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 programs Write simple Java programs Describe the difference between object declaration and creation Describe the process of creating and running Java programs Use the Date, SimpleDateFormat, String, and JOptionPane standard classes Develop Java programs, using the incremental development approach

The First Java Program The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. This program displays a window on the screen. The size of the window is set to 300 pixels wide and 200 pixels high. Its title is set to My First Java Program.

Program Ch2Sample1 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); } Declare a name Create an object Use an object

Program Diagram for Ch2Sample1 myWindow : JFrame Ch2Sample1 setSize(300, 200) setTitle(“My First Java Program”) setVisible(true)

Dependency Relationship myWindow : JFrame Ch2Sample1 Instead of drawing all messages, we summarize it by showing only the dependency relationship. The diagram shows that Ch2Sample1 “depends” on the service provided by myWindow.

More Examples Object Declaration JFrame myWindow; Accountcustomer; Studentjan, jim, jon; Vehiclecar1, car2; Object Name One object is declared here. Object Name One object is declared here. Class Name This class must be defined before this declaration can be stated. Class Name This class must be defined before this declaration can be stated.

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. Object Name Name of the object we are creating here. Class Name An instance of this class is created. Class Name An instance of this class is created. Argument No arguments are used here. Argument No arguments are used here.

Declaration vs. Creation 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 2 : Customer customer 1

State-of-Memory vs. Program customer : Customer State-of-Memory Notation customer : Customer Program Diagram Notation

Name vs. Objects Customer customer; customer = new Customer( ); customer : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost.

Sending a Message myWindow. setVisible ( true ) ; More Examples account.deposit( ); student.setName(“john”); car1.startEngine( ); Object Name Name of the object to which we are sending a message. Object Name Name of the object to which we are sending a message. Method Name The name of the message we are sending. Method Name The name of the message we are sending. Argument The argument we are passing with the message. Argument The argument we are passing with the message.

Program Components A Java program is composed of –comments, –import statements, and –class declarations.

/* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.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); } Program Component: Comment Comment

Matching Comment Markers /* This is a comment on one line */ /* Comment number 1 */ /* Comment number 2 */ /* This is a comment */ Error: No matching beginning marker. These are part of the comment.

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

Import Statement /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.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); } Import Statement

Import Statement Syntax and Semantics. ; e.g. dorm. Resident; More Examples import javax.swing.JFrame; import java.util.*; import com.drcaffeine.simplegui.*; Class Name The name of the class we want to import. Use asterisks to import all classes. Class Name The name of the class we want to import. Use asterisks to import all classes. Package Name Name of the package that contains the classes we want to use. Package Name Name of the package that contains the classes we want to use.

Class Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.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); } Class Declaration

Method Declaration /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.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); } Method Declaration

Method Declaration Elements public static void main( String[ ] args ){ JFramemyWindow; myWindow = new JFrame( ); myWindow.setSize(300, 200); myWindow.setTitle(“My First Java Program”); myWindow.setVisible(true); } Method Body Modifier Return Type Method Name Parameter

Template for Simple Java Programs /* Chapter 2 Sample Program: Displaying a Window File: Ch2Sample2.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); } Import Statements Class Name Comment Method Body

Why Use Standard Classes Don’t reinvent the wheel. When there are existing objects that satisfy our needs, use them. Learning how to use standard Java classes is the first step toward mastering OOP. Before we can learn how to define our own classes, we need to learn how to use existing classes We will introduce four standard classes here: –JOptionPane –String –Date –SimpleDateFormat.

JOptionPane Using showMessageDialog of the JOptionPane class is a simple way to display a result of a computation to the user. JOptionPane.showMessageDialog(null, “I Love Java”); This dialog will appear at the center of the screen.

Displaying Multiple Lines of Text We can display multiple lines of text by separating lines with a new line marker \n. JOptionPane.showMessageDialog(null, “one\ntwo\nthree”);

String The textual values passed to the showMessageDialog method are instances of the String class. A sequence of characters separated by double quotes is a String constant. There are close to 50 methods defined in the String class. We will introduce three of them here: substring, length, and indexOf. We will also introduce a string operation called concatenation.

name String is an Object 1. The identifier name is declared and space is allocated in memory. 2. A String object is created and the identifier name is set to refer to it String name; name = new String(“Jon Java”); : String Jon Java name 2

String Indexing The position, or index, of the first character is 0.

JOptionPane for Input Using showInputDialog of the JOptionPane class is a simple way to input a string. String name; name = JOptionPane.showInputDialog (null, “What is your name?”); This dialog will appear at the center of the screen ready to accept an input.