Chapter 2 Getting Started with Java Part B. Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating.

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.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
 2005 Pearson Education, Inc. All rights reserved Introduction.
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.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
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 Software Development Software Life Cycle UML Diagrams.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 2 Getting Started with Java Structure of.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
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 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.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
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.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 3b Standard Input and Output Sample Development.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Software Engineering Software Development Process.
JOptionPane class. Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Homework Reading Programming Assignments
CS1101: Programming Methodology
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
Java Classes Using Java Classes Introduction to UML.
Getting Started with Java Recitation – 1/23/2009 CS 180 Department of Computer Science, Purdue University.
Java Programming, Second Edition Chapter Five Input and Selection.
Java Programming, Second Edition Chapter One Creating Your First Java Program.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Computer Programming 2 Lab(1) I.Fatimah Alzahrani.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
1 Java Programming 1 Introduction to Basic GUI Lesson 4.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
1 COMP 241: Object-Oriented Programming with Java Fall 2004 Lecture 1 September 27, 2004 Serdar Taşıran.
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.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Application Programming Interfaces. Java comes with a bunch of classes that are already written. Java comes with a bunch of classes that are already written.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Eight String Manipulation.
Creating a Java Application and Applet
 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.
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.
Creating and Using Dialogs ● A dialog is a box that pops up and prompts the user for a value or informs them of something ● One way: directly create objects.
بسم الله الرحمن الرحيم CPCS203: Programming II. ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display., Modifications by Dr.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Object Oriented Programming (OOP) LAB # 3 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Section 64 – Manipulating Data Using Methods – Java Swing
Introduction to Scripting
Chapter 2 - Introduction to Java Applications
Getting Started with Java
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Chapter 2 Getting Started with Java Part B

Topics Components of a Java Program –classes –methods –comments –import statements Declaring and creating objects –Java identifiers Standard Java classes Development Example

Warm-up Exercises First homework assignment (on the web) –Chapter 1 problems 2, 4, 5 –Chapter 2 problems 1, 9 (parts a, c, g, j), 12 Practice –Chapter 1 # 3: CD class –Chapter 1 # 14: Inheritance hierarchy for contact manager (Person, Professional contact, Friend, Student)

UML Class Diagrams An example of a UML diagram for the CD class Diagram has only instance data and methods Class data and methods would be underlined

Inheritance Hierarchy

Template for Java Program

Edit-Compile-Run Cycle Edit vi MyClass.java Compile javac MyClass.java Run java MyClass

Java Standard Classes Java comes with an extensive set of classes that you can use. Using existing Java classes is an important step in becoming a successful and proficient programmer. The standard classes are organized into packages

Standard Classes We will introduce four standard classes here: –String –JOptionPane –Date –SimpleDateFormat

String A string is basically some text –a sequence of characters We've seen String objects in our programs already –"this is a String" –These are constant or literal String objects –They have no name –They can't be changed String class is in java.lang (no import statement required)

Creating Strings Putting a literal string in your program creates a String automatically You can declare a String object and assign it to the literal String String fixedString = "some text"; -the String class is the only one you can do this with You can use new to create a new String from an existing one –String newString = new String( "more text"); –String newString2 = new String( newString);

String Methods The String class has many methods We'll look at a few –length() returns the number of characters in the String String text = "Espresso"; text.length() returns 8 –String concat( String toAdd) creates a new String out of the original String followed by the argument String Can use + as a shortcut for this method (another unique property of the String class) "acro" + "bat" is "acrobat"

indexOf method The indexOf method can be used to locate one String in another –indexOf( String toFind) returns the position of toFind in the String –Positions are numbered starting from 0 –the index of "press" in "Espresso" is 2 –if the toFind String isn't present, the result is -1

substring method substring returns a new String which is part of a larger String –substring( start, justAfter) start is the desired starting index justAfter is the index of the first character past the desired substring

Using the substring method

Special Characters What if you need a string that has several lines of text in it? You can't spread a literal string over multiple lines You need to put a character into the string that represents a carriage return –"\n" is a special character that represents a newline the \ is an escape character that tells the compiler to interpret the next character differently from its usual meaning We'll see other special characters that start with a \

GUI Windows Windows come in 2 forms in a GUI interface 1.application windows which are generally long-lived JFrame objects in Java 2.dialog boxes are short-lived JDialog objects in Java allow the user to interact with the program come in many forms - the JDialog class has to be quite complex to support this variety some common forms –messages –confirmation (yes/no/cancel) –input

JOptionPane Allows you to easily create common types of JDialog objects All methods are class methods – you never create a JOptionPane object –call the methods using JOptionPane.methodName() JOptionPane is in the javax.swing package

Message Dialogs Used when you need to be sure the user has been informed about something –results –error messages and warnings JOptionPane.showMessageDialog( parent, message) –parent is the name of the JFrame the JDialog is attached to (or null if it is not attached to a JFrame) –message is the text to be displayed (a String)

Input Dialogs Another common form of dialog is one used to ask for a small amount of input JOptionPane.showInputDialog( String prompt) –displays a box for the user to enter something – returns the String that the user types in

Using Dates in Java The Date class in Java represents time to the nearest millisecond Create a Date with the current time using Date today = new Date(); The toString method of the Date class returns a String representing the complete time and date Weekday Month day hours:minutes:seconds timezone year Date is in java.util

Other Date Formats Use the SimpleDateFormat class Create a SimpleDateFormat object with a "picture" of what you want it to look like –picture is a string with codes for the various fields SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy"); SimpleDateFormat is in the java.text package

SimpleDateFormat Codes Yyearnumberyyyy->2004 Mmonthtext or number MM->10 MMM-> Oct dday in monthnumberdd->20 hhour in (am/pm)numberhh->09 aam/pmtexta->am mminutes in hournumbermm->35 sseconds in minutenumberss->54 Eday in weektextE-> Sat.

Software Lifecycle The sequence of stages from conception to operation of a program is called software life cycle. Five stages are –Analysis –Design –Coding –Testing –Operation and Maintenance

Sample Development: Printing Initials Problem statement: Write an application that asks for the user’s first, middle, and last names and replies with their initials. Overall plan: –Get the user’s first, middle, and last names. –Extract the initials to form the monogram. –Output the monogram.

What classes do we need?

Necessary classes Name, initials and monogram need String Need JOptionPane for input and output –To input the name, we need an input dialog –To output the monogram, we need a message dialog

UML Diagram for Ch2Monogram

Phase 1 Input the user's name –first middle last Choices –one dialog for each part easy to program tedious for user –single dialog for the entire name harder to program need to specify format in this case nicer for user

Phase 2 Compute and display monogram –need to display result to know whether computation is correct How to separate name into 3 parts? –use indexOf to find space –use substring to separate at space –repeat for second space How to get initial from each part? –substring(0,1)