Chapter 2: Elements of Java



Similar documents
Building Java Programs

Chapter 3. Input and output. 3.1 The System class

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Introduction to Java

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Building Java Programs

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Object-Oriented Programming in Java

Introduction to Java. CS 3: Computer Programming in Java

Topic 11 Scanner object, conditional execution

Computer Programming I

Programming and Data Structures with Java and JUnit. Rick Mercer

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Chapter 2 Elementary Programming

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Lecture 1 Introduction to Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Variables, Constants, and Data Types

CS 106 Introduction to Computer Science I

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Building Java Programs

Java Basics: Data Types, Variables, and Loops

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Chapter One Introduction to Programming

JavaScript: Control Statements I

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

6.1. Example: A Tip Calculator 6-1

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Chapter 2 Introduction to Java programming

Install Java Development Kit (JDK) 1.8

Programming in Java Course Technology, a part of Cengage Learning.

Introduction to Python

Programming Languages CIS 443

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Using Files as Input/Output in Java 5.0 Applications

Moving from CS 61A Scheme to CS 61B Java

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Comp 248 Introduction to Programming

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Basics of Java Programming Input and the Scanner class

Lecture 5: Java Fundamentals III

Informatica e Sistemi in Tempo Reale

Arrays. Introduction. Chapter 7

Statements and Control Flow

Object Oriented Software Design

Week 1: Review of Java Programming Basics

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Some Scanner Class Methods

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Object Oriented Software Design

Example of a Java program

AP Computer Science Static Methods, Strings, User Input

J a v a Quiz (Unit 3, Test 0 Practice)

C++ Language Tutorial

D06 PROGRAMMING with JAVA

Chapter 8. Living with Java. 8.1 Standard Output

CS 106 Introduction to Computer Science I

Introduction to Programming

Event-Driven Programming

MAT 2170: Laboratory 3

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Java Programming Fundamentals

Pemrograman Dasar. Basic Elements Of Java

AP Computer Science Java Subset

13 File Output and Input

Building Java Programs

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Visual Logic Instructions and Assignments

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Conditionals (with solutions)

Visit us at

Introduction to Visual C++.NET Programming. Using.NET Environment

Reading Input From A File

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

PL / SQL Basics. Chapter 3

Introduction to Java Lecture Notes. Ryan Dougherty

In this Chapter you ll learn:

Iteration CHAPTER 6. Topic Summary

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Chapter 2 Basics of Scanning and Conventional Programming in Java

Outline Basic concepts of Python language

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Primitive data types in Java

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

A Grammar for the C- Programming Language (Version S16) March 12, 2016

INPUT AND OUTPUT STREAMS

Transcription:

Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction Computer program: A sequence of statements designed to accomplish a task. Programming: The process of planning and creating a program. 2 1

The Basics of a Java Program Java program: A collection of classes. There is a main method in every Java application program. Token: The smallest individual unit of a program. 3 Java Identifiers Names of things. Consists of: Letters Digits The underscore character (_) The dollar sign ($) Must begin with a letter, underscore, or the dollar sign. 4 2

Illegal Identifiers 5 Data Types A set of values together with a set of operations. 6 3

Primitive Data Types Data type: A set of values together with a set of operations 7 Values and Memory Allocation for Integral Data Types 8 4

Arithmetic Operators and Operator Precedence Five arithmetic operators: + addition - subtraction * multiplication / division % mod (modulus) Unary operator: An operator that has one operand. Binary operator: An operator that has two operands. 9 Order of Precedence * / % (same precedence) + - (same precedence) Operators in 1 have a higher precedence than operators in 2. When operators have the same level of precedence, operations are performed from left to right. 10 5

Expressions Integral expressions Floating-point or decimal expressions Mixed expressions 11 Mixed Expressions Operands of different types. Examples: 2 + 3.5 6 / 4 + 3.9 Integer operands yield an integer result; floatingpoint numbers yield floating-point results. If both types of operands are present, the result is a floating-point number. Precedence rules are followed. 12 6

Type Conversion (Casting) Used to avoid implicit type coercion. Syntax: (datatypename) expression Expression evaluated first, then type converted to: datatypename Examples: (int)(7.9 + 6.7) = 14 (int)(7.9) + (int)(6.7) = 13 13 The class String Used to manipulate strings. String: Sequence of zero or more characters. Enclosed in double quotation marks. Null or empty strings have no characters. Numeric strings consist of integers or decimal numbers. Length is the number of characters in a string. 14 7

Input Standard input stream object is System.in. Input numeric data to program. Separate by blanks, lines, or tabs. To read data: 1. Create an input stream object of the class Scanner. 2. Use the methods such as next, nextline, nextint, and nextdouble. 15 Input static Scanner console = new Scanner(System.in); Example 2-16 static Scanner console = new Scanner(System.in); int feet; int inches; Suppose the input is 23 7 feet = console.nextint(); //Line 1 inches = console.nextint(); //Line 2 16 8

Increment and Decrement Operators ++ increments the value of its operand by 1. -- decrements the value of its operand by 1. Syntax: Pre-increment: ++variable Post-increment: variable++ Pre-decrement: --variable Post-decrement: variable-- 17 Strings and the Operator + Operator + can be used to concatenate two strings, or a string and a numeric value or character. Example 2-20 String str; int num1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226"; 18 9

Strings and the Operator + Example 2-20 String str; int num1, num2; num1 = 12; num2 = 26; str = "The sum = " + num1 + num2; After this statement executes, the string assigned to str is: "The sum = 1226"; Consider the following statement: str = "The sum = " + (num1 + num2); In this statement, because of the parentheses, you first evaluate num1 + num2. Because num1 and num2 are both int variables, num1 + num2 = 12 + 26 = 38. After this statement executes, the string assigned to str is: "The sum = 38"; 19 Output Standard output object is System.out. Methods: print println Syntax: System.out.print(stringExp); System.out.println(stringExp); System.out.println(); 20 10

Commonly Used Escape Sequences 21 Packages, Classes, Methods, and the import Statement Package: A collection of related classes. Class: Consists of methods. Method: Designed to accomplish a specific task. 22 11

import Statement Used to import the components of a package into a program. Reserved word. import java.io.*; Imports the (components of the) package java.io into the program. Primitive data types and the class String: Part of the Java language. Don t need to be imported. 23 Syntax of a class: Creating a Java Application Program Syntax of the main method: 24 12

Programming Style and Form Know common syntax errors and rules. Use blanks appropriately. Use a semicolon as a statement terminator. Important to have well-documented code. Good practice to follow traditional rules for naming identifiers. 25 More on Assignment Statements variable = variable * (expression); is equivalent to: variable *= expression; Similarly, variable = variable + (expression); is equivalent to: variable += expression; 26 13

Chapter Summary Basic elements of a Java program include: The main method Reserved words Special symbols Identifiers Data types Expressions Input Output Statements 27 Chapter Summary To create a Java application, it is important to understand: Syntax rules. Semantic rules. How to manipulate strings and numbers. How to declare variables and named constants. How to receive input and display output. Good programming style and form. 28 14