Вы находитесь на странице: 1из 4

Assignment 0

Web Application Development


Note: This is an ungraded assignment for you to practice your java skills. Those students who were not
able to complete their classwork 1 really need to do this assignment to brush up their java skills.

Question 1:
A class called MyTime, which models a time instance, is designed as shown in the class diagram.
It contains the following private instance variables:
hour: between 0 to 23.
minute: between 0 to 59.
Second: between 0 to 59.
The constructor shall invoke the setTime() method (to be described later) to set the instance variable.
It contains the following public methods:
setTime(int hour, int minute, int second): It
shall
check
if
the
given hour, minute and second are
valid
before setting the instance variables.
(Advanced: Otherwise, it shall throw
an IllegalArgumentException with
the

message "Invalid hour, minute, or


second!".)
Setters setHour(int
hour), setMinute(int
minute), setSecond(int second): It shall

check if the parameters are valid, similar to


the above.
Getters getHour(), getMinute(), getSecond().
toString(): returns "HH:MM:SS".
nextSecond(): Update this instance to the
next second and return this instance (of
type
MyTime).
Take
note
that
the nextSecond() of23:59:59 is 00:00:00.
nextMinute(), nextHour(), previousSecond(), previousMinute(), previousHour(): similar to the above.
Write the code for the MyTime class. Also write a test program (called TestMyTime) to test all the methods
defined in the MyTime class.

Question 2:
A class called Author is designed as follows:
Three private instance variables: name (String), email (String), and gender (char of either 'm' or 'f');
One constructor to initialize the name, email and gender with the given values;
public Author (String name, String email, char gender) {......}

(There is no default constructor for Author, as there are no defaults for name, email and gender.)

public getters/setters: getName(), getEmail(), setEmail(),

(There are no setters for name and gender, as these attributes cannot be changed.)
A toString() method that returns a string in this format : "author-name (gender) at email", e.g., "Sameera
Ghayyur (f) at sameeraghayyur@fccollege.edu.pk".

and getGender();

Write the Author class.


Also write a test program called TestAuthor to test the constructor and all public methods. Try changing
the email of an author, e.g.,
Author anAuthor = new Author("Tan Ah Teck", "ahteck@somewhere.com", 'm');
System.out.println(anAuthor); // call toString()
anAuthor.setEmail("paul@nowhere.com")
System.out.println(anAuthor);

A class called Book is designed as follows:


Four private instance variables: name (String), author (of the class Author you have just created, assume
that each book has one and only one author), price (double), and qtyInStock (int);

Two constructors:

public Book (String name, Author author, double price) {...}

public Book (String name, Author author, double price, int qtyInStock) {...}

public methods getName(), getAuthor(), getPrice(), setPrice(), getQtyInStock(),setQtyInStock().


toString() that
returns
"'book-name'
by
author-name
(gender)
at
email".
(Take note that the Author's toString() method returns "author-name (gender) at email" so use that!)
Write the class Book (which uses the Author class written earlier). Also write a test program called TestBook to
test the constructor and public methods in the class Book. Take Note that you have to construct an instance
ofAuthor before you can construct an instance of Book. E.g.,
Author anAuthor = new Author(......);
Book aBook = new Book("Java for dummy", anAuthor, 19.95, 1000);
//OR
// Use an anonymous instance of Author
Book anotherBook = new Book("more Java for dummy", new Author(......), 29.95, 888);

Take note that both Book and Author classes have a variable called name. However, it can be differentiated
via the referencing instance. For a Book instance says aBook, aBook.name refers to the name of the book;
whereas for an Author's instance say auAuthor, anAuthor.name refers to the name of the author. There is no
need (and not recommended) to call the variables bookName and authorName.

Question 3:
Write a Function isPalisndrome() to check if a given string is palindrome or not. A string is a palindrome
if reverse of the string is also the string itself. E.g. tat, wow , RACECAR, CIVIC. The functions
return type should be a Boolean (true if input string is a palindrome otherwise false). Also ignore case
(upper/lower) of the string. i.e. CiVic , Wow, RaceCar all these strings are still palindrome.
In the main function, test isPalisdrome(String myString) method. Take a string as an input from console
and print whether it was a palindrome or not.
Overload the same method for integer input. An integer is a palindrome if reverse of the integer is the
integer itself. E.g. 1221, 1456541, 1005001. The function should take parameter of type int and should
return a Boolean just like the above function.
You should use division operator ( / ) and remainder operator ( % ) to solve this question. Just
remember, division operator can be used to get rid of last digit e.g. 124/10 will give you 12, and modulus
operator can give you last digit e.g. 124%10 will return 4. Do not use Java API for this problem (like
converting integer to string using java API and then using isPalindrome(String myString)). Solve it using
the mathematical operators (/, %).
In the main function, test isPalisdrome(int number) method. Take an integer as an input from console
and print whether it was a palindrome or not.

Question 4:
Write a function removeDuplicates which takes an ArrayList of Integers as an input. The function should
remove all the duplicate values from the ArrayList and return the ArrayList with all duplicates removed.
For Example if this list {1, 2, 2, 5, 4, 5, 7, 1, 1} should return {1, 2, 5, 4, 7}.
In the main function, test the removeDuplicates method by taking a list of integers as input from console
and displaying the ArrayList with all duplicates removed.
Overload the same method for ArrayList of Strings. The function should return an ArrayList of Strings
with all duplicates removed. While checking if 2 string are duplicates of each other, ignore the case
(upper/lower). For example, Book , book, BOOK or BoOK are all duplicates of each other.
In the main function, test the Overloaded removeDuplicates method by taking a list of strings as input
from console and displaying the ArrayList with all duplicates removed.

Overload the same method for ArrayList of MyTime (class you implemented in question 1). The function
should return an ArrayList of MyTime with all duplicates removed.
In the main function, test the Overloaded removeDuplicates method by taking a list of MyTime as input
(No need to take input from console) and displaying the ArrayList with all duplicates removed.

Вам также может понравиться