Java basics part 2 Where are we Last

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

Display. Forecast. java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class Display. Forecast { // 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 Display. Forecast. 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 – b backspace – n newline – t tab – r carriage 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. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-10.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-11.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-12.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-13.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-14.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-15.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-16.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-17.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-18.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-19.jpg)
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-20.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; Operator evaluation depend upon its operands // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-21.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; Operator evaluation depend upon its operands // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-22.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; Operator evaluation depend upon its operands // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }
![public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-23.jpg)
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0. 454; final double METERS_PER_FOOT = 0. 3046; Math. round(bmi) is 18 // set up person's characteristics double weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " feet"); System. out. println("has a BMI of " + bmi); }

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 = metric. Weight / (metric. Height * metric. Height); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " 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(metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " 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 = 32 + 9/5 celsius

// Purpose: Convert a Celsius temperature to Fahrenheit public class Celsius. To. Fahrenheit { // 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/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 Celsius. To. Fahrenheit { // 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 + 1 * 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 Celsius. To. Fahrenheit { // 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 My. Program { // 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 Age. Complimenter { public static void main(String[] args) { // set Complimenter public class Age. Complimenter { public static void main(String[] args) { // set](http://slidetodoc.com/presentation_image_h2/808102d9fbf1ad394e126fce13bce8c5/image-37.jpg)
Complimenter public class Age. Complimenter { 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. next. Int(); // perform necessary computations int faux. Age = age – 5; // display results System. out. println(age + “you don’t look even “ + faux. Age + “!“); } }

Accessing the standard input stream • Some other Scanner extraction possibilities next. Double(); – Next value as double next(); – Next value as String next. Line(); – Rest of line as String
- Slides: 38