JAVA Class design problem Assignment Description You will create two Class objec
ID: 3844687 • Letter: J
Question
JAVA Class design problem
Assignment Description You will create two Class objects (and subsequently submit two .java files)
• Digitoid
• Driver
Notes:
• ONLY the Driver Class should have static methods.
• The Device Class should NOT have static methods or static instance variables (constants ok)
. • The below is the minimum you must include – you could include additional instance variables, methods, etc. as you need.
Class Design: Digitoid
The Digitoid class is intended to be an abstract and simplified representation of a digital character. Each Digitoid must have some properties associated with it. Data to store
• Digitoid name
o Make this a private instance with a public getter and setter
• Digitoid type (e.g. humanoid, robotic, aquatic, flying, insectoid, alien/xenomorph, etc.)
o Make this a private instance with a public getter and private setter
• Experience (representing how “mature” the Digitoid is)
o Make this a private instance with a public getter and private setter Available actions
• [2 Points] Constructor
o Take in as argument the Digitoid name (enforce invariants) – set to a “default” type of your choice.
o Throw an exception as necessary
• [2 Points] Overloaded Constructor
o Take in as argument the Digitoid name as well as type (enforce invariants)
o Throw an exception as necessary
• [4 Points] Interact with another Digitoid
o If the other Digitoid is
More experienced, increase experience by 3
Equally experienced, increase experience by 2
Less experienced, increase experience by 1
Of the same type, multiple experience gained by 2
Of another type, multiple experience gained by 3
The “same” Digitoid (e.g. equals() returns true), halve all experience gained through this interaction (multiply experience again by 0.5)
• [2 Points] toString() override
o You decide what String it should return (should use at least one of the properties above)
• [2 Points] equals() override
o You decide what makes two instances “equal” (should compare at least one property)
Class Design: Driver
• “Positive” testing (checking for “valid” conditions)
o [1 Point] Create a Digitoid with only name
o [1 Point] Create a Digitoid with both name and type
o [1 Point] Compare two Digitoids where the equals() method returns true
o [1 Point] Compare two Digitoids where the equals() method returns false
o [1 Point] Print the toString() of a Digitoid
• “Negative” testing (checking for “invalid” conditions)
o [1 Point] Attempt to create a Digitoid with “invalid” name (e.g. empty string)
o [1 Point] Attempt to interact with itself (should NOT be allowed)
• Interaction Testing
Test every type of interaction possible for a Digitoid (call the “interact” method)
: o [1 Point] Same type, higher experience
o [1 Point] Same type, same experience
o [1 Point] Same type, lower experience
o [1 Point] Different type, higher experience
o [1 Point] Different type, same experience
o [1 Point] Different type, lower experience
o [1 Point] Where equals() is false
o [1 Point] Where equals() is true
Explanation / Answer
Hi,
Please see the classes and output. Please comment for any queries/feedbacks.
Thanks,
Digitoid.java
public class Digitoid {
private String name;
private String type;
private double experience;
//Constructor
public Digitoid(String name) throws Exception{
super();
if(null!= name && !"".equalsIgnoreCase(name) ){
this.name = name;
this.type = "default";
}
else{
throw new Exception("Invalid name!");
}
}
//Overloaded Constructor
public Digitoid(String name, String type) throws Exception{
super();
if(null!= type && !"".equalsIgnoreCase(type)){
if(null!= name && !"".equalsIgnoreCase(name)){
this.name = name;
this.type = type;
}
else{
throw new Exception("Invalid type!");
}
}
else{
throw new Exception("Invalid name!");
}
}
public void interact(Digitoid digitoid){
if(this.equals(digitoid)){
// Attempt to interact with itself (should NOT be allowed)
System.out.println("Cannot interact with same digitoid");
return;
}
if(this.getExperience()<digitoid.getExperience()){
//If the other Digitoid is More experienced, increase experience by 3
this.setExperience(this.getExperience()+3);
}
else if(this.getExperience() == digitoid.getExperience()){
//If the other Digitoid is Equally experienced, increase experience by 2
this.setExperience(this.getExperience()+2);
}
else if(this.getExperience() > digitoid.getExperience()){
//If the other Digitoid is Less experienced, increase experience by 1
this.setExperience(this.getExperience()+1);
}
if(this.getType().equalsIgnoreCase(digitoid.getType())){
//If the other Digitoid is Of the same type, multiple experience gained by 2
this.setExperience(this.getExperience()*2);
}
else{
//If the other Digitoid is Of another type, multiple experience gained by 3
this.setExperience(this.getExperience()*3);
}
if(this.equals(digitoid)){
//The “same” Digitoid (e.g. equals() returns true),
//halve all experience gained through this interaction
//(multiply experience again by 0.5)
this.setExperience(this.getExperience() * .5);
}
}
//Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getExperience() {
return experience;
}
public void setExperience(double experience) {
this.experience = experience;
}
/**
* toString
*/
public String toString(){
String retString = "Name : "+this.getName()+" Type : "+this.getType()+" Exp : "+this.getExperience();
return retString;
}
/**
* equals
*/
public boolean equals(Digitoid digitoid){
//This will return true if the two digitoids have same name and type
if(this.getName().equalsIgnoreCase(digitoid.getName())
&& this.getType().equalsIgnoreCase(digitoid.getType())){
return true;
}
else{
return false;
}
}
}
Driver.java
public class Driver {
public static void main(String [] args) throws Exception{
/**** "Positive" testing (checking for "valid" conditions) ****/
// Create a Digitoid with only name
System.out.println("Creating digitoid1");
Digitoid digitoid1 = new Digitoid("Dig1");
// Create a Digitoid with both name and type
System.out.println("Creating digitoid2 with type humanoid");
Digitoid digitoid2 = new Digitoid("Dig2","humanoid");
// Compare two Digitoids where the equals() method returns true
Digitoid digitoid3 = new Digitoid("Dig3","robotic");
Digitoid digitoid4 = new Digitoid("Dig3","robotic");
System.out.println("digitoid3 equals digitoid4 ? :" +digitoid3.equals(digitoid4));
// Compare two Digitoids where the equals() method returns false
Digitoid digitoid5 = new Digitoid("Dig5","flying");
Digitoid digitoid6 = new Digitoid("Dig6","flying");
System.out.println("digitoid5 equals digitoid6 ? :" +digitoid5.equals(digitoid6));
// Print the toString() of a Digitoid
Digitoid digitoid7 = new Digitoid("Dig7","insectoid");
System.out.println(digitoid7);
/**** “Negative” testing (checking for “invalid” conditions) ****/
// Attempt to create a Digitoid with “invalid” name (e.g. empty string)
System.out.println("Creating digitoid8 with invalid name");
// Digitoid digitoid8 = new Digitoid(""); // uncomment this line for Invalid name test
// Attempt to interact with itself (should NOT be allowed)
digitoid7.interact(digitoid7);
/**** Interaction Testing ****/
//Same type, higher experience
Digitoid digitoid9 = new Digitoid("digitoid9","alien");
Digitoid digitoid10 = new Digitoid("digitoid10","alien");
digitoid9.setExperience(3);
digitoid10.setExperience(5);
digitoid10.interact(digitoid9);
System.out.println("digitoid10 : " +digitoid10);
// Same type, same experience
Digitoid digitoid11 = new Digitoid("digitoid11","alien");
Digitoid digitoid12 = new Digitoid("digitoid12","alien");
digitoid11.setExperience(3);
digitoid12.setExperience(3);
digitoid11.interact(digitoid12);
System.out.println("digitoid11 : " +digitoid11);
// Same type, lower experience
Digitoid digitoid13 = new Digitoid("digitoid13","alien");
Digitoid digitoid14 = new Digitoid("digitoid14","alien");
digitoid13.setExperience(3);
digitoid14.setExperience(1);
digitoid13.interact(digitoid14);
System.out.println("digitoid11 : " +digitoid13);
// Different type, higher experience
Digitoid digitoid15 = new Digitoid("digitoid15","insectoid");
Digitoid digitoid16 = new Digitoid("digitoid16","alien");
digitoid15.setExperience(3);
digitoid16.setExperience(1);
digitoid15.interact(digitoid16);
System.out.println("digitoid15 : " +digitoid15);
//Different type, same experience
Digitoid digitoid17 = new Digitoid("digitoid17","insectoid");
Digitoid digitoid18 = new Digitoid("digitoid18","alien");
digitoid17.setExperience(3);
digitoid18.setExperience(3);
digitoid17.interact(digitoid18);
System.out.println("digitoid17 : " +digitoid17);
//Different type, lower experience
Digitoid digitoid19 = new Digitoid("digitoid19","insectoid");
Digitoid digitoid20 = new Digitoid("digitoid20","alien");
digitoid19.setExperience(3);
digitoid20.setExperience(8);
digitoid19.interact(digitoid20);
System.out.println("digitoid19 : " +digitoid19);
//Where equals() is false
Digitoid digitoid21 = new Digitoid("digitoid21","insectoid");
Digitoid digitoid22 = new Digitoid("digitoid22","insectoid");
digitoid21.setExperience(3);
digitoid22.setExperience(8);
digitoid21.interact(digitoid22);
System.out.println("digitoid21 : " +digitoid21);
// Where equals() is true
Digitoid digitoid23 = new Digitoid("digitoid23","insectoid");
Digitoid digitoid24 = new Digitoid("digitoid23","insectoid");
digitoid23.setExperience(3);
digitoid22.setExperience(8);
digitoid23.interact(digitoid24);
System.out.println("digitoid23 : " +digitoid23);
}
}
Sample output:
Creating digitoid1
Creating digitoid2 with type humanoid
digitoid3 equals digitoid4 ? :true
digitoid5 equals digitoid6 ? :false
Name : Dig7 Type : insectoid Exp : 0.0
Creating digitoid8 with invalid name
Cannot interact with same digitoid
digitoid10 : Name : digitoid10 Type : alien Exp : 12.0
digitoid11 : Name : digitoid11 Type : alien Exp : 10.0
digitoid11 : Name : digitoid13 Type : alien Exp : 8.0
digitoid15 : Name : digitoid15 Type : insectoid Exp : 12.0
digitoid17 : Name : digitoid17 Type : insectoid Exp : 15.0
digitoid19 : Name : digitoid19 Type : insectoid Exp : 18.0
digitoid21 : Name : digitoid21 Type : insectoid Exp : 12.0
Cannot interact with same digitoid
digitoid23 : Name : digitoid23 Type : insectoid Exp : 3.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.