import java.util.*;
public class Explore
{
public static void main(String[] args)
{
//initializing object data
String s1="aa";
String s2="bb";
String s3="cc";
String s4="dd";
String INVALIDSTR="ajfaworngvowrngvhaworgvnaer;igvnae;rigvnowihefowahg"
+ "owarhgaei;rgvn";
String VALIDSTR="test2";
//Instantiate four ThreeString objects
ThreeString myObject1 = new ThreeString();
ThreeString myObject2 = new ThreeString(s1, s2, s3);
ThreeString myObject3 = new ThreeString();
ThreeString myObject4 = new ThreeString(s2, s3, s4);
System.out.println("=========Original objects========");
System.out.println("Object 1: "+myObject1.toString());
System.out.println("Object 2: "+myObject2.toString());
System.out.println("Object 3: "+myObject3.toString());
System.out.println("Object 4: "+myObject4.toString());
//mutate one member in each object
myObject1.setString1(s4);
myObject2.setString2(s1);
myObject3.setString3(s3);
myObject4.setString1(s2);
System.out.println("=========Mutated objects=========");
System.out.println("Object 1: "+myObject1.toString());
System.out.println("Object 2: "+myObject2.toString());
System.out.println("Object 3: "+myObject3.toString());
System.out.println("Object 4: "+myObject4.toString());
//Mutator Tests
System.out.println("\n=========Mutator tests==========\n");
//Test 1, unsuccessful
if (myObject1.setString1(INVALIDSTR))
{
System.out.println("TEST1: setString1 mutator call successful");
System.out.println("myObject1.string1 is "+ myObject1.getString1());
}
else
{
System.out.println("TEST1: setString1 mutator call unsuccessful");
System.out.println("myObject1.string1 is "+ myObject1.getString1());
}
//Test 2, successful
if (myObject3.setString3(VALIDSTR))
{
System.out.println("TEST2: setString1 mutator call successful");
System.out.println("myObject3.string3 is "+ myObject3.getString3());
}
else
{
System.out.println("TEST2: setString1 mutator call unsuccessful");
System.out.println("myObject3.string3 is "+ myObject3.getString3());
}
//Accessor Tests
System.out.println("\n=========Acessor calls==========\n");
System.out.println("myObject1.string1 is "+ myObject1.getString1());
System.out.println("myObject2.string2 is "+ myObject2.getString2());
}
}
class ThreeString
{
//private members
private String string1;
private String string2;
private String string3;
//public constants
public static final int LENGTH_MIN = 1;
public static final int LENGTH_MAX = 50;
public static final String DEFAULT_STRING = " (undefined) ";
//default constructor
ThreeString()
{
string1 = DEFAULT_STRING;
string2 = DEFAULT_STRING;
string3 = DEFAULT_STRING;
}
//constructor that takes parameters
ThreeString(String str1, String str2, String str3)
{
if (validString(str1) && validString(str2) && validString(str3))
{
string1 = str1;
string2 = str2;
string3 = str3;
}
else
{
string1 = DEFAULT_STRING;
string2 = DEFAULT_STRING;
string3 = DEFAULT_STRING;
}
}
// Mutators and Accessors for all 3 private members----------------------
public boolean setString1(String str)
{
boolean status = false;
if (validString(str))
{
string1 = str;
status = true;
}
return status;
}
public String getString1()
{
return string1;
}
public boolean setString2(String str)
{
boolean status = false;
if (validString(str))
{
string2 = str;
status = true;
}
return status;
}
public String getString2()
{
return string2;
}
public boolean setString3(String str)
{
boolean status = false;
if (validString(str))
{
string3 = str;
status = true;
}
return status;
}
public String getString3()
{
return string3;
}
//------------------------------------------------------------------
//method to display all member information
public String toString()
{
return string1+" "+string2+" "+string3 +"\n";
}
// private method to check if string is valid
private boolean validString( String str )
{
if ((str.length() > LENGTH_MAX) || (str.length() < LENGTH_MIN))
{
return false;
}
else
{
return true;
}
}
}
Explore.main(null);