Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.

Slides:



Advertisements
Similar presentations
1 pritisajja.info Unlocking the World of Java Programming….. Priti Srinivas Sajja February, 2014 Visit pritisajja.info for detail Future Technology for.
Advertisements

Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
MC697 Object-Oriented Programming Using Java. In this class, we will cover: How the class will be structured Difference between object-oriented programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 Java Basics. 2 Compiling A “compiler” is a program that translates from one language to another Typically from easy-to-read to fast-to-run e.g. from.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
VB .NET Programming Fundamentals
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A Review of Programming and C++
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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 CHAPTER 1 1. WHAT IS JAVA ? Java is a programming language and computing platform first released by Sun Microsystems in The.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Introduction to Java Java Translation Program Structure
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Java-02 Basic Concepts Review concepts and examine how java handles them.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
1 2. Program Construction in Java. 01 Java basics.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
© 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.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 2 Variables.
Java Primer 1: Types, Classes and Operators
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to Programming in Java
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 2: Basic Elements of Java
In this class, we will cover:
Module 2: Understanding C# Language Fundamentals
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
In this class, we will cover:
Primitive Types and Expressions
Chapter 2 Variables.
Review of Java Fundamentals
Presentation transcript:

Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals

Chapter 2 - Java Programming Fundamentals2 Chapter 2 Topics Intro to the Java programming language Defining a Java class Declaring variables and constants Writing computational and decision- making statements Writing loops Declaring and accessing arrays

Chapter 2 - Java Programming Fundamentals3 Introducing Java Released mid 1995 by Sun Microsystems Designed to be: –A powerful, full-featured, OO development language –Easy to learn –Used on any platform –Support development of applications for networked environments

Chapter 2 - Java Programming Fundamentals4 Introducing Java Powerful –Class library Hundreds of prewritten classes Provide methods to accomplish various tasks OO –Implements OO concepts described in Ch. 1 –Encourages good software design Reduces debugging and maintenance Simplicity –Keywords Java has 48 keywords –vs. Cobol or VB which have hundreds Have special meaning in the language Used in writing statements

Chapter 2 - Java Programming Fundamentals5

6 Introducing Java Portability –Programs can be written and compiled once, then run on different platforms Important for internet applications (applets) –Achieved by using: Bytecode –Produced when a Java program is compiled Interpreter (Java Virtual Machine – JVM) –Execution environment for bytecode on each platform

Chapter 2 - Java Programming Fundamentals7

8 Introducing Java Development environments –Java Development Kit Available free from Sun Web site: java.sun.com Includes: compiler JVM and prewritten classes –Integrated Development Environments (IDEs) Provide: –Sophisticated editors –Debugging tools –Graphical development tools Examples: Sun Forte or NetBeansSun Forte NetBeans

Chapter 2 - Java Programming Fundamentals9 Building a Java Class In general, each source code file defines a class –Class HelloWorldWideWeb –File HelloWorldWideWeb.java

Chapter 2 - Java Programming Fundamentals10 Building a Java Class Class header –Describes class contained in source code file –Keywords: public –Indicates class has public availability class –Indicates line of code is a class header  public class Student Identifiers –Name of a class, method, or variable Can be any length Can include any character except a space Must begin with a letter of the alphabet, a dollar sign ($), or the underscore (_) character –Java is case sensitive Public isn’t the same as public

Chapter 2 - Java Programming Fundamentals11 Building a Java Class Block of code –Used to group statements –Delineated by open curly brace ({) and closed curly brace (}) –All code in Java is enclosed in a single block of code, which can contain additional blocks Indentation –Not required  recommended Line continuation –Can extend statements over multiple lines No continuation character required Comments –Single line // compiler ignores everything to end of line –Multi-line /* compiler ignores everything in between */ –Multi-line (documentation) /** compiler ignores everything in between */ Used for JavaDoc

Chapter 2 - Java Programming Fundamentals12 Building a Java Class Java code generally consists of: –Variable definitions –One or more methods Method header –Comments to identify method and describe some of its characteristics Argument –Information sent to a method –Contained in parentheses of method call System.out.println(“Welcome ACIS4564 students!”) Literal –Value defined within a statement Semicolon (;) –All java statements end with a semicolon

Chapter 2 - Java Programming Fundamentals13 Using Java Variables and Data Types Variable –Name of place in memory that can contain data –All variables have: Data type  kind of data variable can contain Name  identifier that refers to the variable Value  the default or specified value Declaring and Initializing Variables –Variable data type must be declared prior to initialization Eight available primitive data types –Assignment operator (=) Used to assign value to a variable –char c = ‘a’; –boolean b = true; –double d = 1.25;

Chapter 2 - Java Programming Fundamentals14

Chapter 2 - Java Programming Fundamentals15 Using Java Variables and Data Types Changing Data Types –If changing data type results in no loss of precision, can be done implicitly: int c = 5; double a, b = 3.5; a = b + c; –Casting allows data type changes explicitly with loss of precision: int a, c = 5; double b = 3.5; a = (int) b + c;

