Assignment

This assignment will be marked out of 100 and carries 30% of the overall module weighting.  assignment must be carried out individually so you must   not obtain help from anyone other than the module teaching staff. You must not copy code from any source apart from the module core text and the module WebLearn site. Collusion, plagiarism (unreferenced copying) and other forms of cheating constitute Academic Misconduct, which can lead to failure of the module and suspension from the University.

Aim

The aim of this assignment is to add a class to the project that you developed for the first part of the coursework to make a graphical user interface (GUI) for a system that stores details of gadgets in a shop. The class will contain a main method and will be tested using the command prompt. You will also need to write a report about your program. You are advised to study the learning aid on reflective practice, which is linked from the module WebLearn homepage, and the learning aids entitled “Using the Command Prompt” and “Examples of screenshots for Coursework 2” which are included in the learning and teaching materials. Your report should be no more than 1200 words in length (excluding the table of contents, class diagram, pseudocode and code listing).

Deliverables

Create a new class within the project called GadgetShop. When you are ready to submit your solution, upload your GadgetShop.java file, together with the Gadget.java, Mobile.java and MP3.java files from the first part of the coursework (not any other files from the project) to WebLearn together with your report in Microsoft Word and, when you are absolutely sure that you have uploaded the correct files, press the submit button. (You will not need to make any changes to the Gadget, Mobile or MP3 classes as long as they work correctly, but ask your tutor if you need help completing those classes to the specification of the first part of the coursework.)

Program

An example GUI is shown below:

Your GUI should contain the same components but you are free to use a different layout if you feel that it improves the aesthetics, ease of use etc. The GadgetShop class should store an array list (not an array) of type Gadget to hold the gadgets.

Text Field Input Methods

For each text field, write a method to read its contents and return the value. For the model, size and phone number, the value should simply be returned as a string but, for each of the other text fields, the input value should be converted into its correct numerical data type and then that value should be returned.

There should be text fields for entering:

  • The model
  • The price
  • The weight
  • The size
  • The initial credit (when adding a mobile phone)
  • The initial available memory (when adding an MP3 player)
  • The phone number (when making a mobile phone call)
  • The duration (when making a mobile phone call)
  • The download size (when downloading music to the MP3 player)
  • The display number, which is the same as the index of the gadget in the array list

The input method for the display number is different from the input methods for the other text fields. The method first initialises the display number to -1. If the input from the user via the GUI is valid, the display number will be changed to a value that corresponds to a gadget in the array list. The input from the display number text field must therefore be tested using a try/catch statement to ensure that the gadget number entered is an integer and, if it is, that it is in the correct range. If the value entered is an integer but is not in the correct range, a suitable error message is displayed using a message dialog box. If the display number entered is not an integer then an alternative error message is displayed using a message dialog box. The method will therefore either return –1 or a value that corresponds to a gadget in the array list, depending on whether or not there was an error in the input. Any method that gets the display number should check its value and only use it if its value is not equal to –1.

Buttons

The GUI should have the following buttons:

  1. Add Mobile

The model, price, weight, size and credit are input via the GUI. When this button is pressed, the methods to get the model, price, weight, size and credit are called, and these values are used to create a new object of type Mobile, which is added to the array list of gadgets.

  • Add MP3

The model, price, weight, size and memory are input via the GUI. When this button is pressed, the methods to get the model, price, weight, size and memory are called, and these values are used to create a new object of type MP3, which is added to the array list of gadgets.

  • Clear

When this button is pressed, the text is cleared from each of the ten text fields.

  • Display All

When this button is pressed, the display number is printed and the method to display the details of the gadget is called, for each gadget in the array list.

  • Make A Call

The display number, phone number and duration are input via the GUI. When this button is pressed, the method to get the display number is called and its value is checked. If it is not equal to –1:

  • The display number is used to get the gadget from the array list and cast it to Mobile.
  • The method to make a call in the Mobile class is called with the phone number and duration entered.
  • Download Music

The display number and download size are input via the GUI. When this button is pressed, the method to get the display number is called and its value is checked. If it is not equal to –1:

  • The display number is used to get the gadget from the array list and cast it to MP3.
  • The method to download music in the MP3 class is called with the download size entered.

Marks

