Unit Exercises
- Compile and run a simple Java application, the “Hello World” application, using the SDK.
- Define a Square class with the length of its side as an instance variable. Include an appropriate constructor method, and methods to enlarge an instance as well as compute its area.
- Using the Square class, create ten randomly sized squares and find the sum of their areas. (The method Math.random() returns a random number between 0.0 and 1.0 each time it is invoked.)
- Give a Square object the functionality to draw itself using ASCII characters. For example, a Square of length 4 can be drawn as:
****
* *
* *
****
or
XXXX
X++X
X++X
XXXX
Note: The System.out.print() and System.out.println() methods may be useful.
- Find a number with nine digits d1, d2, d3, …, d9, such that the sub-string number d1, …, dn is divisible by n, 1<=n<=9. That is, the leftmost digit (d1) is divisible by 1. The two leftmost digits (d1 d2) are divisible by 2. The three leftmost digits (d1 d2 d3) are divisible by 3, etc. (If the above is not clear, assume the number can be displayed as d1 d2 d3 d4 d5 d6 d7 d8 d9 d10. Then d1 is divisible by 1, 10d1 + d2 is divisible by 2, 100d1+10d2+d3 is divisible by 3, etc.) What is the number? Note that each of the digits may only be used once.
Updated July 30 2020 by FST Course Production Staff