Chapter 3. Selections. Program Listings

Similar documents
Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 4. Mathematical Functions, Characters, and Strings. Program Listings

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

Computer Programming, I. Laboratory Manual. Experiment #4. Mathematical Functions & Characters

Selections. CSE 114, Computer Science 1 Stony Brook University

Chapter 3 Selection Statements

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

Building Java Programs

Building Java Programs

Returns & if/else. Parameters and Objects

Lecture 1 Java SE Programming

Building Java Programs

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Python - Week 3. Mohammad Shokoohi-Yekta

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Assignment 2.4: Loops

Building Java Programs Chapter 4

Midterm Examination (MTA)

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

AP Computer Science. if/else, return values. Copyright 2010 by Pearson Education

Topic 13 procedural design and Strings

Eng. Mohammed S. Abdualal

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Loops. EECS1022: Programming for Mobile Computing Winter 2018 CHEN-WEI WANG

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

CS110 Programming Language I. Lab 3: Java basics II (model answer) Dr. Hadil Shaiba

Motivation of Loops. Loops. The for Loop (1) Learning Outcomes

Loops. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

EXCEPTION HANDLING. // code that may throw an exception } catch (ExceptionType parametername) {

Chapter 4 Mathematical Functions, Characters, and Strings

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Selection Statements. Pseudocode

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Lecture 2: Intro to Java

Chapter 2. Elementary Programming. Program Listings

Introduction to Computer Science Unit 3. Programs

Topic 11 Scanner object, conditional execution

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

2.3 Flow Control. Flow-Of-Control. If-Else: Leap Year. If-Else

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

2.2 - Making Decisions

Introduction to Computer Science, Winter Term Lab Exercise 6 Discussion:

CSIS 10A Assignment 3 Due: 2/21 at midnight

Values in 2 s Complement

Chapter 6 Arrays. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Java Coding 2. Decisions, decisions!

AP CS A Exam Review Answer Section

Lab Exercise 1. Objectives: Part 1. Introduction

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Chapter 10. Object-Oriented Thinking

CS1150 Principles of Computer Science Arrays

Topic 11 Scanner object, conditional execution

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Conditional Programming

Introduction to Computer Science Exercises for Unit 4A: Classes and Objects 1. This compiles and runs. What is displayed?

Building Java Programs

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. and Java. Chapter 2 Primitive Data Types and Operations

3chapter C ONTROL S TATEMENTS. Objectives

Chapter 8 Multidimensional Arrays

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

COMP Flow of Control: Branching 2. Yi Hong May 19, 2015

CONDITIONAL EXECUTION

CS141 Programming Assignment #8

The correct syntax for the expression is ((x <= 10) && (x >= 1)).

Building Java Programs

In this lab, you will learn more about selection statements. You will get familiar to

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Arrays OBJECTIVES. In this chapter you will learn:

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Chapter 4: Control Structures I

Write a program which converts all lowercase letter in a sentence to uppercase.

Mr. Fahrenbacher AP Computer Science Unit 4 - Strings. List of Java Concepts and Vocabulary From This Unit

RANDOM NUMBER GAME PROJECT

Key Points. COSC 123 Computer Creativity. Java Decisions and Loops. Making Decisions Performing Comparisons. Making Decisions

Task #1 The if Statement, Comparing Strings, and Flags

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

Programming with Java

Built-in data types. logical AND logical OR logical NOT &&! public static void main(string [] args)

Date: Dr. Essam Halim

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Basic Control Structures

Elementary Programming

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Example. Write a program which sums two random integers and lets the user repeatedly enter a new answer until it is correct.

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Built-in data types. public static void main(string [] args) logical AND logical OR logical NOT &&! Fundamentals of Computer Science

Flow of Control Branching 2. Cheng, Wei COMP May 19, Title

Chapter 2 ELEMENTARY PROGRAMMING

Transcription:

Chapter 3 Selections Program Listings

Contents Listing 3.1 Addition Quiz... 3 Listing 3.2 Simple If Demo... 4 Listing 3.3 Subtraction Quiz... 5 Listing 3.4 Compute And Interpret BMI... 6 Listing 3.5 Compute Tax... 7 Listing 3.6 Test Boolean Operators... 9 Listing 3.7 Leap Year... 10 Listing 3.8 Lottery... 11 Listing 3.9 Chinese Zodiac... 13

Listing 3.1 Addition Quiz public class AdditionQuiz //******************************************* // main() //******************************************* int number1 = (int) (System.currentTimeMillis() % 10); int number2 = (int) (System.currentTimeMillis() / 7 % 10); System.out.print("What is " + number1 + " + " + number2 + "?"); int answer = input.nextint(); System.out.println(number1 + " + " + number2 + " = " + answer + " is " + (number1 + number2 == answer));

Listing 3.2 Simple If Demo public class SimpleIfDemo //******************************************* // main() //******************************************* System.out.println("Enter an integer: "); int number = input.nextint(); if (number % 5 == 0) System.out.println("HiFive"); if (number %2 == 0) System.out.println("HiEven");

