Switch Statement.

Slides:



Advertisements
Similar presentations
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Advertisements

1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
LAB 10.
Computer Programming Lab(4).
Computer Programming Lab(5).
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Starting Out with Java: From Control Structures through Objects
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
JAVA PROGRAMMING BASICS CHAPTER 2. History of Java Begin with project Green in 1991 founded by Patrick Noughton, Mike Sheridan and James Gosling who worked.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
Lesson 4 Input. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are using. The reason it is not.
Lab 01-2 Objectives:  Writing a Java program.  How to send output to the command line console.  Learn about escape sequences.  Learn how to compile,
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Object Oriented Programming (OOP) LAB # 1 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
SELF STUDY. IF STATEMENTS SELECTION STRUCTURE if selection statement if … else selection statement switch selection statement.
Simple algorithms on an array - compute sum and min.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Lab 04-2 Objectives:  Understand what a file is.  Learn how to use the File class  Learn how to use the Scanner class to read from files  Learn about.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Introduction to programming in java
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Slides by Evan Gallagher
Slides by Evan Gallagher
User Input ICS2O.
Topic 2 Elementary Programming
CS 160 – Summer 16 Exam 1 Prep.
Chapter 2 Clarifications
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.
TemperatureConversion
CSC1401 Input and Output (and we’ll do a bit more on class creation)
Elementary Programming
Chapter 2 Elementary Programming
Multiple if-else boolean Data
Boolean Expressions and if-else Statements
Introduction to Methods in java
Char data type and the String class.
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
Comp Sci 200 Programming I Jim Williams, PhD.
SELECTION STATEMENTS (1)
Something about Java Introduction to Problem Solving and Programming 1.
Chapter 2.
Boolean Expressions And if…else Statements.
While Statement.
Starting JavaProgramming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
SELECTION STATEMENTS (2)
Introduction to Computer Science and Object-Oriented Programming
Multiple if-else boolean Data
Self study.
Lecture Notes – Week 2 Lecture-2
Week 4 Lecture-2 Chapter 6 (Methods).
Introduction to Java Brief history of Java Sample Java Program
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Unit 7 - Short-Circuit Evaluation - De Morgan’s Laws - The switch statement - Type-casting in Java - Using Math.random()
Random Numbers while loop
Presentation transcript:

switch Statement

Objectives: Learn the syntax for switch statements Another objective is to discuss team development and illustrate software reusability in the context of a simple but realistic case study, the Craps applet.

The switch Statement switch case default break switch (variable) { case value1: ... break; case value2: default: } Reserved words switch case default break There is a legend in software folklore that a missing break in a program caused the whole telephone grid of New York to go down, costing millions of dollars. You have to be careful about breaks; they are among the many opportunities to make a stupid mistake not caught by the compiler. Attention to detail is key. Don’t forget breaks!

The switch Statement (cont’d) switch (num) { case 1: System.out.println ("Buckle your shoe"); break; case 2: ... } This is simply a special case of a case without a break. Case 1 "falls through" to Case 2.

The switch Statement (cont’d) The same case can have two or more labels. For example: switch (num) { case 1: case 2: System.out.println ("Buckle your shoe"); break; case 3: ... } This is simply a special case of a case without a break. Case 1 "falls through" to Case 2.

Top Down Design

Top-Down Design (cont’d) Write a program that reads a simple equation from the keyboard in the form [a + b]. Only four symbols will be used: + - * / Solve the equation and display the results on the console screen.

Top-Down Design (cont’d) Start JCreator. Open the file “Lab11.java”. Lab11.java is in your Lab11 folder.

Top-Down Design (cont’d) The “big problem” is defined in the main method.

Top-Down Design (cont’d) import java.text.DecimalFormat; import java.util.Scanner; public class Lab11 { public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11 lab = new Lab11( ); lab.input(); // Enter data from the kybd lab.process(); // Perform the calculation lab.output( ); // Display output }

Top-Down Design (cont’d) We need four instance fields to solve the problem.

Top-Down Design (cont’d) public class Lab11 { private double a; private double b; private char symbol; private double solution; public static void main(String[] args) for (int i = 0; i < 3; i++) Lab11 lab = new Lab11( ); lab.input(); // Enter data from the kybd lab.process(); // Perform the calculation lab.output( ); // Display output }

Top-Down Design (cont’d) input(), process() and output( ) are the smaller parts of the problem.

Top-Down Design (cont’d) input( )

Top-Down Design (cont’d) public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter an expression in the form [a + b]: "); a= reader.nextDouble(); symbol = reader.next().charAt(0); b= reader.nextDouble(); }

Top-Down Design (cont’d) output( )

Top-Down Design (cont’d) public void output() { DecimalFormat df = new DecimalFormat("#.#"); System.out.println(df.format(a) + " " + symbol + “ " + df.format(b) + " = " + df.format(solution)); }

Top-Down Design (cont’d) process( )

Top-Down Design (cont’d) public void process() { switch (symbol) { case '+': solution = a + b; break; case '*': solution = a * b; case '-': solution = a - b; case '/': solution = a / b; }

Top-Down Design (cont’d) Run The Program: Enter an expression in the form [a + b]: 35 / 6 35 / 6 = 5.8 Enter an expression in the form [a + b]: 14.6 + 27.9 14.6 + 27.9 = 42.5

Questions?

Java Begin Lab 11 Object Oriented Programming The material in this chapter is not tested on the AP CS exams.

Top-Down Design (cont’d) import java.util.Scanner; public class Lab11A { public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11A lab = new Lab11A( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }

Top-Down Design (cont’d) import java.util.Scanner; public class Lab11A { private int age; public static void main(String[ ] args) for (int i = 0; i < 3; i++) Lab11A lab = new Lab11A( ); lab.input(); // Enter data from the kybd lab.output( ); // Display output }

Top-Down Design (cont’d) public void input() { Scanner reader = new Scanner(System.in); System.out.print("Enter a person’s age (Between 10 & 25): "); age = reader.nextInt(); }

Lab 11A – output() public void output() { switch (age) { case 13: System.out.println("Teenager!"); break; case 16: System.out.println("Keys, please?");   case 18: System.out.println("Rise to vote, sir!"); case 21: System.out.println("Full adult privileges"); default: System.out.println("Too young to do anything."); }

“Full Adult Privileges!” Lab 11A – output() Cover all ages from 10 to 25. Ages 21 – 25 should all say “Full Adult Privileges!” public void output() { switch (age) { case 13: System.out.println("Teenager!"); break; case 16: System.out.println("Keys, please?");   case 18: System.out.println("Rise to vote, sir!"); case 21: System.out.println("Full adult privileges"); default: System.out.println("Too young to do anything."); }