Question 1 SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.…
- User
Question 1 SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate. Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are called only when their preconditions are satisfied. In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will not receive full credit.
Assume that the following variables have been properly declared and initialized.
a boolean variable named rsvp an int variable named selection, where 1 represents "beef", 2 represents "chicken", 3 represents "pasta", and all other values represent "fish" a String variable named option1 a String variable named option2 (a) Write a code segment that prints "attending" if rsvp is true and prints "not attending" otherwise.
Write the code segment below.
- Assistant
if (rsvp) { System.out.println("attending"); } else { System.out.println("not attending"); }
- User
(b) Write a code segment that prints the food item associated with selection. For example, if selection is 3, the code segment should print "pasta".
Write the code segment below. Your code segment should meet all specifications and conform to the example.
- Assistant
if (selection == 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"); }
- User
(c) Write a code segment that will store a dinner selection in option1 based on the values of rsvp and selection. The intended behavior of the code segment is described below.
If rsvp is true, the code segment should store in option1 a string indicating the person’s attendance and food choice. For example, if rsvp is true and selection is 1, the following string should be stored in option1.
"Thanks for attending. You will be served beef."
If rsvp is false, the following string should be stored in option1, regardless of the value of selection.
"Sorry you can't make it."
Write the code segment below. Your code segment should meet all specifications and conform to the examples.
- Assistant
if (rsvp) { if (selection == 1) { option1 = "Thanks for attending. You will be served beef."; } else if (selection == 2) { option1 = "Thanks for attending. You will be served chicken."; } else if (selection == 3) { option1 = "Thanks for attending. You will be served pasta."; } else { option1 = "Thanks for attending. You will be served fish."; } } else { option1 = "Sorry you can't make it."; }
- User
(d) Write a code segment that will print true if the strings option1 and option2 contain the same values and will print false otherwise.
Write the code segment below.
- Assistant
System.out.println(option1.equals(option2));
- User
- Assistant
int x1 = x; int y1 = y; int x2 = x + len; int y2 = y; int x3 = x + len; int y3 = y - len; int x4 = x; int y4 = y - len;
Draw.drawLine(x1, y1, x2, y2); Draw.drawLine(x2, y2, x3, y3); Draw.drawLine(x3, y3, x4, y4); Draw.drawLine(x4, y4, x1, y1);
int area = len * len; System.out.println("side length = " + len + ", area = " + area + ".");