Lecture Notes – Week 3 Lecture-1

Slides:



Advertisements
Similar presentations
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
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.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
Computer Programming Lab(5).
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Building Java Programs Chapter 4 Conditional Execution Copyright (c) Pearson All rights reserved.
Introduction to Information and Computer Science Computer Programming Lecture d This material (Comp4_Unit5d) was developed by Oregon Health and Science.
Programming Fundamentals I (COSC- 1336), Lecture 3 (prepared after Chapter 3 of Liang’s 2011 textbook) Stefan Andrei 10/9/20151 COSC-1336, Lecture 3.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
INF120 Basics in JAVA Programming AUBG, COS dept Lecture 05 Title: Decision/Selection Control Structures Reference: MalikFarrell, chap 1, Liang Ch 3.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 3 Selections.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. CHAPTER 3: SELECTIONS 1.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 4.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
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.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 3 Selections.
Chineses Zodiac. The Chinese Zodiac is based on a twelve year cycle, each year being represented by an animal.
Chinese Zodiac. What we are going to learn? Getting to know how to read,write and the meaning of the new words about Chinese Zodiac.
Introduction to Control Statements IT108 George Mason University.
John Hurley Spring 2011 Cal State LA CS 201 Lecture 5:
Copyright 2010 by Pearson Education The if/else statement reading: 4.1, 4.6.
Lecture 4: Introduction to Control Flow Statements and Reading User Input Michael Hsu CSULA.
Chapter 3 Selections Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved
Chapter 4 Selections © Copyright 2012 by Pearson Education, Inc. All Rights Reserved.
Group Project (Find your group members)
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Elementary Programming
Building Java Programs Chapter 4
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Building Java Programs
Data types, Expressions and assignment, Input from User
Building Java Programs Chapter 4
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Lecture Notes – Week 3 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Building Java Programs
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
Lecture Notes – Week 4 Chapter 5 (Loops).
Factoring if/else code
Building Java Programs
Lecture Notes – Week 2 Lecture-2
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
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
Chapter 3 Selections Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1.
Factoring if/else code
Building Java Programs
Obesity Trends Among U.S. Adults Between 1985 and 2010
Presentation transcript:

Lecture Notes – Week 3 Lecture-1 Chapters 3 & 4

Outline Switch statements Character and string types and operations Case study To do list

switch Statements switch (switch-expression) { The switch-expression must yield a value of char, byte, short, or int data type. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The value1, ..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch-expression. value1, ..., and valueN can be constants only. 3 3 3 3

switch Statements The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; … case valueN: statement(s)N; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. When the value in a case statement matches the value of the switch-expression, the statements starting from this case are executed until either a break statement or the end of the switch statement is reached. 4 4 4 4 4

Example Program with a Switch Statement Chinese Zodiac is based on a twelve-year cycle, with each year the cycle represented by an animal monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, or sheep, i.e., year % 12 determines the Zodiac sign. Write a program to find out the Chinese Zodiac sign for a given year. 5 5 5 5 5

Example Program with a Switch Statement import java.util.Scanner; public class ChineseZodiac { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter a year: "); int year = input.nextInt(); switch (year % 12) { case 0: System.out.println("monkey"); break; case 1: System.out.println("rooster"); break; case 2: System.out.println("dog"); break; case 3: System.out.println("pig"); break; case 4: System.out.println("rat"); break; case 5: System.out.println("ox"); break; case 6: System.out.println("tiger"); break; case 7: System.out.println("rabbit"); break; case 8: System.out.println("dragon"); break; case 9: System.out.println("snake"); break; case 10: System.out.println("horse"); break; case 11: System.out.println("sheep"); } 6 6 6 6 6 6

Case Study In this case study, we learn how to design a solution for a given problem, and how to write a program containing a multi-way if-else statement, and how to test such a program to correctly implement the designed solution. 7

Problem Specification The problem is to design, implement and test a program that prompts the user to enter a weight in pounds and a height in inches, calculate the Body Mass Index (BMI) and displays the health interpretation of the BMI. The BMI is calculated by dividing the weight in kilograms by the square of the height in meters. 8 8 8 8

Problem Specification The health interpretation of BMI for people 20 years or older is as follows: 9 9 9 9

Top-level Design Prompt the user to enter a weight and a height. Calculate the BMI. Display the health interpretation of the BMI. 10 10 10

Design Refinement (1. Prompt the user to enter a weight in pounds and heights in inches) is refined by: 1.1. Prompt the user to enter the weight in pounds 1.2. Prompt the user to the height in inches 11 11 11 11 11

Design Refinement (2. Calculate the BMI) is refined by: 2.1. Convert the weight in pounds into weight in kilograms with weight in kilograms = weight * kilograms per pound 2.2. Convert the height in inches into height in metres with height in inches = height * metres per inch 2.2. Calculate the BMI with bmi = weight in kilos /(height in metres * height in metres) 12 12 12 12 12 12

Design Refinement (3. Display the health interpretation of the BMI) is refined by: 3.1. Display the prompt of the BMI interpretation 3.2. Display the interpretation 13 13 13 13 13 13 13

Design Refinement (3.2. Display the interpretation) is refined by 3.2.1. If bmi < 18.5 then displays "Underweight” else if bmi < 25 then "Normal” else if bmi < 30 then displays "Overweight” else "Obese” 14 14 14 14 14 14 14 14

Final Design 1.1. Prompt the user to enter the weight in pounds 1.2. Prompt the user to the height in inches 2.1. Convert the weight in pounds into weight in kilograms with weight in kilograms = weight * kilograms per pound 2.2. Convert the height in inches into height in metres with height in inches = height * metres per inch 2.2. Calculate the BMI with bmi = weight in kilos /(height in metres * height in metres) 3.1. Display the prompt of the BMI interpretation 3.2.1. If bmi < 18.5 then displays "Underweight” else if bmi < 25 then "Normal” else if bmi < 30 then displays "Overweight” else "Obese” 15 15 15 15 15 15 15 15

Implementation import java.util.Scanner; public class ComputeAndInterpretBMI { public static void main(String[] args) { Scanner input = new Scanner(System.in); //Prompt the user to enter weight in pounds System.out.print("Enter weight in pounds: "); double weight = input.nextDouble(); //Prompt the user to enter height in inches System.out.print("Enter height in inches: "); double height = input.nextDouble(); final double KILOGRAMS_PER_POUND = 0.45359237; // Constant final double METERS_PER_INCH = 0.0254; // Constant //Calculate BMI double weightInKilograms = weight * KILOGRAMS_PER_POUND; double heightInMeters = height * METERS_PER_INCH; double bmi = weightInKilograms / (heightInMeters * heightInMeters); //Display the interpretation System.out.println("BMI is " + bmi); if (bmi < 18.5) System.out.println("Underweight"); else if (bmi < 25) System.out.println("Normal"); else if (bmi < 30) System.out.println("Overweight"); else System.out.println("Obese"); } 16 16 16 16 16 16 16 16 16

To Do List Before Next Lecture Selectively read those sections in Chapters 3 and 4 that cover the topics in this lecture. Attend your practical session in Week 3, consisting of an in-class test, a tutorial, a case study and a set of programming exercises. Glance through those sections in Chapter 5 that cover the topics in Week 3’s lecture.