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.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
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.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
CMT Programming Software Applications
Hello, world! Dissect HelloWorld.java Compile it Run it.
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.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
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™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Android How to Program, 2/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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,
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
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 Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
C++ Programming: Basic Elements of C++.
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.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java basics. Task  Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC Programming I Lecture 6 September 4, 2002.
1 Java basics Chapter 2 Spring 2005 CS 101 Aaron Bloomfield.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
1 Java basics Chapter 2 Spring 2005 CS 101 Aaron Bloomfield.
Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple things need determining Without the ability to remember.
By Mr. Muhammad Pervez Akhtar
Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,
1 Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1-3 Stop me if you want me to go over.
1 Java basics Chapter 2 Slides still stolen (and in a very exciting format!) Trey Kirk.
COP 2551 Introduction to Object Oriented Programming with Java Topics –Introduction to the Java language –Code Commenting –Java Program Structure –Identifiers.
1 Review for exam 1 CS 101-E Aaron Bloomfield. 2 Announcements Exam this Wed Exam this Wed In CHM 402 (NOT in Clark G004) In CHM 402 (NOT in Clark G004)
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.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
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: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Review for exam 1 CS 101 Aaron Bloomfield. 2 Today’s lecture An overview of the “review” sections of chapters 1- 3 and 5 Stop me if you want me to go.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Java basics Chapter 2 Fall 2006 CS 101 Aaron Bloomfield.
Java basics.
CompSci 230 S Programming Techniques
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Introduction to C++ Programming
Completing the Problem-Solving Process
Yanal Alahmad Java Workshop Yanal Alahmad
Basic Elements of C++.
Java basics – part 3.
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Java basics.
Chapter 2: Basic Elements of Java
Review for Exam 1 Spring 2007 CS 101/CS 101-E.
Presentation transcript:

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 do it yourself

Software Program –Sequence of instruction that tells a computer what to do Execution –Performing the instruction sequence Programming language –Language for writing instructions to a computer Major flavors –Machine language or object code –Assembly language –High-level Program to which computer can respond directly. Each instruction is a binary code that corresponds to a native instruction

Software Program –Sequence of instruction that tells a computer what to do Execution –Performing the instruction sequence Programming language –Language for writing instructions to a computer Major flavors –Machine language or object code –Assembly language –High-level Symbolic language for coding machine language instructions

Software Program –Sequence of instruction that tells a computer what to do Execution –Performing the instruction sequence Programming language –Language for writing instructions to a computer Major flavors –Machine language or object code –Assembly language –High-level Detailed knowledge of the machine is not required. Uses a vocabulary and structure closer to the problem being solved

Software Program –Sequence of instruction that tells a computer what to do Execution –Performing the instruction sequence Programming language –Language for writing instructions to a computer Major flavors –Machine language or object code –Assembly language –High-level Java is a high-level programming language

Software Program –Sequence of instruction that tells a computer what to do Execution –Performing the instruction sequence Programming language –Language for writing instructions to a computer Major flavors –Machine language or object code –Assembly language –High-level For program to be executed it must be translated

Translation Translator –Accepts a program written in a source language and translates it to a program in a target language Compiler –Standard name for a translator whose source language is a high-level language Interpreter –A translator that both translates and executes a source program

Java translation Two-step process First step –Translation from Java to bytecodes Bytecodes are architecturally neutral object code Bytecodes are stored in a file with extension.class Second step –An interpreter translates the bytecodes into machine instructions and executes them Interpreter is known a Java Virtual Machine or JVM

Task Display the supposed forecast I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.

Sample output

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); }

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Three statements make up the action of method main() Method main() is part of class DisplayForecast

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } A method is a named piece of code that performs some action or implements a behavior

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } An application program is required to have a public static void method named main().

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } public, static, and void are keywords. They cannot be used as names public means the method is shareable

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Consider static and void later

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Java allows a statement to be made up of multiple lines of text Semicolons delimit one statement from the next

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } A class defines an object form. An object can have methods and attributes Keyword class indicates a class definition follows

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The class has a name

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Programs are read by people – make sure they are readable. Use whitespace, comments, and indentation to aid understanding

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Whitespace separates program elements Whitespace between program elements is ignored by Java Whitespace

DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } // indicates rest of the line is a comment Comments are used to document authors, purpose, and program elements Three comments

