Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java basics – part 2. Where are we Last week –Java basics Simple output program Started programs as calculators –Codelab –Lab procedures This week –Types,"— Presentation transcript:

1 Java basics – part 2

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

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

4 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

5 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

6 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

7 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"

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

9 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 }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

24 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

25 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

26 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

27 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

28 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

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

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

31 // 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); }

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

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

34 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

35 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

36 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 }

37 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 + “!“); } }

38 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


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

Similar presentations


Ads by Google