©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 2 - 1 Chapter 2 Getting Started with Java Structure of.

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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Introduction to Java.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
©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.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
©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
©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.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: IO *Standard Output *Formatting Decimal.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
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
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 3: Numerical Data Manipulating Numbers Variables.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
FIRST JAVA PROGRAM. JAVA PROGRAMS Every program may consist of 1 or more classes. Syntax of a class: Each class can contain 1 or more methods. public.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 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.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
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.
 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.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Anatomy of a Java Program. AnotherQuote.java 1 /** A basic java program 2 * 3 Nancy Harris, James Madison University 4 V1 6/2010.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Getting Started with Java: Object declaration and creation Primitive.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Chapter 3 Numerical Data. Objectives After you have read and studied this chapter, you should be able to Select proper types for numerical data. Write.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 2 Getting Started with Java Animated Version.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
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.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
Intro to OOP with Java, C. Thomas Wu
Intro to OOP with Java, C. Thomas Wu Getting Started with Java
Chapter 4 Intermediate (part 1)
Chapter 3 Introduction to Classes, Objects Methods and Strings
Defining Your Own Classes
MSIS 655 Advanced Business Applications Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Getting Started with Java
Computer Programming-1 CSC 111
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of a Java program Declaring and creating objects Program development process Standard Java classes

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Development Process Design the program Use a text editor (vim) to create files containing Java code that define classes vim MyClass.java Compile the java code to create a class file javac MyClass.java Run the program java MyClass Create the documentation javadoc MyClass.java

Structure of a Java Program A Java program (application) consists of one or more classes –Each class is defined in a separate file –One class is the main class A class consists of –Data –Methods The main class has a method named main –Other classes (sometimes)‏

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Simplest Program /** The simplest class */ public class Hello { public static void main(String[ ] args) { System.out.println( "Hello, World"); } The class has a single method, the main method The main method has a single statement

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter An object-oriented program uses objects. The Hello class uses two kinds of objects –System.out System is a class that provides access to standard input and output facilities out is an object in the System class (class data). It belongs to the PrintStream class –"Hello, World!" Double quotes are used to automatically create a String object.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Template for Simple Java Programs import java.package.ClassName; /** Describe the class here */ public class MyClass { // Declare attributes here /** Describe the method here */ public static void main(String[ ] args) { /* main method is used to start the program declare and create objects then call the methods of those objects */ } Import Statements Class Name javadoc Comment javadoc Comment main Method

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Components A Java program is composed of one or more classes –Each class corresponds to a file Each class contains –import statements –comments - ignored by the compiler –class declaration

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 comme nt /** * 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Import Statement Syntax and Semantics We often use classes that already exist in our programs We use an import statement to tell the compiler how to find a class we want to use in our program. ; e.g. dorm. Resident; 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program that creates and uses an object import javax.swing.JFrame; /** Chapter 2 Sample Program: Displaying a Window */ public 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); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Objects Used The fundamental OOP concept illustrated by the program: An object-oriented program uses objects. This program displays a window on the screen. –the window is an object The size of the window is set to 300 pixels wide and 200 pixels high. Its title is set to My First Java Program. –the title is a String, another object

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Program Diagram for Ch2Sample1 myWindow : JFrame Ch2Sample1 setSize(300, 200)‏ setTitle(“My First Java Program”)‏ setVisible(true)‏ Ch2Sample1 is not an object In the main method –declare and create object myWindow –send messages to myWindow

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Declaration Every object we use in a program needs a name Use a declaration statement to tell the compiler –what names we are going to use for the object –what type of object the name will refer to JFrame myWindow; 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Names in Java We need names for classes, objects and methods in a Java program Rules for names –Start with a letter or underscore (_)‏ –remaining characters can be letters, digits, underscore –case sensitive Naming conventions –class names start with a capital letter –method and object names start with a lower case letter –constants are all capitals

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Object Creation Instantiation is the process of creating an object –use the new operator in front of a constructor to create an object If we want to use the object more than once, we have to give it a name –use the assignment operator (=) to assign a name myWindow = new JFrame ( ) ; 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.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Declaration and 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Name vs. Objects Customer customer; customer = new Customer( ); customer2 = customer; customer : Customer Created with the first new. Created with the second new. Reference to the first Customer object is lost. customer2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Sending a Message myWindow. setVisible ( true ) ; 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. We send a message to an object by calling one of its methods When we send a message, we need to specify the object it should be sent to

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Method Definition 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

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Standard Classes Java comes with a large collection of existing classes –Don’t reinvent the wheel. When there are existing classes that satisfy our needs, use them. –Before we can learn how to define our own classes, we need to learn how to use existing classes We will introduce three standard classes here: –String –Date –SimpleDateFormat.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String The String class is used to represent text There are close to 50 methods defined in the String class. For example. –length - tells how many characters a String has –indexOf - gives the position of a particular sequence of characters in a String –substring - get another String containing some part of the original string Java provides a special operation called concatenation to combine Strings –We can concatenate two strings using +

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Literal Strings A sequence of characters separated by double quotes is a String constant, also called a String literal. Putting double-quoted text into your program tells the compiler to create a String object for you. –The literal string does not have a name unless you give it one. String myString = "this is my string"; –If you want to use the same constant string in several places in your program, you can either give it a name or just use the double- quoted form wherever you need it.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter name Naming a Literal String 1. The identifier name is declared and space is allocated in memory. 2. The identifier name is set to refer to the literal String String name; name = “Jon Java”; : String Jon Java name 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter name A 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 Jon Java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String method: length Assume str is a properly initialized String object. str.length( ) will return the number of characters in str. If str is “programming”, then str.length( ) will return 11. –all characters, including spaces, are counted in the length

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Indexing The position, or index, of the first character is 0.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: indexOf Assume str and substr are properly initialized String objects. str.indexOf( substr ) will return the first position substr occurs in str. If str is “programming” and substr is “gram”, then str.indexOf(substr ) will return 3 because the position of the first character of substr in str is 3. If substr does not occur in str, then –1 is returned. The search is case-sensitive.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: substring Assume str is a properly initialized String object. str.substring( i, j ) will return a new string by extracting characters of str from position i through position j-1 where 0  i  str.length, 0  j  str.length, and i  j. If str is “programming”, then str.substring(3, 7) will create a new String whose value is “gram” because g is at position 3 and m is at position 6. The original string str remains unchanged.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String Method: concatenation Assume str1 and str2 are properly initialized String objects. str1 + str2 will return a new string that is a concatenation of two strings. If str1 is “pro” and str2 is “gram”, then str1 + str2 will return “program”. Notice that + is an operator and not a method of the String class. The strings str1 and str2 remain the same.

Example Read a name consisting of –first middle last Print it back out as –last, middle first What happens if there are too few names? What happens if there are too many names

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Date The Date class from the java.util package is used to represent a date. When a Date object is created, it is set to the current date (based on the computers settings)‏ The class has toString method that converts the internal format to a string. Date today; today = new Date( ); today.toString( ); “Fri Oct 31 10:05:18 PST 2003”

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter SimpleDateFormat The SimpleDateFormat class allows the Date information to be displayed with various format. Table 2.1 page 64 shows the formatting options. Date today = new Date( ); SimpleDateFormat sdf1, sdf2; sdf1 = new SimpleDateFormat( “MM/dd/yy” ); sdf2 = new SimpleDateFormat( “MMMM dd, yyyy” ); sdf1.format(today); sdf2.format(today); “10/31/03” “October 31, 2003”

SimpleDateFormat Formatting Symbols yyearyyyy -> 2008 MmonthMM->10, MMM-> Sep, MMMM->September ddaydd->10 hhour(1-12) hh->09 aam/pma-> AM mminutesmm->35 ssecondsss->54 Eday of week E->Thur, EEEE->Thursday

Example Given a date, determine the day of the week. How could we read a date and get month, day and year from it?

Standard Input The technique of using System.in to input data is called standard input. –We can only input a single byte using System.in directly. To input data in a more useful format, we use the Scanner class (from Java 5.0). import java.util.Scanner; … Scanner scanner; scanner = new Scanner( System.in); String word = scanner.next (); String line = scanner.nextLine ();

The GregorianCalendar Class Use a GregorianCalendar object to manipulate calendar information GregorianCalendar today, independenceDay; today = new GregorianCalendar(); independenceDay = new GregorianCalendar(1776, 6, 4); //month 6 means July; 0 means January

Retrieving Calendar Information This table shows the class constants for retrieving different pieces of calendar information from Date.

Sample Calendar Retrieval GregorianCalendar cal = new GregorianCalendar(); //Assume today is Nov 9, 2003 System.out.print(“Today is ” + (cal.get(Calendar.MONTH)+1) + “/” + cal.get(Calendar.DATE) + “/” + cal.get(Calendar.YEAR)); Today is 11/9/2003 Output