Listing 3.3 Subtraction Quiz public class SubtractionQuiz // Generate two random numbers, single digit int number1 = (int) (Math.random() * 10); int number2 = (int) (Math.random() * 10); // if number1 is less than number2 then swap the two numbers if (number1 < number2) int temp = number1; number1 = number2; number2= temp; System.out.print("What is " + number1 + " - " + number2 + "? "); int answer = input.nextint(); if (number1 - number2 == answer) System.out.println("You are correct!"); else System.out.println("Your answer is wrong."); System.out.println(number1 + " - " + number2 + " should be " + (number1 - number2));

Listing 3.4 Compute And Interpret BMI public class ComputeAndInterpretBMI // 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; final double METERS_PER_INCH = 0.0254; // COmpute BMI double weightinkilograms = weight * KILOGRAMS_PER_POUND; double heightinmeters = height * METERS_PER_INCH; double bmi = weightinkilograms / (heightinmeters * heightinmeters); // Display Results 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");

Listing 3.5 Compute Tax public class CompueTax // Prompt the user for the filing status System.out.print("0-single filer, 1-married jointly or " + "qualifying widow(er), 2-married separately, 3-head of household)"); System.out.print("Enter the filing status: "); int status = input.nextint(); // Prompt the user to enter taxable income System.out.print("Enter the taxable income: "); double income = input.nextdouble(); // Compute tax double tax = 0.0; if (status== 0) if (income <= 8350.0) tax = income * 0.10; else if (income <= 33950.0) tax = 8350.0 * 0.10 + (income - 8350.0) * 0.15; else if (income <= 82250.0) tax = 8350.0 * 0.10 + (33950.0-8350.0) * 0.15 + (income - 33950.0) * 0.25; else if (income <= 171550.0) tax = 8350.0 * 0.10 + (33950.0-8350.0) * 0.15 + (82250.0-33950.0) * 0.25 + (income - 82250.0) * 0.28; else if (income <= 372950.0) tax = 8350.0 * 0.10 + (33950.0-8350.0) * 0.15 + (82250.0-33950.0) * 0.25 + (171550.0-82250.0) * 0.28 + (income - 171550.0) * 0.33; else tax = 8350.0 * 0.10 + (33950.0-8350.0) * 0.15 + (82250.0-33950.0) * 0.25 + (171550.0-82250.0) * 0.28 +

(372950.0-171550.0) * 0.33 + (income - 372950) *0.35; else if (status == 1) // Left as an exercise else if (status == 2) else if (status == 3) else System.out.println("Error: Invalid status"); System.exit(1); // Display the result System.out.println("Tax is " + (int) (tax * 100.0) / 100.0);

Listing 3.6 Test Boolean Operators public class TestBooleanOperators System.out.print("Enter an integer: "); int number = input.nextint(); // And if (number % 2== 0 && number % 3 == 0) System.out.println(number + " is divisble by 2 and 3."); // Or if (number % 2 == 0 number % 3 == 0) System.out.println(number + " is divisble by 2 or 3."); // Exclusive Or if (number % 2 == 0 ^ number % 3 == 0) System.out.println(number + " is divisble by 2 or 3, but not both");

Listing 3.7 Leap Year public class LeapYear System.out.print("Enter a year: "); int year = input.nextint(); // Check if the year is a leap year boolean isleapyear = (year % 4 == 0 && year % 100!= 0) (year % 400 == 0); // Display the Result System.out.println(year + " is a leap year? " + isleapyear);

Listing 3.8 Lottery public class Lottery // Generate a lottery number int lottery = (int)(math.random() * 100); // Prompt the user to enter a guess System.out.print("Enter your lottery pick (two digits): "); int guess = input.nextint(); // Get digits from lottery int lotterydigit1 = lottery / 10; int lotterydigit2 = lottery % 10; // Get digits from guess int guessdigit1 = guess / 10; int guessdigit2 = guess % 10; System.out.println("The lottery number is " + lottery); // Check the guess if (guess == lottery) System.out.println("Exact match: you win $10,000."); else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2) System.out.println("Match all digits: you win $3,000."); else if (guessdigit1 == lotterydigit1 guessdigit1 == lotterydigit2 guessdigit2 == lotterydigit1 guessdigit2 == lotterydigit2) System.out.println("Match one digit: you win $1,000.");

else System.out.println("Sorry, no match");

Listing 3.9 Chinese Zodiac public class ChineseZodiac System.out.print("Enter a year: "); int year = input.nextint(); switch (year % 12) case 0: System.out.println("mankey"); case 1: System.out.println("rooster"); case 2: System.out.println("dog"); case 3: System.out.println("pig"); case 4: System.out.println("rat"); case 5: System.out.println("ox"); case 6: System.out.println("tiger"); case 7: System.out.println("rabit"); case 8: System.out.println("dragon"); case 9: System.out.println("snake"); case 10: System.out.println("horse"); case 11: System.out.println("sheep");