Facebook has hired you to help write programs that interact with the user and his or her friends.
Your boss has asked you to write a program that will automatically group your friends into different
categories.
Let's review if statements.
Basic Format
if( age < 18 )
{
eligible = false;
}
age < 18 is the condition in the above example. If age is less than 18, then we set the variable
eligible to false. If age is not less than 18, then we don't set eligible to false.
We can also add an else or an else if clause on the end of our if statement.
Example:
The following series of if statements will assign a rotation for class registration based on a student's initial.
if( initial < 'G')
{
rotation = 1;
}
else if ( initial > 'M' )
{
rotation = 3;
}
else
{
rotation = 2;
}
Your first task is to group friends by age into 5 categories.
- A) Under 13
- B) 13-17
- C) 18-21
- D) 22-30
- E) 31+
Let's review some examples to make sure we understand how this should work. Given an input age, what is the expected category?
Age (Input)
Expected Category (Output)
14
B
50
E
12
A
Now it's your turn. Enter the category that corresponds with the given age:
Age (Input)
Expected Category (Output)
13
17
30
40
9
regex
^\s*B\s*$
failed-message
I'm sorry, the first row of the first table is incorrect.
success-message
group1
regex
^\s*B\s*$
failed-message
I'm sorry, the 2nd row of the first table is incorrect.
success-message
group2
regex
^\s*D\s*$
failed-message
I'm sorry, the 3rd row of the first table is incorrect.
success-message
group3
regex
^\s*E\s*$
failed-message
I'm sorry, the 4th row of the first table is incorrect.
success-message
group4
regex
^\s*A\s*$
failed-message
I'm sorry, the last row of the first table is incorrect.
success-message
group5
Now let's go the other way. Enter an age that would correspond with the given category.
Age
Expected Category
A
B
C
D
E
regex
^\s*[0-9]|10|11|12\s*$
failed-message
I'm sorry the first row of the 2nd table is incorrect.
success-message
age1
regex
^\s*13|14|15|16|17\s*$
failed-message
I'm sorry the 2nd row of the 2nd table is incorrect.
success-message
age2
regex
^\s*18|19|20|21\s*$
failed-message
I'm sorry the 3rd row of the 2nd table is incorrect.
success-message
age3
regex
^\s*22|23|24|25|26|27|28|29|30\s*$
failed-message
I'm sorry the 4th row of the 2nd table is incorrect.
success-message
age4
regex
^\s*[3][1-9]|[4-9][0-9]|[1-9][1-9][0-9]\s*$
failed-message
I'm sorry the last row of the 2nd table is incorrect, or you have inputted a vastly unrealistic human age.
success-message
Good Job!
age5
How can we put friends of different ages into different groups?
You can use an if statement to assign friends of specific ages to a certain group.
Assume that the variables age and group have been defined as int and char respectively.
For example: under 13
if ( age < 13 )
{
group = 'A';
}
Notice that the expression within the if statement determines which friends belong
in the group and the assignment inside the then block tells in which group these friends belong.
Now write the if statement for Group E (ages 31+). Remember to include age 31 in your expression!
regex
\s*if\s*\(\s*age\s*((>=\s*31)|(>\s*30))\s*\)\s*\{\s*group\s*=\s*'E'\s*;\s*\}\s*
failed-message
I'm sorry your if statement is incorrect, please try again.
success-message
Good Job!
groupE
Great job! Now let's try to group some of our other friends.
For example: ages 13-17
if ( age >= 13 && age <= 17)
{
group = 'B';
}
Notice that we have to make a separate comparison for each condition that we want to check.
We also used the && operator to make multiple comparisons.
Now write the if statements for the other groups.
Group C (ages 18-21)
function
public char assignGroup(int age) {
char group = 'x';
@groupC
return group;
}
functionCall
assignGroup(18)
expectedOutput
'C'
smessage
Good Job!
fmessage
I'm sorry, your if statement for group C is incorrect.
groupC
Group D (ages 22-30)
function
public char assignGroup(int age) {
char group = 'x';
@groupD
return group;
}
functionCall
assignGroup(22)
expectedOutput
'D'
smessage
Good Job!
fmessage
I'm sorry, your if statement for group D is incorrect.
groupD
Great! When we put all these if statements together, we can use else if since the conditions are
mutually exclusive. You should get something that looks like this:
if ( age < 13 )
{
group = 'A';
} else if ( age >= 13 && age <= 17)
{
group = 'B';
} else if ( age >= 18 && age <= 21)
{
group = 'C';
} else if ( age >= 22 && age <= 30)
{
group = 'D';
} else if ( age >= 31 ) //could even leave off this last if and just set group to 'E'
{
group = 'E';
}
Next you want to sort friends into groups by the first letter of their name.
- a) A-G
- b) H-M
- c) N-Z
Test and Check!
Input proper values for both initial and group in the format: Initial, group.
Input (initial)
Expected Ouput (group)
A
a
function
public int assignGroup(char initial, char group) { if(group == 'a') { if(initial < 'G') return 1; } if (group == 'b') { if(initial >= 'H' && initial <= 'M') return 1; } if (group == 'c') { if(initial >= 'N' && initial <= 'Z') return 1; } return 0; }
functionCall
assignGroup('@test1', '@testb1')
expectedOutput
1
smessage
fmessage
I'm sorry, your test is incorrect
test1
testb1
function
public int assignGroup(char initial, char group) { if(group == 'a') { if(initial < 'G') return 1; } if (group == 'b') { if(initial >= 'H' && initial <= 'M') return 1; } if (group == 'c') { if(initial >= 'N' && initial <= 'Z') return 1; } return 0; }
functionCall
assignGroup('@test2', '@testb2')
expectedOutput
1
smessage
fmessage
I'm sorry, your test is incorrect
test2
testb2
function
public int assignGroup(char initial, char group) { if(group == 'a') { if(initial < 'G') return 1; } if (group == 'b') { if(initial >= 'H' && initial <= 'M') return 1; } if (group == 'c') { if(initial >= 'N' && initial <= 'Z') return 1; } return 0; }
functionCall
assignGroup('@test3', '@testb3')
expectedOutput
1
smessage
Good Job!
fmessage
I'm sorry, your test is incorrect
test3
testb3
We can use if statements to assign friends with different names to different groups.
Assume we have a char variable named initial that contains the first initial of our friend's last name.
For example: A-G
if ( initial <= 'G')
{
group = 'a';
}
Again, the expression within the if statement determines which friends belong in the group,
and the assignment inside the then block tells in which group these friends belong.
Now you write the if statement for Group b (H-M)
Recall!
In the previous exercise we used
age >= 13 && age <= 17
for friends ages 13 to 17.
This one is similar!
Group b (H-M)
function
public char assignGroup(char initial) { char group = 'x'; @groupb; return group; }
functionCall
assignGroup('M')
expectedOutput
'b'
smessage
fmessage
I'm sorry, your if statement for group b is incorrect
groupb
Group c (N-Z)
function
public char assignGroup(char initial) { char group = 'x'; @groupc return group; }
functionCall
assignGroup('Z')
expectedOutput
'c'
smessage
Good Job!
fmessage
I'm sorry, your if statement for group c is incorrect
groupc
Great! Now let's sort our friends by age and by name.
We will use the same age groupings.
- A) Under 13
- B) 13-17
- C) 18-21
- D) 22-30
- E) 31+
- and within each group you will have alphabetic groups
- a) A-G
- b) H-M
- c) N-Z
Our group names will now be a string that is the combination of the age and alphabetic group characters.
Test and Check!
Example:
Age (Input)
Initial (Input)
Group (Expected Output)
14
A
Ba
50
M
Eb
12
S
Ac
Enter the correct group that corresponds with the age and initial inputted.
Age (Input)
Initial (Input)
Group (Expected Output)
12
R
17
S
30
C
40
L
21
M
regex
\s*Ac\s*
failed-message
I'm sorry your first line of input is incorrect, please try again.
success-message
groupAc
regex
\s*Bc\s*
failed-message
I'm sorry your 2nd line of input is incorrect, please try again.
success-message
groupBc
regex
\s*Da\s*
failed-message
I'm sorry your 3rd line of input is incorrect, please try again.
success-message
groupDa
regex
\s*Eb\s*
failed-message
I'm sorry your 4th line of input is incorrect, please try again.
success-message
groupEb
regex
\s*Cb\s*
failed-message
I'm sorry your last line of input is incorrect, please try again.
success-message
Good Job!
groupCb
Enter an age and initial that would correspond with the group output given.
Age (Input)
Initial (Input)
Group (Expected Output)
Ac
Ba
Ca
Db
Ec
function
public boolean assignGroup(int age, char initial) { if(age < 13) { if(initial >= 'N') return true; } return false; }
functionCall
assignGroup(@input1, '@inputb1')
expectedOutput
true
smessage
fmessage
I'm sorry, your first row of table 2 is incorrect
input1
inputb1
function
public boolean assignGroup(int age, char initial) { if(age >= 13 && age <= 17) { if(initial <= 'G') return true; } return false; }
functionCall
assignGroup(@input2, '@inputb2')
expectedOutput
true
smessage
fmessage
I'm sorry, your 2nd row of table 2 is incorrect
input2
inputb2
function
public boolean assignGroup(int age, char initial) { if(age >= 18 && age <= 21) { if(initial <= 'G') return true; } return false; }
functionCall
assignGroup(@input3, '@inputb3')
expectedOutput
true
smessage
fmessage
I'm sorry, your 3rd row of table 2 is incorrect
input3
inputb3
function
public boolean assignGroup(int age, char initial) { if(age >= 22 && <= 30) { if(inital >= 'H' && initial <= 'M') return true; } return false; }
functionCall
assignGroup(@input4, '@inputb4')
expectedOutput
true
smessage
fmessage
I'm sorry, your 4th row of table 2 is incorrect
input4
inputb4
function
public boolean assignGroup(int age, char initial) { if(age >= 31) { if(initial >= 'N') return true; } return false; }
functionCall
assignGroup(@input5, '@inputb5')
expectedOutput
true
smessage
Good Job!
fmessage
I'm sorry, your last row of table 2 is incorrect
input5
inputb5
How can we categorize friends by two different factors?
You can use nested if statements to examine friends with multiple characteristics.
Since our group variable now contains two characters, we will assume it has been declared as a String,
and we will use double quotes.
For example: under 13
if ( age < 13 )
{
if( initial <= 'G')
{
group = "Aa";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Ab";
} else if( initial >= 'N')
{
group = "Ac";
}
}
You know the drill. Now it is your turn to write the if statements for all of your friends using both age and initial.
Group B (ages 13-17)
function
public String assignGroup(int age, char initial) { String group = "blah"; @groupB2 return group; }
functionCall
assignGroup(13, 'B')
expectedOutput
"Ba"
smessage
fmessage
I'm sorry, your if statement for group b is incorrect
groupB2
Group C (ages 18-21)
function
public String assignGroup(int age, char initial) { String group = "blah"; @groupC2 return group; }
functionCall
assignGroup(19, 'Z')
expectedOutput
"Cc"
smessage
fmessage
I'm sorry, your if statement for group c is incorrect
groupC2
Group D (ages 22-30)
function
public String assignGroup(int age, char initial) { String group = "blah"; @groupD2 return group; }
functionCall
assignGroup(30, 'M')
expectedOutput
"Db"
smessage
fmessage
I'm sorry, your if statement for group d is incorrect
groupD2
Group E (ages 31+)
function
public String assignGroup(int age, char initial) { String group = "blah"; @groupE2 return group; }
functionCall
assignGroup(31, 'Z')
expectedOutput
"Ec"
smessage
Good Job!
fmessage
I'm sorry, your if statement for group e is incorrect
groupE2
Great! When we put all these if statements together, you should get something that looks like this:
if ( age < 13 )
{
if( initial <= 'G')
{
group = "Aa";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Ab";
} else if( initial >= 'N')
{
group = "Ac";
}
} else if ( age >= 13 && age <= 17)
{
if( initial <= 'G')
{
group = "Ba";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Bb";
} else if( initial >= 'N')
{
group = "Bc";
}
} else if ( age >= 18 && age <= 21)
{
if( initial <= 'G')
{
group = "Ca";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Cb";
} else if( initial >= 'N')
{
group = "Cc";
}
} else if ( age >= 22 && age <= 30)
{
if( initial <= 'G')
{
group = "Da";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Db";
} else if( initial >= 'N')
{
group = "Dc";
}
} else if ( age >= 31 )
{
if( initial <= 'G')
{
group = "Ea";
} else if( initial >= 'H' && initial <= 'M')
{
group = "Eb";
} else if( initial >= 'N')
{
group = "Ec";
}
}