|
To do this assignment, you need to download the BeanShell interpreter.
Download Beanshell
The file that you download, bsh-2.0b1.jar , is not an installer. It is the actual BeanShell program. You just double click on this file to start up BeanShell. You use BeanShell by typing in a Java statement at the BeanShell prompt and then hitting the Enter key. BeanShell executes the Java statement and then gives you another prompt. Remember that BeanShell will only print out some output if the Java statement is a print() statement or a System.out.println() statement.
Use BeanShell to execute Java statements that implement each of the following steps (in the order that they are given) listed below. When you have done all of the steps, use your mouse to highlight all of the text in the BeanShell window (be sure to highlight all of the text, not just the text visible in the window). Then hit the CTRL-C key combination to copy the text in the BeanShell window into the clipboard. Open any editor, use CTRL-V to paste the text copied from BeanShell into the editor, and save the text as a file called CS123Ass3Surname.txt . Turn in this file. (Unfortunately, BeanShell does not have a way to save the contents of a BeanShell window, so you have to use copy and paste and an editor.)
The following instructions all involve the Rectangle class, which is built into Java. There is online documentation for every built in Java class, including the Rectangle class. Part of the documentation page for each class is a list of the data items that are stored in an object of that class. The data items are called "fields". Here is the list of fields for the Rectangle class (notice that there are four fields). Another part of the documentation page for every class is a list of the messages that you can send to an object of that class. The messages are called "methods". Here is the list of methods for the Rectangle class (notice that there are a lot of them). If you click on the name of a method, you are taken to a part of the documentation page that gives detailed information about that method.
- Create a
Rectangle object named r1 that has its upper left hand corner at x=0 and y=0 and dimensions height=10 and width=10.
- Create a
Rectangle object named r2 that has its upper left hand corner at x=5 and y=5 and dimensions height=10 and width=10.
- Send the object named
r1 the message that it should compute its intersection with the object named r2 and have the resulted printed out.
- Declare an object reference variable named
r3 of type Rectangle .
- Have BeanShell print out the value of the reference variable
r3 .
- Have BeanShell print out the value of the variable
r4 (which has not been declared yet).
- Send the object named r2 the message that it should compute its
intersection with an anonymous Rectangle object that has its upper left hand corner at x=5 and y=10 and dimensions height=5 and width=5. Have the result assigned to the reference variable r3 .
- Have BeanShell print out the value of the variable
r3 .
- In one command, declare the variable
r4 to be of type Rectangle and initialize it with the result of sending an anonymous Rectangle object (with upper left hand corner at x=15 and y=15 and dimensions height=10 and width=10) the message that it should compute its intersection with the object named r1 .
- Have BeanShell print the value of the variable
r4 . Notice that the two rectangles in the last step did not intersect. Use a C-style comment in the BeanShell window to write out answers to the following two questions. How might you interpret the result that is stored in the object named r4 as a rectangle? What does the online documentation page referenced above say about the intersection of two rectangles that do not intersect?
- Send the object named
r1 the message that it should translate itself 10 units in the x direction and 15 units in the y direction.
- Have BeanShell print the value of the variable
r1 .
- Every class has a "default constructor", which is a constructor that does not take any arguments. Assign the variable
r1 to a Rectangle object constructed with the default Rectangle constructor. Print out the value of r1 to see what values the default constructor stored in the object referred to by r1 .
- From the online list of methods for the
Rectangle class mentioned above, choose a method that has not been used so far in this assignment and use that method to send a message to the object created in the last step. Print out the value of the object to see what, if any, was the effect of the message on the object. Use a C-style comment to describe in your own words what the purpose of the message is.
|
|