From fc11fa2d2e39725bba11daa251874435a6db08d1 Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Sun, 21 Sep 2025 11:05:05 -0700 Subject: [PATCH] add comments, override equals + hashCode methods --- .idea/workspace.xml | 11 ++- src/code/ca/bcit/comp2522/lab03/IDevice.java | 28 +++++- src/code/ca/bcit/comp2522/lab03/IPad.java | 66 ++++++++++++- src/code/ca/bcit/comp2522/lab03/IPhone.java | 72 +++++++++++++- src/code/ca/bcit/comp2522/lab03/IPhone16.java | 93 ++++++++++++++++++- src/code/ca/bcit/comp2522/lab03/IPod.java | 71 +++++++++++++- src/code/ca/bcit/comp2522/lab03/Main.java | 11 +++ 7 files changed, 341 insertions(+), 11 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index beb1c4d..84263ca 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -6,6 +6,12 @@ + + + + + + diff --git a/src/code/ca/bcit/comp2522/lab03/IDevice.java b/src/code/ca/bcit/comp2522/lab03/IDevice.java index f5a22bc..00b3f5a 100644 --- a/src/code/ca/bcit/comp2522/lab03/IDevice.java +++ b/src/code/ca/bcit/comp2522/lab03/IDevice.java @@ -1,18 +1,40 @@ package ca.bcit.comp2522.lab03; +/** + * IDevice holds common information between + * devices and common functionality. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public abstract class IDevice { private final String purpose; - IDevice(final String purpose) { + /** + * IDevice constructor + * @param purpose of IDevice + */ + public IDevice(final String purpose) { this.purpose = purpose; } - abstract void printDetails(); + /** + * printDetails of IDevice child + */ + public abstract void printDetails(); - protected String getPurpose() { + /** + * getPurpose of IDevice + * @return purpose of IDevice + */ + public String getPurpose() { return this.purpose; } + /** + * toString returns details of IDevice + * @return purpose of IDevice + */ @Override public String toString() { return this.purpose; diff --git a/src/code/ca/bcit/comp2522/lab03/IPad.java b/src/code/ca/bcit/comp2522/lab03/IPad.java index 27eba40..9a4fc46 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPad.java +++ b/src/code/ca/bcit/comp2522/lab03/IPad.java @@ -1,10 +1,22 @@ package ca.bcit.comp2522.lab03; +/** + * IPad is an IDevice that contains related variables to IPads + * such as operating system version, and if it has a case. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public class IPad extends IDevice { private final boolean hasCase; private final String OSVersion; - IPad( + /** + * IPad constructor + * @param hasCase determines if the IPad has a case + * @param OSVersion of the IPad + */ + public IPad( final boolean hasCase, final String OSVersion ) { @@ -14,7 +26,19 @@ public class IPad extends IDevice { this.OSVersion = OSVersion; } - protected void printDetails() { + /** + * getOSVersion of IPad + * @return operating system version + */ + public String getOSVersion() { + return this.OSVersion; + } + + /** + * printDetails of IPad including if it has a case + * and which operating system version it is running + */ + public void printDetails() { final StringBuilder details; details = new StringBuilder(); @@ -27,8 +51,46 @@ public class IPad extends IDevice { System.out.println(details.toString()); } + /** + * toString returns the simple string details of the IPad + * @return IPad instance variables + */ @Override public String toString() { return super.toString() + " " + this.hasCase + " " + this.OSVersion; } + + /** + * equals check if a given IPad has the same operating + * system version as this IPad + * @param o the reference object with which to compare. + * @return if the IPads have the same OS Version + */ + @Override + public boolean equals(final Object o) { + // Ensure object exists + if (o == null) { + return false; + } + + // Ensure object is an IPad + if(!(o.getClass().equals(this.getClass()))) { + return false; + } + + final IPad otherIPad; + + otherIPad = (IPad) o; + + return otherIPad.getOSVersion().equals(this.OSVersion); + } + + /** + * hashCode of IPad + * @return hash code of IPad + */ + @Override + public int hashCode() { + return this.OSVersion.hashCode(); + } } diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone.java b/src/code/ca/bcit/comp2522/lab03/IPhone.java index fbf9092..be17fca 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone.java @@ -1,10 +1,25 @@ package ca.bcit.comp2522.lab03; +/** + * IPhone is an IDevice that has a phone plan from a carrier, and + * how many minutes remain on the phone plan. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public class IPhone extends IDevice { + private static final int IPHONE_HASH = 11; + private static final int IPHONE_HASH_CODE = 101; + private final double remainingPhonePlanMinutes; private final String carrier; - IPhone( + /** + * IPhone constructor + * @param remainingPhonePlanMinutes on this IPhone + * @param carrier that the phone plan is registered to + */ + public IPhone( final double remainingPhonePlanMinutes, final String carrier ) { @@ -14,7 +29,19 @@ public class IPhone extends IDevice { this.carrier = carrier; } - protected void printDetails() { + /** + * getRemainingPhonePlanMinutes of this IPHone + * @return remaining phone plan minutes + */ + public double getRemainingPhonePlanMinutes() { + return this.remainingPhonePlanMinutes; + } + + /** + * printDetails of IPhone including carrier and remaining + * phone plan minutes + */ + public void printDetails() { final StringBuilder details; details = new StringBuilder(); @@ -27,8 +54,49 @@ public class IPhone extends IDevice { System.out.println(details.toString()); } + /** + * toString returns the simple string details of the IPhone + * @return IPhone instance variables + */ @Override public String toString() { return super.toString() + " " + this.remainingPhonePlanMinutes + " " + this.carrier; } + + + /** + * equals check if a given IPhone has the same remaining + * phone plan minutes as this IPhone. + * @param o the reference object with which to compare. + * @return if the IPhone has the same remaining phone plan minutes + */ + @Override + public boolean equals(final Object o) { + // Ensure object exists + if (o == null) { + return false; + } + + // Ensure object is an IPhone + if(!(o.getClass().equals(this.getClass()))) { + return false; + } + + final IPhone otherIPhone; + + otherIPhone = (IPhone) o; + + return otherIPhone.getRemainingPhonePlanMinutes() == this.remainingPhonePlanMinutes; + } + + /** + * hashCode of IPhone + * @return hash code of IPhone + */ + @Override + public int hashCode() { + int hash; + hash = IPHONE_HASH_CODE * IPHONE_HASH * (int) this.remainingPhonePlanMinutes; + return hash; + } } diff --git a/src/code/ca/bcit/comp2522/lab03/IPhone16.java b/src/code/ca/bcit/comp2522/lab03/IPhone16.java index 6c8cf65..a16b68e 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPhone16.java +++ b/src/code/ca/bcit/comp2522/lab03/IPhone16.java @@ -1,10 +1,26 @@ package ca.bcit.comp2522.lab03; +/** + * IPhone16 extends the IPhone and has additional + * features. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public class IPhone16 extends IPhone { + private static final int IPHONE_HASH_WITH_HIGH_RES = 103; + final boolean highResolutionCamera; final int memoryGB; - IPhone16( + /** + * IPhone16 constructor + * @param remainingPhonePlanMinutes of this IPhone + * @param carrier that the phone plan is registered to + * @param highResolutionCamera status of this IPhone + * @param memoryGB of this IPhone + */ + public IPhone16( final double remainingPhonePlanMinutes, final String carrier, final boolean highResolutionCamera, @@ -15,4 +31,79 @@ public class IPhone16 extends IPhone { this.highResolutionCamera = highResolutionCamera; this.memoryGB = memoryGB; } + + /** + * getHghResolutionCamera status of this IPhone + * @return if this IPhone has a high resolution camera + */ + public boolean getHighResolutionCamera() { + return this.highResolutionCamera; + } + + /** + * toString returns the simple string details of the IPhone16 + * @return IPhone16 instance variables + */ + @Override + public String toString() { + final StringBuilder builder; + + builder = new StringBuilder(); + builder.append(super.toString()); + builder.append(" high-res camera: "); + builder.append(this.highResolutionCamera); + builder.append(" memory GB: "); + builder.append(this.memoryGB); + + return builder.toString(); + } + + /** + * equals check if a given IPhone16 has the same remaining + * phone plan minutes as this IPhone16 and both have high + * resolution cameras. + * @param o the reference object with which to compare. + * @return if the IPhone has the same remaining phone plan minutes and high resolution camera + */ + @Override + public boolean equals(final Object o) { + // Ensure object exists + if (o == null) { + return false; + } + + // Ensure object is an IPhone + if(!(o.getClass().equals(this.getClass()))) { + return false; + } + + final IPhone16 otherIPhone; + + otherIPhone = (IPhone16) o; + + final boolean matchingMinutes; + final boolean matchingCameraResolution; + + matchingMinutes = otherIPhone.getRemainingPhonePlanMinutes() == this.getRemainingPhonePlanMinutes(); + matchingCameraResolution = this.highResolutionCamera && otherIPhone.getHighResolutionCamera(); + + return matchingMinutes && matchingCameraResolution; + } + + /** + * hashCode of IPhone16 + * @return hash code of IPhone16 + */ + @Override + public int hashCode() { + int hash; + + hash = super.hashCode(); + + if (this.highResolutionCamera) { + hash *= IPHONE_HASH_WITH_HIGH_RES; + } + + return hash; + } } diff --git a/src/code/ca/bcit/comp2522/lab03/IPod.java b/src/code/ca/bcit/comp2522/lab03/IPod.java index f18c67a..9f6219a 100644 --- a/src/code/ca/bcit/comp2522/lab03/IPod.java +++ b/src/code/ca/bcit/comp2522/lab03/IPod.java @@ -1,10 +1,25 @@ package ca.bcit.comp2522.lab03; +/** + * IPod is an IDevice that has a number of songs and a max + * volume in decibels. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public class IPod extends IDevice { + private static final int IPOD_HASH = 7; + private static final int IPOD_HASH_CODE = 97; + private final int numberOfSongs; private final double maxVolumeDecibels; - IPod( + /** + * IPod constructor + * @param numberOfSongs the IPod is holding + * @param maxVolumeDecibels is the maximum volume + */ + public IPod( final int numberOfSongs, final double maxVolumeDecibels ) { @@ -14,7 +29,11 @@ public class IPod extends IDevice { this.maxVolumeDecibels = maxVolumeDecibels; } - protected void printDetails() { + /** + * printDetails of IPod including number of songs + * and max volume in decibels. + */ + public void printDetails() { final StringBuilder details; details = new StringBuilder(); @@ -27,8 +46,56 @@ public class IPod extends IDevice { System.out.println(details.toString()); } + /** + * getNumberOfSongs of the IPod + * @return the number of songs + */ + public int getNumberOfSongs() { + return this.numberOfSongs; + } + + /** + * toString returns the simple string details of the IPod + * @return IPod instance variables + */ @Override public String toString() { return super.toString() + " " + this.numberOfSongs + " " + this.maxVolumeDecibels; } + + /** + * equals check if a given IPod has the same number of songs as + * this IPod + * @param o the reference object with which to compare. + * @return if the IPods have the same number of songs + */ + @Override + public boolean equals(final Object o) { + // Ensure object exists + if (o == null) { + return false; + } + + // Ensure object is an IPod + if(!(o.getClass().equals(this.getClass()))) { + return false; + } + + final IPod otherIPod; + + otherIPod = (IPod) o; + + return otherIPod.getNumberOfSongs() == this.numberOfSongs; + } + + /** + * hashCode of IPod + * @return hash code of IPod + */ + @Override + public int hashCode() { + int hash; + hash = IPOD_HASH_CODE * IPOD_HASH + this.numberOfSongs; + return hash; + } } diff --git a/src/code/ca/bcit/comp2522/lab03/Main.java b/src/code/ca/bcit/comp2522/lab03/Main.java index 005441d..7ad972c 100644 --- a/src/code/ca/bcit/comp2522/lab03/Main.java +++ b/src/code/ca/bcit/comp2522/lab03/Main.java @@ -1,6 +1,17 @@ package ca.bcit.comp2522.lab03; +/** + * Main class to test IDevice classes, and abstract concepts. + * + * @author Braeden Sowinski + * @version 1.0.0 + */ public class Main { + + /** + * main program entry + * @param args from user input + */ public static void main(final String[] args) { // Create IPod objects final IPod ipod1;