SlideShare une entreprise Scribd logo
1  sur  38
Object – Oriented Programming
Week 4 – do - while and switch
Ferdin Joe John Joseph, PhD
Faculty of Information Technology
Thai-Nichi Institute of Technology
Java's console input
• The console is the terminal window that is running the
Java program
I.e., that's the terminal window where you type in the
command java ProgramName
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Java's console input
• When a Java program starts running, the Java runtime
system will initialize many variables in support for the
running program.
One of these variables is the Java system variable:
which represents the console input
The variable System.in is included in every Java
program (you don't need to define it).
System.in
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Java's console input
• A Java program can obtains inputs from the console
through the keyboard
• In other words:
• The Java system variable System.in represents
the keyboard
Faculty of Information Technology,
Thai-Nichi Institute of Technology
A note on the notation "System.in"
• At this moment in the course, we want to learn how to read
input from the keyboard
All you need to know is:
• It is too early in the course to explain the notation
System.in
• We will explain this after we have covered classes
• The variable named System.in represents the
keyboard
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Java's Scanner library functions
• Fact:
• The details of what the computer must do to read in a
number will be discussed in CS255
• The Java programming language provides a collection of
methods stored in the Scanner class that perform read
operations
(Remember that a class is a container for methods)
• There is a lot of work that the computer must do to read in a floating
point number
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Java's Scanner library functions (cont.)
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Java's Scanner library functions (cont.)
• We will now learn how to use the methods in the
Scanner class to read in floating point numbers
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Importing the Scanner class definition
• Recall the Rule of usage of methods in the Java library:
• If a Java program wants to use a method in the Java library, the Java
program must first import the containing class
• All classes in the java.lang package have already been imported into a Java
program
(You can use methods in these classes without the import clause)
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Importing the Scanner class definition
(cont.)
• We can use the following import clause to import the
Scanner class:
import java.util.Scanner;
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Preparation before we can read input
from the keyboard
• Before a Java program can read input from the keyboard,
the program must " construct a Scanner object
It is too early to explain what this means... I will only tell
you
how to do it
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Preparation before we can read input
from the keyboard (cont.)
• A Scanner object is constructed using the following
statement:
The name varName is an identifier
Example: constructing a Scanner object named in
Scanner varName = new Scanner(System.in);
Scanner in = new Scanner(System.in);
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading in a floating point number
from the keyboard
• After having constructed the Scanner object named in, you
can use the following expression to read a floating point
number from the keyboard:
You must save (store) the number read in by
"in.nextDouble()" in a variable with an assignment
statement
in.nextDouble()
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading in a floating point number
from the keyboard (cont.)
• What happens inside the computer:
• Just like Math.sqrt(..), the method call in.nextDouble() will
invoke (run) a method in Java's library.
The task performed by in.nextDouble() is to read a floating
point number from the keyboard:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading in a floating point number
from the keyboard (cont.)
If you type in "3.5" on the keyboard at the time that
in.nextDouble() is running, then the call will return the value
3.5
• The return value will replace the method call:
The input value 3.5 is then stored in the variable a !!!
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Summary: steps to read in a floating
point number
• This figure summarizes the programming steps to read
in a floating point number:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Example: reading input for the a,b,c-
formula
• Programming Example: ABC formula
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct a Scanner object
a = in.nextDouble(); // Read in next number and store in a
b = in.nextDouble(); // Read in next number and store in b
c = in.nextDouble(); // Read in next number and store in c
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading in a floating point number
from the keyboard (cont.)
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("a = ");
System.out.println(a);
System.out.print("b = ");
System.out.println(b);
System.out.print("c = ");
System.out.println(c);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Good programming practice: Prompting
user for input (cont.)
• Example
import java.util.Scanner; // Import Scanner class (contains methods
// for reading keyboard input)
public class Abc2
{
public static void main(String[] args)
{
double a, b, c, x1, x2; // Define 5 variable
Scanner in = new Scanner(System.in); // Construct a Scanner object
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Good programming practice:
Prompting user for input (cont.)
System.out.print("Enter a = "); // ******* Prompt message
a = in.nextDouble(); // Read in next number and store in a
System.out.print("Enter b = ");
b = in.nextDouble(); // Read in next number and store in b
System.out.print("Enter c = ");
c = in.nextDouble(); // Read in next number and store in c
x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a);
x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a);
System.out.print("a = ");
System.out.println(a);
System.out.print("b = ");
System.out.println(b);
System.out.print("c = ");
System.out.println(c);
System.out.print("x1 = ");
System.out.println(x1);
System.out.print("x2 = ");
System.out.println(x2);
}
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading other types of input from the
keyboard
• The procedure to read other types of inputs from the
keyboard is similar to the one above:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading other types of input from the
keyboard (cont.)
• The only different is that we need to use a different
method in the Scanner class that read the correct type of
data.
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Reading other types of input from the
keyboard (cont.)
• Reading an integer number from the keyboard: use
nextInt()
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Loops – While, Do, For
• Repetition Statements
– Do – While
• Control Statements
- Switch
Faculty of Information Technology,
Thai-Nichi Institute of Technology
While Vs Do While
Faculty of Information Technology,
Thai-Nichi Institute of Technology
The do-while Repetition
Structure
do
{
statement(s)
} while ( condition ) ;
• The body of a do-while is ALWAYS
executed at least once. Is this true of a
while loop? What about a for loop?Faculty of Information Technology,
Thai-Nichi Institute of Technology
Example
do
{
num = prompt("Enter a positive number: ");
num = parseInt(num);
if (num <= 0)
{
alert("That is not positive. Try again.");
}
}while (num <= 0);
Faculty of Information Technology,
Thai-Nichi Institute of Technology
An Equivalent while Loop
num = prompt("Enter a positive number: ");
num = parseInt(num);
while ( num <= 0 )
{
alert("That is not positive. Try again.");
num = prompt("Enter a positive number: ");
num = parseInt(num);
}
• Notice that using a while loop in this case requires a priming
read.
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Switch Case
Faculty of Information Technology,
Thai-Nichi Institute of Technology
The Switch Statement
• The switch statement
provides another way to
decide which statement to
execute next
• The switch statement
evaluates an expression,
then attempts to match the
result to one of several
possible cases
• The match must be an exact
match.
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
The Switch Statement
• Each case
contains a value
and a list of
statements
• The flow of control
transfers to
statement
associated with
the first case value
that matches
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Switch - syntax
• The general syntax of a switch statement is:
switch ( expression ){
case value1 :
statement-list1
case value2 :
statement-list2
case value3 :
statement-list3
case ...
}
switch
and
case
are
reserved
words
If expression
matches value3,
control jumps
to hereFaculty of Information Technology,
Thai-Nichi Institute of Technology
The Switch Statement
• The break statement can
be used as the last
statement in each case's
statement list
• A break statement
causes control to transfer
to the end of the switch
statement
• If a break statement is
not used, the flow of
control will continue into
the next case
switch ( expression ){
case value1 :
statement-list1
break;
case value2 :
statement-list2
break;
case value3 :
statement-list3
break;
case ...
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Switch Example
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
• Examples of the switch statement:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Switch – no breaks!!!
switch (option){
case 'A':
aCount++;
case 'B':
bCount++;
case 'C':
cCount++;
}
• Another Example:
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
}
Faculty of Information Technology,
Thai-Nichi Institute of Technology
Switch - default
• A switch statement can have an optional
default case
• The default case has no associated value and
simply uses the reserved word default
• If the default case is present, control will transfer
to it if no other case value matches
• If there is no default case, and no other value
matches, control falls through to the statement
after the switch
Faculty of Information Technology,
Thai-Nichi Institute of Technology
The switch Statement
switch (option){
case 'A':
aCount++;
break;
case 'B':
bCount++;
break;
case 'C':
cCount++;
break;
default:
otherCount++;
break;
}
• Switch
with
default
case:
Faculty of Information Technology,
Thai-Nichi Institute of Technology
To Switch or not to Switch
• The expression of a switch statement must result in an
integral type, meaning an integer (byte, short, int,
long) or a char
• It cannot be a boolean value or a floating point value
(float or double)
• The implicit boolean condition in a switch statement is
equality
• You cannot perform relational checks with a switch
statement
Faculty of Information Technology,
Thai-Nichi Institute of Technology

Contenu connexe

Tendances

Tendances (20)

19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity19. Data Structures and Algorithm Complexity
19. Data Structures and Algorithm Complexity
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
06.Loops
06.Loops06.Loops
06.Loops
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Java Tutorial: Part 1. Getting Started
Java Tutorial: Part 1. Getting StartedJava Tutorial: Part 1. Getting Started
Java Tutorial: Part 1. Getting Started
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
Java Foundations: Arrays
Java Foundations: ArraysJava Foundations: Arrays
Java Foundations: Arrays
 
10. Recursion
10. Recursion10. Recursion
10. Recursion
 
Python basics
Python basicsPython basics
Python basics
 
13. Java text processing
13.  Java text processing13.  Java text processing
13. Java text processing
 
18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set18. Dictionaries, Hash-Tables and Set
18. Dictionaries, Hash-Tables and Set
 
Java introduction
Java introductionJava introduction
Java introduction
 
09. Java Methods
09. Java Methods09. Java Methods
09. Java Methods
 
Java Foundations: Strings and Text Processing
Java Foundations: Strings and Text ProcessingJava Foundations: Strings and Text Processing
Java Foundations: Strings and Text Processing
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...03 and 04 .Operators, Expressions, working with the console and conditional s...
03 and 04 .Operators, Expressions, working with the console and conditional s...
 
Chapter 2 Method in Java OOP
Chapter 2   Method in Java OOPChapter 2   Method in Java OOP
Chapter 2 Method in Java OOP
 

Similaire à DSA 103 Object Oriented Programming :: Week 4

Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
ASU Online
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutions
QUONTRASOLUTIONS
 
香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShare香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShare
yayao
 
Format of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptxFormat of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptx
MOHAMMADANISH12
 

Similaire à DSA 103 Object Oriented Programming :: Week 4 (20)

Reading and writting v2
Reading and writting v2Reading and writting v2
Reading and writting v2
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#
 
Introduction to java programming part 2
Introduction to java programming  part 2Introduction to java programming  part 2
Introduction to java programming part 2
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
 
02basics
02basics02basics
02basics
 
DIY Java Profiling
DIY Java ProfilingDIY Java Profiling
DIY Java Profiling
 
Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1Begin with c++ Fekra Course #1
Begin with c++ Fekra Course #1
 
Core java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutionsCore java over view basics introduction by quontra solutions
Core java over view basics introduction by quontra solutions
 
Java final lab
Java final labJava final lab
Java final lab
 
JAVA (UNIT 3)
JAVA (UNIT 3)JAVA (UNIT 3)
JAVA (UNIT 3)
 
香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShare香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShare
 
ch01-basic-java-programs.ppt
ch01-basic-java-programs.pptch01-basic-java-programs.ppt
ch01-basic-java-programs.ppt
 
Java Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O MechanismsJava Wrapper Classes and I/O Mechanisms
Java Wrapper Classes and I/O Mechanisms
 
Applets
AppletsApplets
Applets
 
Java input
Java inputJava input
Java input
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 
Introduction to java 101
Introduction to java 101Introduction to java 101
Introduction to java 101
 
Anish PPT-GIT.pptx
Anish PPT-GIT.pptxAnish PPT-GIT.pptx
Anish PPT-GIT.pptx
 
Format of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptxFormat of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptx
 
PPT-GIT.pptx
PPT-GIT.pptxPPT-GIT.pptx
PPT-GIT.pptx
 

Plus de Ferdin Joe John Joseph PhD

Plus de Ferdin Joe John Joseph PhD (20)

Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022Invited Talk DGTiCon 2022
Invited Talk DGTiCon 2022
 
Week 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud ComputingWeek 12: Cloud AI- DSA 441 Cloud Computing
Week 12: Cloud AI- DSA 441 Cloud Computing
 
Week 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud ComputingWeek 11: Cloud Native- DSA 441 Cloud Computing
Week 11: Cloud Native- DSA 441 Cloud Computing
 
Week 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud ComputingWeek 10: Cloud Security- DSA 441 Cloud Computing
Week 10: Cloud Security- DSA 441 Cloud Computing
 
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
Week 9: Relational Database Service Alibaba Cloud- DSA 441 Cloud Computing
 
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud ComputingWeek 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
Week 7: Object Storage Service Alibaba Cloud- DSA 441 Cloud Computing
 
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
Week 6: Server Load Balancer and Auto Scaling Alibaba Cloud- DSA 441 Cloud Co...
 
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
Week 5: Elastic Compute Service (ECS) with Alibaba Cloud- DSA 441 Cloud Compu...
 
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud ComputingWeek 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
Week 4: Big Data and Hadoop in Alibaba Cloud - DSA 441 Cloud Computing
 
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
Week 3: Virtual Private Cloud, On Premise, IaaS, PaaS, SaaS - DSA 441 Cloud C...
 
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud ComputingWeek 2: Virtualization and VM Ware - DSA 441 Cloud Computing
Week 2: Virtualization and VM Ware - DSA 441 Cloud Computing
 
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud ComputingWeek 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
Week 1: Introduction to Cloud Computing - DSA 441 Cloud Computing
 
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculumSept 6 2021 BTech Artificial Intelligence and Data Science curriculum
Sept 6 2021 BTech Artificial Intelligence and Data Science curriculum
 
Hadoop in Alibaba Cloud
Hadoop in Alibaba CloudHadoop in Alibaba Cloud
Hadoop in Alibaba Cloud
 
Cloud Computing Essentials in Alibaba Cloud
Cloud Computing Essentials in Alibaba CloudCloud Computing Essentials in Alibaba Cloud
Cloud Computing Essentials in Alibaba Cloud
 
Transforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approachTransforming deep into transformers – a computer vision approach
Transforming deep into transformers – a computer vision approach
 
Week 11: Programming for Data Analysis
Week 11: Programming for Data AnalysisWeek 11: Programming for Data Analysis
Week 11: Programming for Data Analysis
 
Week 10: Programming for Data Analysis
Week 10: Programming for Data AnalysisWeek 10: Programming for Data Analysis
Week 10: Programming for Data Analysis
 
Week 9: Programming for Data Analysis
Week 9: Programming for Data AnalysisWeek 9: Programming for Data Analysis
Week 9: Programming for Data Analysis
 
Week 8: Programming for Data Analysis
Week 8: Programming for Data AnalysisWeek 8: Programming for Data Analysis
Week 8: Programming for Data Analysis
 

Dernier

+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
Health
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
ptikerjasaptiker
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Klinik kandungan
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
ranjankumarbehera14
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
ahmedjiabur940
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
vexqp
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
chadhar227
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
q6pzkpark
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
vexqp
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
vexqp
 

Dernier (20)

+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
 
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi ArabiaIn Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
In Riyadh ((+919101817206)) Cytotec kit @ Abortion Pills Saudi Arabia
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Ranking and Scoring Exercises for Research
Ranking and Scoring Exercises for ResearchRanking and Scoring Exercises for Research
Ranking and Scoring Exercises for Research
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
怎样办理旧金山城市学院毕业证(CCSF毕业证书)成绩单学校原版复制
 

DSA 103 Object Oriented Programming :: Week 4

  • 1. Object – Oriented Programming Week 4 – do - while and switch Ferdin Joe John Joseph, PhD Faculty of Information Technology Thai-Nichi Institute of Technology
  • 2. Java's console input • The console is the terminal window that is running the Java program I.e., that's the terminal window where you type in the command java ProgramName Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 3. Java's console input • When a Java program starts running, the Java runtime system will initialize many variables in support for the running program. One of these variables is the Java system variable: which represents the console input The variable System.in is included in every Java program (you don't need to define it). System.in Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 4. Java's console input • A Java program can obtains inputs from the console through the keyboard • In other words: • The Java system variable System.in represents the keyboard Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 5. A note on the notation "System.in" • At this moment in the course, we want to learn how to read input from the keyboard All you need to know is: • It is too early in the course to explain the notation System.in • We will explain this after we have covered classes • The variable named System.in represents the keyboard Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 6. Java's Scanner library functions • Fact: • The details of what the computer must do to read in a number will be discussed in CS255 • The Java programming language provides a collection of methods stored in the Scanner class that perform read operations (Remember that a class is a container for methods) • There is a lot of work that the computer must do to read in a floating point number Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 7. Java's Scanner library functions (cont.) Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 8. Java's Scanner library functions (cont.) • We will now learn how to use the methods in the Scanner class to read in floating point numbers Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 9. Importing the Scanner class definition • Recall the Rule of usage of methods in the Java library: • If a Java program wants to use a method in the Java library, the Java program must first import the containing class • All classes in the java.lang package have already been imported into a Java program (You can use methods in these classes without the import clause) Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 10. Importing the Scanner class definition (cont.) • We can use the following import clause to import the Scanner class: import java.util.Scanner; Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 11. Preparation before we can read input from the keyboard • Before a Java program can read input from the keyboard, the program must " construct a Scanner object It is too early to explain what this means... I will only tell you how to do it Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 12. Preparation before we can read input from the keyboard (cont.) • A Scanner object is constructed using the following statement: The name varName is an identifier Example: constructing a Scanner object named in Scanner varName = new Scanner(System.in); Scanner in = new Scanner(System.in); Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 13. Reading in a floating point number from the keyboard • After having constructed the Scanner object named in, you can use the following expression to read a floating point number from the keyboard: You must save (store) the number read in by "in.nextDouble()" in a variable with an assignment statement in.nextDouble() Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 14. Reading in a floating point number from the keyboard (cont.) • What happens inside the computer: • Just like Math.sqrt(..), the method call in.nextDouble() will invoke (run) a method in Java's library. The task performed by in.nextDouble() is to read a floating point number from the keyboard: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 15. Reading in a floating point number from the keyboard (cont.) If you type in "3.5" on the keyboard at the time that in.nextDouble() is running, then the call will return the value 3.5 • The return value will replace the method call: The input value 3.5 is then stored in the variable a !!! Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 16. Summary: steps to read in a floating point number • This figure summarizes the programming steps to read in a floating point number: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 17. Example: reading input for the a,b,c- formula • Programming Example: ABC formula import java.util.Scanner; // Import Scanner class (contains methods // for reading keyboard input) public class Abc2 { public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable Scanner in = new Scanner(System.in); // Construct a Scanner object a = in.nextDouble(); // Read in next number and store in a b = in.nextDouble(); // Read in next number and store in b c = in.nextDouble(); // Read in next number and store in c Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 18. Reading in a floating point number from the keyboard (cont.) x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a); System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); } } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 19. Good programming practice: Prompting user for input (cont.) • Example import java.util.Scanner; // Import Scanner class (contains methods // for reading keyboard input) public class Abc2 { public static void main(String[] args) { double a, b, c, x1, x2; // Define 5 variable Scanner in = new Scanner(System.in); // Construct a Scanner object Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 20. Good programming practice: Prompting user for input (cont.) System.out.print("Enter a = "); // ******* Prompt message a = in.nextDouble(); // Read in next number and store in a System.out.print("Enter b = "); b = in.nextDouble(); // Read in next number and store in b System.out.print("Enter c = "); c = in.nextDouble(); // Read in next number and store in c x1 = ( -b - Math.sqrt( b*b - 4*a*c ) ) / (2*a); x2 = ( -b + Math.sqrt( b*b - 4*a*c ) ) / (2*a); System.out.print("a = "); System.out.println(a); System.out.print("b = "); System.out.println(b); System.out.print("c = "); System.out.println(c); System.out.print("x1 = "); System.out.println(x1); System.out.print("x2 = "); System.out.println(x2); } } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 21. Reading other types of input from the keyboard • The procedure to read other types of inputs from the keyboard is similar to the one above: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 22. Reading other types of input from the keyboard (cont.) • The only different is that we need to use a different method in the Scanner class that read the correct type of data. Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 23. Reading other types of input from the keyboard (cont.) • Reading an integer number from the keyboard: use nextInt() Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 24. Loops – While, Do, For • Repetition Statements – Do – While • Control Statements - Switch Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 25. While Vs Do While Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 26. The do-while Repetition Structure do { statement(s) } while ( condition ) ; • The body of a do-while is ALWAYS executed at least once. Is this true of a while loop? What about a for loop?Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 27. Example do { num = prompt("Enter a positive number: "); num = parseInt(num); if (num <= 0) { alert("That is not positive. Try again."); } }while (num <= 0); Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 28. An Equivalent while Loop num = prompt("Enter a positive number: "); num = parseInt(num); while ( num <= 0 ) { alert("That is not positive. Try again."); num = prompt("Enter a positive number: "); num = parseInt(num); } • Notice that using a while loop in this case requires a priming read. Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 29. Switch Case Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 30. The Switch Statement • The switch statement provides another way to decide which statement to execute next • The switch statement evaluates an expression, then attempts to match the result to one of several possible cases • The match must be an exact match. switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 31. The Switch Statement • Each case contains a value and a list of statements • The flow of control transfers to statement associated with the first case value that matches switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 32. Switch - syntax • The general syntax of a switch statement is: switch ( expression ){ case value1 : statement-list1 case value2 : statement-list2 case value3 : statement-list3 case ... } switch and case are reserved words If expression matches value3, control jumps to hereFaculty of Information Technology, Thai-Nichi Institute of Technology
  • 33. The Switch Statement • The break statement can be used as the last statement in each case's statement list • A break statement causes control to transfer to the end of the switch statement • If a break statement is not used, the flow of control will continue into the next case switch ( expression ){ case value1 : statement-list1 break; case value2 : statement-list2 break; case value3 : statement-list3 break; case ... } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 34. Switch Example switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } • Examples of the switch statement: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 35. Switch – no breaks!!! switch (option){ case 'A': aCount++; case 'B': bCount++; case 'C': cCount++; } • Another Example: switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; } Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 36. Switch - default • A switch statement can have an optional default case • The default case has no associated value and simply uses the reserved word default • If the default case is present, control will transfer to it if no other case value matches • If there is no default case, and no other value matches, control falls through to the statement after the switch Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 37. The switch Statement switch (option){ case 'A': aCount++; break; case 'B': bCount++; break; case 'C': cCount++; break; default: otherCount++; break; } • Switch with default case: Faculty of Information Technology, Thai-Nichi Institute of Technology
  • 38. To Switch or not to Switch • The expression of a switch statement must result in an integral type, meaning an integer (byte, short, int, long) or a char • It cannot be a boolean value or a floating point value (float or double) • The implicit boolean condition in a switch statement is equality • You cannot perform relational checks with a switch statement Faculty of Information Technology, Thai-Nichi Institute of Technology