Java Exam (2006 Spring)

advertisement
CS 111 Java Exam
13.05.2006
Name and Surname:
10:40-13:00
Student-ID:
Dept:
Open book, open notes exam
Rules:
1. You can use only your own books, notes, pencil, calculator, dictionary, etc. Sharing any resource
is not allowed.
2. Any attempt of copying will be punished.
3. Do not forget to sign the attendance list.
4. This exam contains 2-standard questions of worth 50+50= 100 points and a 3rd bonus question of
worth 20 points in 7 pages.
5. Write your name and surname on each page.
Good luck!
Question 1
50
Bonus
20
Question 2
50
Total
Page 1
120
Name and Sur name: ___________________________ ________________________________
Question 1 (50 points) Write a class, namely point that represents a point (x, y) in the 2-dimensional
coordinate system such that x corresponds to the value along the x-axis and y corresponds to the value
along the y-axis and the point (0, 0) is called the ORIGIN. Make sure that your class contains the
following data and methods:
1. The instance variables to represent the x and y value properties of any point object.
2. The class constant, ORIGIN to represent the ORIGIN, that is, the point (0, 0).
3. The constructor with proper parameters to set x and y properties appropriately.
4. The constructor without any parameters to set both properties to 0 (ORIGIN).
5. The getter methods to return the value of each instance variable.
6. The toString() method that returns the state of a point as the string, “(x, y)“ where x corresponds
to the value along the x-axis and y corresponds to the value along the y-axis. See the driver
example execution for details.
7. A method, say findDistance that computes the distance between the current, "this" point and the
"other" point given in the input argument. Note that the distance, d between two points (a, b)
and (x, y) is defined by the following formula:
d 
(a  x) 2  (b  y ) 2
8. The equals method that will be used to test the equality of “this” point and another point, and
return true or false appropriately. Note that both x-values and y-values must be equal in the given
order only.
Page 2
Name and Sur name: ___________________________ ________________________________
Page 3
Name and Sur name: ___________________________ ________________________________
Bonus Question (20 points) Write a driver program (a Java application) for the class point. In this
program you should generate two point objects with properties set according to values read from the
keyboard. Then, print these points (as an outcome of the toString() method). Then, test if these two
points are equal. If they are equal points, just display a message and stop. If they are different points,
then first display an appropriate message, and then compute and display the distance between these two
points by using the appropriate method in the point class, and finally compute the distances between
each point and the ORIGIN, and display a message indicating which point is closer to the origin
appropriately. See the following three execution samples for details:
Type the value for the x value of first point:1,5
Type the value for the y value of first point:2
Type the value for the x value of second point:2
Type the value for the y value of second point:1,5
Point 1 =(1.5,2.0)
Point 2 =(2.0,1.5)
Different points
The distance between the two points is 0,71
Press any key to continue...
Type the value for the x value
Type the value for the y value
Type the value for the x value
Type the value for the y value
Point 1 =(2.0,3.0)
Point 2 =(2.0,3.0)
Equal points
Press any key to continue...
of
of
of
of
first point:2
first point:3
second point:2
second point:3
Type the value for the x value of first point:4
Type the value for the y value of first point:5
Type the value for the x value of second point:1
Type the value for the y value of second point:2
Point 1 =(4.0,5.0)
Point 2 =(1.0,2.0)
Different points
The distance between the two points is 4,24
Point 2 is closer to the origin.
Press any key to continue...
Page 4
Name and Sur name: ___________________________ ________________________________
Page 5
Name and Sur name: ___________________________ ________________________________
Question 2 (50 points) The aim of the program you will write in this assignment is to display the
depreciation of an asset using the fixed-declining balance method. The inputs for the program are
described as below:
1. Cost is the initial cost of the asset.
2. Salvage is the value at the end of the depreciation (sometimes called the salvage value of the
asset).
3. Life is the number of periods over which the asset is being depreciated (sometimes called the
useful life of the asset).
4. Months is the number of months in the first year.
Furthermore, Period is defined as the period for which you want to calculate the depreciation. For
instance, if Life is 6 years, then, your program should calculate the depreciation for
Period = 1, 2, 3, 4, 5, 6, and 7 years.
The fixed-declining balance method computes depreciation at a fixed rate. Use the following formulas to
calculate depreciation for a period:
(cost - total depreciation from prior periods) * rate
where
rate = 1 - (salvage / cost) (1 / life)
Note that rate should be displayed as a percentage..
Depreciation for the first and last periods is a special case. For the first period, use this formula:
cost * rate * months / 12
For the last period, use this formula:
((cost - total depreciation from prior periods) * rate * (12 - months)) / 12
Note that any of the data should be positive; Salvage should be less than or equal to Cost; and Months
should be in the range 1-12! If any of these rules are not satisfied, then display an appropriate error
message.
Investigate the following execution to better understand the program:
Cost:1000
Salvage:10
Months:6
Life:2
The rate is 90%
Period = 1
Depreciation = 450,00 YTL
Period = 2
Depreciation = 495,00 YTL
Period = 3
Depreciation = 24,75 YTL
You want to try again ? (true/false):true
Cost:-200
Salvage:10
Months:4
Life:3
Incorrect data!
You want to try again ? (true/false):true
Cost:1000
Salvage:1000
Months:12
Life:1
The rate is 0%
Period = 1
Depreciation = 0,00 YTL
Period = 2
Depreciation = 0,00 YTL
You want to try again ? (true/false):false
Press any key to continue...
Page 6
Total Depreciation = 450,00 YTL
Total Depreciation = 945,00 YTL
Total Depreciation = 969,75 YTL
Total Depreciation = 0,00 YTL
Total Depreciation = 0,00 YTL
Name and Sur name: ___________________________ ________________________________
Page 7
Download