being lab 03, TODO: override equals + hashCode

This commit is contained in:
SowinskiBraeden committed 2025-09-19 21:41:12 -07:00
1 parent 18b7c28e65
commit 080506e1ef
9 files changed
+289 -25

No files matched your search

@@ -0,0 +1,20 @@
package ca.bcit.comp2522.lab03;
public abstract class IDevice {
private final String purpose;
IDevice(final String purpose) {
this.purpose = purpose;
}
abstract void printDetails();
protected String getPurpose() {
return this.purpose;
}
@Override
public String toString() {
return this.purpose;
}
}
+34
View File
@@ -0,0 +1,34 @@
package ca.bcit.comp2522.lab03;
public class IPad extends IDevice {
private final boolean hasCase;
private final String OSVersion;
IPad(
final boolean hasCase,
final String OSVersion
) {
super("learning");
this.hasCase = hasCase;
this.OSVersion = OSVersion;
}
protected void printDetails() {
final StringBuilder details;
details = new StringBuilder();
details.append("Has case: ");
details.append(this.hasCase);
details.append(" OS Version: ");
details.append(this.OSVersion);
System.out.println(details.toString());
}
@Override
public String toString() {
return super.toString() + " " + this.hasCase + " " + this.OSVersion;
}
}
@@ -0,0 +1,34 @@
package ca.bcit.comp2522.lab03;
public class IPhone extends IDevice {
private final double remainingPhonePlanMinutes;
private final String carrier;
IPhone(
final double remainingPhonePlanMinutes,
final String carrier
) {
super("talking");
this.remainingPhonePlanMinutes = remainingPhonePlanMinutes;
this.carrier = carrier;
}
protected void printDetails() {
final StringBuilder details;
details = new StringBuilder();
details.append(this.remainingPhonePlanMinutes);
details.append(" remaining minutes (");
details.append(this.carrier);
details.append(")");
System.out.println(details.toString());
}
@Override
public String toString() {
return super.toString() + " " + this.remainingPhonePlanMinutes + " " + this.carrier;
}
}
@@ -0,0 +1,18 @@
package ca.bcit.comp2522.lab03;
public class IPhone16 extends IPhone {
final boolean highResolutionCamera;
final int memoryGB;
IPhone16(
final double remainingPhonePlanMinutes,
final String carrier,
final boolean highResolutionCamera,
final int memoryGB
) {
super(remainingPhonePlanMinutes, carrier);
this.highResolutionCamera = highResolutionCamera;
this.memoryGB = memoryGB;
}
}
+34
View File
@@ -0,0 +1,34 @@
package ca.bcit.comp2522.lab03;
public class IPod extends IDevice {
private final int numberOfSongs;
private final double maxVolumeDecibels;
IPod(
final int numberOfSongs,
final double maxVolumeDecibels
) {
super("music");
this.numberOfSongs = numberOfSongs;
this.maxVolumeDecibels = maxVolumeDecibels;
}
protected void printDetails() {
final StringBuilder details;
details = new StringBuilder();
details.append(this.numberOfSongs);
details.append(" songs, ");
details.append(this.maxVolumeDecibels);
details.append(" dB.");
System.out.println(details.toString());
}
@Override
public String toString() {
return super.toString() + " " + this.numberOfSongs + " " + this.maxVolumeDecibels;
}
}
+100
View File
@@ -0,0 +1,100 @@
package ca.bcit.comp2522.lab03;
public class Main {
public static void main(final String[] args) {
// Create IPod objects
final IPod ipod1;
final IPod ipod2;
final IPod ipod3;
ipod1 = new IPod(300, 80.0); // 300 songs, max volume 80.0 dB
ipod2 = new IPod(400, 85.0); // 400 songs, max volume 85.0 dB
ipod3 = new IPod(300, 70.0); // 300 songs, max volume 70.0 dB
// Test equality and inequality for IPod
System.out.println("IPod Equality Test:");
if (!ipod1.equals(ipod2)) {
System.out.println("CORRECT: ipod1 is not equal to ipod2");
} else {
System.out.println("INCORRECT: ipod1 should not be equal to ipod2");
}
if (ipod1.equals(ipod3)) {
System.out.println("CORRECT: ipod1 is equal to ipod3");
} else {
System.out.println("INCORRECT: ipod1 should be equal to ipod3");
}
System.out.println();
// Create IPad objects
final IPad ipad1;
final IPad ipad2;
final IPad ipad3;
ipad1 = new IPad(true, "iPadOS 15"); // Has case, OS version iPadOS 15
ipad2 = new IPad(false, "iPadOS 14"); // No case, OS version iPadOS 14
ipad3 = new IPad(true, "iPadOS 15"); // Has case, OS version iPadOS 15
// Test equality and inequality for IPad
System.out.println("IPad Equality Test:");
if (!ipad1.equals(ipad2)) {
System.out.println("CORRECT: ipad1 is not equal to ipad2");
} else {
System.out.println("INCORRECT: ipad1 should not be equal to ipad2");
}
if (ipad1.equals(ipad3)) {
System.out.println("CORRECT: ipad1 is equal to ipad3");
} else {
System.out.println("INCORRECT: ipad1 should be equal to ipad3");
}
System.out.println();
// Create IPhone objects
final IPhone iphone1;
final IPhone iphone2;
final IPhone iphone3;
iphone1 = new IPhone(120.0, "Verizon"); // 120 minutes, carrier Verizon
iphone2 = new IPhone(180.0, "T-Mobile"); // 180 minutes, carrier T-Mobile
iphone3 = new IPhone(120.0, "AT&T"); // 120 minutes, carrier AT&T
// Test equality and inequality for IPhone
System.out.println("IPhone Equality Test:");
if (!iphone1.equals(iphone2)) {
System.out.println("CORRECT: iphone1 is not equal to iphone2");
} else {
System.out.println("INCORRECT: iphone1 should not be equal to iphone2");
}
if (iphone1.equals(iphone3)) {
System.out.println("CORRECT: iphone1 is equal to iphone3");
} else {
System.out.println("INCORRECT: iphone1 should be equal to iphone3");
}
System.out.println();
// Create IPhone16 objects
final IPhone16 iphone16_1;
final IPhone16 iphone16_2;
final IPhone16 iphone16_3;
iphone16_1 = new IPhone16(100.0, "Verizon", true, 512); // 100 minutes, high-res camera, 512 GB
iphone16_2 = new IPhone16(100.0, "Verizon", true, 256); // 100 minutes, high-res camera, 256 GB
iphone16_3 = new IPhone16(100.0, "Verizon", false, 512); // 100 minutes, no high-res camera, 512 GB
// Test equality and inequality for IPhone16
System.out.println("IPhone16 Equality Test:");
if (iphone16_1.equals(iphone16_2)) {
System.out.println("CORRECT: iphone16_1 is equal to iphone16_2");
} else {
System.out.println("INCORRECT: iphone16_1 should be equal to iphone16_2");
}
if (!iphone16_1.equals(iphone16_3)) {
System.out.println("CORRECT: iphone16_1 is not equal to iphone16_3");
} else {
System.out.println("INCORRECT: iphone16_1 should not be equal to iphone16_3");
}
System.out.println();
}
}