Java Object Oriented Programming Sentinel Controlled Loops Objectives

Java Object Oriented Programming Sentinel Controlled Loops

Objectives: Learn to use a sentinel-controlled loop. Lab 15 -2

Sentinel-Controlled § In a Sentinel-Controlled loop, a special value called a sentinel value is used to change the loop control expression from true to false. § For example, when reading data we may indicate the "end of data" by a special value, like -1 or 999. § The control variable is called the sentinel variable. Lab 15 -3

Sentinel-Controlled (cont’d) int n = reader. next. Int(); initialize the sentinel variable NOTE: reader is an instance of a Scanner object reading from the keyboard or a data file. Lab 15 -4

Sentinel-Controlled (cont’d) int n = reader. next. Int(); while ( n != -1 ) { initialize the sentinel variable Loop as long as the sentinel variable is NOT -1 } NOTE: reader is an instance of a Scanner object reading from the keyboard or a data file. Lab 15 -5

Sentinel-Controlled (cont’d) int n = reader. next. Int(); while ( n != -1 ) { statement 1; statement 2; . . . n = reader. next. Int(); } initialize the sentinel variable Loop as long as the sentinel variable is NOT -1 change the value of the sentinel variable NOTE: reader is an instance of a Scanner object reading from the keyboard or a data file. Lab 15 -6

Java Object Oriented Programming Top Down Design

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

Top Down Design (cont’d) § You are the Historian for the Starship Enterprise. Your job is to keep a record of all the planets visited by the Enterprise during it’s five year mission. § Write a program that reads the names of planets from the keyboard storing them in an array of type String. § Sort the array of Strings and display them on the console screen. Lab 15 -9

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

Top Down Design (cont’d) import java. util. *; public class Lab 15 { public static void main(String[ ] args) { Lab 15 lab = new Lab 15( ); lab. input(); // Enter data from the kybd lab. process(); // Sort the array lab. output( ); // Display output } } Lab 15 -11

Top Down Design (cont’d) We need two instance fields to solve the problem. Lab 15 -12
![Top Down Design (cont’d) public class Lab 15 { private String[] planets = new Top Down Design (cont’d) public class Lab 15 { private String[] planets = new](http://slidetodoc.com/presentation_image_h2/3d8aba494db33d538af5a3881ba57cdb/image-13.jpg)
Top Down Design (cont’d) public class Lab 15 { private String[] planets = new String[20]; private int count; public static void main(String[ ] args) { Lab 15 lab = new Lab 15( ); lab. input(); // Enter data from the kybd lab. process(); // Sort the array lab. output( ); // Display output } } Lab 15 -13

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

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

Top Down Design (cont’d) public void process() { Arrays. sort(planets, 0, count); } Lab 15 -16

Top Down Design (cont’d) output( ) Lab 15 -17

Top Down Design (cont’d) public void output() { System. out. println(“These are the voyages of the Starship Enterprise: ”); for (int i = 0; i < count; i++) System. out. println(planets[i]); System. out. println(); } Lab 15 -18

Top Down Design (cont’d) input( ) Lab 15 -19

Top Down Design (cont’d) public void input() { } Lab 15 -20

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); } Lab 15 -21

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”); } Lab 15 -22

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”); String planet = reader. next. Line(); } Lab 15 -23

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”) String planet = reader. next. Line(); while (!planet. equals. Ignore. Case(“quit”)) { } } Lab 15 -24

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”) String planet = reader. next. Line(); while (!planet. equals. Ignore. Case(“quit”)) { planets[count++] = planet; } } Lab 15 -25

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”); String planet = reader. next. Line(); while (!planet. equals. Ignore. Case(“quit”)) { planets[count++] = planet; System. out. print(“Name of the Planet (“quit” to exit): ”); } } Lab 15 -26

Top Down Design (cont’d) public void input() { Scanner reader = new Scanner(System. in); System. out. print(“Name of the Planet (“quit” to exit): ”); String planet = reader. next. Line(); while (!planet. equals. Ignore. Case(“quit”)) { planets[count++] = planet; System. out. print(“Name of the Planet (“quit” to exit): ”); planet = reader. next. Line(); } } Lab 15 -27

Top Down Design (cont’d) Run The Program: Name of the Planet ("quit" to exit): Remus Name of the Planet ("quit" to exit): Sauria Name of the Planet ("quit" to exit): Alderaan Name of the Planet ("quit" to exit): Tyree Name of the Planet ("quit" to exit): Vulcan Name of the Planet ("quit" to exit): Mudd Name of the Planet ("quit" to exit): Xantoras Name of the Planet ("quit" to exit): Yadera Prime Name of the Planet ("quit" to exit): Zeon Name of the Planet ("quit" to exit): Borg Prime Name of the Planet ("quit" to exit): quit Lab 15 -28

Top Down Design (cont’d) Run The Program: These are the planets visited by the Starship Enterprise: Alderaan Borg Prime Mudd Remus Sauria Tyree Vulcan Xantoras Yadera Prime Zeon Lab 15 -29

Java Object Oriented Programming Questions?

Java Object Oriented Programming Begin Lab 15
- Slides: 31