Chapter 2 - Java Programming Fundamentals16 Using Java Variables and Data Types Using Constants –Variable with a value that doesn’t change –Keyword final –Denotes value cannot change –Example: final double SALES_TAX_RATE = 4.5; Using Reference Variables –Java has two types of variables: Primitive Reference –Uses class name as a data type –Points to an instance of that class –Example: »String s = “Hello World”;

Chapter 2 - Java Programming Fundamentals17 Using Java Variables and Data Types Creating a Java Class to Demonstrate Variables –Invoking a method Send the method a message asking it to execute –Concatenation operator (+) Joins String literals and variables Automatically converts numeric and Boolean values to strings before use in println method –Example (See Figure 2-6) System.out.println(“c = ” + c);

Chapter 2 - Java Programming Fundamentals18 Computing with Java Operators –Arithmetic operators (+,-,*,/) addition, subtraction, multiplication, division Precedence using parentheses –Remainder (modulus) operator (%) Produces remainder from division of two integers 5 % 3 = ? Math Class –Methods for exponentiation, rounding, etc. –max, abs, round, max, min, sqrt, random

Chapter 2 - Java Programming Fundamentals19 Computing with Java Special Operators –For writing shortcut code Increment operator (++) –Add one to a variable e.g., i++ ; Decrement operator (--) –Subtract one from a variable e.g., i--; Assignment operator with arithmetic operators: total = total + 5; total += 5;

Chapter 2 - Java Programming Fundamentals20 Writing Decision-Making Statements Decision Making Statement –Determine whether a condition is true, and take some action based on determination Three ways to write decision-making statements: –if statement –switch statement –conditional operator

Chapter 2 - Java Programming Fundamentals21 Writing Decision-Making Statements Writing if Statements –if statement: Interrogates logical expression enclosed in parentheses Determines whether it is true or false Uses logical operators to compare values: –e.g., (studentAge < 21) –Compound expression Two expressions joined using logical operators –OR  || –AND  && –Nested if statement if statement written inside another if statement

Chapter 2 - Java Programming Fundamentals22

Chapter 2 - Java Programming Fundamentals23

Chapter 2 - Java Programming Fundamentals24

Chapter 2 - Java Programming Fundamentals25 Writing Decision-Making Statements Using the Conditional Operator –Conditional operator (?) Provides a shortcut to writing an if-else statement Structure: –variable = expression ? value1:value2; –e.g., unitWaterFee = ( usage > 30 ) ? 2 : 3; Writing switch Statements –Acts like a multiple-way if statement –Transfers control to one of several statements or blocks depending on the value of a variable –Used when there are more than two values to evaluate –Restrictions: Each case evaluates a single variable for equality only Variable being evaluated must be: char, byte, short, or int

Chapter 2 - Java Programming Fundamentals26 Writing Loops Loops –Provides for repeated execution of one or more statements until a terminating condition is reached –Three basic types: while do for

Chapter 2 - Java Programming Fundamentals27 Writing Loops

Chapter 2 - Java Programming Fundamentals28 Writing Loops Writing while Loops –Loop counter Counts number of times the loop is executed –Pre-test loop Tests terminating condition at the beginning of the loop –while (expression) { } Writing do Loops –Loop counter Counts number of times the loop is executed –Post-test loop –Tests terminating condition at the end of the loop –Forces execution of statements in the loop body at least once –do { } while (expression);

Chapter 2 - Java Programming Fundamentals29 Writing Loops Writing for Loops –Loop counter Counts number of times the loop is executed –Pre-test loop –Tests terminating condition at the beginning of the loop –Includes counter initialization and incrementing code in the statement itself for (int k = 1; k <= 5; k++) { } Writing Nested Loops –A loop within a loop –Useful for processing data arranged in rows and columns –Can be constructed using any combinations of while, do, and for loops

Chapter 2 - Java Programming Fundamentals30 Declaring and Accessing Arrays Arrays –Allows the creation of a group of variables with the same data type –Consist of elements: Each element behaves like a variable –Can be: One dimensional Multi-dimensional

Chapter 2 - Java Programming Fundamentals31 Declaring and Accessing Arrays Using One-Dimensional Arrays –Keyword new –Used to create a new array instance –int testScores[] = new int[10]; –Use brackets ([]) and indices ( starting from 0 ) to denote elements: –testScores[2] = 70;

Chapter 2 - Java Programming Fundamentals32

Chapter 2 - Java Programming Fundamentals33 Declaring and Accessing Arrays Using Multidimensional Arrays –Array of arrays Three dimensions  cube Four dimensions  ??? –Each dimension has its own set of brackets: testScoreTable[4][0] = 90;

Chapter 2 - Java Programming Fundamentals34