Marks will be awarded as follows:

  1. A class diagram of all 4 classes
  2. GUI
  3. The actionPerformed method
  4. The main method
  5. Functionality of buttons
  6. Reading input
  7. Use of try/catch, checking input and displaying appropriate message dialogs
  8. Program style (see http://www.bluej.org/objects-first/styleguide.html)

Report

The report should contain:

A class diagram of the GadgetShop class only, showing the name of the class, the data types and names of the fields, and the return types and signatures of all of the methods.

Pseudocode for the following button-handling methods:

  • Getting the display number from the GUI
  • Adding a mobile
  • Adding an MP3
  • Displaying all gadgets in the array list
  • Making a call
  • Downloading music

A short description of each of the 6 methods above

You should give evidence (through appropriate screenshots) of the following testing that you carried out on your program:

Test 1: Adding a mobile to the array list

Test 2: Adding an MP3 player to the array list

Test 3: Displaying the details of all of the gadgets in the array list

Test 4: Making a call

Test 5: Downloading music

Test 6: Test that the program can be compiled and run using the command prompt, including a screenshot similar to Figure 1 in the command prompt learning aid.

Test 7: Test that appropriate dialog boxes appear when unsuitable values are entered for the display number.

(Include a screenshot of the dialog box, together with a corresponding screenshot of the GUI, showing the values that were entered.)

The report should contain a section on error detection and error correction where you give examples and evidence of three errors encountered in your implementation. The errors (syntax and/or runtime) should be distinctive and not of the same type.

The  report  should  contain  a  conclusion,  where  you  evaluate  your  work,  reflecting  on  what  you  learnt  from   assignment. You are advised to study the learning aid  on  reflective  practice,  which  is  linked  from  the  module  in WebLearn.

The report should include a title page, a table of contents (with page numbers), and a listing of the code (in an appendix). Marks will also be awarded for the quality of writing and the presentation of the report.

METHODS DESCRIPTION

1. Class: Gadget

  • public Gadget(String aModel, double aPrice, int aWeight, String aSize): 
    • method that develops the constructor that all gadget objects use to get the user to input the value of each property a gadget has.
  • public String getModel(): 

an accessor method for the model of the gadget, returns model

  • public double getPrice(): 

an accessor method for the price of the gadget, returns price

  • public int getWeight(): 

       an accessor method for the weight in kilograms of the gadget, returns weight

  • public String getSize():

       an accessor method for the physical dimensions of the gadget, returns size

  • public void details(): 
    • method displaying the gadget’s description

2.  Class: Mobile

  • public Mobile(String model, double price, int weight, String size,int aCallCredit): 
    • method of connecting the mobile subclass to the gadget superclass. Develops the constructor that all gadget objects use to get the user to input the value of each property a gadget has, plus aCallCredit which gets the user to input the value of the callCredit property unique to the Mobile subclass. 
  • public int getCallCredit(): 

an accessor method for calling credit currently on the mobile, returns callCredit.

  • public void addCredit(int credit): 
    • method of applying more calling credit to the mobile. The amount is determined by user input.
  • public void makeCall(String phone, int duration): 
    • method of simulating a phone call which will use up a curtain amount of calling credit that the mobile currently has stored. The caller and the amount of calling credit used is determined by user input.
  • public void details(): 
    • method displaying the mobile’s description

3. Class: MP3

  • public MP3(String model, double price, int weight, String size,double aAvailableMemory): 
    • method of connecting the mp3 subclass to the gadget superclass. Develops the constructor that all gadget objects use to get the user to input the value of each property a gadget has, plus

aAvailableMemory which gets the user to input the value of the availableMemory property unique to the MP3 subclass.

  • public double getAvailableMemory(): 

an accessor method for available memory currently on the mp3, returns availableMemory

  • public void downloadMusic(double memoryNeeded): 
    • method of simulating a music download which will use up a curtain amount of memory that the mp3 currently has remaining. The amount of memory used is determined by user input.
  • public void deleteMusic(double memory):
    • method of simulating deleting music which will increase the amount of available memory by a curtain amount. The amount of memory gained is determined by user input.
  • public void details(): 
    • method displaying the mp3’s description

TESTS

1. Test 1: Inspect a mobile phone, add calling credit, re-inspect the mobile phone

We first need to produce a new mobile object for testing.

I used a Nokia 3310’s details for this test and gave it 100 minutes of calling credit.

Inspecting the object showed that everything was inputted correctly.

I then st
arted w
ith the
add
Credit
 
method
, deciding to add 20
minut
es of calling credit.
 
A
fter
clicking okay
, this
text window poped up displaying this
.
 
 
 
 
 
 
 
 
 
 
Inspecting t
he object
 
once ag
a
in
 
show
ed
 
that
 
the
text
 
window was
a
ccurate,
 
and
 
everything was inputted correctly.
 
 

       2.     Test 2: Inspect a mobile phone, make a call, re-inspect the mobile phone

Inspecting again as a point of reference.

I then st
arted w
ith the
makeCall
 
method
,
 
naming the callee in this
scenario
 
J
on D
oe
 
a
nd
 
deciding
that it would last
4
0
 minut
es
.
 

After clicking okay, this text window poped up displaying this.

Inspecting the object once again showed that the text window was accurate, and everything was inputted correctly.

       3. Test 3: Call the method to display the details of a mobile phone  

Inspecting again as a point of reference.

I then started with the “details” method.

After clicking, this text window poped up displaying this.

       4.       Test 4: Inspect an MP3 player, download music, re-inspect the MP3 player

 We first need to produce a new mp3 object for testing.

I used a SONY Walkman NW-E394R’s details for this test and gave it 8000 megabytes of available memory.

Inspecting the object showed that everything was inputted correctly.

then st
arted w
ith the
downloadMusic
” method
,
deciding that it
would be 200
MB
.
 

After clicking okay, this text window poped up displaying this.

Inspecting the object once again showed that the text window was accurate, and everything was inputted correctly.

5. Test 5: Inspect an MP3 player, delete a piece of music, re-inspect the MP3 player 

Inspecting again as a point of reference.

then st
arted w
ith the
delet
e
Music
” method
,
deciding that
 
I
 
would
get rid of
 
3
00
MB
 
of music
.
 
 

After clicking okay, this text window poped up displaying this.

Inspecting the object once again showed that the text window was accurate, and everything was inputted correctly.

6. Test 6: Call the method to display the details of an MP3 player

Inspecting again as a point of reference.

I then started with the “details” method.

After clicking, this text window poped up displaying this.

ERROR DECTECTION AND CORRECTION

1. Using assign operator “=” instead of logical equals “==” in checking a condition: 

Syntax errors are an annoying but frequent mistake that everyone makes at least once when first coding. Simple errors that cause the breakage of the strict and stringent computer logic rule your program must run by. They can be iso incredibly simple and seem so obtuse that even the programming software you use may not catch it.

            if (callCredit = 0) {

                System.out.println(“\n” + “You’ve used all your credit.” + “\n” + “Add more credit to make further call.”);

            } else {

                System.out.println(“\n” + “You have ” + callCredit + ” minutes of calling credit remaining.”);

Do you see the issue here? I use the wrong operator. I used “=” instead of “==”. While seemingly a mundane error where to the common eye that seems to be not an error at all, the” =” operator is used in changing the value of a variable while the “==” is used for checking and comparing the value of a variable which for the purpose of the condition of an if statement, is much more appropriate.

            if (callCredit == 0) {

                System.out.println(“\n” + “You’ve used all your credit.” + “\n” + “Add more credit to make further call.”);

            } else {

                System.out.println(“\n” + “You have ” + callCredit + ” minutes of calling credit remaining.”);

2. No data validity checks:

When commanding a program to perform an action as simple as subtracting from one variable to another variable, the code is principally, incredibly simple at its core.

public void makeCall(String phone, int duration) {

            callCredit -= duration;

}

There are, however, complications to actions done when variable must fit into certain criteria such as in this case where “callCredit” needs to always be a positive integer. This means that the code must elaborate so that the “callCredit -= duration” condition does not run if the value of “duration” exceeds the value of “callCredit”.

public void makeCall(String phone, int duration) {

        if (duration <= callCredit) {

            callCredit -= duration;

        } else {

            System.out.println(“\n” + “You don’t have enough credit to make this call.”  + “\n” + “Add more credit to make this call”);

        }

    }

In my code, the “callCredit -= duration” condition will only run IF the value of “callCredit” equals or exceeds the value of “duration”. Otherwise, the program will print out an error statement stating that “You don’t have enough credit to make this call.”  And that the user must “Add more credit to make this call”.

    public void makeCall(String phone, int duration) {

        if (duration <= callCredit) {

            callCredit -= duration;

            System.out.println(“\n” + “Making a call to ” + phone + “\n” + “…” + “\n” + “…” + “\n” + “…” + “\n” + “Call ended”);

            if (callCredit == 0) {

                System.out.println(“\n” + “You’ve used all your credit.” + “\n” + “Add more credit to make further call.”);

            } else if(callCredit == 1) {

                System.out.println(“\n” + “You have 1 minute of calling credit remaining.”);

            } else {

                System.out.println(“\n” + “You have ” + callCredit + ” minutes of calling credit remaining.”);

            }

        } else {

            System.out.println(“\n” + “You don’t have enough credit to make this call.”  + “\n” + “Add more credit to make this call”);

        }

    }

I also decided it would be valuable if the I coded in that every to the “callCredit” variable had its value change that it would inform the user of its updated value by printing out a message like “You have (insert “callCredit” value here) minutes of calling credit remaining.” If the was still calling credit left or + “You’ve used all your credit. Add more credit to make further call.” If the was no calling credit left and so on and so forth. This was added in as to give a level of ease of use to the user as they would need to constantly use different methods to get the exact amount in the variable.  

3. Passing variables declared in a superclass into a subclass:

Constructors are the best way to get a user input from a user in java. The gadget class has four variables; a string variable called model, a double variable called price, an integer variable called weight and a string variable called size. Thew user was required to input the value of each variable when creating a new instance of the gadget class.

    public Gadget(String model, double price, int weight, String size)

    {         

    }

This however has the problem of not being able to pass on these values in the subclasses of the Gadget super class.

 

After inptting the values of each variable. The input the mobile class remenbers is the one decleared in its class.

public class Gadget

{

        private String model;

        private double price;

        private int weight;

        private String size;

    public Gadget(String aModel, double aPrice, int aWeight, String aSize)

    {

        model = aModel;

        price = aPrice;

        weight = aWeight;

        size = aSize;

    }

The solution to this dilemma was rather simple. I first needed to create the four variables outside the constructor method, I then coded in the constructor method using four different named but clearly linked variables and inside the method, I made the first four variables equal the same values as the four linked constructor variables so that when the user input all the values, those values would be stored in the variables outside of the method. Thus, making them useable in the superclass and its subclasses.

CONCLUSION  

This coursework to me felt like the perfect culmination to this first term’s studies from the lectures and workshops. Every challenge it presented felt appropriate enough so that I was lost on anything it was asking but complex enough so that I needed comprehend and focus on all the work I had done for this course up till now.

Where I found the most struggle was in the error detection and correction. While I felt that I understood the concepts and theories around it, I was challenged in implementing it in my code as tying to thing outside of my perspective as the designer of the program thus the one who knew exactly what you should input, to a user, unaware of the internal code of the program thus potentially inputting variables that would break the logic.

The resolution to this dilemma came to me be looking through the codes of the past workshops and coursework examples. When looking at the structure of all those programs it became more obvious as what I need to do. I looked at the challenge criteria again, this looking what each variable needed to be, and the most common need was that “callCredit” and “availableMemory” need to always be at least 0 or above. That meant every time a method was changing their values there needed to be a safeguard that prevent the user from inputting something that would break this criterion.

This felt like I was using all my knowledge form this first term when coming to this revelation. It helped me connect everything I learnt, dot to dot, leaving me with the feeling that I had a semblance of understanding of the programming course and has me excited to carry on next term.

APPENDIX PROJECT CODE

1. Class Gadget

/**

  • Superclass Gadget.

 *

  • @author (Fred James Rivers MacKenzie-Williams)
  • @version (ver1.4 18/01/2022)

 */

public class Gadget

{

    /**

  • declaring variable of the Gadget

     */

        private String model;

        private double price;

        private int weight;

        private String size;

    /**

  • Constructor for objects of superclass Gadget

     */

    public Gadget(String aModel, double aPrice, int aWeight, String aSize)

    {

        model = aModel;

        price = aPrice;

        weight = aWeight;

        size = aSize;

    }

    /**

  • methods to display the Gadget details individually

     */

    public String getModel() 

    {

        return model;

    }

    public double getPrice() {

        return price;

    }

    public int getWeight() 

    {

        return weight;

    }

    public String getSize() 

    {

        return size;

    }

    /**

     * method of desplaying all the Gadget details at once

     */

    public void details() 

    {

        System.out.println(“Model: ” + model);

        System.out.println(“Price: £” + price);

        System.out.println(“Weight: ” + weight + ” grams”);

        System.out.println(“Size: ” + size);

    }

}

2. Class Mobile

/**

  • Subclass Mobile.

 *

  • @author (Fred James Rivers MacKenzie-Williams)
  • @version (ver1.4 18/01/2022)

 */

public class Mobile extends Gadget

{

    /**

  • declaring variable unique to the Mobile

     */     private int callCredit;

    /**

  • constructor for all objects of superclass Gadget and subclass Mobile

     */

    public Mobile(String model, double price, int weight, String size,int aCallCredit)

    {

        super(model, price, weight, size);

        callCredit = aCallCredit;      

    }

    /**

  • method of getting the calling credit in the Mobile

     */

    public int getCallCredit() {

        return callCredit;

    }

    /**

  • method of adding more calling credit to the Mobile

     */

    public void addCredit(int credit) {

        if (credit > 0) {

            callCredit += credit;

            if(callCredit == 1) {

                System.out.println(“\n” + “You have 1 minute of calling credit remaining.”);

            } else {

                System.out.println(“\n” + “You have ” + callCredit + ” minutes of calling credit remaining.”);

            }

        } else {

            System.out.println(“\n” + “You will need to add more calling credit than 0 mins.”);

        }

    }

    /**

     * method of calling from the Mobile

     */

    public void makeCall(String phone, int duration) {

        if (duration <= callCredit) {

            callCredit -= duration;

            System.out.println(“\n” + “Making a call to ” + phone + “\n” + “…” + “\n” + “…” + “\n” + “…” + “\n” + “Call ended”);

            if (callCredit == 0) {

                System.out.println(“\n” + “You’ve used all your credit.” + “\n” + “Add more credit to make further call.”);

            } else if(callCredit == 1) {

                System.out.println(“\n” + “You have 1 minute of calling credit remaining.”);

            } else {

                System.out.println(“\n” + “You have ” + callCredit + ” minutes of calling credit remaining.”);

            }

        } else {

            System.out.println(“\n” + “You don’t have enough credit to make this call.”  + “\n” + “Add more credit to make this call”);

        }

    }

    /**

     * method of desplaying details of the Mobile

     */     public void details() {

        super.details();

        System.out.println(“Calling Credit: ” + callCredit + ” minutes”);

    }

}

3. Class MP3  

/**

  • Subclass MP3.

 *

  • @author (Fred James Rivers MacKenzie-Williams)
  • @version (ver1.4 18/01/2022)

 */

public class MP3 extends Gadget

{

    /**

  • Declaring variable unique to the MP3

     */

    private double availableMemory;

    /**

  • Constructor for all objects of superclass Gadget and subclass MP3

     */

    public MP3(String model, double price, int weight, String size,double aAvailableMemory) {

        super(model, price, weight, size);

        availableMemory = aAvailableMemory;

    }

    /**

  • method of getting the available memory in the MP3

     */

    public double getAvailableMemory() {

        return availableMemory;

    }

    /**

  • method of downloading music to the MP3

     */

    public void downloadMusic(double memoryNeeded) {

        if (memoryNeeded <= availableMemory) {

            availableMemory -= memoryNeeded;

            System.out.println(“\n” + “Download 0%”  + “\n” + “…25%” + “\n” + “…50%” + “\n” + “…75%”);

            if (availableMemory == 0) {

                System.out.println(“…100%…Download Complete”);

                System.out.println(“\n” + “You’ve used all your memory.” + “\n” + “Delete some stored music to download new music.”);

            } else {

                System.out.println(“…100%…Download Complete”);

                System.out.println(“\n” + “You have ” + availableMemory + “MB of memory available.”);

            }

        } else {

            System.out.println(“\n” + “You don’t have enougth memory to download more music.”  + “\n” +

“Delete some stored music to download new music.”);

        }

    }

    /**

  • method of deleting music from the MP3

     */

    public void deleteMusic(double memory) {

        if (memory > 0) {

            availableMemory += memory;

            System.out.println(“\n” + “You have ” + availableMemory + “MB of memory available.”);

        } else {

            System.out.println(“\n” + “You will need to delete music that is more than 0MB of memory.”);

        }

    }

         /**

  • methods of desplaying details of the MP3

     */

    public void details() {

        super.details();

        System.out.println(“Available Memory: ” + availableMemory + “MB”);

    }

}          

All papers are written by ENL (US, UK, AUSTRALIA) writers with vast experience in the field. We perform a quality assessment on all orders before submitting them.

Do you have an urgent order?  We have more than enough writers who will ensure that your order is delivered on time. 

We provide plagiarism reports for all our custom written papers. All papers are written from scratch.

24/7 Customer Support

Contact us anytime, any day, via any means if you need any help. You can use the Live Chat, email, or our provided phone number anytime.

We will not disclose the nature of our services or any information you provide to a third party.

Assignment Help Services
Money-Back Guarantee

Get your money back if your paper is not delivered on time or if your instructions are not followed.

We Guarantee the Best Grades
Assignment Help Services