add comments, override equals + hashCode methods

This commit is contained in:
SowinskiBraeden committed 2025-09-21 11:05:05 -07:00
1 parent 0e3eb54256
commit fc11fa2d2e
7 files changed
+341 -11

No files matched your search

+10 -1
View File
@@ -6,6 +6,12 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="4481728b-524b-48ee-9d70-e414a15f05bd" name="Changes" comment=""> <list default="true" id="4481728b-524b-48ee-9d70-e414a15f05bd" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IDevice.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IDevice.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPad.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPad.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPhone.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPhone.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPhone16.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPhone16.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPod.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/IPod.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/code/ca/bcit/comp2522/lab03/Main.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -40,7 +46,7 @@
"ModuleVcsDetector.initialDetectionPerformed": "true", "ModuleVcsDetector.initialDetectionPerformed": "true",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
"git-widget-placeholder": "main", "git-widget-placeholder": "lab03",
"kotlin-language-version-configured": "true", "kotlin-language-version-configured": "true",
"last_opened_file_path": "/home/flami/Projects/comp2522", "last_opened_file_path": "/home/flami/Projects/comp2522",
"node.js.detected.package.eslint": "true", "node.js.detected.package.eslint": "true",
@@ -107,6 +113,9 @@
<workItem from="1758254368145" duration="997000" /> <workItem from="1758254368145" duration="997000" />
<workItem from="1758294307794" duration="4368000" /> <workItem from="1758294307794" duration="4368000" />
<workItem from="1758342126924" duration="1015000" /> <workItem from="1758342126924" duration="1015000" />
<workItem from="1758413771974" duration="332000" />
<workItem from="1758425336799" duration="21000" />
<workItem from="1758473282464" duration="4563000" />
</task> </task>
<servers /> <servers />
</component> </component>
+25 -3
View File
@@ -1,18 +1,40 @@
package ca.bcit.comp2522.lab03; 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 { public abstract class IDevice {
private final String purpose; private final String purpose;
IDevice(final String purpose) { /**
* IDevice constructor
* @param purpose of IDevice
*/
public IDevice(final String purpose) {
this.purpose = 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; return this.purpose;
} }
/**
* toString returns details of IDevice
* @return purpose of IDevice
*/
@Override @Override
public String toString() { public String toString() {
return this.purpose; return this.purpose;
+64 -2
View File
@@ -1,10 +1,22 @@
package ca.bcit.comp2522.lab03; 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 { public class IPad extends IDevice {
private final boolean hasCase; private final boolean hasCase;
private final String OSVersion; 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 boolean hasCase,
final String OSVersion final String OSVersion
) { ) {
@@ -14,7 +26,19 @@ public class IPad extends IDevice {
this.OSVersion = OSVersion; 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; final StringBuilder details;
details = new StringBuilder(); details = new StringBuilder();
@@ -27,8 +51,46 @@ public class IPad extends IDevice {
System.out.println(details.toString()); System.out.println(details.toString());
} }
/**
* toString returns the simple string details of the IPad
* @return IPad instance variables
*/
@Override @Override
public String toString() { public String toString() {
return super.toString() + " " + this.hasCase + " " + this.OSVersion; 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();
}
} }
+70 -2
View File
@@ -1,10 +1,25 @@
package ca.bcit.comp2522.lab03; 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 { 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 double remainingPhonePlanMinutes;
private final String carrier; 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 double remainingPhonePlanMinutes,
final String carrier final String carrier
) { ) {
@@ -14,7 +29,19 @@ public class IPhone extends IDevice {
this.carrier = carrier; 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; final StringBuilder details;
details = new StringBuilder(); details = new StringBuilder();
@@ -27,8 +54,49 @@ public class IPhone extends IDevice {
System.out.println(details.toString()); System.out.println(details.toString());
} }
/**
* toString returns the simple string details of the IPhone
* @return IPhone instance variables
*/
@Override @Override
public String toString() { public String toString() {
return super.toString() + " " + this.remainingPhonePlanMinutes + " " + this.carrier; 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;
}
} }
+92 -1
View File
@@ -1,10 +1,26 @@
package ca.bcit.comp2522.lab03; 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 { public class IPhone16 extends IPhone {
private static final int IPHONE_HASH_WITH_HIGH_RES = 103;
final boolean highResolutionCamera; final boolean highResolutionCamera;
final int memoryGB; 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 double remainingPhonePlanMinutes,
final String carrier, final String carrier,
final boolean highResolutionCamera, final boolean highResolutionCamera,
@@ -15,4 +31,79 @@ public class IPhone16 extends IPhone {
this.highResolutionCamera = highResolutionCamera; this.highResolutionCamera = highResolutionCamera;
this.memoryGB = memoryGB; 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;
}
} }
+69 -2
View File
@@ -1,10 +1,25 @@
package ca.bcit.comp2522.lab03; 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 { 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 int numberOfSongs;
private final double maxVolumeDecibels; 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 int numberOfSongs,
final double maxVolumeDecibels final double maxVolumeDecibels
) { ) {
@@ -14,7 +29,11 @@ public class IPod extends IDevice {
this.maxVolumeDecibels = maxVolumeDecibels; this.maxVolumeDecibels = maxVolumeDecibels;
} }
protected void printDetails() { /**
* printDetails of IPod including number of songs
* and max volume in decibels.
*/
public void printDetails() {
final StringBuilder details; final StringBuilder details;
details = new StringBuilder(); details = new StringBuilder();
@@ -27,8 +46,56 @@ public class IPod extends IDevice {
System.out.println(details.toString()); 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 @Override
public String toString() { public String toString() {
return super.toString() + " " + this.numberOfSongs + " " + this.maxVolumeDecibels; 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;
}
} }
+11
View File
@@ -1,6 +1,17 @@
package ca.bcit.comp2522.lab03; package ca.bcit.comp2522.lab03;
/**
* Main class to test IDevice classes, and abstract concepts.
*
* @author Braeden Sowinski
* @version 1.0.0
*/
public class Main { public class Main {
/**
* main program entry
* @param args from user input
*/
public static void main(final String[] args) { public static void main(final String[] args) {
// Create IPod objects // Create IPod objects
final IPod ipod1; final IPod ipod1;