+ All Categories
Home > Documents > Chapter 2: Java Fundamentals cont’d

Chapter 2: Java Fundamentals cont’d

Date post: 13-Feb-2016
Category:
Upload: anevay
View: 57 times
Download: 5 times
Share this document with a friend
Description:
Chapter 2: Java Fundamentals cont’d. Lecture 5: Wednesday Sept 13, 2006. Outline. 2.1 The Parts of a Java Program 2.2 The print and println Methods, and the Java Standard Class Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators - PowerPoint PPT Presentation
27
Chapter 2: Java Fundamentals cont’d Lecture 5: Wednesday Sept 13, 2006
Transcript
Page 1: Chapter 2: Java Fundamentals cont’d

Chapter 2: Java Fundamentals cont’d

Lecture 5: Wednesday Sept 13, 2006

Page 2: Chapter 2: Java Fundamentals cont’d

Outline 2.1 The Parts of a Java Program 2.2 The print and println Methods, and the Java Standard Class

Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators 2.6 Combined Assignment Operators 2.7 Conversion Between Primitive Types 2.8 Creating Named Constants with final 2.9 The String Class 2.10 Scope 2.11 Comments 2.12 Programming Style 2.13 Reading Keyboard Input 2.14 Dialog Boxes 2.15 Common Errors to Avoid

Page 3: Chapter 2: Java Fundamentals cont’d

Scope The variable scope is the part of the

