L2:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.

Slides:



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

Chapter 2 Elementary Programming
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,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
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) 2007 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
1 Chapter 4 Primitive Data Types and Operations. 2 Objectives F To write Java programs to perform simple calculations F To use identifiers to name variables,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
1 Chapter 2: Elementary Programming Shahriar Hossain.
Introduction to Java Programming, 4E Y. Daniel Liang.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Java Overview CS2336: Computer Science II1. First Program public class HelloWorld { public static void main(String args[]) { System.out.println("Hello.
Programming with Java Instructor : Neelima Gupta.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2 Elementary Programming 1. Introducing Programming with an Example Listing 2.1 Computing the Area of a Circle This program computes the area.
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data Types.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 4 Mathematical Functions, Characters, and Strings.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 4 Mathematical Functions, Characters,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Programming Fundamentals I (COSC- 1336), Lecture 2 (prepared after Chapter 2 of Liang’s 2011 textbook) Stefan Andrei 10/17/20151 COSC-1336, Lecture 2.
S.W.Ma/CIM/LWL41211/2 Prog. IIA Page 1 Lecture 2&3 Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables,
Chapter 2 Elementary Programming
Liang, Introduction to Java Programming1 Primitive Data Types and Operations Gang Qian Department of Computer Science University of Central Oklahoma.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 Primitive Data.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 3 Mathematical Functions, Strings, and Objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computers, Programs,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
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.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Chapter 2 Elementary Programming 1. Motivations 2 In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2 Elementary Programming
Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Chapter 4 Mathematical Functions, Characters, and Strings
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 2 Primitive Data Types and Operations
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
Chapter 4: Mathematical Functions, Characters, and Strings
Chapter 2 Elementary Programming
elementary programming
Anatomy of a Java Program
Java Programming Language
Chapter 2 Primitive Data Types and Operations
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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

L2:CSC © Dr. Basheer M. Nasef Chapter2: Elementary Programming 2

L2:CSC © Dr. Basheer M. Nasef 3 Objectives To use Java operators to write numeric expressions. To represent characters using the char type. To represent a string using the String type. To obtain input from the console using the Scanner class. To distinguish syntax errors, runtime errors, and logic errors. To debug logic errors. (GUI) To obtain input using the JOptionPane input dialog boxes.

L2:CSC © Dr. Basheer M. Nasef 4 Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = '\u0041'; (Unicode) char numChar = '\u0034'; (Unicode) Four hexadecimal digits. NOTE: The increment and decrement operators can also be used on char variables to get the next or preceding Unicode character. For example, the following statements display character b. char ch = 'a'; System.out.println(++ch);

L2:CSC © Dr. Basheer M. Nasef 5 Unicode Format Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to support the interchange, processing, and display of written texts in the world’s diverse languages. Unicode takes two bytes, preceded by \u, expressed in four hexadecimal numbers that run from '\u0000' to '\uFFFF'. So, Unicode can represent characters. Unicode \u03b1 \u03b2 \u03b3 for three Greek letters

L2:CSC © Dr. Basheer M. Nasef 6 Problem: Displaying Unicodes Write a program that displays two Chinese characters and three Greek letters. DisplayUnicode Run

L2:CSC © Dr. Basheer M. Nasef 7 Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b\u0008 Tab \t\u0009 Linefeed \n\u000A Carriage return \r\u000D Backslash \\\u005C Single Quote \ ' \u0027 Double Quote \ " \u0022

L2:CSC © Dr. Basheer M. Nasef 8 Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

L2:CSC © Dr. Basheer M. Nasef 9 ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to \u007f

L2:CSC © Dr. Basheer M. Nasef 10 The String Type The char type only represents one character. To represent a string of characters, use the data type called String. For example, String message = "Welcome to Java";

L2:CSC © Dr. Basheer M. Nasef 11 String Concatenation // Three strings are concatenated String message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2 String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character B String s1 = "Supplement" + 'B'; // s1 becomes SupplementB

L2:CSC © Dr. Basheer M. Nasef 12 Problem 2-1 Write a program that asks the user to enter two integers, obtains the numbers from the user, and then prints the larger number followed by the words "is larger." If the numbers are equal, print the message "These numbers are equal.“ MaxInt

L2:CSC © Dr. Basheer M. Nasef 13 Write a program that inputs three integers from the keyboard and prints the sum, average, product, smallest and largest of these numbers. The screen dialog should appear as follows: Problem 2-2 Prob2_2

L2:CSC © Dr. Basheer M. Nasef 14 Quiz: (Summing the digits in an integer) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93. Here is a sample run:

L2:CSC © Dr. Basheer M. Nasef 15 Example : (Distance of two points)

L2:CSC © Dr. Basheer M. Nasef 16 Quiz: (area of a triangle)

L2:CSC © Dr. Basheer M. Nasef 17 Naming Conventions, cont. Class names: Capitalize the first letter of each word in the name. For example, the class name ComputeArea. Constants: Capitalize all letters in constants, and use underscores to connect words. For example, the constant PI and MAX_VALUE

L2:CSC © Dr. Basheer M. Nasef 18 Proper Indentation and Spacing Indentation Indent two spaces. Spacing Use blank line to separate segments of the code.

L2:CSC © Dr. Basheer M. Nasef 19 Block Styles Use end-of-line style for braces.

L2:CSC © Dr. Basheer M. Nasef 20 Programming Errors Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic Errors Produces incorrect result

L2:CSC © Dr. Basheer M. Nasef 21 Syntax Errors public class ShowSyntaxErrors { public static void main(String[] args) { i = 30; System.out.println(i + 4); }

L2:CSC © Dr. Basheer M. Nasef 22 Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args) { int i = 1 / 0; }

L2:CSC © Dr. Basheer M. Nasef 23 Logic Errors suppose you wrote the following program to add number1 to number2.

L2:CSC © Dr. Basheer M. Nasef 24 Debugging Logic errors are called bugs. The process of finding and correcting errors is called debugging. You can hand-trace the program (i.e., catch errors by reading the program), or you can insert print statements in order to show the values of the variables or the execution flow of the program.

L2:CSC © Dr. Basheer M. Nasef 25 Debugger Debugger is a program that facilitates debugging. You can use a debugger to Execute a single statement at a time. Trace into or stepping over a method. Set breakpoints. Display variables. Display call stack. Modify variables.

L2:CSC © Dr. Basheer M. Nasef 26 Getting Input from Input Dialog Boxes String input = JOptionPane.showInputDialog( "Enter an input");

L2:CSC © Dr. Basheer M. Nasef 27 Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog( null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);

L2:CSC © Dr. Basheer M. Nasef 28 Two Ways to Invoke the Method There are several ways to use the showInputDialog method. For the time being, you only need to know two ways to invoke it. One is to use a statement as shown in the example: String string = JOptionPane.showInputDialog(null, x, y, JOptionPane.QUESTION_MESSAGE); where x is a string for the prompting message, and y is a string for the title of the input dialog box. The other is to use a statement like this: JOptionPane.showInputDialog(x); where x is a string for the prompting message.

L2:CSC © Dr. Basheer M. Nasef 29 Converting Strings to Integers The input returned from the input dialog box is a string. If you enter a numeric value such as 123, it returns “123”. To obtain the input as a number, you have to convert a string into a number. To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.

L2:CSC © Dr. Basheer M. Nasef 30 Converting Strings to Doubles To convert a string into a double value, you can use the static parseDouble method in the Double class as follows: double doubleValue =Double.parseDouble(doubleString); where doubleString is a numeric string such as “123.45”.

L2:CSC © Dr. Basheer M. Nasef 31 Decision Making: Equality and Relational Operators