Indentation // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Indentation indicates subcomponents Method main() is part of DisplayForecast Statements are part of method main()

Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Class System supplies objects that can print and read values System variable out references the standard printing object –Known as the standard output stream Variable out provides access to printing methods –print(): displays a value –println(): displays a value and moves cursor to the next line

System.out

Selection

Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Method print() and println() both take a string parameter –The parameter specifies the value that is to be used in the invocation

Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The print() statement starts the program output I think there is a world market for ░

Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The first println() statement completes the first line of output I think there is a world market for maybe five computers ░

Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The second println() statement starts and completes the second line of output I think there is a world market for maybe five computers Thomas Watson, IBM, 1943 ░

Experiments How do we do the following? –I will do good in my classes. You will do good in your classes. –I'll do good in my classes. –"You will do good in your classes," said Cohoon. –It was a dark and stormy night; the rain fell in torrents-- except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets (for it is in London that our scene lies), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.

Computation Programmers frequently write small programs for computing useful things Example – body mass index (BMI) –Measure of fitness Ratio of person’s weight to the square of the person’s height –Weight in is kilograms, height is in meters Person of interest is 4.5 feet and weighs 75.5 pounds Metric conversions –Kilograms per pound –Meters per foot

Common program elements Type –Set of values along with operators that can manipulate and create values from the set Primitive types support numeric, character, logical values –double and float Values with decimals –byte, short, int, long Integers –char Characters (considered numeric) –boolean Logical values Basic operators –+ addition- subtraction –* multiplication/ division

Common program elements Constant –Symbolic name for memory location whose value does not change KILOGRAMS_PER_POUND Variable –Symbolic name for memory location whose value can change weightInPounds

Program outline for BMI.java // Purpose: Compute BMI for given weight and height public class BMI { // main(): application entry point public static void main(String[] args) { // define constants // set up person's characteristics // convert to metric equivalents // perform bmi calculation // display result }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); }

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); } Operator evaluation depend upon its operands

public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = ; // 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 = 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 " + Math.round(bmi)); } Math.round(bmi) is 18

// Purpose: Convert a Celsius temperature to Fahrenheit public class CelsiusToFahrenheit { // main(): application entry point public static void main(String[] args) { // set Celsius temperature of interest int celsius = 28; // convert to Fahrenheit equivalent int fahrenheit = 32 + ((9 * celsius) / 5); // display result System.out.println("Celsius temperature"); System.out.println(" " + celsius); System.out.println("equals Fahrenheit temperature"); System.out.println(" " + fahrenheit); }

Interactive programs Programs that interact with their users through statements performing input and output BMI.java –Not interactive – weight and height are fixed

Support for interactive console programs Variable System.in –Associated with the standard input stream – the keyboard Class Scanner –Supports extraction of an input as a numbers, characters, and strings Scanner stdin = Scanner.create(System.in);

Accessing the standard input stream Set up Scanner stdin = Scanner.create(System.in); The method returns a reference to a new Scanner object. This object is built using out of the standard input stream

Program outline interactive programs public class MyProgram { // main(): application entry point public static void main(String[] args) { // set up scanner for input stream // prompt and extract input // perform necessary computations // display results }

Complimenter public class AgeComplimenter { public static void main(String[] args) { // set up scanner for input stream Scanner stdin = Scanner.create(System.in); // prompt and extract System.out.print("Enter your age: "); int age = stdin.nextInt(); // perform necessary computations int fauxAge = age – 5; // display results System.out.println(age + “you don’t look even “ + fauxAge + “!“); } }

Accessing the standard input stream Some other Scanner extraction possibilities nextDouble(); –Next value as double next(); –Next value as String nextLine(); –Rest of line as String

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 = Scanner.create(System.in); System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble();

Primitive variable assignment Assignment operator = –Allows the memory location for a variable to be updated Consider int numberOfRabbits = 11; numberOfRabbits = 121;

Primitive variable assignment Assignment operator = –Allows the memory location for a variable to be updated Consider int numberOfRabbits = 11; numberOfRabbits = 121;

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

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

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

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

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

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

Primitive variable assignment Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; Consider int i = 0; i = i + 1; Consider 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 ;

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);

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++); 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++); 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);

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++); 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); System.out.print(++i); System.out.println(i++); // display then update 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); System.out.print(++i); System.out.println(i++); System.out.println(i); // display

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 = * 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

Escape sequences Java provides escape sequences for printing special characters –\bbackspace –\nnewline –\ttab –\rcarriage 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