program that has access to itpublic class Scope{ public static void main(String[] args) { System.out.println(value); // ERROR! int value = 100; }}

Page 4: Chapter 2: Java Fundamentals cont’d

Scopepublic class Scope { public static void main(String[] args){ int number = 100;

System.out.println(number);int number = 200; //ERROR}

}

Page 5: Chapter 2: Java Fundamentals cont’d

Comments Java provides three methods for

commenting code.// Single line comment. Anything after the // on the line will be

ignored by the compiler.

/* … */

Block comment. Everything beginning with /* and ending with the first */ will be ignored by the compiler. This comment type cannot be nested.

/** … */

Javadoc comment. This is a special version of the previous block comment that allows comments to be documented by the javadoc utility program. Everything beginning with the /** and ending with the first */ will be ignored by the compiler. This comment type cannot be nested.

Page 6: Chapter 2: Java Fundamentals cont’d

Programming Style Although Java has a strict syntax,

whitespace characters are ignored by the compiler.

The Java whitespace characters are: space tab newline carriage return form feed

Page 7: Chapter 2: Java Fundamentals cont’d

Programming Stylepublic class Compact {public static void

main(String[] args){int shares=220; double averagePrice=14.67; System.out.println("There were "+shares+" shares sold at $"+averagePrice+ " per share.");}}

Compiles !!!

Page 8: Chapter 2: Java Fundamentals cont’d

Indentation Programs should use proper

indentation. Each block of code should be

indented a few spaces from its surrounding block.

Two to four spaces are sufficient

Page 9: Chapter 2: Java Fundamentals cont’d

/** This example is much more readable than Compact.java.*/public class Readable{ public static void main(String[] args) { int shares = 220; double averagePrice = 14.67;

System.out.println("There were " + shares + " shares sold at $" + averagePrice + " per share."); }}

Programming Style

Page 10: Chapter 2: Java Fundamentals cont’d

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 can be displayed using the JOptionPane class.

Two of the dialog boxes are: Message Dialog - a dialog box that displays a

message. Input Dialog - a dialog box that prompts the user

for input.

Page 11: Chapter 2: Java Fundamentals cont’d

Using the import Statement The JOptionPane class is not

automatically available to your Java programs.

The following statement must be before the program’s class header:import javax.swing.JOptionPane;

This statement tells the compiler where to find the JOptionPane class. 

Page 12: Chapter 2: Java Fundamentals cont’d

Dialog BoxesThe JOptionPane class provides static methods to display each type of dialog box.

Page 13: Chapter 2: Java Fundamentals cont’d

Message Dialogs JOptionPane.showMessageDialog

method is used to display a message dialog.JOptionPane.showMessageDialog(null,

"Hello World"); The second argument is the message

that is to be displayed.

Page 14: Chapter 2: Java Fundamentals cont’d

Input Dialogs An input dialog is a quick and simple

way to ask the user to enter data. The dialog displays a text field, an Ok

button and a Cancel button. If Ok is pressed, the dialog returns the

user’s input. If Cancel is pressed, the dialog

returns null.

Page 15: Chapter 2: Java Fundamentals cont’d

Input DialogsString name;name = JOptionPane.showInputDialog( "Enter your name.");

The argument passed to the method is the message to display.

If the user clicks on the OK button, name references the string entered by the user.

If the user clicks on the Cancel button, name references null.

Page 16: Chapter 2: Java Fundamentals cont’d

NamesDialog.javaimport javax.swing.JOptionPane;public class NamesDialog{ public static void main(String[] args) { String firstName; // The user's first name String middleName; // The user's middle name String lastName; // The user's last name // Get the user's first name firstName = JOptionPane.showInputDialog("What is " + "your first name? ");

Page 17: Chapter 2: Java Fundamentals cont’d

NamesDialog.java // Get the user's middle name. middleName = JOptionPane.showInputDialog(

"What is " + "your middle name? ");

// Get the user's last name. lastName =

JOptionPane.showInputDialog("What is " + "your last name? ");

Page 18: Chapter 2: Java Fundamentals cont’d

Example// Display a greeting JOptionPane.showMessageDialog(null,

"Hello " + firstName + " " +middleName + " " + lastName);

System.exit(0); }}

Page 19: Chapter 2: Java Fundamentals cont’d

The System.exit() Method A program that uses JOptionPane does

not automatically stop executing when the end of the main method is reached.

Java generates a thread, which is a process running in the computer, when a JOptionPane is created.

If the System.exit method is not called, this thread continues to execute.

Page 20: Chapter 2: Java Fundamentals cont’d

The System.exit() Method The System.exit method requires an

integer argument.System.exit(0);

This argument is an exit code that is passed back to the operating system.

This code is usually ignored, however, it can be used outside the program: to indicate whether the program ended

successfully or as the result of a failure. The value 0 traditionally indicates that the

program ended successfully.

Page 21: Chapter 2: Java Fundamentals cont’d

Converting a String to a Number The JOptionPane’s showInputDialog

method always returns the user's input as a String

String containing a number, such as “127.89, can be converted to a numeric data type.

Page 22: Chapter 2: Java Fundamentals cont’d

The Parse Methods Parse methods convert strings to

numeric data types They are:

Byte.parseByte Integer.parseInt Short.parseShort Long.parseLong Float.parseFloat Double.parseDouble

Page 23: Chapter 2: Java Fundamentals cont’d

The Parse Methods- Examples byte bVar = Byte.parseByte("1"); int iVar = Integer.parseInt("2599"); short sVar = Short.parseShort("10"); long lVar = Long.parseLong("15908"); float fVar = Float.parseFloat("12.3"); double dVar =

Double.parseDouble("7945.6");

Page 24: Chapter 2: Java Fundamentals cont’d

PayrollDialog.javaimport javax.swing.JOptionPane;

public class PayrollDialog{ public static void main(String[] args) { String inputString; // For reading input String name; // The user's name int hours; // The number of hours worked double payRate; // The user's hourly pay rate double grossPay; // The user's gross pay

Page 25: Chapter 2: Java Fundamentals cont’d

PayrollDialog.java// Get the user's name. name = JOptionPane.showInputDialog("What

is " + "your name? ");

// Get the hours worked. inputString = JOptionPane.showInputDialog( "How many hours” + “ did you work this week? "); // Convert the input to an int. hours = Integer.parseInt(inputString);

Page 26: Chapter 2: Java Fundamentals cont’d

PayrollDialog.java // Get the hourly pay rate. inputString = JOptionPane.showInputDialog("What is”

+ " your hourly pay rate? "); // Convert the input to a double. payRate =

Double.parseDouble(inputString); // Calculate the gross pay. grossPay = hours * payRate;

Page 27: Chapter 2: Java Fundamentals cont’d

PayrollDialog.java // Display the results. JOptionPane.showMessageDialog(null,

"Hello " + name + ". Your gross pay is $" + grossPay);

// End the program. System.exit(0); }}


Recommended