Question

In: Computer Science

A) Modify the ArrayOfCircles.java file such that it implements the missing methods in the given SimpleListInterface....

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();

}

Solutions

Expert Solution

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 **********************************************************


Related Solutions

Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Write a C++ program that implements a simple scanner for a source file given as a...
Write a C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Optionally, your program can build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a...
Create MySortedArrayCollection.java This file is inherited from SortedArrayCollection.java This file implements MySortedArrayCollectionInterface.java ****** Make new and...
Create MySortedArrayCollection.java This file is inherited from SortedArrayCollection.java This file implements MySortedArrayCollectionInterface.java ****** Make new and just complete MySortedArrayCollection.java, dont modify others //////////////////////////////////////////////////////// SortedArrayCollection.java public class SortedArrayCollection<T> implements CollectionInterface<T> {    protected final int DEFCAP = 100; // default capacity    protected int origCap; // original capacity    protected T[] elements; // array to hold collection elements    protected int numElements = 0; // number of elements in this collection    // set by find method    protected boolean found;...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the...
(Javascript) Modify the JavaScript file to implement a function named calculateTotalPrice. At the bottom of the file are sample inputs and outputs to test your implementation. /* * The price per ticket depends on the number of tickets * purchased, there are 4 ticket pricing tiers. Given the * number of tickets return the total price, formatted to * exactly two decimal places with a leading dollar sign. * Tier 1: *   Minimum number of tickets: 1 *   Price per...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
You should assume that there will be an object file named “foreignsub.o” which implements a function...
You should assume that there will be an object file named “foreignsub.o” which implements a function whose prototype is given in the header file “foreignsub.h”. The header file “foreignsub.h” consists of the following line. int sub(int n, int *A, int *B, int *C); However, you do not know what this function does exactly. For your testing purpose, you may produce such a function, e.g., returning the maximum value among the 3n integers in the three arrays. You are also given...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT