Hi guys,
So for my next assignment I am required to, at some point, create a function that searches through an array of objects and sets a boolean value in a corresponding boolean array at the same position as the object in the object array, if that makes any sense.
I'll show you the code and where I'm having trouble:
ELEMENT ABSTRACT CLASS:
package assignment.pkg2;
public abstract class Element {
public String getClassName()
{
String resultStr;
int location;
resultStr = this.toString();
location = resultStr.indexOf('@');
return resultStr.substring(0, location);
}
public abstract void readIn();
public abstract void display();
public abstract boolean equals(Element otherElement);
public abstract Element clone(); [B]// Here I am told to add a @Override annotation for this statement and each subsequent clone function declarations in each class, why is this?[/B]
}
SUBSCRIBER CLASS:
package assignment.pkg2;
import java.util.Scanner;
public class Subscriber extends Element {
private String name;
private String url;
private int adClicks;
public Subscriber()
{
name = "";
url = "";
adClicks = 0;
}
public Subscriber(String aName)
{
name = aName.toUpperCase();
}
public Subscriber(Subscriber originalSub)
{
name = originalSub.name;
url = originalSub.url;
adClicks = originalSub.adClicks;
}
public String getName()
{
return name;
}
public String getUrl()
{
return url;
}
public int getAdClicks()
{
return adClicks;
}
public void setName(String aName)
{
name = aName.toUpperCase();
}
public void setUrl(String aUrl)
{
url = aUrl;
}
public void setAdClicks(int aAdClicks)
{
adClicks = aAdClicks;
}
public void readIn()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Subscriber's name: ");
name = keyboard.nextLine().toUpperCase();
System.out.println("Subscriber's url: ");
url = keyboard.nextLine();
System.out.println("Subscriber's ad clicks: ");
adClicks = keyboard.nextInt();
}
public void display()
{
System.out.println("\nSubscriber: " + name);
System.out.println("URL: " + url);
System.out.println("\n" + adClicks + " users have clicked on "
+ "your ads.");
}
public boolean equals(Element otherSubscriber)
{
return name.equals(((Subscriber) otherSubscriber).name);
}
public Element clone()
{
Subscriber aClone = new Subscriber();
aClone.name = name;
aClone.url = url;
aClone.adClicks = adClicks;
return aClone;
}
}
APPLICATION CLASS:
package assignment.pkg2;
import java.util.Scanner;
public class Application extends Element{
public String name;
public String url;
public int downloads;
public Application()
{
name = "";
url = "";
downloads = 0;
}
public Application(String aName)
{
name = aName.toUpperCase();
}
public Application(Application originalApp)
{
name = originalApp.name;
url = originalApp.url;
downloads = originalApp.downloads;
}
public String getName()
{
return name;
}
public String getUrl()
{
return url;
}
public int getDownloads()
{
return downloads;
}
public void setName(String aName)
{
name = aName.toUpperCase();
}
public void setUrl(String aUrl)
{
url = aUrl;
}
public void setDownloads(int aDownload)
{
aDownload = downloads;
}
public void readIn()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Application's name: ");
name = keyboard.nextLine().toUpperCase();
System.out.println("Application's url: ");
url = keyboard.nextLine();
System.out.println("Application's downloads: ");
downloads = keyboard.nextInt();
}
public void display()
{
System.out.println("\nApplication: " + name);
System.out.println("URL: " + url);
System.out.println("\n" + downloads + " users have downloaded "
+ "this application.");
}
public boolean equals(Element otherApplication)
{
return name.equals(((Application) otherApplication).name);
}
public Element clone()
{
Application aClone = new Application();
aClone.name = name;
aClone.url = url;
aClone.downloads = downloads;
return aClone;
}
}
ELEMENT SET CLASS
:
package assignment.pkg2;
public class ElementSet {
private Element[] elementList;
private boolean[] flagged;
private int currentIndex;
private int currentSize;
private final int MAXSETSIZE = 100;
public ElementSet()
{
elementList = new Element[MAXSETSIZE];
flagged = new boolean[MAXSETSIZE];
currentIndex = -1;
currentSize = 0;
}
public boolean isMemberOf(Element anElement)
{
String paramClass = anElement.getClassName();
String currClass;
for (int i = 0; i < currentSize; i++)
{
currClass = elementList[i].getClassName();
if (currClass.equals(paramClass))
{
if (elementList[i].equals(anElement))
{
return true;
}
}
}
return false;
}
public boolean isFull()
{
return currentSize == MAXSETSIZE;
}
public boolean isEmpty()
{
return currentSize == 0;
}
public int size()
{
return currentSize;
}
public Element getCurrent()
{
// Local data ...
int saveIndex = currentIndex;
// Logic ...
if (currentIndex == currentSize - 1)
{
// Recycle to beginning of list
currentIndex = 0;
}
else
{
// Advance currentIndex to next object
currentIndex++;
}
// Return a reference to a clone of the current object
return elementList[saveIndex].clone();
}
public int add(Element anElement)
{
// Logic ...
if (currentSize == MAXSETSIZE)
{
return 0; // set is full
}
else if (this.isMemberOf(anElement))
{
return -1; // it's already in there
}
// We will add a clone of anElement to
// the set.
elementList[currentSize] = anElement.clone();
// Increment currentSize.
currentSize++;
// Set currentIndex to object we just added if it was the
// first object in the set.
if (currentSize == 1) currentIndex = 0;
// We succeeded.
return 1;
}
public void clear()
{
// Clean up the memory associated with this object
// while it is still in use.
for (int i = 0; i < currentSize; i++)
{
elementList[i] = null;
}
// Reset currentSize and currentIndex to empty set
// values.
currentIndex = -1;
currentSize = 0;
}
public void display()
{
if (currentSize == 0)
{
System.out.println("There are no objects in the set. ");
}
else
{
System.out.println("Here are the objects in the set: \n");
for (int i = 0; i < currentSize; i++)
{
elementList[i].display();
System.out.println("\n");
}
}
}
public int flagIt(Element anElement) [B]//this is the function I am unsure is correct[/B]
{
String paramClass = anElement.getClassName();
String currClass;
for (int i = 0; i < currentSize; i++)
{
currClass = elementList[i].getClassName();
if (currClass.equals(paramClass))
{
if (elementList[i].equals(anElement) && flagged[i] != false)
{
System.out.println("Subscriber found, but has already been"
+ " flagged.");
return 1;
}
else if (elementList[i].equals(anElement) && flagged[i] != true)
{
System.out.println("Subscriber found and is now"
+ " flagged.");
flagged[i] = true;
return 0;
}
}
}
System.out.println("Subscriber not found.");
return -1;
}
/* public int unFlagIt(Element anElement)
{
}
public void displayAllInClass(String theClassName)
{
}
*/
}
Some of the parts in the ElementSet class were given my our instructor, such as the "isMemberOf" function. When he explained what we needed to do with the "flagIt" functions I assumed it would be fairly similar to his "isMemberOf" function, but now I'm just unsure. Could anyone give me some guidance?
Also, when I attempt to call the function in main like this:
public class Assignment2 {
public static void main(String[] args) {
Subscriber sub1;
sub1 = new Subscriber();
sub1.flagIt();
}
}
it returns an error "cannot find symbol" on the calling line; is the function not integrated into the other classes? Why would that be?
Thank you for any help!