Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2 Elementary Programming
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte,
Chapter 2 - Introduction to Java Applications
Chapter 2: Using Data.
 2003 Prentice Hall, Inc. All rights reserved. 1 Lecture 3 Variables Primitive Data Types calculations & arithmetic operators Readings: Chapter 2.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
 2003 Prentice Hall, Inc. Modified for use with this class. All rights reserved. 1 Introduction to Computers and Programming in JAVA: V Primitive.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Introduction to Java Programming, 4E Y. Daniel Liang.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Programming Style and Documentation Objective(s) F To become familiar with Java Style and Documentation Guidelines.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 3e Chapter 3 Introduction to Objects and Input/Output.
©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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Liang, Introduction to Java Programming1 Primitive Data Types and Operations Gang Qian Department of Computer Science University of Central Oklahoma.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Java Programming: From Problem Analysis to Program Design, 4e Chapter 3 Introduction to Objects and Input/Output.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
M105 - Week 2 Chapter 3 Numerical Data 1 Prepared by: M105 Team - AOU - SAB.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Chapter 2 Elementary Programming
Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
2.5 Another Java Application: Adding Integers
Chapter 3 Java Input/Output.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Introduction to Classes and Methods
Chapter 2 - Introduction to Java Applications
Chapter 2: Basic Elements of Java
MSIS 655 Advanced Business Applications Programming
Fundamentals 2.
Chapter 3 Input/Output.
Anatomy of a Java Program
CS2011 Introduction to Programming I Elementary Programming
In this class, we will cover:
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)

Programming Style and Documentation Comments Comments Comments are very important. You should comment your code in order to provide explanations of what the program is doing Comments are very important. You should comment your code in order to provide explanations of what the program is doing Indentation and Spacing Indentation and Spacing Proper indenting makes the code reflect the actual logic of the program. Proper indenting makes the code reflect the actual logic of the program. Block Styles Block Styles Placement of the { and } braces Placement of the { and } braces Naming conventions Naming conventions Use descriptive names for classes, methods, and variables Use descriptive names for classes, methods, and variables By convention, class names begin with uppercase, method and variable names begin with lowercase, and constants are all uppercase. By convention, class names begin with uppercase, method and variable names begin with lowercase, and constants are all uppercase.

Comments // for single line // for single line /* */ for multiple lines /* */ for multiple lines Comment at top of source file to identify the author and the purpose of the program Comment at top of source file to identify the author and the purpose of the program Comment to describe classes, methods, and sections of code (steps of the algorithm) Comment to describe classes, methods, and sections of code (steps of the algorithm) Comments help readers understand the program code!

Indentation and Spacing Statements within blocks should be indented Statements within blocks should be indented Statements that are executed within control structures should be indented Statements that are executed within control structures should be indented If a statement continues from one line to the next, the subsequent line(s) should be indented. If a statement continues from one line to the next, the subsequent line(s) should be indented.

Block Styles Choices: Choices: Next-line: Next-line: void myMethod() { } End-of-line: End-of-line: void myMethod(){ } Either way is fine

Naming Conventions Variables and method names: Variables and method names: Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea. Use lowercase. If the name consists of several words, concatenate all in one, use lowercase for the first word, and capitalize the first letter of each subsequent word in the name. For example, the variables radius and area, and the method computeArea.

Naming Conventions, cont. Class names: Class names: Capitalize the first letter of each word in the name. For example, the class name TestComputeArea. Capitalize the first letter of each word in the name. For example, the class name TestComputeArea. Constants: Constants: Capitalize all letters in constants. For example, the constant PI. Capitalize all letters in constants. For example, the constant PI.

Chapter 2 Examples ComputeArea (Listing 2.1, p27) ComputeArea (Listing 2.1, p27) TestScanner (Listing 2.6, p45) TestScanner (Listing 2.6, p45) ComputeLoan (Listing 2.7, p46) ComputeLoan (Listing 2.7, p46) ComputeChange (Listing 2.8, p48) ComputeChange (Listing 2.8, p48) ShowCurrentTime (Listing 2.9, p50) ShowCurrentTime (Listing 2.9, p50) ComputeLoanUsingInputDialog (Listing 2.10, p57) ComputeLoanUsingInputDialog (Listing 2.10, p57)

Putting it all together: Listing 2.1 (available from Liang Code Samples)

Putting it all together: Listing 2.1 Variable declarations Assignment of literal value into variable Arithmetic expression and assignment Output with concatenated data values.

A method Call System.out.println("The area for the circle of radius " + radius + " is " + area); System.out.println("The area for the circle of radius " + radius + " is " + area); The member variable (out) is an instance of another class (called PrintStream) This method does not return a value  no assignment The System class…this contains static member variable (out). The name (identifier) of the member variable PrintStream has a non-static (instance- specific) method called println The arguments passed to the println method

