Question 1
(Using the GregorianCalendar class) Java API has the GregorianCalendar in class in [login to view URL] package that can be used to obtain the year, month, and day of a date. The no-arg constructor constructs an instance for the current date, and the method get([login to view URL]), get(GregorianCalendar. MONTH), and get(GegorianCalendar.DAY_OF_Month) return the year, month, and day. Write a program to perform these two tasks.
Display the current year, month and day
The GregorianCalander 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.
Question 2
(The MyPoint class) Design a class called MyPoint to represent a point with x and y-coordinates. The class contains:
Two data fields x and y that represents the coordinates with get methods
A no-arg constructor that creates a point(0,0)
A constructor that constructs a point with specified coordinates
Two get methods for data fields x and y respectively.
A method named distance that returns the distance from this point to another point of the MyPoint type
A method named distance that returns the distance from this point to another point with specified x- and y- coordinates
Draw the UML diagram for the class. Implement the class. Write a test program that creates two points (0,0) and (10, 30.5) and displays the distance between them-
question 3
(Implementing MyStack using inheritance) In Listing 11.9, MyStack is implemented using composition. Create a new stack class that extends ArrayList.
Public class MyStack {
Private [login to view URL] list = new java .[login to view URL]();
Public Boolean isEmpty() {
Return [login to view URL]();
}
Public int getSize() {
Return [login to view URL]();
}
Public Object peek(){
Return [login to view URL](getSize() – 1);
}
Public Object pop(){
Object o = [login to view URL](getSize() – 1);
[login to view URL](getSize() – 1);
Return 0;
}
Public void push(Object o){
[login to view URL](0);
}
Public int search(Object o){
Return [login to view URL](o);
}
/**Override the toString in the Object class */
Public string toString(){
Return “stack:”+ [login to view URL]();
}
}
Listing 11.9
Draw the UML diagram for the classes. Implement Mystack. Write a test program that prompts the user to enter five strings and displays them in reverse order