Java Object Oriented Programming
Java Object Oriented Programming
Introduction......................................................................................................................2
Gadgets.java...................................................................................................................3
Mobile.java......................................................................................................................3
Mp3.java.........................................................................................................................4
Gadget Class.................................................................................................................4
Mobile Class..................................................................................................................5
Mp3 Class.....................................................................................................................6
Conclusion.....................................................................................................................8
Introduction
This Assignment covers the concept of Object Oriented Program of a Java language using a
parent class Gadgets class which contained the common attributes and methods to all child
classes. Here we have Gadgets class which is the parent of Phone and Mp3 subclasses which
I have included the required attributes (Variables of different types) and methods (class
function) that were defined to each Gadget. Java language is used to implement the classes
Gadget
+model: String
+size:String
+price:double+int:weight
+getModel(): String
+getSize():String
+getPrice():double
+getWeight():int+display(
):void
Extends
Extends
MP3
Mobile +availableMemory:double
+noOfMinutesOfCallingCredits:double
+getAvailableMemory():double
+downloadMusic(double): void
+addCallingCredits(double): void
+deleteMemory(double): void
+phoneCall (String, double): void
+display (): void
+display (): void
1. Gadget.java
just to keep a standard format for the coding according to the requirements I always write the
code by keeping following guidelines in view.
gadget.java
class Gadget {
// Instance Variables
String model;
int weight;
float price;
String size;
Mobile.java
class Mobile extends Gadget {
int minutes;
String model;
int weight;
float price;
String size;
public Mobile (String model, int weight, float price, String size, int minutes){
super (model, weight, price, size);
this.minutes = minutes;
}
int memory;
String model;
int weight;
float price;
String size;
public MP3 (String model, int weight, float price, int memory, String size) {
this. memory = memory;
super (model, weight, price, size);
}
2. Mobile.java
In this class which is extend from Gadget Class we can say it is a child of the Gadget class
Mobile class has single declared attribute:
This attribute is initialized in the constructor of the class and one of the pre-declared parameters
is assigned and it has a related accessor method.
The rest of parameters declared in constructor define the price, weight, model, and size. In the
mobile class and then all declared parameters are called by the parent constructor of the
Gadget class.
The parameter callCredit is used as the integer number to calculate the minutes of dialing time
credit which is the remaining time.
This class is composed of a method for the front user of mobile which provide functionality to
add dialing minutes using a parameter use to calculate the dialing credit and add it to the
available minutes that the system has stored. This addition of the minutes first checks if the
input value is positive then it is added to the system. Alternatively, an exception is alerted which
prompt the user to provide an integer number greater than zero.
In this class I have also included a method which actually represent the user making a phone
call. The user needs to provide the phone number and the duration of the call-in minutes. If
there is enough credit then a message giving the phone number and duration is displayed and
the remaining calling credit is reduced by the number of minutes that the call lasted. Otherwise,
a message informing the user that there is insufficient credit to make the call is displayed.
A method to display the details of the mobile is required. I have checked to make sure that the
display details have exactly the same signature as the Main Gadget class. It actually is calling
the Gadget class display method, the size, the price, and the weight. The number of minutes of
dialing minutes remaining is then output correctly formatted.
3. MP3.java
It is a sub class of the Gadget class it has an attribute with a name availableMemory.
This attribute is initialized inside the constructor using the values assigned by of any of the
constructor pre declared parameters with a related accessor method. The rest of the
parameters declared in the constructor show model, weight, size and price of the MP3 player all
these parameters are called by the constructor of the Gadget class.
Now MP3 class has a functionality of downloading music which takes a parameter representing
the amount of memory that the music will take up and, if there is sufficient available memory on
the MP3 player, decreases the available memory accordingly, otherwise an appropriate error
message is printed. There is also a method for deleting music which takes a parameter
representing the amount of memory that the music took up and increases the available memory
of the MP3 player accordingly.
A method to display the details of the MP3 player is required. the Gadget class display method
format and signature is exactly same as the display method in MP3 class. It will call the method
in the Gadget class to display the model, the size, the weight and the price. The available
memory is then output suitably annotated.
Report
* Test 1: Inspect a mobile phone, add calling credit, re-inspect the mobile phone
In above test case the javac (compiling) the error was detected, so rectify this issue the File
name Gadget.java and class name were compared and corrected.
the new minutes were added and results come again with the new Minutes count
* Test 2: Inspect a mobile phone, make a call, re-inspect the mobile phone
* Test 3: Call the method to display the details of a mobile phone
* Test 4: Inspect an MP3 player, download music, re-inspect the MP3 player
At first execution the insufficient Memory appears as an exception after deleting memory
(Deleting step is covered in Test 5) the next Results come appear with Memory Remaining 32
(MB).
* Test 5: compile the MP3 player, remove a piece of music, and then check again the MP3
player
This is the same steps involved as it was in Test 4 in order to deleted the Memory and refresh
the RAM (Random Access Memory) with new available Memory.
So it was corrected by adding “public static void main (String[] args) { “ in the Gadget class
Conclusion:
After the implementation of the GadgetShop.java class I have learnt many things. I have learnt
the use of Command based instruction in JAVA, we can see that how to respond to the
command driven responses. We can also see how to use an arraylist to store data dynamically
manner. This assignment provides me opportunity to practice the getters and the setters
method. The assignment has good places to utilize the try and catch statement for exception
handling and validations. This task is a good example to look into the best implementation and
best practices to use Object Oriented Techniques practically. Although the assignment has no
practical implemented the GUI (Graphical User Interface) but I have implemented the
functions to get the data from the user using command prompts and store it into the “array list”
corresponding to the appropriate object. In the displayM () I have implemented some good
validation to see if the value entered id saves in Display Number and then it checks the integer
or not. It will check if the entered values are an int type and then I checked if its value ranges
from 0 to the size of the array list i.e. the number of items added in the array list. After the
implementation of the methods, I implemented the ActionListener on each button to respond to
the events appropriately. In some ActionListener functions, I have implemented the try catch
statement to handle the exception handling in case if there is any.