Java basics – part 3.

Slides:



Advertisements
Similar presentations
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Advertisements

1 Java basics Chapter 2 CS 101-E. 2 DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Lab session 3 and 4 Topics to be covered Escape sequences Escape sequences Variables /identifiers Variables /identifiers Constants Constants assignment.
1 Chapter 2: Elementary Programming Shahriar Hossain.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Basic Elements of C++ Chapter 2.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
1 Java basics Chapter 2 (part 1 of 2) Spring 2007 CS 101 Aaron Bloomfield.
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.
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 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Chapter 2 Elementary Programming
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
Java basics. Task  Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
1 Java basics Chapter 2 Spring 2005 CS 101 Aaron Bloomfield.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Java basics Chapter 2 Spring 2005 CS 101 Aaron Bloomfield.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Computer Programming with Java Chapter 2 Primitive Types, Assignment, and Expressions.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 Java basics Chapter 2 Slides still stolen (and in a very exciting format!) Trey Kirk.
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.
Java basics. Programming Problem solving through the use of a computer system Maxim –You cannot make a computer do something if you do not know how to.
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 Java basics Chapter 2 Fall 2006 CS 101 Aaron Bloomfield.
© 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 basics.
CompSci 230 S Programming Techniques
Chapter 02 Data and Expressions.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Unit 2 Elementary Programming
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Basic Elements of C++.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
Basic Elements of C++ Chapter 2.
Escape Sequences What if we wanted to print the quote character?
CSS 161 Fundamentals of Computing Introduction to Computers & Java
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Escape Sequences Some Java escape sequences: See Roses.java (page 68)
Chapter 2: Basic Elements of Java
Fundamentals 2.
elementary programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Chapter 2 Variables.
Instructor: Alexander Stoytchev
Presentation transcript:

Java basics – part 3

Where are we Last time Constants and variables Initialization Simple expressions Casting Interactive programs Scanner This time Assignment Operators Assign programming assignment to compute windchill Discuss new lab

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight / (metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + bmi); }

Making BMI general purpose Change double weight = 75.5; double height = 4.5; To ????

Making BMI general purpose Change double weight = 75.5; double height = 4.5; To Scanner stdin = new Scanner(System.in); System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble();

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up input acquistion Scanner stdin = new Scanner(System.in); // get person's characteristics System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble(); // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight / (metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + bmi); }

Can we display one place after the decimal? Yes using a new out method named printf().

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up input acquistion Scanner stdin = new Scanner(System.in); // get person's characteristics System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble(); // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight / (metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.printf("has a BMI of %4.1f“, bmi); }

Quick survey I am ok with Scanner and its methods Pretty much With a little review, I’ll have it down Not really I’m so lost

Primitive variable assignment Assignment operator = Allows the memory location for a variable to be updated Consider int numberOfRabbits = 11; numberOfRabbits = 121; Besides signaling in a variable definition that the variable is to be initialized, the symbol = is also the Java assignment operator. The assignment operator takes two operands. The left operand is the target variable; the right operand is the modifying expression. When evaluated, the assignment operator updates the value in the memory location associated with a target variable based on the modifying expression. The assignment operator works in the following manner. First, the left operand is evaluated to make sure that its value can be modified (e.g., a non-final variable). The right operand then is evaluated and its value is used to update the memory location associated with the left operand. For our particular assignment, the memory location associated with variable j is reset to the int value 1985. The assignment expression is read as “j gets 1985” or “j is assigned 1985.”

Primitive variable assignment Assignment operator = Allows the memory location for a variable to be updated Consider int numberOfRabbits = 11; numberOfRabbits = 121; Besides signaling in a variable definition that the variable is to be initialized, the symbol = is also the Java assignment operator. The assignment operator takes two operands. The left operand is the target variable; the right operand is the modifying expression. When evaluated, the assignment operator updates the value in the memory location associated with a target variable based on the modifying expression. The assignment operator works in the following manner. First, the left operand is evaluated to make sure that its value can be modified (e.g., a non-final variable). The right operand then is evaluated and its value is used to update the memory location associated with the left operand. For our particular assignment, the memory location associated with variable j is reset to the int value 1985. The assignment expression is read as “j gets 1985” or “j is assigned 1985.”

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

Primitive variable assignment Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

Primitive variable assignment Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

Primitive variable assignment Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

Primitive variable assignment Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

Primitive variable assignment Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

Quick survey If I wanted to assign variable n the value of variable m I would do n = m; m = n; int n = m; int m = n;

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++);

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; // define ++i; System.out.println(i); System.out.print(++i); System.out.println(i++);

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; // increment System.out.println(i); System.out.print(++i); System.out.println(i++);

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; System.out.println(i); // display System.out.print(++i); System.out.println(i++); System.out.println(i); 5

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); // update then display System.out.println(i++); 5 6

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); // update then display 5 6 6

Increment and decrement operators ++ Increments a number variable by 1 -- Decrements a numeric variable by 1 Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); System.out.println(i); // display 5 6 6 7

Question Does the following statement compute the average of double variables a, b, and c? Why double average = a + b + c / 3.0;

Expressions What is the value used to initialize expression int expression = 4 + 2 * 5; What value is displayed System.out.println(5 / 2.0); Java rules in a nutshell Each operator has a precedence level and an associativity Operators with higher precedence are done first * and / have higher precedence than + and - Associativity indicates how to handle ties When floating-point is used the result is floating point It seems that there are two possible values for expr. If the addition is performed first, then expr is initialized to 30; if instead, the multiplication is performed first, then expr is initialized to 14. N

Question Does the following statement compute the average of double variables a, b, and c? Why double average = (a + b + c) / 3.0;

Escape sequences Java provides escape sequences for printing special characters \b backspace \n newline \t tab \r carriage return \\ backslash \" double quote \' single quote

Escape sequences What do these statements output? System.out.println("Person\tHeight\tShoe size"); System.out.println("========================="); System.out.println("Hannah\t5‘1\"\t7"); System.out.println("Jenna\t5'10\"\t9"); System.out.println("JJ\t6'1\"\t14"); Output Person Height Shoe size ========================= Hannah 5‘1" 7 Jenna 5'10" 9 JJ 6'1" 14