Java Software Solutions - Chapter 6: More Conditionals and Loops
Now we can fill in some additional details regarding Java conditional and repetition statements
Chapter 6 focuses on:
the switch statement
the conditional operator
the do loop
the for loop
drawing with the aid of conditionals and loops
dialog boxes
and " + limit + " (inclusive) are:"); for (mult = value; mult object The following loop will print each book: for (Book myBook : bookList) System.out.println (myBook); This version of a for loop is often called a for-each loop Copyright © 2012 Pearson Education, Inc. For-each Loops A for-each loop can be used on any object that implements the Iterable interface It eliminates the need to retrieve an iterator and call the hasNext and next methods explicitly It also will be helpful when processing arrays, which are discussed in Chapter 8 Copyright © 2012 Pearson Education, Inc. Quick Check Copyright © 2012 Pearson Education, Inc. Write a for-each loop that prints all of the Student objects in an ArrayList object called roster. Quick Check Copyright © 2012 Pearson Education, Inc. Write a for-each loop that prints all of the Student objects in an ArrayList object called roster. for (Student student : roster) System.out.println (student); Outline The switch Statement The Conditional Operator The do Statement The for Statement Drawing with Loops and Conditionals Dialog Boxes Copyright © 2012 Pearson Education, Inc. Drawing Techniques Conditionals and loops enhance our ability to generate interesting graphics See Bullseye.java See BullseyePanel.java See Boxes.java See BoxesPanel.java Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Bullseye.java Author: Lewis/Loftus // // Demonstrates the use of loops to draw. //******************************************************************** import javax.swing.JFrame; public class Bullseye { //----------------------------------------------------------------- // Creates the main frame of the program. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Bullseye"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); BullseyePanel panel = new BullseyePanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Bullseye.java Author: Lewis/Loftus // // Demonstrates the use of loops to draw. //******************************************************************** import javax.swing.JFrame; public class Bullseye { //----------------------------------------------------------------- // Creates the main frame of the program. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Bullseye"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); BullseyePanel panel = new BullseyePanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // BullseyePanel.java Author: Lewis/Loftus // // Demonstrates the use of conditionals and loops to guide drawing. //******************************************************************** import javax.swing.JPanel; import java.awt.*; public class BullseyePanel extends JPanel { private final int MAX_WIDTH = 300, NUM_RINGS = 5, RING_WIDTH = 25; //----------------------------------------------------------------- // Sets up the bullseye panel. //----------------------------------------------------------------- public BullseyePanel () { setBackground (Color.cyan); setPreferredSize (new Dimension(300,300)); } continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Paints a bullseye target. //----------------------------------------------------------------- public void paintComponent (Graphics page) { super.paintComponent (page); int x = 0, y = 0, diameter = MAX_WIDTH; page.setColor (Color.white); for (int count = 0; count < NUM_RINGS; count++) { if (page.getColor() == Color.black) // alternate colors page.setColor (Color.white); else page.setColor (Color.black); page.fillOval (x, y, diameter, diameter); diameter -= (2 * RING_WIDTH); x += RING_WIDTH; y += RING_WIDTH; } // Draw the red bullseye in the center page.setColor (Color.red); page.fillOval (x, y, diameter, diameter); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Boxes.java Author: Lewis/Loftus // // Demonstrates the use of loops to draw. //******************************************************************** import javax.swing.JFrame; public class Boxes { //----------------------------------------------------------------- // Creates the main frame of the program. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Boxes"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); BoxesPanel panel = new BoxesPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // Boxes.java Author: Lewis/Loftus // // Demonstrates the use of loops to draw. //******************************************************************** import javax.swing.JFrame; public class Boxes { //----------------------------------------------------------------- // Creates the main frame of the program. //----------------------------------------------------------------- public static void main (String[] args) { JFrame frame = new JFrame ("Boxes"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); BoxesPanel panel = new BoxesPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } } Copyright © 2012 Pearson Education, Inc. //******************************************************************** // BoxesPanel.java Author: Lewis/Loftus // // Demonstrates the use of conditionals and loops to guide drawing. //******************************************************************** import javax.swing.JPanel; import java.awt.*; import java.util.Random; public class BoxesPanel extends JPanel { private final int NUM_BOXES = 50, THICKNESS = 5, MAX_SIDE = 50; private final int MAX_X = 350, MAX_Y = 250; private Random generator; //----------------------------------------------------------------- // Sets up the drawing panel. //----------------------------------------------------------------- public BoxesPanel () { generator = new Random(); setBackground (Color.black); setPreferredSize (new Dimension(400, 300)); } continue Copyright © 2012 Pearson Education, Inc. continue //----------------------------------------------------------------- // Paints boxes of random width and height in a random location. // Narrow or short boxes are highlighted with a fill color. //----------------------------------------------------------------- public void paintComponent(Graphics page) { super.paintComponent (page); int x, y, width, height; for (int count = 0; count < NUM_BOXES; count++) { x = generator.nextInt(MAX_X) + 1; y = generator.nextInt(MAX_Y) + 1; width = generator.nextInt(MAX_SIDE) + 1; height = generator.nextInt(MAX_SIDE) + 1; continue Copyright © 2012 Pearson Education, Inc. continue if (width <= THICKNESS) // check for narrow box { page.setColor (Color.yellow); page.fillRect (x, y, width, height); } else if (height <= THICKNESS) // check for short box { page.setColor (Color.green); page.fillRect (x, y, width, height); } else { page.setColor (Color.white); page.drawRect (x, y, width, height); } } } } Outline The switch Statement The Conditional Operator The do Statement The for Statement Drawing with Loops and Conditionals Dialog Boxes Copyright © 2012 Pearson Education, Inc. Dialog Boxes A dialog box is a window that appears on top of any currently active window It may be used to: convey information confirm an action allow the user to enter data pick a color choose a file A dialog box usually has a specific, solitary purpose, and the user interaction with it is brief Copyright © 2012 Pearson Education, Inc. Dialog Boxes The JOptionPane class provides methods that simplify the creation of some types of dialog boxes See EvenOdd.java Specialized dialog boxes for choosing colors and files are covered in Chapter 9 Copyright © 2012 Pearson Education, Inc. Copyright © 2012 Pearson Education, Inc. /******************************************************************** // EvenOdd.java Author: Lewis/Loftus // // Demonstrates the use of the JOptionPane class. //******************************************************************** import javax.swing.JOptionPane; public class EvenOdd { //----------------------------------------------------------------- // Determines if the value input by the user is even or odd. // Uses multiple dialog boxes for user interaction. //----------------------------------------------------------------- public static void main (String[] args) { String numStr, result; int num, again; continue Copyright © 2012 Pearson Education, Inc. continue do { numStr = JOptionPane.showInputDialog ("Enter an integer: "); num = Integer.parseInt(numStr); result = "That number is " + ((num%2 == 0) ? "even" : "odd"); JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "Do Another?"); } while (again == JOptionPane.YES_OPTION); } } Copyright © 2012 Pearson Education, Inc. continue do { numStr = JOptionPane.showInputDialog ("Enter an integer: "); num = Integer.parseInt(numStr); result = "That number is " + ((num%2 == 0) ? "even" : "odd"); JOptionPane.showMessageDialog (null, result); again = JOptionPane.showConfirmDialog (null, "Do Another?"); } while (again == JOptionPane.YES_OPTION); } } Summary Chapter 6 focused on: the switch statement the conditional operator the do loop the for loop drawing with the aid of conditionals and loops dialog boxes Copyright © 2012 Pearson Education, Inc.
File đính kèm:
- LESSON_06_More Conditionals and Loops.pptx