Chapter 2 Introduction to Java Programming PDF

Summary

This document is an introduction to Java programming. It covers fundamental concepts and examples, useful to beginner-level students. It details the different types of variables, operators, and control statements.

Full Transcript

Chapter 2 Introduction to Java Programming ©1992-2012 by Pearson Education, Inc. All Rights Reserved. :In Chapter 1, we have learned The different types of programming languages Basic object-technology concept...

Chapter 2 Introduction to Java Programming ©1992-2012 by Pearson Education, Inc. All Rights Reserved. :In Chapter 1, we have learned The different types of programming languages Basic object-technology concepts A typical Java program-development environment To write simple Java application To use different output statements ©1992-2012 by Pearson Education, Inc. All Rights Reserved. :Chapter 2 Objectives In this chapter, you will learn: ⮚ To use different input statements ⮚ Basic memory concepts (variables) ⮚ To use arithmetic operators ⮚ To use compound assignment operator ⮚ To use Increment and Decrement ⮚ Identify Java’s primitive data type ⮚ Logical Operators ⮚ Selection Statements (if, if.. Else, swich) ⮚ Repetition Statements (for, while, do.. While, break, continue) ©1992-2012 by Pearson Education, Inc. All Rights Reserved. :Chapter 2 Content Input Statements Memory Concepts Arithmetic Operators Decision Making: Equality and Relational Operators Control Structure: 1. Selection Statements: If – Single Selection Statement If.. Else Double Selection Statement Switch Multiple Selection Statement 2. Repetition Statements (Looping Statements): for, while Do … while Break and continue ©1992-2012 by Pearson Education, Inc. All Rights Reserved. :Chapter 2 Content (Cont) Compound Assignment Operators Increment and Decrement Operators Primitive Types Logical Operators ©1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements Integers Whole numbers, like –22, 7, 0 and 1024 Programs remember numbers and other data in the computer’s memory and access that data through program elements called variables. The program of Fig. 2.7 demonstrates these concepts. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) import declaration Helps the compiler locate a class that is used in this program. Rich set of predefined classes that you can reuse rather than “reinventing the wheel.” Classes are grouped into packages—named groups of related classes—and are collectively referred to as the Java class library, or the Java Application Programming Interface (Java API). You use import declarations to identify the predefined classes used in a Java program. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Variable declaration statement Scanner input = new Scanner( System.in ); Specifies the name (input) and type (Scanner) of a variable that is used in this program. Variable A location in the computer’s memory where a value can be stored for use later in a program. Must be declared with a name and a type before they can be used. A variable’s name enables the program to access the value of the variable in memory. The name can be any valid identifier. A variable’s type specifies what kind of information is stored at that location in memory. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Scanner Enables a program to read data for use in a program. Data can come from many sources, such as the user at the keyboard or a file on disk. Before using a Scanner, you must create it and specify the source of the data. The equals sign (=) in a declaration indicates that the variable should be initialized (i.e., prepared for use in the program) with the result of the expression to the right of the equals sign. The new keyword creates an object. Standard input object, System.in, enables applications to read bytes of information typed by the user. Scanner object translates these bytes into types that can be used in a program. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Variable declaration statements int number1; // first number to add int number2; // second number to add int sum; // sum of number1 and number2 declare that variables number1, number2 and sum hold data of type int They can hold integer. Range of values for an int is –2,147,483,648 to +2,147,483,647. Actual int values may not contain commas. Several variables of the same type may be declared in one declaration with the variable names separated by commas. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Prompt Output statement that directs the user to take a specific action. System is a class. Part of package java.lang. Class System is not imported with an import declaration at the beginning of the program. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Scanner method nextInt number1 = input.nextInt(); // read first number from user Obtains an integer from the user at the keyboard. Program waits for the user to type the number and press the Enter key to submit the number to the program. The result of the call to method nextInt is placed in variable number1 by using the assignment operator, =. “number1 gets the value of input.nextInt().” Operator = is called a binary operator—it has two operands. Everything to the right of the assignment operator, =, is always evaluated before the assignment is performed. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Arithmetic sum = number1 + number2; // add numbers Assignment statement that calculates the sum of the variables number1 and number2 then assigns the result to variable sum by using the assignment operator, =. “sum gets the value of number1 + number2.” In general, calculations are performed in assignment statements. Portions of statements that contain calculations are called expressions. An expression is any portion of a statement that has a value associated with it. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Integer formatted output System.out.printf( "Sum is %d\n", sum ); Format specifier %d is a placeholder for an int value The letter d stands for “decimal integer.” © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Input Statements (Cont.) Method Description nextBoolean() Reads a Boolean value from the user nextByte() Reads a Byte value from the user nextDouble() Reads a Double value from the user nextFloat() Reads a Float value from the user nextInt() Reads a Int value from the user nextLine() Reads a Line value from the user nextLong() Reads a Long value from the user nextShort() Reads a Short value from the user Memory Concepts Variables Every variable has a name, a type, a size (in bytes) and a value. When a new value is placed into a variable, the new value replaces the previous value (if any) The previous value is lost. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Arithmetic Operators Arithmetic operators are summarized in Fig. 2.11. The asterisk (*) indicates multiplication The percent sign (%) is the remainder operator The arithmetic operators are binary operators because they each operate on two operands. Integer division yields an integer quotient. Any fractional part in integer division is simply discarded (i.e., truncated)—no rounding occurs. The remainder operator, %, yields the remainder after division. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. Decision Making: Equality and Relational Operators Condition An expression that can be true or false. if selection statement Allows a program to make a decision based on a condition’s value. Equality operators (== and !=) Relational operators (>, = and = 60 ) System.out.println( "Passed" ); If the condition is false, the Print statement is ignored, and the next pseudocode statement in order is performed. Indentation Optional, but recommended Emphasizes the inherent structure of structured programs © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. if…else Double-Selection Statement if…else double-selection statement—specify an action to perform when the condition is true and a different action when the condition is false. The If…Else statement in Java: if ( grade >= 60 ) System.out.println( "Passed" ); else System.out.println( "Failed" ); Note that the body of the else is also indented. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. if…else Double-Selection Statement (Cont.) Can test multiple cases by placing if…else statements inside other if…else statements to create nested if…else statements. This may be written in Java as if ( studentGrade >= 90 ) System.out.println( "A" ); else if ( studentGrade >= 80 ) System.out.println( "B" ); else if ( studentGrade >= 70 ) System.out.println( "C" ); else if ( studentGrade >= 60 ) System.out.println( "D" ); else System.out.println( "F" ); If studentGrade >= 90, the first four conditions will be true, but only the statement in the if part of the first if…else statement will execute. After that, the else part of the “outermost” if…else statement is skipped. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. if…else Double-Selection Statement (Cont.) Most Java programmers prefer to write the preceding nested if…else statement as if ( studentGrade >= 90 ) System.out.println( "A" ); else if ( studentGrade >= 80 ) System.out.println( "B" ); else if ( studentGrade >= 70 ) System.out.println( "C" ); else if ( studentGrade >= 60 ) System.out.println( "D" ); else System.out.println( "F" ); The two forms are identical except for the spacing and indentation, which the compiler ignores. © Copyright 1992-2012 by Pearson Education, Inc. All Rights Reserved. if…else Double-Selection Statement (Cont.) The Java compiler always associates an else with the immediately preceding if unless told to do otherwise by the placement of braces ({ and }). Referred to as the dangling-else problem. The following code is not what it appears: if ( x > 5 ) if ( y > 5 ) System.out.println( "x and y are > 5" ); else System.out.println( "x is 5" ); else System.out.println( "x is 5" ); } else System.out.println( "x is

Use Quizgecko on...
Browser
Browser