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.

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
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,
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
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 2 Elementary Programming.
1 Chapter 2: Elementary Programming Shahriar Hossain.
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.
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, 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.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 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.
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.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
VARIABLES Introduction to Computer Science 1- COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
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.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Java Overview. Comments in a Java Program Comments can be single line comments like C++ Example: //This is a Java Comment Comments can be spread over.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
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 C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
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.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Elementary Programming 1. Motivations 2 In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Elementary Programming
Unit 2 Elementary Programming
Chapter 2 Elementary Programming
Lecture 3: Operators, Expressions and Type Conversion
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Chapter 2 Elementary Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Elementary Programming
Chapter 2 Elementary Programming
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Numerical Data Types.
Chapter 2 Elementary Programming
elementary programming
Anatomy of a Java Program
Chapter 2 Primitive Data Types and Operations
Chapter 2: Beginning to Program
Presentation transcript:

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

public class ComputeArea { public static void main(String[] args) { //Step 1: Read in radius //Step 2: Compute area //Step 3: Display the area } 3

public class ComputeArea { public static void main(String[] args) { double radius; double area //Step 1: Read in radius //Step 2: Compute area //Step 3: Display the area } 4

public class ComputeArea { public static void main(String[] args) { double radius; double area; radius = 20; //Step 2: Compute area //Step 3: Display the area } 5

public class ComputeArea { public static void main(String[] args) { double radius; double area; radius = 20; area = radius * radius * ; //Step 3: Display the area } 6

public class ComputeArea { public static void main(String[] args) { double radius; double area; radius = 20; area = radius * radius * ; System.out.println(“The area for the circle of radius “ + radius + “ is ” + area); } 7

Example 2.1 Computing the Area of a Circle public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * ; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); } 8

 Special symbols to name such programming entities as variables, constants, methods, classes and packages.  An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($).  An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit. An identifier cannot be a reserved word. An identifier cannot be true, false, or null.  An identifier can be of any length. 9

 Example: $2, ComputeArea, area, radius, showMessageDialog, payRate, salary, J42Stop, gross_salary  Invalid identifiers: 2A, *salary, gross- salary, class, public, String 10

Used to store data in program. // Compute the first area radius = 1.0; area = radius * radius * ; System.out.println("The area is “ + area + " for radius "+radius); // Compute the second area radius = 2.0; area = radius * radius * ; System.out.println("The area is “ + area + " for radius "+radius); 11

int x; // Declare x to be an // integer variable; double radius; // Declare radius to // be a double variable; char a; // Declare a to be a // character variable; 12

x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0 to radius; a = 'A'; // Assign 'A' to a; 13

int x = 1; double d = 1.4; float f = 1.4; 14

Represents permanent data that never changes. final datatype CONSTANTNAME = VALUE; final double PI = ; final int SIZE = 3; 15

public class ComputeArea { public static void main(String[] args) { double radius, area; final double PI = ; radius = 20; area = radius * radius * PI; System.out.println(“The area for the circle of radius “ + radius + “ is ” + area); } 16

byte 8 bits short 16 bits int 32 bits long 64 bits float 32 bits double 64 bits 17

+, -, *, /, and % 5 / 2 yields an integer / 2 yields a double value % 2 yields 1 (the remainder of the division) 18

19 NameMeaningExampleResult +Addition Substraction34.0 – *Multiplication300 * /Division1.0/ %Remainder20 % 32

Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example, System.out.println( ); displays , not 0.5, and System.out.println( ); displays , not 0.1. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result. 20

A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements: int i = 34; long x = ; double d = 5.0; 21

 An integer literal can be assigned to an integer variable as long as it can fit into the variable.  A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type.  An integer literal is assumed to be of the int type, whose value is between ( ) to 2 31 –1 ( ).  To denote an integer literal of the long type, append it with the letter L or l. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one). 22

 written with a decimal point.  By default, a floating-point literal is treated as a double type value.  For example, 5.0 is considered a double value, not a float value. You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D.  For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number. 23

Floating-point literals can also be specified in scientific notation. For example, e+2, same as e2, is equivalent to , and e-2 is equivalent to E (or e) represents an exponent and it can be either in lowercase or uppercase. 24

25 is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

x   (3+4*x)/5 ) 94 (9 y x x    9*(4/x + (9+x)/y) ))(5(10 x cbay   10*(y-5)*(a+b+c)/x

 How would you write the following arithmetic expression in Java? 27

28 OperatorExampleEquivalent +=i+=8i = i+8 -=f-=8.0f = f-8.0 *=i*=8i = i*8 /=i/=8i = i/8 %=i%=8i = i%8

29 OperatorNameDescription ++varpreincrementThe expression (++var) increments var by 1 and evaluates to the new value in var after the increment. var++postincrementThe expression (var++) evaluates to the original value in var and increments var by 1. --varpredecrementThe expression (--var) decrements var by 1 and evaluates to the new value in var after the decrement. var--postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.

30

31 Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this: int k = ++i + i.

1. ++, -- (preincrement, predecrement) 2. *, /, % 3. +, - 4. =, +=, -=, *=, /=, %= 32

 Assume that int a = 1 and double d = 1.0 and that each expression is independent. What are the results of the following expressions? 33 a = 46 % * 4 – 2; a = % 5 * (23 * 3 % 2); a %= 3 / a + 3; d = 4 + d + 4; d += 1 * a;

Consider the following statements: byte i = 100; long k = i * 3 + 4; double d = i * k / 2; 34

When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules: 1. If one of the operands is double, the other is converted into double. 2. Otherwise, if one of the operands is float, the other is converted into float. 3. Otherwise, if one of the operands is long, the other is converted into long. 4. Otherwise, both operands are converted into int. 35

Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated) 36

char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter = '\u0041'; (Unicode) char numChar = '\u0034'; (Unicode) 37 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);

38 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

39 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

40 int i = ' a ' ; // Same as int i = (int) ' a ' ; char c = 97; // Same as char c = (char)97;

If you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) in Table 2.5 that can be used to compare two values. The result of the comparison is a Boolean value: true or false. boolean b = (1 > 2); 41

42 Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to

43 Operator Name ! not && and || or ^ exclusive or

44

45

46

47

 Appropriate Comments  Naming Conventions  Proper Indentation and Spacing Lines  Block Styles 48

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses. Include your name, class section, instructor, date, and a brief description at the beginning of the program. 49

 Choose meaningful and descriptive 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. 50

 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 51

 Indentation Indent two spaces.  Spacing Use blank line to separate segments of the code. 52

Use end-of-line style for braces. 53

 Write a program that reads a Fahrenheit degree in double from an input dialog box, then converts it to Celsius and displays the result in a message dialog box. The formula for the conversion is as follows: celsius = (5/9) * (fahrenheit – 32) 54

 Write a program that reads a number in feet, converts it to meters, and displays the result. One foot is meters. 55