Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows...

25
Introduction to Computing Concepts Note Set 15

Transcript of Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows...

Page 1: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Introduction to Computing ConceptsNote Set 15

Page 2: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

JOptionPane.showMessageDialog•Message Dialog•Allows you to give a brief message to the user

•Can be used to give helpful messages, errors, or warnings.

Page 3: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Example

import javax.swing.JOptionPane;

public class Tester {

public static void main (String [] args) {

JOptionPane.showMessageDialog(null, "Hello There!!!");

}

}

Page 4: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

JOptionPane.showInputDialog•Pops up a dialog and allows the user to enter a string•Method returns a string

•Can be used to ask for strings or numbers.▫Must convert string version of number to numerical

type

Page 5: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Example

import javax.swing.JOptionPane;

public class Tester { public static void main (String [] args) { String data; data = JOptionPane.showInputDialog(null,

"Please input your name."); System.out.println("Hello“ + data); }}

Page 6: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

•Use JOptionPane to get 5 numbers from the user then display to the screen.

public static void main(String [] args) {

Page 7: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Breakout 1

Page 8: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

do…while Loop

do //some Java Statementwhile (expression);

do { //some //statements //can go here} while (expression);

single statementno braces

multiple statementsneed braces

Page 9: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Two Types of Loops

do { //some //statements //can go here} while (expression);

VS:

while(expression) { //do something //here}

Page 10: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

What is Displayed

int x = 1;while (x < 0) System.out.println(x);

do System.out.println(x);while (x < 0);

Page 11: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

What is Displayed?

int c = 10;do { System.out.println(“hello”); c++;} while (c < 4);

Page 12: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

What is Displayed?

int v = 0;do { v++; System.out.println(v); } while (v < 4);

Page 13: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

What is Displayed?int count = 0;int funny = 1;int serious = 0;int limit = 4;do { funny ++; serious = serious + 2; count = count + 1;} while (count < limit);System.out.println(funny + “ “ + serious + “ “ + count );

Page 14: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

QuickCode•Write a snippet of code that asks the user to enter

integers until the sum of the numbers entered is greater than 300. Use a do…while loop

Page 15: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Nested Loops• It is possible to have one looping structure inside

another

int first = 0;int second = 0;while (first < 3) { do { System.out.print(“*”); //doesn’t go to next line second += 1; }while (second < 4); System.out.println(); first += 1; second = 0;}

Page 16: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

Breakout 2

Page 17: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

17

For Loop

• Ideal for situations that require a counter

•Has built in expressions that initialize and update variables

Page 18: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

18

For Loop

for (initialization; test; update)

statement;

for (initialization; test; update){

statement;statement;//Place as many here as needed

}

Page 19: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

19

For Loop

for (initialization; test; update)

statement;

Used to set up counter variable

Page 20: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

20

For Loop

for (initialization; test; update)

statement;

Controls execution of loop

Page 21: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

21

For Loop

for (initialization; test; update)

statement;

Updates (increments) counter variable

Page 22: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

22

For Loop

for (int num=0; num<4; num++)

System.out.println(num + “\t” + num*num);

1. Perform initialization of counter

Page 23: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

23

for (int num=0; num<4; num++)

System.out.println(num + “\t” + num*num);

For Loop

1. Perform initialization of counter

2. Evaluate test

• if num < 4, go to step 3

• else terminate loop

Page 24: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

24

for (int num=0; num<4; num++)

System.out.println(num + “\t” + num*num);

For Loop

1. Perform initialization of counter

2. Evaluate test

3. Execute statement

Page 25: Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.

25

for (int num=0; num<4; num++)

System.out.println(num + “\t” + num*num);

For Loop

1. Perform initialization of counter

2. Evaluate test

3. Execute statement

4. Perform update – go back to step 2