Write a code segment that prints "attending" if rsvp is true and prints "not attending" otherwise.
if (rsvp)
{
System.out.println("attending");
}
else
{
System.out.println("not attending");
}
if (!rsvp) {
System.out.print("not ");
}
System.out.println("attending");
Write a code segment that prints the food item associated with selection. For example, if selection is 3, the code segment should print "pasta"
if (selction == 1)
{
System.out.println("beef");
}
else if (selection == 2)
{
System.out.println("chicken");
}
else if (selection == 3)
{
System.out.println("pasta");
}
else
{
System.out.println("fish");
}
Note that that was written as one multi-way selection, with all the
conditions chained together with else if
and a final
else
. It wouldn't work otherwise.
This will only really make sense once you've looked at Unit 6 and learned about arrays.
// Make an array of the possible choices.
// Line thnigs up so the 1-3 selections index the
// right elements.
String[] choices = { "fish", "beef", "chicken", "pasta" };
// This is is gnarly because "all other values" get fish.
int idx = 0 <= selection && selection < 4 ? selection : 0;
System.out.println(choices[idx]);
?:
The fancy alternative used an operator, called the
conditional operator. It's like an
if
statement but it's an expression, i.e. it produces a
value:
condition ? valueIfTrue : valueIfFalse
Optional material.
But sometimes it can make code a lot more elegant.
Also called the ternary operator because it has three operands.
Write a code segment that will store a dinner selection in option1 based on the values of rsvp and selection.
"Thanks for attending. You will be served beef."
or
"Sorry you can't make it."
if (rsvp) {
option1 = "Thanks for attending. You will be served ";
if (selction == 1) {
option1 += "beef.";
} else if (selection == 2) {
option1 += "chicken.";
} else if (selection == 3) {
option1 += "pasta.";
} else {
option1 += "fish.";
}
} else {
option1 = "Sorry you can't make it.";
}
String[] choices = { "fish", "beef", "chicken", "pasta" };
int idx = 0 <= selection && selection < 4 ? selection : 0;
if (rsvp) {
option1 = "Thanks for attending. You will be served ";
option1 += choices[idx] + ".";
} else {
option1 = "Sorry you can't make it.";
}
Write a code segment that will print true if the strings
option1
and option2
contain the same
values and will print false otherwise.
System.out.println(option1.equals(option2));
There are other ways to do it but there's really no reason to write anything other than this.
Assume that x
, y
, and len
,
have been properly declared and initialized, write the program code
to draw a square below.
if (x + len > 10)
{
len = 10 - x;
}
if (y - len < 0)
{
len = y;
}
Draw.drawLine(x, y, x + len, y);
Draw.drawLine(x + len, y, x + len, y - len);
Draw.drawLine(x + len, y - len, x, y - len);
Draw.drawLine(x, y - len, x, y);
System.out.println(
"side length = " + len + ", area = " + len * len);
int side = Math.min(len, Math.min(10 - x, y));
int x2 = x + side;
int y2 = y - side;
Draw.drawLine(x, y, x2, y);
Draw.drawLine(x, y2, x2, y2);
Draw.drawLine(x, y, x, y2);
Draw.drawLine(x2, y, x2, y2);
System.out.println(
"side length = " + side + ", area = " + (side * side));
if
statements are overrated.
They have their place but should not be overused.
The more you learn, the more other options you will have.