Lesson 10..The switch Statement and char

Similar documents
CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

5) (4 points) What is the value of the boolean variable equals after the following statement?

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Programming with Java

AP Computer Science A

Operators in java Operator operands.

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

Operators. Java operators are classified into three categories:

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

Course Outline. Introduction to java

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Chapter 2: Data and Expressions

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CompSci 125 Lecture 11

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

DEPARTMENT OF MATHS, MJ COLLEGE

Oct Decision Structures cont d

Expressions and Casting

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

C Programming Language. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Any Integer Can Be Written as a Fraction

The Arithmetic Operators

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Mr. Monroe s Guide to Mastering Java Syntax

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

Elementary Programming

Java Basic Programming Constructs

ECE 122 Engineering Problem Solving with Java

Program Fundamentals

More Programming Constructs -- Introduction

Chapter 2: Data and Expressions

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

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Lecture 3 Tao Wang 1

Informatics Ingeniería en Electrónica y Automática Industrial

4 Programming Fundamentals. Introduction to Programming 1 1

Programming in C++ 5. Integral data types

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

St. Edmund Preparatory High School Brooklyn, NY

CT 229. Java Syntax 26/09/2006 CT229

Ex: If you use a program to record sales, you will want to remember data:

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Data Conversion & Scanner Class

Accelerating Information Technology Innovation

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

CSC 1214: Object-Oriented Programming

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CEN 414 Java Programming

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Introduction to Java & Fundamental Data Types

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

COMP Primitive and Class Types. Yi Hong May 14, 2015

Operators & Expressions

Computer Programming : C++

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

First Java Program - Output to the Screen

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2: Using Data

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CS111: PROGRAMMING LANGUAGE II

Chapter 2 ELEMENTARY PROGRAMMING

JAVA Programming Fundamentals

More on control structures

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Unit 3. Operators. School of Science and Technology INTRODUCTION

CS 302: Introduction to Programming

Important Java terminology

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Section 2.2 Your First Program in Java: Printing a Line of Text

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

QUIZ: What value is stored in a after this

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Fundamentals of Programming CS-110. Lecture 2

Chapter 2 Elementary Programming

Chapter 3: Operators, Expressions and Type Conversion

These are reserved words of the C language. For example int, float, if, else, for, while etc.

2/29/2016. Definition: Computer Program. A simple model of the computer. Example: Computer Program. Data types, variables, constants

Logical Operators and switch

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Declaration and Memory

UNIT- 3 Introduction to C++

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Full file at

Computer System and programming in C

CS 106 Introduction to Computer Science I

JAVA OPERATORS GENERAL

2.5 Another Application: Adding Integers

STUDENT LESSON A12 Iterations

ME 461 C review Session Fall 2009 S. Keres

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

LECTURE 3 C++ Basics Part 2

Transcription:

Lesson 10..The switch Statement and char 10-1 The if statement is the most powerful and often used decision-type command. The switch statement is useful when we have an integer variable that can be one of several quantities. For example, consider the following menu scenario (enter and run this program): //This code should be placed inside the main method of a class Make your arithmetic selection from the choices below:\n ); 1. Addition ); 2. Subtraction ); 3. Multiplication ); 4. Division\n ); System.out.print( Your choice? ); Scanner kbreader = new Scanner(System.in); int choice = kbreader.nextint( ); System.out.print( \nenter first operand. ); double op1 = kbreader.nextdouble( ); System.out.print( \nenter second operand. ); double op2 = kbreader.nextdouble( ); ); switch (choice) case 1: //addition System.out.println(op1 + plus + op2 + = + (op1 + op2) ); //subtraction System.out.println(op1 + minus + op2 + = + (op1 - op2) ); //multiplication System.out.println(op1 + times + op2 + = + (op1 * op2) ); //division System.out.println(op1 + divided by + op2 + = + (op1 / op2) ); Hey dummy, enter only a 1, 2, 3, or 4! ); The optional The default command is optional. You can use it if there might be a possibility of the value of choice not being one of the cases. Give me a break: The break statements are normally used. Try leaving them out and see what happens here. In the next section we will look at an application in which they are omitted.

