Selection and Repetition
Lab #3 (CS1101 AY2007/8 Semester 1)
Date of release: 11 September 2007, Tuesday, 23:59.
Submission deadline: 19 September 2007, Wednesday, 23:59.
School of Computing, National University of Singapore

0 Introduction

This lab requires you to write two programs for Exercise 1. (Exercises 2 and 3 are for your own practice and they will not be graded.)

You are advised to review the material covered in chapters 1 through 6 and read Programming Style, Design and Marking Guidelines before attempting this lab assignment. In particular, this lab assignment reinforces materials covered in chapters 5 and 6.

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). Unless a template is given, you should start each program afresh (ie. don't cut and paste and modify) so that you get sufficient practice with writing a complete Java program.

A word of advice: Code incrementally. 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.

Your 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 12. For subsequent labs, it may be reduced.


1 Exercise 1: Types of Triangle (100%)

1.1 Learning objectives

1.2 Task

You are given these partial codes:

MyTriangle class has four members: the first three, vertex1, vertex2, and vertex3 are Point2D.Double objects representing the three vertices of a triangle. The fourth data member, triangleType, contains an integer denoting the type of triangle:

The diagrams below illustrate the three types of triangle.

(Sidenote: There is an enum (enumerated) type from Java Version 5.0 onwards, not covered in this course though, which may be used for such purpose. Here we are contented with integer to denote the type of triangle.)

You are to write two programs: MyTriangle.java which contains the definition of MyTriangle class, and Lab3Ex1.java which is an application of MyTriangle class.

MyTriangle.java

The four members of MyTriangle class have been included for you in the given partial code, and you should NOT change or remove them, or add any new data members.

MyTriangle.java should include these methods (not all of which are needed in Lab3Ex1.java though):

For the constructors, you should not just care about the three vertices. Your constructors should also set the correct value for the fourth member triangleType. This is to maintain data integrity. Apply the same principle to other appropriate methods.

You may include additional methods of your own.

Lab3Ex1.java

This program accepts six non-negative values that represent the x- and y-coordinates of the three distinct vertices of a triangle. For example, if the user enters 12.2 28.3 0.4 16.7 2.9 42.2, this defines a triangle with vertices at (12.2, 28.3), (0.4, 16.7) and (2.9, 42.2).

You may assume that the input data are all non-negative, and that the data represent three distinct vertices. However, you need to check whether the data represent a triangle. (How?) Your program should keep requesting for a new set of six values until the valid data are entered.

Your program then creates a MyTriangle object, which should include the correct value for its fourth data member triangleType as well.

(Do NOT call any method of Point2D.Double class in Lab3Ex1.java! This program should not import the java.awt.geom package, because the implementation details of MyTriangle are internal information which should not be known to other classes.)

Your program retrieves the triangle type by calling an appropriate method you provide in MyTriangle class, and reports it.

You may include additional methods of your own.

1.3 Sample runs

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

$ javac MyTriangle.java
$ javac Lab3Ex1.java
$ java Lab3Ex1
Enter 6 values for 3 vertices: 1.3 0.8 7.8 0.8 5.2 0.8
Enter 6 values for 3 vertices: 2.5 2.5 5.0 5.0 6.3 6.3
Enter 6 values for 3 vertices: 12.2 28.3 0.4 16.7 2.9 42.2
Triangle is scalene. 

Second sample run:

$ java Lab3Ex1
Enter 6 values for 3 vertices: 10.5 9.7 23.1 6.0 10.5 2.3
Triangle is isosceles. 

Third sample run:

$ java Lab3Ex1
Enter 6 values for 3 vertices: 0 0 1 0 0.5 0.866
Triangle is equilateral. 

1.4 Submission

Submit your programs MyTriangle.java and Lab3Ex1.java through CourseMarker.

1.5 Important notes


2 Exercise 2: Monte Carlo (0%)

2.1 Learning objectives

2.2 Task

Consider the unit circle which, in cartesian coordinates, is defined by the equation x*x + y*y = 1. The area of the part of the unit circle that lies in the first quadrant is pi/4, where pi is a well-known constant.

Imagine that you throw darts at random at the unit square (in the first quadrant) so that the darts land randomly on (x, y) coordinates satisfying 0 ≤ x < 1 and 0 ≤ y < 1. Some of these darts will land inside the unit circle. Since the first quadrant part of the unit circle has area pi/4 and the unit square has area 1, the proportion of darts you throw that land in the unit circle will be approximately pi/4.

(How do you determine if a dart lands inside the unit circle?)

If the dart lands just on the boundary of the unit circle, since this is very unlikely, you may arbitrarily treat it as landed inside or outside the circle.

Write a program MonteCarlo.java that asks the user to enter the number of darts to throw. To generate random (x, y) values representing points in the unit square, you may use Math.random() or any appropriate method of the Random class in the java.util package.

In your loop, the variable inside stores the number of darts that land in the unit circle (in that quadrant). Use this value to approximate the value of pi.

Your program then outputs the value of pi thus computed, rounded to 4 decimal places.

2.3 Sample runs

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

$ javac MonteCarlo.java
$ java MonteCarlo
How many darts? 5000
Number of darts = 5000
Darts landed inside unit circle = 3934
Approximated pi = 3.1472 

Another sample run:

$ java MonteCarlo
How many darts? 90000000
Number of darts = 90000000
Darts landed inside unit circle = 70686491
Approximated pi = 3.1416 

2.4 Submission

Submit your program MonteCarlo.java through CourseMarker.

2.5 Important notes


3 Exercise 3: Perfect Number (0%)

3.1 Learning objectives

3.2 Task

A perfect number is a positive integer that is equal to the sum of its proper divisors. A proper divisor is a positive integer other than the number itself that divides the number evenly (i.e. no remainder).

For example, 6 is the smallest perfect number, because the sum of its proper divisors 1, 2, and 3 is equal to 6. 8 is not a perfect number because 1 + 2 + 4 is not equal to 8.

Write a program PerfectNumber.java that accepts a positive integer in the range [1, 10000] and determines whether the number is perfect.

Your program then outputs an appropriate message, as shown in the sample runs below.

3.3 Sample runs

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

$ javac PerfectNumber.java
$ java PerfectNumber
Enter value: 6
6 is a perfect number. 

Another sample run:

$ java PerfectNumber
Enter value: 30
30 is not a perfect number, its sum of proper divisors is 42. 

3.4 Submission

Submit your program PerfectNumber.java through CourseMarker.

3.5 Important notes


4 Deadline

The deadline for handing in all programs is 19 September 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
Tue Sep 18 09:40:39 SGT 2007