Posts

Showing posts from April, 2017

Task 10b - Interfaces and Exception Handling in Java

Create a UserService Interface with following methods. void addUser(User user) void updateUser(User user) void deleteUser(int userID) User getUser(int userID) void unblockUser(int userID) void blockUser(int userID) ArrayList<User> getAllUsers() Define InMemoryUserService class that shall implement UserService contract or interface. Use ArrayList for storage, update and retrieve operations inside InMemoryUserService i.e. you shall define a static attribute ArrayList<User> users = new ArrayList<>(); The implemented methods shall change/read this list to perform the required operation.  Test all methods from UserTest class. Getting information from user input is encourged but optional.  Define at least id, name, and status instsance 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. Make a test cl

Task 9 - Polymorphism in Java

9.1:  Make Circle , Rectangle , Square and Triangle classes that extends Shape.  Shape has color attribute and an abstract double calcArea() method. Circle has radius attribute. Triangle has base and height. Rectangle has length and width.  Square have only one attribute i.e. size (to represent width and height both, as both are same in case of square). In ShapeTest class's main method, ask the user how many shapes he want to create. Then, ask the type of each shape, then get the required data to instantiate that shape object. Keep putting each shape in ArrayList of type Shape . e.g. How many shapes you want to create: 4 Choose 1st Shape type: Press 1, to create Circle Press 2, to create Rectangle Press 3 to create Triangle Press 4 to create Square >1 Enter the radius of circle: > 5 .... .... Then outside the loop, display following information: Sum of area of each shape type  Total shapes of each shape type Total cost to paint each shape type  Cost to paint all shapes You ma

Task 8 - OOP Assignment 2 - Inheritance + Arrays

Image
Create a class Game with start, stop, restart methods. Define two protected attributes to store player 1 and player 2 names. Methods shall overridden by subclasses of Game class. Subclass shall decide what shall be written in the body of these methods depending on the particular game logic. Create a class TicTacToe, a subclass of Game, that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enum type to represent the value in each cell of the array. The enum’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X in the specified square, and place an O wherever the second player moves. You can get coordinates from each user to place X or 0. Each move must be to an empty square. If user a position that is already occupied, let user to re-enter the

Task 7 - Inheritance in Java

Last Updated on Nov. 26, 2020 Program 7.1  (With reference to Employee inheritance hierarchy, given in Chapter 9 of our reference book) You studied an inheritance hierarchy in which the class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, you shall create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, CNIC, getFirstName, getLastName, getCNIC and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite the class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in its parent type. Class CommissionEmployee’s constr