Link Search Menu Expand Document

Question 1

Type in and run the following program:

public class DialogViewer {
   public static void main(String[] args) {
      Scanner s = new Scanner(System.in);
      System.out.println("What's your favorite food?");
      String name = s.nextLine();
      System.out.println(name); }
}

Part i

[3 pts]

Modify the program to show the message: "Oh, I love <FOOD>." where <FOOD> is replaced with the food that the user answers.

Part ii

[7 pts]

Modify the program again so that it prompts the user for input again by asking exactly "How do you like that cooked?". You should then print out the message "Disgusting.". Finally, you should print out the message "I prefer my <FOOD> deep fried.". Again, replace <FOOD> with whatever the user originally responded with.

For example:

What's your favorite food?
>> apples
Oh, I love apples.
How do you like that cooked?
>> I don't cook apples, you freak.
Disgusting.
I prefer my apples deep fried.

Make sure to match the provided questions/statements exactly.

Question 2

The following parts will require you to study the documentation for Java’s built-in Rectangle class.

Part i

[10 pts]

Write a program PerimeterTester.java that uses a Scanner and nextInt() to prompt the user first for the height and then for the width of a rectangle. The program should then construct a Rectangle object with those dimensions and then compute and print its perimeter. Although you may already have the height and width saved as variables, you should use the getWidth and getHeight methods to do this computation on the rectangle that you’ve created.

Example execution:

Height?
>> 4
Width?
>> 7
22.0

Part ii

[10 pts]

Write a program IntersectionTester.java that will test the behavior of Java’s Rectangle class. This is going to be a basic form of unit test, which is a piece of code that checks whether one method’s behavior is following expectations.

Specifically, construct two Rectangles. The first Rectangle should have a width of 7, a height of 4, and have its upper-left corner at the point (1, 1). The second Rectangle should have a width of 2, a height of 3, and be have its upper-left corner at the point (0, 3). Use the intersects(Rectangle r) method to print out true if the Rectangles intersect and false otherwise. Check for yourself: does your printed result make sense geometrically given the dimensions of the Rectangles you were provided?


You can submit all of the work you have completed until this point on Gradescope. Use the assignment marked “Homework 1 Programming”. Submit DialogViewer.java for Q1, PerimeterTester.java for Q2.i, and IntersectionTester.java for Q2.ii.

Question 3

These questions do not require that you write any code. Instead, you will answer these questions in the Gradescope assignment marked “Homework 1 Short Answers”.

1.

What is the value of mystery after this sequence of statements?

int mystery = 1;
mystery = 1 - 2 * mystery;
mystery = mystery + 1;

2.

This question is intended to give you some practice in searching through the official Java documentation. You’ll have to look at the documentation of the String class, the Rectangle class, and the Random class. Look at the java.oracle documentation.

Consult the API documentation to find methods for the following operations. For each method, list the class in which it is defined, the return type, the method name, and the types of the arguments in the order they are expected.

  1. Concatenating two strings, that is, making a string consisting of the first string, followed by the second string.
  2. Removing leading and trailing white space of a string.
  3. Converting a rectangle object to a String object.
  4. Returning a random floating-point number.

Be careful to report these answers precisely as they are given in the documentation.

3.

[15 points]

List at least five compile-time errors in the following program and indicate how you would fix them. Note that the line numbers are numbered so that you can easily reference the lines when listing the errors.

Try first to do this without using Eclipse–you can correct your work using Eclipse afterwards, but you should learn to rely on your own perception of these issues for faster programming in the future.

1. public class HasErrors { 
2.     public static void main(); { 
3.         System.out.print(Please enter two numbers:) 
4.         x = in.readDouble; 
5.         y = in.readDouble; 
6.         System.out.printline("The sum is " + x + y);
7.      }
8. }