20
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

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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

5

Project1: Calculating BMI

Online, see such a calculator at 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.

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

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

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

The formula for BMI

For w = weight in pounds

h = height in inches

BMI = 703 w/h2 using math conventions

Example height h = 68 in., w = 150 pounds

BMI = 703*150/682 = …

But Java doesn’t allow superscripts in its expressions!

How can we square the height in Java??7

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

The BMI formula in Java

• Sure, squaring a number just means multiplying it by itself, so h2 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

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

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

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

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

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

The Simple BMI Calculator

double bmi = 703*weight/(totalInches*totalInches);

And finally we print this out.

11

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

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

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

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

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

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

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

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

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

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”

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

17

Factoring our Program

• Proposed “class diagram” for our program:

BmiCLI

BmiSolver

+ main(String [ ]): void

A dotted arrow means that theBmiCLI class “dependson” the BmiSolver class

Remember that oneof our classes musthave a main method

+ getResult (totalInches : double, weight : double) : String

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

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 printed

18

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

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 getResultSystem.out.println( BmiSolver.getResult(totalInches,weight));

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

20

The BMISolver Class

A method to provide the result as a Stringpublic 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 herereturn result;

}