Introduction to Java Java Translation Program Structure

Slides:



Advertisements
Similar presentations
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.
Advertisements

Constants and Data Types Constants Data Types Reading for this class: L&L,
The Java Programming Language
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
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 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Outline Java program structure Basic program elements
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L8: More Conditional Statements February 7, 2007 ECE 122 Engineering Problem Solving with Java Lecture 8 More Conditional Statements.
Logical Operators and Conditional statements
Chapter 1 Introduction. © 2004 Pearson Addison-Wesley. All rights reserved1-2 Outline Computer Processing Hardware Components Networks The Java Programming.
Chapter 1 Introduction.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
String Escape Sequences
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Java Language and SW Dev’t
The Java Programming Language
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 3: Program Statements
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
1 Data Comparisons and Switch Data Comparisons Switch Reading for this class: L&L 5.3,
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 1: Introduction Java Programming Language How the Java Virtual Machine Works (compiling, etc…) Update by: Dan Fleck Coming up: The Java Programming.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
1 Lecture 2 b declaration and use of variables b expressions and operator precedence b introduction to objects b class libraries b flow of control b decision-making.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
CPS120: Introduction to Computer Science Variables and Constants.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Java Review Outline Java Primitives, Program Structure Operators, Control Flow, Loops Classes and Objects Arrays and ArrayList Most of these slides are.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Working with Java.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Chapter 3: Program Statements
Chapter 1: Computer Systems
Chapter 3: Program Statements
CS139 October 11, 2004.
Chap 2. Identifiers, Keywords, and Types
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

Introduction to Java Java Translation Program Structure Classes and Objects Primitive data types Flow of Control Loops

Java Translation The Java compiler translates Java source code into a special representation called bytecode in the .class file Java bytecode is not the machine language for any specific CPU Another software tool, called an interpreter (in our case the Java Virtual Machine), executes the bytecode Java is considered to be architecture-neutral The Java compiler is not tied to any particular machine The JVM can be implemented on any machine

Java Program Structure In the Java programming language: A program is made up of one or more classes A class contains zero or more attributes A class contains one or more methods A method contains program statements A Java application starts with a class containing a method called main See Lincoln.java (page 28)

Java Program Structure // comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere

Java Program Structure // comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body

Objects An object has: state - descriptive characteristics behaviors - what it can do (or what can be done to it) The state of a bank account includes its balance The behaviors associated with a bank account include the ability to get the balance, make deposits, and make withdrawals Note that the behavior of an object might change its state, e.g. making a deposit will increase the balance

Classes An object is defined by a class representing a concept A class is the blueprint for each instance of an object Multiple objects can be created from the same class A class has attributes that define the state of each object A class has methods that define the behavior of the object The class that contains the main method represents the starting point for a Java program The program can and usually does contain more classes than just the one that contains the main method

Objects and Classes A Class (The Concept) Three objects (Three Instances of the Concept) BankAccount - balance: float + getBalance(): float + deposit(float amount): bool + withdraw(float amount): bool John’s Bank Account Balance: $5,257.51 Bill’s Bank Account Balance: $1,245,069.89 Multiple objects of the same class Mary’s Bank Account Balance: $16,833.27

Primitive Data There are eight primitive data types in Java Four of them represent integers: byte, short, int, long Two of them represent floating point numbers: float, double One of them represents characters: char And one of them represents boolean values: boolean

Characters A char variable stores a single character Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold a sequence of multiple characters

Boolean A boolean value represents a true or false condition The reserved words true and false are the only valid values for a boolean type boolean done = false; A boolean variable can represent any two states such as a light bulb being on or off boolean isOn = true;

Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming statements allow us to: decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions) that evaluate to true or false The order of statement execution is called the flow of control

Conditional Statements A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The Java conditional statements are the: if statement if-else statement switch statement

The if Statement The if statement has the following syntax: The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped.

The if-else Statement An else clause can be added to an if statement to make an if-else statement If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both See Wages.java (page 211) if ( condition ) statement1; else statement2;

Block Statements Several statements can be grouped into a block statement delimited by braces A block statement can be used wherever a statement is called for in the Java syntax if (total > MAX) { System.out.println ("Error!!"); errorCount++; } Now the increment will only occur when the if condition is true

The switch Statement The general syntax of a switch statement is: and case are reserved words switch ( expression ) { case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } If expression matches value2, control jumps to here

Comparing Characters Therefore, if we want to base a decision in our program on whether a character is a digit or not, we can use the following code: if (character >= ‘0’ && character <= ‘9’) System.out.println (“Yes, it’s a digit!”); We can also check for a valid upper case alphabetic character as follows: if (character >= ‘A’ && character <= ‘Z’) System.out.println (“It’s a capital letter!”);

Comparing Strings Remember that in Java a string is an object We cannot use the == operator to determine if the values of two strings are identical (character by character) The equals method can be called with strings to determine if two strings contain exactly the same characters in the same order The equals method returns a boolean result if (name1.equals(name2)) System.out.println ("Same name");

Comparing Strings We cannot use the relational operators to compare strings The String class contains a method called compareTo to determine if one string comes before another A call to name1.compareTo(name2) returns zero if name1 and name2 are equal (contain the same characters) returns a negative value if name1 is less than name2 returns a positive value if name1 is greater than name2

Strings String is basically just a collection of characters. Thus, the string “Martyn” could be thought of as a 6-element array ('M', 'a', 'r', 't', 'y', 'n'). The String class allows us to manipulate these data items.

Strings in java String is a class in Java Strings are constant (values cannot be changed after they are created) Set of characters "Lisa Simpson" is a string. "A" is a string, 'A' is a character. Note the difference here. Character literals are delimited by single quotes and String literals are delimited by double quotes.

Methods on Strings The String class provides methods to – Return the character at a specific index (charAt) – Return the index of a specific character (indexOf) – Compare two strings (compareTo) – Concatenate two strings (concat) – Check the length of a string (length) – Extract a sub-string (substring) – Replace all instances of a character with another character (replace)

Repetition Statements Repetition statements allow us to execute a statement or a block of statements multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: the while loop the do loop the for loop The programmer should choose the right kind of loop for the situation

while(Boolean_Expression) The while Statement while(Boolean_Expression) Loop_Body Start Evaluate Boolean_Expression false End loop true Execute Loop_Body

The do-while Statement Start do Loop_Body while(Boolean_Expression); Execute Loop_Body true Evaluate Boolean_Expression false End loop

Initialization_Action The for Statement for( Initialization_Action ; Boolean_Expression ; Update_Action) Loop_Body Start Execute Initialization_Action Execute Update_Action Evaluate Boolean_Expression Execute Loop_Body true false End loop