Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,

Slides:



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

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
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.
03 Data types1June Data types CE : Fundamental Programming Techniques.
Lecture 5 Strings and more I/O COMP1681 / SE15 Introduction to Programming.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
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;
Computer Programming Lab(4).
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Computer Programming Lab(5).
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
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.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Chapter 2 Elementary Programming
Chapter 2: Java Fundamentals
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.
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.
Java Concepts Chapter 2 - Using Objects Mr. Smith AP Computer Science A.
CSC Programming I Lecture 6 September 4, 2002.
1 Java basics Chapter 2 Spring 2005 CS 101 Aaron Bloomfield.
Objects. Getting classy Your current job –Gain experience creating and manipulating objects from the standard Java types Why –Prepares you for defining.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by
Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not.
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[]
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.
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
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
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.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
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.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
1 Java basics Chapter 2 Fall 2006 CS 101 Aaron Bloomfield.
Introduction to programming in java
Java basics.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Java basics – part 3.
Data types, Expressions and assignment, Input from User
Java Programming: From Problem Analysis to Program Design, 4e
OUTPUT STATEMENTS GC 201.
Primitive Types Operators Basic input and output
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Java basics.
Chapter 2: Basic Elements of Java
elementary programming
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Presentation transcript:

Java basics – part 2

Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types, precedence, casting –Class methods –Assign programming assignment to compute windchill –Discuss new lab

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."); }

Quick survey Program DisplayForecast.java makes sense to me a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost I have done the readings a.True b.False

Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types, precedence, casting –Class methods –Assign programming assignment to compute windchill –Discuss new lab

CS has made it to prime time A key computer science question –Does P = NP? What is P –Problems solvable in polynomial time What is NP –Problems whose solutions can be checked in polynomial time

Question from last class How do we display a quotation –Answer use escape sequences Example System.out.println( “The mysterious stranger said \“Hello\"“ ). Produces The mysterious stranger said “Hello"

Escape sequences Java provides escape sequences for printing special characters –\bbackspace –\nnewline –\ttab –\rcarriage return –\\backslash –\"double quote –\'single quote

BMI.java outline // 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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); }

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 = (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); } 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 = (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); } 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 = (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); } 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 = (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); } Math.round(bmi) is 18

Quick survey Program BMI.java makes sense to me a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost I have done the readings a.True b.False

Alternative end to the program? int 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 " + bmi); Does not compile –Cannot assign a floating point value to an integer variable! Floating point values are wider than integer values

Alternative end to the program int bmi = (int) Math,round(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); Casting still necessary as Math.round() returns a long

Quick survey I understand the purpose of casting a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost

Problem Write a program to convert Fahrenheit to Celsius for a requested temperature of 28 degrees Celsius The conversion formula fahrenheit = /5 celsius

// 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 = /5 * celsius; // display result System.out.println("Celsius temperature"); System.out.println(" " + celsius); System.out.println("equals Fahrenheit temperature"); System.out.println(" " + fahrenheit); }

// 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 = * celsius; // display result System.out.println("Celsius temperature"); System.out.println(" " + celsius); System.out.println("equals Fahrenheit temperature"); System.out.println(" " + fahrenheit); }

// 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 = new Scanner(System.in);

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

Quick survey I realize I cannot use Scanner.create() even the book says so a.Pretty much b.With a little review, I’ll have it down c.Not really d.I’m so lost

Interactive program outline 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 = new Scanner(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