About the System Class Part of the Java Class Library Part of the Java Class Library Useful for standard input (keyboard) and output (screen) in console-based applications Useful for standard input (keyboard) and output (screen) in console-based applications More details from Sun’s Java documentation site: More details from Sun’s Java documentation site:

Java documentation from Sun’s Java site

Putting it all together: Listing 2.1 A package is a collection of classes. In practice, a package is equivalent to a directory (folder). To indicate that a class is in a package, you must: 1)Place the.java file in the appropriate folder, and 2)Write a package statement with the appropriate package name as the first line of code in the.java file.

The ch02 package consists of all the classes (ComputeArea, ComputeChange, ComputeMortgage, InputDialogDemo, MyInput, ShowCurrentTime, ShowRuntimeErrors, ShowSyntaxErrors, TestMyInput).

Listing 2.10 ComputeLoanUsingInputDialog

If you are using a class that is not in your own package, or in the package java.lang, you must import that class.

Listing 2.10 ComputeLoanUsingInputDialog Input from user and convert to appropriate data type.

User Input via JOptionPane Object JOptionPane is a class in the Java class library that can display an input dialog for obtaining user input or show message dialogs for display to the user. JOptionPane is a class in the Java class library that can display an input dialog for obtaining user input or show message dialogs for display to the user. Two methods: Two methods: showInputDialog – for user input showInputDialog – for user input showMessageDialog – for displaying a message to the user showMessageDialog – for displaying a message to the user

String annualInterestRateString = JOptionPane.showInputDialog( "Enter yearly interest rate, for example 8.25:"); The JOptionPane class…this contains static methods for displaying various types of dialogs The JOptionPane class’s ShowInputDialog method displays a dialog with parameters controlling the look.

After the user enters a value and clicks OK, the value is returned as a String. Here, we assign that value into a String variable In this case, the variable nnualInterestRateString will contain the string “5.8”. String annualInterestRateString = JOptionPane.showInputDialog( "Enter yearly interest rate, for example 8.25:");

Listing 2.10 ComputeLoanUsingInputDialog Simple arithmetic expressions and assignments. NOTE: variables are declared and initialized in one statement.

Listing 2.10 ComputeLoanUsingInputDialog Note the use of the cast operator. This use is a clever way to make sure that the displayed output has no more than two decimal places.

Anatomy of an arithmetic statement involving casting (assume that monthlyPayment had been calculated as ) (1) (3) Assign into variable monthlyPayment (4) (2) Casting from double to int truncates

Listing 2.10 ComputeLoanUsingInputDialog A more complex arithmetic expression and assignment. Here the expression includes: 1.Parentheses to override normal operator precedence. 2.Calling another method and passing arguments

Anatomy of a Complex Arithmetic Expression 1) Declare the variable 2) Perform calculation 3) Perform calculation 4) Call the Math.pow method passing the results of (2) and (3) as arguments. 5) Divide the result of (4) into the number 16) Subtract the result of (5) from the number 1 7) Do the multiplication and division 8) Assign results to monthlyPayment

About the Math Class Part of the Java Class Library Part of the Java Class Library Contains methods for performing basic numeric operations such as exponentiation, logarithm, square root, and trigonometric functions. Contains methods for performing basic numeric operations such as exponentiation, logarithm, square root, and trigonometric functions. More details from Sun’s Java documentation site: More details from Sun’s Java documentation site:

Java documentation from Sun’s Java site

1. Create a Scanner object Scanner scanner = new Scanner(System.in); 2. Use the methods next(), nextByte(), nextShort(), nextInt(), nextLong(), nextFloat(), nextDouble(), or nextBoolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, System.out.print("Enter a double value: "); Scanner scanner = new Scanner(System.in); double d = scanner.nextDouble(); The Scanner Class Another way to do User Input

Listing 2.6 TestScanner

If you are using a class that is not in your own package, or in the package java.lang, you must import that class.

Listing 2.6 TestScanner Creating an instance of the Scanner class, associating it with the standard input stream (System.in), and assigning a reference to that instance into the variable called input.

Listing 2.6 TestScanner Input from user via scanner, by calling the input method that reads user input for the appropriate data type.

How Scanner-based User I/O looks in NetBeans System.out.print and System.out.println sends standard output to Output window, and the Scanner reads from the same window

Summary: User Input/Output Options Standard I/O Streams Standard I/O Streams Input: Scanner class and methods Input: Scanner class and methods Output: System.out.print (or println) Output: System.out.print (or println) GUI (popup dialogs) GUI (popup dialogs) Input: JOptionPane.ShowInputDialog Input: JOptionPane.ShowInputDialog Output: JOptionPane.ShowMessageDialog Output: JOptionPane.ShowMessageDialog Many more GUI options…we’ll study later in semester Many more GUI options…we’ll study later in semester