Assignment 1 Java

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Assignment-1

1. You are given an interface AdvancedArithmetic which contains a method signature int
divisor_sum(int n). You need to write a class called MyCalculator which implements
the interface.
divisorSum function just takes an integer as input and return the sum of all its divisors.
For example divisors of 6 are 1, 2, 3 and 6, so divisor_sum should return 12. The
value of n will be at most 1000.

2. Create a UserService Interface with following methods signatures.


void addUser(User user) throws UserAlreadyExistException
User getUser(int userID) throws UserAccountIsBlockedException
void updateUser(User user)
void deleteUser(int userID)
void unblockUser(int userID)
ArrayList getAllUsers()

Define InMemoryUserService class that shall implement UserService interface.


In InMemoryUserService class, use User ArrayList for storage of user objects. The
methods shall change/read this list to perform the required operations. Test all
methods from UserTest class. Getting information from user input is encouraged but
optional.

Define and properly encapsulate atleast id, name and status instance attributes in User
class of type int, String and boolean. How you check whether a user account is
blocked? If status attribute is false, it means user account is blocked. True represent
the account is active.
UserAlreadyExistException exception shall be thrown if user already exist in the
users array list. The exception object shall also store the user ID for which the
exception occured. Check using equals methods (override the method in User class,
based on user ID). Make UserAlreadyExistException, a runtime
exception. UserAccountIsBlockedException should be thrown if status attribute of the
user object (in the array list) is false, define this exception as checked.

Update the test class by handling both exceptions. Call different methods passing
parameters that shall make the user service to throw the exception to demonstrate you
handled them appropriately.

Make a test class to test all the functionality of UserService. Call its different methods
to see how it works, keep printing related information on console to show what the
program is doing. You must also handle above defined exceptions from your test
class.
3. Given the following code:
package p3;
public class Util {
public enum Format {
JPEG { public String toString() {return "Jpeggy"; }},
GIF { public String toString() {return "Giffy"; }},
TIFF { public String toString() {return "Tiffy"; }};
}
public static <T> void print(T t) {
System.out.print("|" + t + "|");
}
}
------------------------------------------------------------
// (1) INSERT IMPORT STATEMENTS HERE
public class NestedImportsA {
public static void main(String[] args) {
Util u = new Util();
Format[] formats = {
GIF, TIFF,
JPEG,
Format.JPEG,
Util.Format.JPEG,
p3.Util.Format.JPEG

};
for (Format fmt : formats)
print(fmt);
}
}

Which sequence of import statements, when inserted at (1), will result in the code
compiling, and the execution of the main() method printing:

|Giffy||Tiffy||Jpeggy||Jpeggy||Jpeggy||Jpeggy|

You might also like