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

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs Lecture 3-3: Interactive Programs w/
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Introduction to Computer Programming Decisions If/Else Booleans.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
CS 201 Functions Debzani Deb.
Begin Java having used Alice Pepper - Some slides from Alice in Action with Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Warm-Up: Monday, March 3 List all devices you can think of that can be used to input information into the computer.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
CSci 111 – computer Science I Fall 2014 Cynthia Zickos WRITING A SIMPLE PROGRAM IN JAVA.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSE 1201 Object Oriented Programming Using Classes and Objects.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Interactive Programs Programs that get input from the user 1 In PowerPoint, click on the speaker icon then click the "Play" button to hear the narration.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Introduction to programming in java Lecture 21 Arrays – Part 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Slides by Evan Gallagher
Slides by Evan Gallagher
Project 1.
Java Course Review.
CSC 1051 – Data Structures and Algorithms I
Building Java Programs
SELECTION STATEMENTS (1)
Introduction to Classes and Methods
Java Programming Function Introduction
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Outline Software Development Activities
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Java Programming Function Introduction
Methods/Functions.
Building Java Programs
Building Java Programs
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

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

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

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

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

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

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

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* = In Java, we put double totalInches = feet*12 + inches; Then get weight from the user. double bmi = ? 10

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

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

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

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