CS110 Programming Language I. Lab 6: Multiple branching Mechanisms

Similar documents
CS141 Programming Assignment #6

Oct Decision Structures cont d

Midterm Examination (MTA)

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

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

ITERATION WEEK 4: EXMAPLES IN CLASS

Object Oriented Programming. Java-Lecture 1

Over and Over Again GEEN163

Lectures 3-1, 3-2. Control Structures. Control Structures, Shimon Schocken IDC Herzliya, slide 1

CS141 Programming Assignment #5

Selection Statements and operators

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

Fundamentals of Programming Data Types & Methods

Chapter 3 Lab Decision Structures

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Programming with Java

H212 Introduction to Software Systems Honors

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Conditional Programming

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

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

Supplementary Test 1

Selection Statements and operators

Supplement: Case Study: Sudoku. For Introduction to Java Programming By Y. Daniel Liang

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

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

St. Edmund Preparatory High School Brooklyn, NY

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Java Classes: Random, Character A C S L E C T U R E 6

Chapter 2: Basic Elements of Java

Anatomy of a Java Program: Comments

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

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

Reading Input from Text File

Introduction to Computer Science, Shimon Schocken, IDC Herzliya. Lectures Control Structures

Chapter 5 Lab Methods

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

CS111: PROGRAMMING LANGUAGE II

2.8. Decision Making: Equality and Relational Operators

download instant at

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

CAT.woa/wa/assignments/eclipse

New York University Intro to Computer Science (CSCI-UA.101) Fall 2014 Midterm #1 Test G. Instructions:

Chapter 3. Selections

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Handout 4 Conditionals. Boolean Expressions.

AP COMPUTER SCIENCE A

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

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

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Lesson 10..The switch Statement and char

Example: Monte Carlo Simulation 1

AP Computer Science A Unit 2. Exercises

Arrays. Eng. Mohammed Abdualal

CSE 8A Lecture 12. Reading for next class: : Sounds! Today s topics: Logical Operators:! && Start PSA 6: Chromakey!

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Question: Total Points: Score:

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Darrell Bethea May 10, MTWRF 9:45-11:15 AM Sitterson 011

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

Chapter 5 Lab Methods

Selection and Repetition Revisited

Question: Total Points: Score:

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Control Structures: if and while A C S L E C T U R E 4

Ch. 6. User-Defined Methods

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Eng. Mohammed S. Abdualal

Loops. CSE 114, Computer Science 1 Stony Brook University

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

CS141 Programming Assignment #8

Chapter 4: Control Structures I

CS 231 Data Structures and Algorithms Fall Event Based Programming Lecture 06 - September 17, Prof. Zadia Codabux

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

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Introduction to Computer Science Unit 2. Exercises

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

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

CSIS 10A Assignment 4 SOLUTIONS

CS110: PROGRAMMING LANGUAGE I

Eng. Mohammed Alokshiya

Question: Total Points: Score:

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Name Section Number. CS110 Exam #1 *** PLEASE TURN OFF ALL CELLPHONES ***

COMP-202A, Fall 2007, All Sections Assignment 1

Chapter 8 Multi-Dimensional Arrays

Full file at

CSIS 10A Assignment 4 SOLUTIONS

Software and Programming 1

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

This exam is open book. Each question is worth 3 points.

Lesson 7 Part 2 Flags

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

CSC 1051 Data Structures and Algorithms I

Final Exam Practice. Partial credit will be awarded.

CCHS Math Recursion Worksheets M Heinen CS-A 12/5/2013. Recursion Worksheets Plus Page 1 of 6

Chapter 1 Lab Algorithms, Errors, and Testing

Transcription:

CS110 Programming Language I Lab 6: Multiple branching Mechanisms Computer Science Department Fall 2016

