Tutorial 4 Classes and Objects: 1. Java Api Has The Gregoriancalendar Class in The Java - Util Package, Which
Tutorial 4 Classes and Objects: 1. Java Api Has The Gregoriancalendar Class in The Java - Util Package, Which
NOTE: Put all the source code and answers into a word file, and upload the word file (just
ONE Word file! name it with “your ID_name_date”, e.g. 20191963****_***_20201028.doc).
Attempt ALL questions. Each question is worth 2 marks, with 10 marks in total.
1. Java API has the GregorianCalendar class in the java.util package, which
you can use to obtain the year, month, and day of a date. The no-arg
constructor constructs an instance for the current date, and the methods
get(GregorianCalendar.YEAR), get(GregorianCalendar.MONTH), and
get(GregorianCalendar.DAY_OF_MONTH) return the year, month, and day.
Write a program to perform two tasks:
■ Display the current year, month, and day.
■ The GregorianCalendar class has the setTimeInMillis(long), which can be
used to set a specified elapsed time since January 1, 1970. Set the value to
1234567898765L and display the year, month, and day.
Rectangle
-length: double
-width: double
+Rectangle()
+Rectangle(len:double, wid:double)
+getArea(): double
+getPerimeter(): double
+drawRect(): void
Where:
- the default constructor (without any parameters) set the length and width
- the constructor with parameters will set the length and width of that
rectangle with the given value (if the given value are larger than 0,
- getArea() returns the area of that rectangle if the range of the length and
- drawRect() draws a shape of that rectangle using asterisk (*) with the
length and width change to integer if the length and width are between
Write a program (another class) called TestRectangle with the main() to test all
Methods:
parameters)
- move() – print a string with current speed, eg. “The vehicle is running
(and the others if necessary). And print the speed of the vehicle object.
Use the methods to reset and change the speed, and then print the
information.
method from String class that you can invoke with an string object to get the
These methods are useful only if the discriminant is nonnegative. Let these
methods return 0 if the discriminant is negative.
Draw the UML diagram for the class and then implement the class. Write a
test
program that prompts the user to enter values for a, b, and c and displays the
result based on the discriminant. If the discriminant is positive, display the two
roots. If the discriminant is 0, display the one root. Otherwise, display “The
equation has no roots.”