Unit-41 Programming in Java Sample-Btechnd
Task 1: Understands the principles of programming in Java
1.1 Briefly list and discuss three main features of programming in Java
Java is a very powerful programming language which provide functionality for developing applications that can target multiple platforms and to create highly customizable features. Following features are provide by Java:
- Platform Independent:Java is platform independent language. Program written in Java can execute on variety of machines which support JRE (Java Runtime Environment). Java follows the principal write once and run anywhere thus programs using Java can be run on different platforms without making any changes.
- Easy to adapt:Java programming language is simple and easy language to adapt. It provides clear and easy to understand syntaxes that can be understood and learn easily.
- Security:Java programming language enable developer to implement high security features for users. JRE also checks for security of an application for example Applets which can run on any browser that support java, are not allowed to access hard disk to prevent unwanted access of user data that can be confidential to enhance user data security.
Java programming language provide many feature to the programmers and there are many more features of java programming language. It serves a very wide range of features to its developers. Following are other features of Java:
- Object Oriented Programming.
- Multithread Application Support to utilize full performance of CPU.
- Provides API’s for distributed applications.
- Automatic garbage collection to remove and release memory from unused objects and object pooling to enhance application performance.
- Can be used to develop a variety of application like Console Applications, Desktop Applications, Web and Mobile Applications.
1.2 What do we mean by BYTECODE and JVM in Java and thus discuss the environmental flexibility of programming in Java?
Java says write once and run anywhere so when we create a program in Java and then compile the java program source, instead of generated specific machine targeted code the java compiler generates BYTECODE. The BYTECODE that java compiler generates is platform independent i.e. it can be executed on a variety of machine which has Java Runtime Environment installed on it. This BYTECODE is executed by a program called Java Virtual Machine which is a part of Java Runtime Environment.
The Java program source file has the extension .java and the BYTECODE file which is generated by Java compiler after successful compilation of the java program source file has the extension .class. We can have multiple classes in a java program source file but best practice is to create individual java source file for each class that we want to design for our application usage. The BYTECODE file which has the .class extension is generated for each class and has the classname.class name format. This file is automatically generated by the Java Compiler after the successful compilation of code.
The Java Virtual Machine uses .class file which contains BYTECODE to execute the java programs. JRE has a compiler called JIT (Just-In-Time) compiler which is responsible to converts the BYTECODE targeting the platform in which it is executing.
Task 2: Design Java solutions
2.1 Design a Computing programming assignment solution to the rental system by using use-case diagrams that show interaction of user with the system.
Designing of Java Programming Solution: A system has been designed for CD/DVD rental system. Two type of user can use the system.
- General User or Customer
To use the system there is login functionality for user which decides the type of user as administrator or as a customer.
Is the user is an administrator then he/she can perform the tasks that are decided for administrator like:
- Add New Customer
- Update Existing Customer
- Delete Existing Customer
- Add a CD/DVD
- View all CDs/DVDs
- Update Existing CD/DVD
- Remove Existing CD/DVD
If the user is a customer then he/she can perform the tasks that are decided for the customer as following:
- View all Existing CDs/DVDs
- Rent any CD/DVD
- Returning any Rented CD/DVD
Admin Use Case Diagram
User Use Case Diagram
2.2 Identify the components and file structures required to implement the rental system.
Components: In the system requirements, the CD/DVD Rental System can be divided into 3(three) components:
- CD/DVD Item: In the “CD/DVD Rental System” it is required to save information about CDs and DVDs available in the store. To have an integrated format we have created an Interface by name “BasicItem”. It defines base functionality for the item. CDItem and DVDItem classes implement BasicItem interface in order to provide functionality.
- Users for application: There are two types of the users in the application. One admin and another one a general customer. User class in the CD/DVD rental system stores user information.
- Data Storage: Data storage is required to save data permanently. So it is one of the necessary components in the system. DataStorageUtility class provides functionality for data storage.
Since we are instructed to use only text files to store the application data, I decided to create different text files to store item information, User login information and information about the rented items for every user.
Here are the description of files created by the application:
- users-of-application.txt:- This file is used to store information about the application users. It stores user’s information in semi-colon separated format and single user per line.
Structure for this file is as follows:
collection-cd-dvd.txt:- This file is used to store information for all available items in the store i.e. CD or DVD. It stores information in semi-colon separated format and single item information per line.
Format for this file is as follows
movie-title; item-price; item-quantity; movie-category; item-type
Rented-Items-[Username].txt:- This file is used to store the information of items those were taken on rent by a user. A separate file is created for each user who is using this application to take items on rent and file name is same as the username of the user. It stores information in semicolon separated format with one item information per line.
(Figure: Sample rent data for user adminuser.Rented-Items-adminuser.txt)
2.3 Use UML class diagrams to show the classes in your system, attributes and operations of each class and the relationship between classes. Define clearly each class, main methods and properties
BasicItem Interface: To provide similar behavior for all items we designed an interface named BasicItem. It provides basic characteristics and behaviors of an item available in the application. It abstracts behaviors of items which must be implemented by all the item classes.
CDItem class: It is a sub class of BasicItem. It provides functionality for the item “CD”in the applicaiton. It defines some basic properties for CD Item. These are:
DVDItem class: It is a sub class of BasicItem. It provides functionality for the item “DVD”in the application. It defines some basic properties for DVD Item. These are:
SearchItemDialog class: it displays an item selection dialog to select items for update or remove.
AppLoginDialog Class: It displays login dialog to the user. All users must logged in before using the application.
MyItemsPanel Class: This class is used to display current rented items of a particular user.
DataStorageService Class: It provides functionality to read and write information to or from persistent storage i.e. hard disk. It defines some static methods to store application data in text files as semicolon separated values with single value per line.
RentedItem Class: It represents an item which is rented by the user.
AppUser Class: It stores information about application users such as their name, username, password, mobile number, credit limit etc.
MyAddEditItemPanel Class: It displays an interface for addition and update of an item to admin user.
MyCustomerPanel Class: This class displays an interface for addition and update of a customer to the administrator.
SearchUserDialog Class: It displays a user selection dialog to the administrator for update and removal of users.
Task 3: Implement Java solutions
3.1 Implement the CD/DVD rental system solution based on your design.
I have implemented the “CD/DVD Rental System” based the design described above. Inheritance hierarchy is used wherever possible to implement classes. BasicItem interface is defined to specify similar functionality for all items. It defines basic behaviors for the items available on the store.
Code Screen Shot form BasicItem.java:
GetItemName() and SetItemName() methods are used to get and set name of item (Cd/Dvd).
GetItemCategory() and SetItemCategory() methods are used to get and set category of the item.
GetItemPrice() and SetItemPrice() methods are used to get and set price of the item.
GetItemQuantity() and SetItemQuantity() methods are used to get and set quantity of the item.
GetItemType() method is used to get type of the item whether item is a CD or DVD.
3.2 Implement classes using inheritance relationship.
After defining the BasicItem interface we have created two more classes implementing this interface the CDItem class and DVDItem class.
CDItem.java: It is the subclass of BasicItem interface. This class defines the basic behavior and properties of item CD.
Code screen shot from CDItem.java
DVDItem.java: It is also a subclass of BasicItem interface. This class defines the basic behavior and properties of item DVD.
Code screen shot from DVDItem.java
We have also created some classes using inheritance hierarchy for user interface from javax.swing package.
3.3 Use methods and attributes in different classes using instances/objects.
In Object Oriented Programming (OOP) to access the methods and fields of any call we use object or instance of the class. In the application there are many classes created for various components. So access the method of these classes we have used the instance of the class.
Instances/Objects of these classes are created in “MainFrame.java” (Main class) file. Main is starting point for this application because of main method. Main is the subclass of the JFrame class of the swing package to generate a user interface of application.
This main class uses the method and fields of other classes so this class also has the instance or objects of these classes.
Displaying Login Dialog for user authentication:
Instance creation and method calling in SearchItemDialog class
3.4 Identify and implement opportunities for error handling and reporting
Testes are performed after the basic functionality of the CD/DVD Rental System. The application is tested against the user input. I have checked the application for invalid input or empty input values or if the values inputted are not in expected format. Application checks form input and display errors in the application.
After the tests I have found following for error handling and reporting:
1. Data Files does not Exist: While testing the application I found the application terminate by the error it the data file doesn’t exist and my application try to open it. To resolve this issue I implemented a check before processing the file. I made a check for the existence of file and it the file doesn’t exist it will be created.
2. Invalid values in files: While developing the application I found the data was not properly formatted or sometimes not being written in the data files. I made a cross check in the code to remove the error and after that the above mentioned problem is resolved.
3.5 Make effective use of an Integrated Development environment (IDE) including code and screen templates.
The application is developed in NET Beans IDE because IDEs are helpful in code writing and managing the files more easily.
It is helpful for us to create derived classes for the existing classes also. For example if we want to create a derive class from an existing class that java provided. It will automatically generate basic functionality for the class like the constructor. If we want to override a base class method then the basic structure of the function is generated.
In the application “CD-DVD Rental System” I have used Net Beans and its code templates and screen templates are used to generate the application functionality.Order Now