Lab Objectives: In this lab, the student will practice: Using switch as a branching mechanism Lab Exercise 1: Programming Output Question 1: a. Convert the following if statements to Switch statement: int x = 10; if( x == 10 x == 15) x += 15; else if( x == 12 ) x -= 5; else x *= 3; Solution: int x = 10; Switch( x ){ Case 10 : case 15: X+=15; Break; Case 12 : x-=5; default: x*=3; b. What would be the value of x after executing the switch statement? Solution: 25 P a g e 2

Question 2: Suppose the input is 5. What is the output of the following java code? (Assume that alpha is an int variable and input is a Scanner object initialized to the keyboard). alpha = input.nextint(); switch ( alpha ){ case 1: case 2: alpha = alpha + 2; System.out.print("2 is added to alpha"); case 4: alpha ++; System.out.print("alpha is increased \n"); case 5 : alpha = 2 * alpha; System.out.println("alpha is multiplied by 2"); case 6: alpha = alpha +5; System.out.println("5 is added to alpha"); default: alpha --; System.out.println("The value of alpha is "+ alpha); Solution: Alpha is multiplied by 2 5 is added to alpha The value of alpha is 15 P a g e 3

Lab Exercise 2: Products Problem Description A Shopping website sells four different designs of a specific t-shirt. Each design has its own name: DY: Yellow t-shirt with red sleeves for $10.98. DR: Red t-shirt with white sleeves for $12.98. DB: Blue t-shirt with white sleeves for $11.98. DP: Pink t-shirt with Yellow sleeves for $9.98. Write an application that displays the menu to the customer and gets the Design name, and the Quantity of the design which the customer would like to buy. Your program should use a switch statement to determine the menu. It also should calculates and displays the total price of the t-shirts bought by the customer. Sample output DY : Yellow t-shirt with red sleeves for $10.98. DR : Red t-shirt with white sleeves for $12.98. DB : Blue t-shirt with white sleeves for $11.98. DP : Pink t-shirt with Yellow sleeves for $9.98. Enter the Design name: DB Enter the quantity : 5 The total is $ 59.90 P a g e 4

Solution: package test2; import java.util.scanner; public class Test2 { public static void main(string[] args) { Scanner input=new Scanner (System.in); double total_price=0; System.out.println("DY: Yellow t-shirt with red sleeves for $10.98.\n" + "DR: Red t-shirt with white sleeves for $12.98.\n" + "DB: Blue t-shirt with white sleeves for $11.98. \n" + "DP: Pink t-shirt with Yellow sleeves for $9.98."); System.out.println("Enter the Design name :"); String tshirt=input.next(); System.out.println("Enter the quantity :"); int quantity=input.nextint(); switch(tshirt) { case "DY":case "dy": total_price=10.98*quantity; case "DR":case "dr": total_price=12.98*quantity; case "DB":case "db": total_price=11.98*quantity; case "DP":case "dp": total_price=9.98*quantity; default: System.out.println("invalid design.."); System.out.printf("The total is $%.2f\n",total_price); P a g e 5

Assignment Problem(s) Question 1: Solve the following problems: double Max = 12.7; if (Max >= 12) System.out.print("FRENCH"); else System.out.print("FRIES"); Can the code be converted to Switch statement? Choose one of the following answers: No. Why? the switch statement cannot use double value as an operand Yes. The code after converting is Question 2: What is the output of the following code segment: int x = 12; if (x >= 12) System.out.print("HEY\t"); System.out.print("YOU"); The Output is HEY YOU. P a g e 6

Question 3: A fresh juice shop has a very amazing way to serve its delicious fresh juice.it serves the juice in three different sizes: S: small size which costs $10.25 M: medium size which costs $12.5 L: Large size which costs $15.25 Write a program that displays the menu to the customer and reads from the customer: The size of drink(s) ( S, M, or L) How many drinks would the customer like to buy? Using switch statement, your program should calculate and display the total price followed by the following message: "Please GO to the First line and enjoy our fresh juice and Have a nice day.." If the customer chooses any other letter or enters a number the following message will appear: "sorry the size is invalid.. Have a nice day " Sample output: Welcome to the Fresh juice shop S: small size which costs $10.25 M: medium size which costs $12.5 L: Large size costs $15.25 Enter the size of your drink: S Enter the number of drinks you would like to buy: 5 The total is $ 51.25 Please GO to the First line and enjoy our fresh juice and Have a nice day.. P a g e 7

Solution: package juiceshop; import java.util.scanner; public class JuiceShop { public static void main(string[] args) { Scanner input=new Scanner(System.in); double total=0; System.out.println("Welcome to the Fresh juice shop\n"); System.out.println("S: small size which costs 10.25$, \n"+ "M: medium size which costs 12.5$ \n" + "L: Large size costs 15.25$.\n"); System.out.println("Enter the size of your drink :"); char size=input.next().charat(0); System.out.println("Enter the number of drinks you would like to buy :"); int num=input.nextint(); switch(size) { case 's':case's': total=10.25*num; case 'm':case'm': total=12.5*num; case 'l':case'l': total=15.25*num; default: System.out.println("Sorry the size is undefined.. Have a nice day "); System.out.printf("The total is %.2f\n",total); System.out.println("Please GO to the First line and enjoy our fresh juice and Have a nice day.."); // TODO code application logic here P a g e 8