Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs...

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

If you can't read please download the document

description

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

Transcript of Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs...

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, Ill have it down c.Not really d.Im 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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 persons weight double heightInFeet = 4.5; // our persons 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, Ill have it down c.Not really d.Im 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, Ill have it down c.Not really d.Im 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, Ill have it down c.Not really d.Im 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 dont 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