10-2 Basically, break jumps us out of the switch structure and then code execution continues with the first line immediately after the closing switch brace. Specifically, you might want to omit the break within the case 1: section. If choice is 1 then the result will be that it prints the answer for both addition and subtraction. The next experiment you might want to do is to leave the parenthesis off of (op1 + op2) in the case 1: section. Since op1 + op2 is no longer in parenthesis, the plus between them no longer means addition. It now means concatenation since all the activity to the left of this point in the code was also String concatenation. Leaving off the break: Now, let s look at an example where we intentionally omit break: //Suppose at this point in the program we have an integer variable, j. If j equals 1, //2, or 3 we want to set String variable s to low and if j equals 4, 5, or 6 we want //to set s to high. If j equals 7, set s to lucky. switch ( j ) case 1: s = low ; case 5: case 6: s = high ; case 7: s = lucky ; A new data type char: Before we look further at the switch statement, we must look at a new data type, char. This stands for character. Following is a typical way to declare and initialize a character: char ch = h ; Notice that a character is always enclosed in single quotes. Characters can be anything, even numbers or symbols: char x = 6 ; char pp = @ ; int and char are permissible types: switch( ) statements primarily switch on integers or characters (short and byte types can also be used, but rarely are). Modify the example on the previous page to switch on a char instead of int. See the next page for the necessary modifications:

Make your arithmetic selection from the choices below:\n ); 10-3 A. Addition ); S. Subtraction ); M. Multiplication ); D. Division\n ); System.out.print( Your choice? ); Scanner kbreader = new Scanner(System.in); String choice = kbreader.nextline( ); //char ch = choice; //You would think this would work but it doesn t. char ch = choice.charat(0); //you just learned another String method. System.out.print( \nenter first operand. ); double op1 = kbreader.nextdouble( ); System.out.print( \nenter second operand. ); double op2 = kbreader.nextdouble( ); ); switch (ch) case A : //addition case a : //Notice we are providing for both capital A and little a. System.out.println(op1 + plus + op2 + = + (op1 + op2) ); case S : //subtraction case s : System.out.println(op1 + minus + op2 + = + (op1 - op2) ); case M : //multiplication case m : System.out.println(op1 + times + op2 + = + (op1 * op2) ); case D : //division case d : System.out.println(op1 + divided by + op2 + = + (op1 / op2) ); Hey dummy, enter only a A, S, M, or D! );

Exercise on Lesson 10 10-4 1. What are two permissible data types to use for x in the following? switch (x)... 2. What is the output of the following code? int x = 3, p = 5, y = -8; switch(x) p++; y+=(--p); case 5: y+=(p++); System.out.println(y); 3. Write a switch structure that uses the character mychar. It should increment the integer variable y if mychar is either a capital or small letter G. It should decrement y if mychar is either a capital or a small letter M. If mychar is anything else, add 100 to y. 4. What is output by the following code? int z = 2, q = 0; switch(z) case 1: System.out.println(--q); 5. Write a line of code that declares the variable chr as a character type and assigns the letter z to it. 6. What is output by the following? int x = 10, y = 12; System.out.println( The sum is + x + y ); System.out.println( The sum is + (x + y) ); 7. Convert the following code into a switch statement. if(speed = = 75)

Exceeding speed limit ); else if( (speed = = 69) (speed = = 70) ) Getting close ); 10-5 else if(speed = = 65) Cruising ); else Very slow ); 8. Is default a mandatory part of a switch structure? 9. Write a line of code that converts String s = X into a character called chr. Project Weight on Other Planets Write a program that will determine the user s weight on another planet. The program should ask the user to enter his weight (on earth) via the keyboard and then present a menu of the other mythical planets. The user should choose one of the planets from the menu, and use a switch (with an integer) statement to calculate the weight on the chosen planet. Use the following conversion factors to determine the user s weight on the chosen planet. Planet Multiply weight by Voltar 0.091 Krypton 0.720 Fertos 0.865 Servontos 4.612 A typical output screen will be similar to the following: What is your weight on the Earth? 135 1. Voltar 2. Krypton 3. Fertos 4. Servontos Selection? 1 Your weight on Voltor would be 12.285