Take-home labs are graded to provide you with feedback. However, the marks do not contribute to your final grade. Instead, one mark (attempt-mark) is awarded for each assignment -- and this mark goes into the computation of your final grade -- on the following conditions:
This lab requires you to do two exercises.
You are advised to review the material covered in chapters 1 through 7 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. You should not use syntax or constructs not yet covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined). If in doubt, please ask for clarification in lecture or in the IVLE discussion forum.
Remember to spend some time thinking about the algorithm for each exercise and write out the pseudo-code on your own before you start typing in your program.
A word of advice: Code incrementally. Do not type in the whole program (especially when it is long) and then compile it. Instead, type your program in bits and pieces and compile it frequently. Try to maintain a compilable program even while you are working on it. Submitting a compilable program that partially works is better than submitting an un-compilable one. This last point especially applies to your sit-in labs and practical exam.
Note that:
For more information on CourseMarker, please refer to CS1101 Labs page.
For this lab, the number of submissions is set to 10. Only your last submission will be graded.
If you have any questions, you may post your queries in the IVLE discussion forum. Do not post your programs (partial or complete) in the forum before the deadline!
Cuboids are 3-dimensional boxes. You are given these 3 codes:
We create two classes MyPoint3D and MyCuboid. MyCuboid uses the class MyPoint3D. Lab4Ex1 is an application of MyCuboid.
You are to write three programs: MyPoint3D.java, MyCuboid.java and Lab4Ex1.java.
MyPoint3D.java
MyPoint3D class consists of three private data members: the integer x-, y- and z-coordinates of a 3D point. These three members of MyPoint3D class have been included in the provided partial code, and you should NOT change them. Neither should you add new data members.
You should add these methods:
MyCuboid.java
MyCuboid class defines a right-angled cuboid where its edges are parallel to one of x-, y- or z-axis. The class consists of two private data members: the two opposite vertices of a cuboid. Each vertex is an object of class MyPoint3D. Figure 1 below shows a cuboid with its two opposite vertices highlighted. You may assume that all cuboids have non-zero volume.
Figure 1. Cuboid with opposite vertices A and B.
The data members of MyCuboid class have been included in the provided partial code, and you should NOT change them. Neither should you add new data members.
You should add these methods:
Lab4Ex1.java
The program Lab4Ex1.java is to create two MyCuboid objects, check whether they are the same, and compute the volume of their common space.
The main() method and the signatures of the two methods readCuboid(...) and commonVolume(...) are given in the provided partial code, and you should NOT change them.
Please note that you should create only one instance of Scanner. (If multiple instances of Scanner are created, you will encounter problem with CourseMarker.) As illustrated in the partial code, a single scanner object is created in the main() method, and passed into readCuboid(...) method.
Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first four lines (in green) below are commands issued to compile and run your Java programs if you are using in-line commands, for example, on UNIX. If you are not, you may ignore them.
$ javac MyPoint3D.java $ javac MyCuboid.java $ javac Lab4Ex1.java $ java Lab4Ex1 Enter 1st cuboid: -10 20 10 20 0 -5 Enter 2nd cuboid: 10 30 -10 60 18 -3 1st cuboid: [ (-10,20,10); (20,0,-5) ] 2nd cuboid: [ (10,30,-10); (60,18,-3) ] The cuboids are not the same. Volume of common space = 40
Second sample run:
$ java Lab4Ex1 Enter 1st cuboid: 1 2 3 4 5 6 Enter 2nd cuboid: 6 5 4 3 2 1 1st cuboid: [ (1,2,3); (4,5,6) ] 2nd cuboid: [ (6,5,4); (3,2,1) ] The cuboids are not the same. Volume of common space = 3
Third sample run:
$ java Lab4Ex1 Enter 1st cuboid: 5 -10 6 -12 2 8 Enter 2nd cuboid: 5 2 8 -12 -10 6 1st cuboid: [ (5,-10,6); (-12,2,8) ] 2nd cuboid: [ (5,2,8); (-12,-10,6) ] The cuboids are the same. Volume of common space = 408
Submit your programs MyPoint3D.java, MyCuboid.java and Lab4Ex1.java through CourseMarker.
In the parital code Lab4Ex1.java we provided a class method commonVolume(MyCuboid, MyCuboid). Would it be possible to write it as an instance method instead? If so, which class should the method be in?
For your own practice, write a version of the program that uses an instance method instead of a class method to compute the common volume of two Cuboid objects. You do not need to submit this version; it is only for your extra practice.
This is the second problem in the AY2005/6 Semester 1 Practical Exam (PE). The time limit given was 1.5 hours, so you should try to complete this exercise within this time limit. You may refer to the following website for Past Year's PE questions.
You are given this code:
Two rectangles A and B, whose sides are parallel to the x-axis and y-axis, can be found to be in one of the following 3 cases:
Figure 2 below shows the various positions of the rectangles in the 3 cases.
Figure 2. Two rectangles that overlap, touch, or are disjoint.
Write a program MyRectangle.java to create the MyRectangle class that includes two data members: the bottom-left corner and the top-right corner of the rectangle, both of which are objects of the Point2D.Double class. You are also to design relevant methods for the following requirements.
The main method in your program should create 2 MyRectangle objects from user's inputs, print the rectangles' information, and output a conclusion that states whether the two rectangles overlap, touch, or are disjoint. Please refer to the 3 sample runs below.
If the two rectangles overlap each other, you need also to compute and display the overlapped area correct to 2 decimal places. Please refer to the first sample run below for an example.
You are to note that in the creation of a rectangle, the user may enter ANY 2 opposite corners of the rectangle, not necessarily the bottom-left corner and top-right corner. Therefore, you need to check the values and make the necessary adjustments before creating the rectangle.
You may assume that the user enters two distinct points for the corners, and that the rectangle has a positive area.
You may write any additional methods you deem necessary. A more modular program will receive more credit than a less modular one.
Sample run using interactive input (user's input shown in blue; output shown in bold purple). Note that the first two lines (in green) below are commands issued to compile and run your Java programs if you are using in-line commands, for example, on UNIX. If you are not, you may ignore them.
$ javac MyRectangle.java $ java MyRectangle Enter one corner of 1st rectangle: 7.6 2.2 Enter opp corner of 1st rectangle: 1.5 0.3 Enter one corner of 2nd rectangle: 9.4 -2.5 Enter opp corner of 2nd rectangle: 4 4.2 Rectangle A = ([1.5, 0.3], [7.6, 2.2]) Rectangle B = ([4.0, -2.5], [9.4, 4.2]) Rectangles A and B overlap each other. Overlapped area = 6.84
Second sample run:
$ java MyRectangle Enter one corner of 1st rectangle: 33.3 -1.1 Enter opp corner of 1st rectangle: 20.5 3.6 Enter one corner of 2nd rectangle: 20.5 8.6 Enter opp corner of 2nd rectangle: 10.3 -5.2 Rectangle A = ([20.5, -1.1], [33.3, 3.6]) Rectangle B = ([10.3, -5.2], [20.5, 8.6]) Rectangles A and B touch each other.
Third sample run:
$ java MyRectangle Enter one corner of 1st rectangle: 4 0 Enter opp corner of 1st rectangle: 9 5 Enter one corner of 2nd rectangle: 0 11 Enter opp corner of 2nd rectangle: 5 6 Rectangle A = ([4.0, 0.0], [9.0, 5.0]) Rectangle B = ([0.0, 6.0], [5.0, 11.0]) Rectangles A and B are disjoint.
Submit your programs MyRectangle.java through CourseMarker.
You would normally have written a program to define MyRectangle and a separate application program to test it out. However, in that PE, we wanted to cut down on the number of programs submitted, so we asked students to submit only one program MyRectangle.java which includes a main method. We shall stick to the same instruction here.
The deadline for submitting all programs is 12 October 2009, Monday, 23:59hr. Late submissions will NOT be accepted.