Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.

Similar presentations


Presentation on theme: "1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for."— Presentation transcript:

1 1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for this class: L&L, 2.6

2 2 Interactive Applications (CLI) An interactive program with a command line interface contains a sequence of steps to: –Prompt the user to enter input data –Read and save the user’s responses –Process the data after all input(s) are received We can prompt the user: System.out.println(“prompt text”); We can read and format user responses: type variable = scan.nextType();

3 3 Interactive Applications (CLI) Example GasMileage (Page 91) int miles; double gallons, mpg; Scanner scan = new Scanner (System.in); System.out.print ("Enter the number of miles: "); miles = scan.nextInt(); System.out.print("Enter the gallons of fuel used: "); gallons = scan.nextDouble(); // Now we have the data to solve the equation // Two variables holding data: miles, gallons // Now we do the calculation for mpg

4 Gas Mileage, cont Now have input data sitting in variables miles and gallons: int miles; // a whole number double gallons; // possibly with fraction double mpg; // ready for result Do the calculation and print result: mpg = miles/gallons; System.out.println ("Miles Per Gallon: " + mpg); 4

5 5 Project1: Calculating BMI Online, see such a calculator at http://www.nhlbi.nih.gov/guidelines/obesity/BMI/bmicalc.htm http://www.nhlbi.nih.gov/guidelines/obesity/BMI/bmicalc.htm It is only a general guide, not a perfect tool for all cases. Try it out, for example by entering 5 ft. 5 in., and 130 lb. You should see a resulting BMI of 21.6, which is in the normal range.

6 Our BMI Program We will use a simpler user interface, just “line oriented input/output” or CLI, like this: Enter feet: 5 Enter inches: 5 Enter weight: 130 BMI = 21.6, in normal range. 6

7 The formula for BMI For w = weight in pounds h = height in inches BMI = 703 w/h 2 using math conventions Example height h = 68 in., w = 150 pounds BMI = 703*150/68 2 = … But Java doesn’t allow superscripts in its expressions! How can we square the height in Java?? 7

8 The BMI formula in Java Sure, squaring a number just means multiplying it by itself, so h 2 is h*h in Java. BMI = 703*w/h*h OK? No! This will divide by h, then multiply by h! How can we force the squaring properly? Parentheses to the rescue! BMI = 703*w/(h*h); But we want to use better variable names… 8

9 BMI Calculator We could morph GasMileage.java into a simple BMI Calculator. Variables: use better names in Java int feet; double inches; double weight; Scanner scan = new Scanner(System.in); System.out.println("Enter height in feet“); feet = scan.nextInt(); System.out.println("Enter height inches"); inches = scan.nextDouble(); 9

10 The Simple BMI Program Now we have feet and inches, like 5 feet, 4.5 inches. We need total inches. 5 feet means 5*12 inches, so total inches = 5*12 + 4.5 = 64.5. In Java, we put double totalInches = feet*12 + inches; Then get weight from the user. double bmi = ? 10

11 The Simple BMI Calculator double bmi = 703*weight/(totalInches*totalInches); And finally we print this out. 11

12 Simple BMI Calculator The program will run like this: Enter height in feet: 5 Enter height inches: 5 Enter weight: 130 BMI = 21.6 But our plan was to print out: BMI = 21.6, in normal range. 12

13 Making decisions in Java For the planned BMI calculator, we need to print out “in normal range” in some cases, “overweight” in others, etc., based on the calculated BMI value. In other words, we need to know how to make decisions in our programs. 13

14 14 Control Flow Up until now, all of our programs just ran sequentially through a sequence of steps Each statement did something and then continued to the next statement in sequence To make decisions, we need to control the flow of the execution of statements in our program We will see how to do that in the next lecture

15 15 Factoring our Program But before we do that, let’s see how to divide our program into smaller parts This is called factoring the program If we think about it, we can envision two different things that our program has to do –Gather input from the user and display results –Calculate the results based on the formulas At a top level, we can create one class for each of those two parts of the problem

16 16 Factoring our Program Why would we want to break our program down into two parts or classes? There are many possible reasons, two are: –We may assign two programmers to the job Each programmer can write one of the classes –We may be able to reuse one part separately from the other part, e.g. use the calculation class with a CLI class initially and re-use it later with a GUI class to make it more “user friendly”

17 17 Factoring our Program Proposed “class diagram” for our program: BmiCLI BmiSolver + main(String [ ]): void A dotted arrow means that the BmiCLI class “depends on” the BmiSolver class Remember that one of our classes must have a main method + getResult (totalInches : double, weight : double) : String

18 Factoring our Program Note that the method names in both classes are underlined in the class diagram That means that they are specified to be static (the only kind of method we know about so far) We call the BmiSolver method with the class name and pass the specified parameters to the method by listing them inside the ( ) The getResult method returns a String to be printe d 18

19 19 The BmiCLI Class Gets input numbers from the user Passes height, weight numbers to the BmiSolver class Gets back a String from the BmiSolver class –For example, “BMI = 21.6, in normal range” Prints out the String for the user to see. // Get height feet and inches from user // Calculate totalInches // Get weight from user // pass totalInches and weight to getResult // and print out the String returned by getResult System.out.println( BmiSolver.getResult(totalInches,weight));

20 20 The BMISolver Class A method to provide the result as a String public static String getResult(double height, double weight) { String result = null; // Check for zero or negative incoming values // Calculate BMI value // Use if-else to choose the correct String // that describes the BMI. // Use string concatenation to create a // solution String that can be returned // to the BmiCLI class....  Your Java statements go here return result; }


Download ppt "1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for."

Similar presentations


Ads by Google