Download presentation
Presentation is loading. Please wait.
Published byChrystal Edwards Modified over 9 years ago
2
if (condition) { // always either true or false then clause } if (condition) {// always either true or false then clause } else { // optional else clause else clause }
3
if (it is raining) { then I will need my umbrella } if (your_age > 65) { then you can get your state pension } condition then clause
4
if (it is raining) { then I will need my umbrella } else { I will not need my umbrella } If (your_age > 65) { then you can get your state pension } else { you cannot get your state pension } condition then clause else clause
5
if (it is raining or snowing) then I will need my umbrella } else { I will not need my umbrella (because it is not raining and it is not snowing) } if (it is sunny and hot) { then I will need a sunshade and a cold drink } else { this means it is not both sunny and hot it might be sunny but cold, or it might be cloudy but hot }
6
if (it is sunny) { if (it is hot) { then I will need sunshade and drink } else { I will need a sunshade } } else { if (it is hot) { then I will need a cold drink } else { I will need neither sunshade nor drink } then clause else clause (sunny but not hot) (not sunny but hot) (not sunny and not hot) (sunny and hot)
7
Demonstrate use of “selection” in the design and development of a Java program to guess who the user is thinking of. ◦ James has fair hair and blue eyes. ◦ Suzie has brown hair and brown eyes. ◦ Jessica has black hair and brown eyes. ◦ David has brown hair and blue eyes. The program initially needs to ask two questions to find out: - 1.the colour of hair (fair, or brown, or black) and 2.the colour of eyes (blue or brown). Design and code a solution to the problem. Depending on how you solve the problem, your answer will need to contain several if.. then.. else statements. Think about the meaning. James: fair and blue Suzie: brown and brown Jessica: black and brown David: brown and blue James is the only one with fair hair, Jessica is the only one with black hair, if we check for these first then we only need to check for eye colour to find the third person, and then only one person is left.
8
1. Get unknown data. 1.1 get colour of hair. 1.2 get colour of eyes. 2. Processing. 2.1 if hair is fair 2.2 then it is James 2.3 else if hair is black 2.4 then it is Jessica 2.5 else if eyes are blue 2.6 then it is David 2.7 else it is Suzie. 3. Output. 3.1 Output the name of the person. Think about the meaning. James: fair and blue Suzie: brown and brown Jessica: black and brown David: brown and blue James is the only one with fair hair, Jessica is the only one with black hair, if we check for these first then we only need to check for eye colour to find the third person, and then only one person is left.
9
22 [hair is fair] Ask for hair colour Ask for colour of eyes Person is James Person is Jessica Person is David [hair is black] [eyes are not blue] Person is Suzie Output name of person [hair is not fair] [hair is not black] [eyes are blue]
10
import javax.swing.JOptionPane; public class week8 { public static void main (String[] args) { String hair = JOptionPane.showInputDialog(null,"Enter hair colour"); String eye = JOptionPane.showInputDialog(null,"Enter eye colour"); String name = " "; if (hair.equals("fair")) { // first if name = "James"; } else { if (hair.equals("black")) { // second if name = "Jessica"; } else { if (eye.equals("blue")) { // third if name = "David"; } else { // no need for if name = "Suzie"; } // end third if } // end second if } // end first if System.out.println("The name is " + name); } // end main } // end class week8 Get colour of hair and eyes If hair is fair Then it is James If hair is black Then it is Jessica Then it is David If eyes are blue Else it is Suzie Output the name
11
1. Get unknown data. 1.1 get colour of hair. 1.2 get colour of eyes. 2. Processing. 2.1 if hair is fair 2.2 then it is James 2.3 if hair is black 2.4 then it is Jessica 2.5 how do we find David? 2.7 how do we find Suzie? 3. Output. 3.1 Output the name of the person. Think about the meaning. James: fair and blue Suzie: brown and brown Jessica: black and brown David: brown and blue James is the only one with fair hair, Jessica is the only one with black hair, How do we find the difference between Suzie and David for hair colour, or James and David for eye colour?
12
22 [hair is fair] Ask for hair colour Ask for colour of eyes Person is James Person is Jessica Person is David [hair is black] [???] Person is Suzie Output name of person [hair is not fair] [hair is not black] [???]
13
Demonstrate use of “selection” in the design and development of a Java program to guess who the user is thinking of. ◦ James has fair hair and blue eyes. ◦ Suzie has brown hair and brown eyes. ◦ Jessica has black hair and brown eyes. ◦ David has brown hair and blue eyes. The program initially needs to ask two questions to find out: - 1.the colour of hair (which will be either black or brown) and 2.the colour of eyes (which will be either blue or brown). Design and code a solution to the problem. Depending on how you solve the problem, your answer will need to contain several if.. then.. else statements. Guess the name example Another solution. James: check for fair hair and blue eyes Suzie: check for brown hair and brown eyes Jessica: check black hair and brown eyes David: check brown hair and blue eyes
14
22 [hair is fair] Ask for hair colour Ask for colour of eyes Person is James Person is Jessica Person is David [hair is black] [hair is brown and eyes are brown] Person is Suzie Output name of person [hair is not fair] [hair is not black] [hair is brown and eyes are blue]
15
1. Get unknown data. 1.1 get colour of hair. String hair = JOptionPane.showInputDialog(null,"Enter hair colour"); 1.2 get colour of eyes. String eye = JOptionPane.showInputDialog(null,"Enter eye colour"); 2. Processing. 2.1 if hair is fair and eyes are blue if (hair.equals("fair")&& eye.equals("blue")) { 2.2 then it is James name = "James"; } 2.3 if hair is black and eyes are brown if (hair.equals("black"))&& eye.equals("brown")) { 2.4 then it is Jessica name = "Jessica"; } 2.5 if hair is brown and eyes are blue if (hair.equals("brown"))&& (eye.equals("blue")) { 2.6 then it is David name = "David"; } 2.7 if hair is brown and eyes are brown if (hair.equals("brown"))&& (eye.equals("brown")) { 2.8 then it is Suzie. name = "Suzie"; } 3. Output. 3.1 Output the name of the person. System.out.println("The name is " + name);
16
1. Get unknown data. 1.1 get colour of hair. String hair = JOptionPane.showInputDialog(null,"Enter hair colour"); 1.2 get colour of eyes. String eye = JOptionPane.showInputDialog(null,"Enter eye colour"); 2. Processing. 2.1 if hair is fair and eyes are blue if (hair.equals("fair")&& eye.equals("blue")) { 2.2 then it is James name = "James"; 2.3 else if hair is black and eyes are brown } else {if (hair.equals("black"))&& eye.equals("brown")) { 2.4 then it is Jessica name = "Jessica"; 2.5 else if hair is brown and eyes are blue } else {if (hair.equals("brown"))&& (eye.equals("blue")) { 2.6 then it is David name = "David"; 2.7 else it is Suzie. (no need for “if” here) } else { name = "Suzie"; 3. Output. 3.1 Output the name of the person. System.out.println("The name is " + name);
17
Activity Diagram 22 [hair is fair and eyes are blue] Ask for hair colour Ask for colour of eyes Person is James Person is Jessica Person is David [hair is black and eyes are brown] [hair is not brown or eyes are not blue] Person is Suzie Output name of person [hair is not fair or eyes are not blue] [hair is not black or eyes are not brown] [hair is brown and eyes are blue]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.