In: Computer Science
A) Modify the ArrayOfCircles.java file such that it implements the missing methods in the given SimpleListInterface. circArray and size are member fields which can be accessed by all method. Make sure both circArray and size are updated as needed in the method you write. 1. The methods which are already implemented are: constructor (not specified in interface) size, isFull, and the two get methods
2. The methods you need to implement (given and explained in the SimpleListInterface) are: insert search delete Implement the methods in this order and TEST each once you have implemented it. For initial tests update the testing code in the main method of ArrayOfCirclesApp.java. Only move to the next once all prior functions work. (All code needed for testing is on Blackboard and there is a movie which shows how to create the eclipse project with them and run them using the test cases I made available.) B) (i) Add a readFromFile method to the ArrayOfCirclesApp.java file with the heading given below: /** * Insert the circle info from the given file into circLst * as long as circLst is not full; will not remove elements * from circLst * @param fileName The file with the circle information */ public static void readFromFile(String fileName) { (ii) Add a printCircles method to the ArrayOfCirclesApp.java file with the heading given below: /** * Prints the info about the list: * first a message with the number of elements the list holds * and then each element (with its position in the list) * @param list The list to be printed */ public static void printCirles(SimpleListInterface list) { Do not remove or otherwise modify the existing printCircles ( ) method which is already implemented in the ArrayOfCirclesApp.java file. C) Write a short report which includes: a. An illustration of how you implemented the delete method (with diagram and explanation). Be specific – it should be clear from the diagram/explanation how each element in the array is moved during the delete. b. A description of what you considered the most challenging aspect of lab2 (which you overcame) and how you dealt with it. What lesson for future labs/assignment in this or other classes did you learn from that experience?
public class ArrayOfCircles implements SimpleListInterface { protected Circle [] circArray = null; protected int size; /** * Constructor with specified capacity * @param capacity The size of the array to be created */ public ArrayOfCircles (int capacity) { circArray = new Circle[capacity]; size = 0; } /** * * @return The number of elements in the list */ public int size() { return size; } /** * @ returns True if the list is not full; false otherwise */ public boolean isFull( ) { return (size >= circArray.length) ; } /** * Gets the circle element - * @param The element to be returned * @return The circle element in the list (with the lowest position if more than one); * null if no element in the list is equal to the given element */ public Circle get (Circle element) { int index = search(element); if (index != -1) return circArray[index]; // not found return null; } /** * Gets the circle at the specified position - * @param position The position of the elem in the list * (Note: position must be between 0 and size-1 (inclusive)) * @return The circle at the specified position */ public Circle get (int position) { return circArray[position]; } }
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ArrayOfCirclesApp { static SimpleListInterface circLst = new ArrayOfCircles(5); public static void main(String[] args) { // testing insert circLst = new ArrayOfCircles(3); circLst.insert(new Circle(1, 2, 3, "four")); circLst.insert(new Circle(3, 4, 5, "three")); printCirles(); // testing readFromFile circLst = new ArrayOfCircles(5); readFromFile("circleLoadtesting.txt"); printCirles(); } /** * Loads the circLst with the circle info from the given file * @param fileName The file with the circle information */ public static void readFromFile(String fileName) { /** * Prints the info about the list: first a message with the number of elements * and then each element * * @param list The list to be printed */ public static void printCirles(SimpleListInterface list) { System.out.println("The list has " + list.size() + " elements."); for (int i = 0; i < list.size(); i++) { System.out.println(i + ": " + list.get(i)); } } /** * Prints the info about the current list: first a message with the number of * elements and then each element */ public static void printCirles() { printCirles(circLst); } }
import java.util.Arrays; // A class to demonstrate how parameters are passed in Java public class Circle { private double radius; private double [] center = {0,0}; private String label; private int identifier; private static int counter = 1; // constructors - function OVERLOADING - one name, several DIFFERENT signatures /** * no-argument constructor - creates a circle of radius 1 centered at (0,0) */ public Circle () { this(1, 0, 0, "none"); // calls the appropriate constructor with 3 parameters } /** * Creates an object of type circle * @param radius The radius of the circle. The center defaults to (0,0) */ public Circle (double radius) { this(radius, 0, 0, "none"); // calls the appropriate constructor with 3 parameters } /** * Creates an object of type circle * @param radius The radius of the circle. * @param xCenter The x-coordinate of the center * @param yCenter The y-coordinate of the center */ public Circle(double radius, double xCenter, double yCenter, String label) { // super(); this.radius = radius; this.center[0] = xCenter; this.center[1] = yCenter; this.label = label; identifier = counter++; } /** * Creates an object of type circle * @param radius The radius of the circle. * @param center The 2D coordinates of the center of the circle */ public Circle(double radius, double[] center, String label) { this ( radius, center[0], center[1], label); } // transformer and observer methods for all instance variables @Override public String toString() { return "Circle [radius=" + radius + ", center=" + Arrays.toString(center) + ", label=" + label + ", identifier=" + identifier + "]"; } /** * Determines whether this object is equal to a given object. * @param obj The given object which may be equal (or not) * @return True if obj is equal to this; false otherwise */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Circle other = (Circle) obj; if (!Arrays.equals(center, other.center)) return false; if (Double.doubleToLongBits(radius) != Double.doubleToLongBits(other.radius)) return false; return true; } /** * @return the label */ public String getLabel() { return label; } /** * @param label the label to set */ public void setLabel(String label) { this.label = label; } /** * @return the radius */ public double getRadius() { return radius; } /** * @param radius the radius to set */ public void setRadius(double radius) { this.radius = radius; } /** * @return the center */ public double[] getCenter() { return center; } /** * @param center the center to set */ public void setCenter(double[] center) { this.center = center; } /** * @return the iD */ public int getID() { return identifier; } }
// Note: Usually this should be a generic interface - but for the purpose of lab2 we fix the // type to Circle - we will later look into how to update this to a generic interface and a // generic implementation public interface SimpleListInterface { /** * * @return The number of elements in the list */ int size(); /** * Search for the element in the list (using the equals function of T) * @param element * @return The position (start counting at 0) of the element in the list (if found); -1 otherwise */ int search (Circle element); /** * Add an element to the end of the list; if the array is full, the element is * not added. * Note: The 'end' is not at index array.length-1 but at size-1 * @param The element to be added * @return True if the element was added; false otherwise */ boolean insert(Circle element); /** * Delete the element from the list without changing the order * of the remaining elements in the list. If there is more than one element * equal to the one to be deleted, then the one with the lowest position is deleted. * @param The element to be deleted * @return true - if the element was deleted; false if the element was not found in the list */ boolean delete (Circle element) ; /** * Gets the circle element - * @param The element to be returned * @return The circle element in the list (with the lowest position if more than one); * null if no element in the list is equal to the given element */ Circle get (Circle element) ; /** * Gets the circle at the specified position - * @param position The position of the elem in the list * (Note: position must be between 0 and size-1 (inclusive)) * @return The circle at the specified position */ Circle get (int position) ; /** * @ returns True if the list is not full; false otherwise */ boolean isFull(); }
SimpleListInterface.java (Interface)
public interface SimpleListInterface {
/**
*
* @return The number of elements in the list
*/
int size();
/**
* Search for the element in the list (using the equals function of
T)
*
* @param element
* @return The position (start counting at 0) of the element in the
list (if
* found); -1 otherwise
*/
int search(Circle element);
/**
* Add an element to the end of the list; if the array is full, the
element
* is not added. Note: The 'end' is not at index array.length-1 but
at
* size-1
*
* @param The element to be added
* @return True if the element was added; false otherwise
*/
boolean insert(Circle element);
/**
* Delete the element from the list without changing the order of
the
* remaining elements in the list. If there is more than one element
equal
* to the one to be deleted, then the one with the lowest position
is
* deleted.
*
* @param The element to be deleted
* @return true - if the element was deleted; false if the element
was not
* found in the list
*/
boolean delete(Circle element);
/**
* Gets the circle element -
*
* @param The element to be returned
* @return The circle element in the list (with the lowest position
if more
* than one); null if no element in the list is equal to the given
element
*/
Circle get(Circle element);
/**
* Gets the circle at the specified position -
*
* @param position The position of the elem in the list (Note:
position must
* be between 0 and size-1 (inclusive))
* @return The circle at the specified position
*/
Circle get(int position);
/**
* @ returns True if the list is not full; false otherwise
*/
boolean isFull();
}
Circle.java
import java.util.Arrays;
public class Circle {
private double radius;
private double[] center = {0, 0};
private String label;
private int identifier;
private static int counter = 1;
public Circle()
{
this.radius = 1;
this.center[0] = 0;
this.center[1] = 0;
this.label = "none";
}
public Circle(double radius)
{
this.radius = radius;
this.center[0] = 0;
this.center[1] = 0;
this.label = "none";
}
public Circle(double radius, double xCenter, double yCenter, String
label)
{
this.radius = radius;
this.center[0] = xCenter;
this.center[1] = yCenter;
this.label = label;
identifier = counter++;
}
public Circle(double radius, double[] center, String label)
{
this.radius = radius;
this.center[0] = center[0];
this.center[1] = center[1];
this.label = label;
}
@Override
public String toString()
{
return "Circle [radius=" + radius + ", center=" +
Arrays.toString(center) + ", label=" + label + ",
identifier="
+ identifier + "]";
}
@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Circle other = (Circle) obj;
if (!Arrays.equals(center, other.center)) {
return false;
}
if (Double.doubleToLongBits(radius) !=
Double.doubleToLongBits(other.radius)) {
return false;
}
return true;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double[] getCenter() {
return center;
}
public void setCenter(double[] center) {
this.center = center;
}
public int getID() {
return identifier;
}
}
ArrayOfCircles.java
public class ArrayOfCircles implements SimpleListInterface {
protected Circle[] circArray = null;
protected int size;
/**
* Constructor with specified capacity
*
* @param capacity The size of the array to be created
*/
public ArrayOfCircles(int capacity) {
circArray = new Circle[capacity];
size = 0;
}
/**
*
* @return The number of elements in the list
*/
@Override
public int size() {
return size;
}
/**
* @ returns True if the list is not full; false otherwise
*/
@Override
public boolean isFull() {
return (size >= circArray.length);
}
/**
* Gets the circle element -
*
* @param element The element to be returned
* @return The circle element in the list (with the lowest position
if more
* than one); null if no element in the list is equal to the given
element
*/
@Override
public Circle get(Circle element) {
int index = search(element);
if (index != -1) {
return circArray[index];
}
// not found
return null;
}
/**
* Gets the circle at the specified position -
*
* @param position The position of the elem in the list (Note:
position must
* be between 0 and size-1 (inclusive))
* @return The circle at the specified position
*/
@Override
public Circle get(int position) {
return circArray[position];
}
@Override
public int search(Circle element) {
int index = -1;
for(int i = 0; i < size; i++)
{
if(circArray[i].equals(element))
{
index = i;
break;
}
}
return index;
}
@Override
public boolean insert(Circle element) {
boolean isInserted;
if(isFull())
isInserted = false;
else
{
circArray[size++] = element;
isInserted = true;
}
return isInserted;
}
@Override
public boolean delete(Circle element) {
int indexToDelete = search(element);
if(indexToDelete != -1)
{
System.arraycopy(circArray, indexToDelete + 1, circArray,
indexToDelete, size - 1 - indexToDelete);
return true;
}
else
return false;
}
}
ArrayOfCirclesApp.java (Main class)
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ArrayOfCirclesApp {
static SimpleListInterface circLst = new ArrayOfCircles(5);
public static void main(String[]args)
{
// testing insert
circLst = new ArrayOfCircles(3);
circLst.insert(new Circle(1, 2, 3, "four"));
circLst.insert(new Circle(3, 4, 5, "three"));
printCirles();
// testing readFromFile
System.out.println();
circLst = new ArrayOfCircles(5);
readFromFile("circleLoadtesting.txt");
printCirles();
}
public static void readFromFile(String fileName)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(fileName));
while(fileReader.hasNextLine())
{
// assuming data fields in the file are separated by comma
String line = fileReader.nextLine().trim();
String[] info = line.split(",");
double radius = Double.parseDouble(info[0]);
double xCenter = Double.parseDouble(info[1]);
double yCenter = Double.parseDouble(info[2]);
String label = info[3];
circLst.insert(new Circle(radius, xCenter, yCenter, label));
}
}catch(FileNotFoundException fnfe){
System.out.println("Could not locate file: " + fileName);
System.exit(0);
}
}
public static void printCirles(SimpleListInterface list) {
System.out.println("The list has " + list.size() + "
elements.");
for (int i = 0; i < list.size(); i++) {
System.out.println(i + ": " + list.get(i));
}
}
public static void printCirles() {
printCirles(circLst);
}
}
***************************************************************** SCREENSHOT **********************************************************