0% found this document useful (0 votes)
22 views

CC4001 Programming Engineering

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

CC4001 Programming Engineering

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Cc4001 Programming Engineering

//start of Gadget.java

public class Gadget {

// Gadget class attributes

private String model;

private double price;

private int weight;

private String size;

// Constructor to initialize all fields

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

this.model = model;

this.price = price;

this.weight = weight;

this.size = size;

// Getter method for model

public String getModel() {

return model;

// Getter method for price

public double getPrice() {

return price;

// Getter method for weight

public int getWeight() {

return weight;

}
// Getter method for size

public String getSize() {

return size;

// Method to display the gadget details

public void display() {

System.out.println("Model: " + model);

System.out.println("Price: " + price + " pounds");

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

System.out.println("Size: " + size);

// End of Gadget.java

// Start of Mobile.java

public class Mobile extends Gadget {

// Additional attribute

private int callCredit;

// Constructor to initialize all attributes

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

// Passing model, price, weight, and size to superclass constructor

super(model, price, weight, size);

// Initializing call credit

this.callCredit = callCredit;

// Getter method for call credit


public int getCallCredit() {

return callCredit;

// Method to add credits to the call credit value

public void addCredit(int credits) {

// Validating credit

if (credits > 0) {

// Valid value, adding

callCredit += credits;

} else {

// Invalid value, displaying error message

System.out.println("ERROR: Invalid credit value");

// Method to make a call if there is enough call credit

public void makeCall(String phone, int duration) {

// Checking if there is enough credit to make the call

if (duration <= callCredit) {

// Making call

System.out.println("Making call to " + phone);

// Deducting call credit

callCredit -= duration;

} else {

// Not enough credit, displaying error message

System.out.println("ERROR: Insufficient credits to make this call");

// Method to display all details of Mobile


public void display() {

// Displaying Gadget details

super.display();

// Displaying available call credit

System.out.println("Call Credit: " + callCredit);

System.out.println("Call Credit: " + callCredit + " minutes");

// End of Mobile.java

// Start of MP3.java

public class MP3 extends Gadget {

// Additional attribute

private double availableMemory;

// Constructor to initialize all fields

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

// Passing model, price, weight, and size to superclass constructor

super(model, price, weight, size);

// Initializing available memory

this.availableMemory = availableMemory;

// Getter method for the available memory

public double getAvailableMemory() {

return availableMemory;

// Method to simulate downloading music

public void downloadMusic(double memoryNeeded) {


// Checking if there is enough memory

if (memoryNeeded <= availableMemory) {

// Valid, deducting from availableMemory

availableMemory -= memoryNeeded;

} else {

// Not enough memory

System.out.println("ERROR: Insufficient memory!");

// Method to free up memory

public void deleteMusic(double memory) {

// Validating memory

if (memory > 0) {

// Adding to available memory

availableMemory += memory;

} else {

System.out.println("ERROR: Invalid memory value");

// Method to display MP3 details

public void display() {

// Displaying common gadget details

super.display();

// Displaying available memory

System.out.println("Available Memory: " + availableMemory);

// End of MP3.java

// UML class diagram

You might also like