Here it is in C#. I didn't do it in Java because I didn't want to do all the work for you, but at least this shows how to implement what you want and some test cases. Keep in mind your instructor asked for only 1 instance variable in your class, and you have 2, 4 if you count the static vars as instance variables, although technically a static variable isn't the same as an instance variable, so you should be safe there. It depends on if he meant member variables and made a mistake and said instance variables instead. Better to be safe than get dinged on it though.
Code:
using System;
namespace ConsoleApplication
{
/// <summary>
/// Some goofy class to print out zeroes and ones as spaces and asterisks.
/// </summary>
class LetterPrinter
{
// The single instance variable allowed which caches the string passed in to the constructor.
private string _Characters = string.Empty;
/// <summary>
/// Creates an instance of the LetterPrinter class.
/// </summary>
/// <param name="Characters">A string containing exactly 35 characters consisting entirely of zeroes and ones.</param>
public LetterPrinter(string Characters)
{
// Check to make sure the string isn't null or empty, and that it contains exactly 35 characters.
if (string.IsNullOrEmpty(Characters) || Characters.Length != 35)
{
throw new ArgumentException("Class needs to be initialized with 35 \"0\" or \"1\" characters.");
}
// Validate the data, ensuring that each character in the string is either a zero or a one.
foreach (char c in Characters)
{
if ('0' != c && '1' != c)
{
throw new ArgumentOutOfRangeException("Constructor arguments are out of range. Values need to be within the range of 0 or 1.");
}
}
// Store the validated string.
_Characters = Characters;
}
/// <summary>
/// Get accessor to obtains access to the internal _Characters variable.
/// </summary>
public string Characters
{
get
{
return _Characters;
}
}
/// <summary>
/// Used to compare two instances of a LetterPrinter class.
/// </summary>
/// <param name="lp">An instance of a LetterPriner class to compare to.</param>
/// <returns>True if the two instances are equal, otherwise False.</returns>
public bool Equals(LetterPrinter lp)
{
if (lp == null)
{
return false;
}
return string.Equals(this._Characters, lp._Characters);
}
/// <summary>
/// Prints the internal character string as a series of spaces or asterisks, in the form of a 5x7 grid.
/// </summary>
public void PrintString()
{
for (int i = 0; i < _Characters.Length; i++)
{
if ('0' == _Characters[i])
{
Console.Write(" ");
}
else
{
Console.Write("*");
}
if (0 == ((i + 1) % 5))
{
Console.Write('\n');
}
}
}
}
class Program
{
static void Main(string[] args)
{
// Some test variables.
bool CaughtException = false;
LetterPrinter lp1 = null;
LetterPrinter lp2 = null;
string s = string.Empty;
// Initialize a new instance here.
lp1 = new LetterPrinter("01110001000010000100001000010001110");
// Test out the Get accessor.
s = lp1.Characters;
// Test out the Equals method, passing in an uninitialized variable first to make sure the two instance do not match.
if(lp1.Equals(lp2))
{
throw new ArgumentException("lp1 class matches lp2 when lp2 is null.");
}
// Test the constructor to make sure an exception is thrown when invalid arguments are passed (incorrect length.)
try
{
lp2 = new LetterPrinter("0101");
}
catch (ArgumentException)
{
CaughtException = true;
}
if (!CaughtException)
{
throw new Exception("Didn't catch ArgumentException when passing invalid args to LetterPrinter class constructor.");
}
CaughtException = false;
// Test the constructor to make sure an exception is thrown when invalid arguments are passed (invalid characters.)
try
{
lp2 = new LetterPrinter("342356`sds");
}
catch (ArgumentException)
{
CaughtException = true;
}
if (!CaughtException)
{
throw new Exception("Didn't catch ArgumentException when passing invalid args to LetterPrinter class constructor.");
}
// Assign the instance variable to an unintialized instance and make sure they match.
lp2 = lp1;
if (!lp1.Equals(lp2))
{
throw new ArgumentException("lp1 class does not match lp2 when lp2 should be a match.");
}
lp2 = new LetterPrinter("01110001000010000100001000010001110");
if (!lp1.Equals(lp2))
{
throw new ArgumentException("lp1 class does not match lp2 when lp2 should be a match.");
}
lp1.PrintString();
}
}
}