Cuboids
Lab #5 (AY2007/8 Semester 1)
Date of release: 2 October 2007, Tuesday, 23:59.
Submission deadline: 10 October 2007, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

This lab contains two exercises. Only exercise 1 will be graded. You are required to submit three programs for exercise 1.

Exercise 2, which is the second question in AY2005/6 Semester 1 Practical Exam (PE), is for your own practice. Time limit for it is 1.5 hours, so you should try to complete it within this time limit.

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 covered in lectures; you shouldn't need to and may even be penalized for doing so (if the objective of the assignment is undermined).

A word of advice: Code incrementally. This is even more important now than before as your program gets longer. Don't try to finish all parts of a program then compile it. Write your program in bits and pieces and compile 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 the programming exams. Seriously, code incrementally.

The following topics have not been covered and hence you should not use them (if you are in doubt whether you can use certain features not mentioned below and are not yet covered in class, please consult your discussion leader or lecturer first):

You may assume that all input data are correct.

You should use Scanner class on System.in for input and System.out for output in your programs, unless otherwise stated.

You last output statement should be a println and not print.

Test your programs thoroughly with your own input data before you submit to CourseMarker. Do not use CourseMarker as a debugging tool. For this lab, the number of submissions is set to 8. Only your last submission will be graded.

In general, the expected time to complete a lab assignment (including all graded exercises) is between 3 to 5 hours. You should aim not to spend more than 5 hours. It might be difficult at first, but it should get easier as you gain experience. Make that your target.


1 Exercise 1: Cuboids (100%)

1.1 Learning objectives

1.2 Task

You are given these 3 codes:

We create two classes MyPoint3D and MyCuboid. MyCuboid uses the class MyPoint3D. Lab5Ex1 is an application of MyCuboid.

You are to write three programs: MyPoint3D.java, MyCuboid.java and Lab5Ex1.java.

MyPoint3D.java

MyPoint3D class consists of three 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 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.

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:

Lab5Ex1.java

The program Lab5Ex1.java is to create two MyCuboid objects and computes 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.

1.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ javac MyPoint3D.java
$ javac MyCuboid.java
$ javac Lab5Ex1.java
$ java Lab5Ex1
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) ]
Volume of common space = 40

Another sample run:

$ java Lab5Ex1
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) ]
Volume of common space = 3

1.4 Submission

Submit your programs MyPoint3D.java, MyCuboid.java and Lab5Ex1.java through CourseMarker.

1.5 Important notes


2 Exercise 2: Overlapping Rectangles (0%)

2.1 Learning objectives

2.2 Task

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. 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.

2.3 Sample runs

Sample run using interactive input (user's input shown in blue; output shown in bold purple):

$ 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.

2.4 Submission

Submit your programs MyRectangle.java through CourseMarker.

2.5 Important notes

The following points are meant for the Practical Exam.


3 Deadline

The deadline for handing in all programs is 10 October 2007, Wednesday, 23:59. Late submissions will NOT be accepted.

Start early. Do not wait till the eleventh hour and rush to meet the deadline.





tantc@comp.nus.edu.sg
Fri Sep 7 14:55:48 SGT 2007