Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. 1

Similar documents
Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

CSc Introduction to Computing

Java Basic Datatypees

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Following is the general form of a typical decision making structure found in most of the programming languages:

IT 374 C# and Applications/ IT695 C# Data Structures

Introduction to Programming Using Java (98-388)

Computer Programming, I. Laboratory Manual. Final Exam Solution

CSC Java Programming, Fall Java Data Types and Control Constructs

SOFT 161. Class Meeting 1.6

Java Control Statements

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Computational Expression

Peer Instruction 1. Elementary Programming

VARIABLES AND TYPES CITS1001

Lecture 2 Tao Wang 1

Fundamentals of Programming Session 4

1 Lexical Considerations

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Lexical Considerations

Review for Test 1 (Chapter 1-5)

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

Control Structures in Java if-else and switch

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Key Differences Between Python and Java

Programming with Java

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Java+- Language Reference Manual

CS112 Lecture: Working with Numbers

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Lexical Considerations

The Warhol Language Reference Manual

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Chapter 2: Using Data

2.8. Decision Making: Equality and Relational Operators

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 9: OCT. 4TH INSTRUCTOR: JIAYIN WANG

Introduction to Object-Oriented Programming

Flow Control. CSC215 Lecture

CS11 Java. Fall Lecture 1

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Generating/Updating code from whole project

Object-Oriented Programming

Basics of Java Programming

Manufactured Home Production by Product Mix ( )

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

Java Bytecode (binary file)

CS 231 Data Structures and Algorithms, Fall 2016

CS260 Intro to Java & Android 03.Java Language Basics

Handout 4 Conditionals. Boolean Expressions.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Course Outline. Introduction to java

The C++ Language. Arizona State University 1

Java Identifiers, Data Types & Variables

An overview of Java, Data types and variables

Visual C# Instructor s Manual Table of Contents

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

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

APCS Semester #1 Final Exam Practice Problems

MapMarker Plus v Release Notes

Full file at

CSI33 Data Structures

CS 139 Practice Midterm Questions #2

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Introduction to Programming EC-105. Lecture 2

Language Reference Manual

Introduction to Classes and Objects

LAB #6: DATA HANDING AND MANIPULATION

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

C: How to Program. Week /Mar/05

Datatypes, Variables, and Operations

Lab # 02. Basic Elements of C++ _ Part1

Variables. Data Types.

Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Activity 3: Data Types

MapMarker Plus 12.0 Release Notes

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Lexical Structure (Chapter 3, JLS)

Key Concept: all programs can be broken down to a combination of one of the six instructions Assignment Statements can create variables to represent

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CS313D: ADVANCED PROGRAMMING LANGUAGE

An Introduction to Processing

PROGRAMMING FUNDAMENTALS

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

ESc101 : Fundamental of Computing

Data Types, Literals, Operators

Transcription:

Java How to Program, 10/e Rights Reserved. 1

switch multiple-selection statement Case study on switch statement Rights Reserved. 2

switch multiple-selection statement performs different actions based on the possible values of a constant integral expression of type byte, short, int or char. Rights Reserved. 3

The switch statement consists of a block that contains a sequence of case labels and an optional default case. The program evaluates the controlling expression in the parentheses following keyword switch. The program compares the controlling expression s value (which must evaluate to an integral value of type byte, char, short or int, or to a String) with each case label. If a match occurs, the program executes that case s statements. The break statement causes program control to proceed with the first statement after the switch. Rights Reserved. 4

Rights Reserved. 5

switch does not provide a mechanism for testing ranges of values every value must be listed in a separate case label. Note that each case can have multiple statements. switch differs from other control statements in that it does not require braces around multiple statements in a case. Without break, the statements for a matching case and subsequent cases execute until a break or the end of the switch is encountered. This is called falling through. If no match occurs between the controlling expression s value and a case label, the default case executes. If no match occurs and there is no default case, program control simply continues with the first statement after the switch. Rights Reserved. 6

Write a program that prompts the user to enter a year and displays the animal for the year. Rights Reserved. 7

Case 0 True Print monkey break False Case 1 True Print rooster break False False... Case 11 True Print rooster break Default Action Rights Reserved. 8

Rights Reserved. 9

Figure 5.10 shows the UML activity diagram for the general switch statement. Most switch statements use a break in each case to terminate the switch statement after processing the case. The break statement is not required for the switch s last case (or the optional default case, when it appears last), because execution continues with the next statement after the switch. Rights Reserved. 10

When using the switch statement, remember that each case must contain a constant integral expression. An integer constant is simply an integer value. In addition, you can use character constants specific characters in single quotes, such as 'A', '7' or '$' which represent the integer values of characters. The expression in each case can also be a constant variable a variable that contains a value which does not change for the entire program. Such a variable is declared with keyword final. Rights Reserved. 11

Rights Reserved. 12

Strings can be used as controlling expressions in switch statements, and String literals can be used in case labels. App requirements: You ve been hired by an auto insurance company that serves these northeast states Connecticut, Maine, Massachusetts, New Hampshire, New Jersey, New York, Pennsylvania, Rhode Island and Vermont. The company would like you to create a program that produces a report indicating for each of their auto insurance policies whether the policy is held in a state with no-fault auto insurance Massachusetts, New Jersey, New York and Pennsylvania. Rights Reserved. 13

Class AutoPolicy represents an auto insurance policy. The class contains: int instance variable accountnumber to store the policy s account number String instance variable makeandmodel to store the car s make and model (such as a "Toyota Camry") String instance variable state to store a two-character state abbreviation representing the state in which the policy is held (e.g., "MA" for Massachusetts) a constructor that initializes the class s instance variables Rights Reserved. 14

Rights Reserved. 15

Rights Reserved. 16

Rights Reserved. 17

methods setaccountnumber and getaccountnumber to set and get an AutoPolicy s accountnumber instance variable methods setmakeandmodel and getmakeandmodel to set and get an AutoPolicy s makeandmodel instance variable methods setstate and getstate to set and get an AutoPolicy s state instance variable method isnofaultstate to return a boolean value indicating whether the policy is held in a nofault auto insurance state; note the method name the naming convention for a get method that returns a boolean value is to begin the name with "is" rather than "get" (such a method is commonly called a predicate method). Class AutoPolicyTest (Fig. 5.12) creates two AutoPolicy objects. Rights Reserved. 18

Draw the flow chart of the AutoPolicy case study Rights Reserved. 19

Rights Reserved. 20

Rights Reserved. 21

Chapter 5: Read In Version 9: 5.6 Or In Version 10: 5.6 and 5.7 Rights Reserved. 22