Factoring if/else code

Slides:



Advertisements
Similar presentations
The Green Team is the Biggest Loser! By Student A Student B Student C Student D.
Advertisements

BUILDING JAVA PROGRAMS CHAPTER 4 Conditional Execution.
1 BUILDING JAVA PROGRAMS CHAPTER 4 CONDITIONAL EXECUTION.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: Scanner ; if/else reading: , 4.2, 4.6.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 4: Conditional Execution.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 3: Parameters, Return, and Interactive Programs with Scanner.
Body Mass Index and Calorie. Body Mass Index Body Mass Index, or BMI, is a tool that helps you measure the amount of body fat you have based on your height.
Body Mass Index. What is Body Mass Index(BMI)? measurement of body fat based on height and weight.
Daily Trivia This little girl is sick. And sad .
Body Mass Index (BMI) This is the most commonly used index of over or underweight Body Mass Index = body weight ( Height) squared ClassificationBMI.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Topic 13 procedural design and Strings Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Conditionals (Cont’d). 2 Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program.
WHAT IS BMI? BMI BODY MASS INDEX- BASED ON HEIGHT AND WEIGHT TO DETERMINE AMOUNT OF FAT AN INDIVIDUAL HAS OBESE BMI > 30.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Copyright 2008 by Pearson Education Building Java Programs Chapter 4 Lecture 4-1: if and if/else Statements reading: 4.2 self-check: #4-5, 7, 10, 11 exercises:
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
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.
How to Calculate Your Body Mass Index (BMI)
MATH AND HEALTHCARE PROFESSION AS A MEDICAL ASSISTANT MM Konette Davis
Body Mass Index – BMI Going for the 3 Increases: Increase in Health, Increase in Happiness & Increase in Energy Strategies for Success in Weight Management.
Copyright 2010 by Pearson Education Building Java Programs Scanner ; if / else; while loops ; random reading: 3.3 – 3.4, 4.1, 4.5, 5.1, 5.6.
How fit are you? Learning Objectives: * To consider different ways of measuring fitness. * To collect measurements and calculate your own BMI. * To use.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
How to Calculate Your Body Mass Index (BMI)
Building Java Programs
Building Java Programs
Lecture 7: Input and Miscellaneous
Conditional Execution
Building Java Programs Chapter 4
Factoring if/else code
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Autumn 2016 Lecture 12: Advanced if/else; Cumulative sum
Weightproblems by teenagers
Lecture 4: Conditionals
Topic 13 procedural design and Strings
CSc 110, Spring 2017 Lecture 8: input; if/else
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs Chapter 4
Building Java Programs
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 17: while Loops and decomposition
Building Java Programs
Self study.
Building Java Programs
Lecture 7: Methods AP Computer Science Principles
Lecture Notes – Week 3 Lecture-1
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 – 3.4, 4.1, 4.5
Factoring if/else code
Building Java Programs
Building Java Programs
Obesity Trends Among U.S. Adults Between 1985 and 2010
Presentation transcript:

Factoring if/else code factoring: Extracting common/redundant code. Can reduce or eliminate redundancy from if/else code. Example: if (a == 1) { System.out.println(a); x = 3; b = b + x; } else if (a == 2) { x = 6; y = y + 10; } else { // a == 3 x = 9; } System.out.println(a); x = 3 * a; if (a == 2) { y = y + 10; } b = b + x;

Nested if/else question Formula for body mass index (BMI): Write a program that produces output like the following: This program reads data for two people and computes their body mass index (BMI). Enter next person's information: height (in inches)? 70.0 weight (in pounds)? 194.25 height (in inches)? 62.5 weight (in pounds)? 130.5 Person 1 BMI = 27.868928571428572 overweight Person 2 BMI = 23.485824 normal Difference = 4.3831045714285715 BMI Weight class below 18.5 underweight 18.5 - 24.9 normal 25.0 - 29.9 overweight 30.0 and up obese // This program computes two people's body mass index (BMI) and // compares them. The code uses parameters, returns, and Scanner. import java.util.*; // so that I can use Scanner public class BMI { public static void main(String[] args) { System.out.println("This program reads in data for two people and"); System.out.println("computes their body mass index (BMI)"); System.out.println(); // finish me! }