Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my code and it is not working can you please help fix it. these are the output requirements. Create short (three to five

Here is my code and it is not working can you please help fix it. these are the output requirements.

Create short (three to five long) String type linked list to test all the methods.

The displayList( ) method must be tested on Rectangle type list

Add two static fields of type long to the application class named squares and occurrences

Add a static void method named counting to the applications class. The method takes a Rectangle array named boxes and a Rectangle object named target for parameters.

The method counts the number of squares among the array element, and stores the value in the squares field, and it also counts the number of array elements that are equal to the target and stores the result in the occurrences field.

Step 1: Create a LinkedSequence of 100 000 Rectangle objects each having integer dimensions randomly selected between 1 and 30.

Step 2: Verify that the listPosition() method returns the tail reference for position number 100 000.

Step 3: instantiate a Rectangle array to the list length (do not use literals) and load the Rectangles from the list to the array.

Step 4: Create a target Rectangle with side 15, 15 and call the counting method passing your array and target as parameters.

Step 5: Measure the running time of each step above as well as the combined time and record the results. Hint: use the method call System.nanoTime() to record the current real time in nanoseconds (the return type of the method is long); note that 109nanos make one second.

Step 6: Repeat Steps 1- 5 for 1 000 000 Rectangle objects

Step 7: Repeat Steps 1 5. 10 000 000 Rectangle objects.

Step 8: Repeat Step 7 by adding the non-random target rectangle of step 4 to the empty list 10 000 000 times. Check out if the random selection in step 7 is a significant overhead for the running time of the algorithm or not.

Analyze your running time observations, deduce Big-Oh estimates and advise about the expected time for the case of 100 000 000 rectangles. Attach your report as a comment to the source code following the application class.

import java.util.Random;

import java.util.Scanner;

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

/**

*

* @author Scott

*/

public class Fox_Project1 {

public static void main(String[] args) {

long squares;

long occurrances;

long size = 100000;

long time;

long time1;

long time2;

long time3;

long time4;

long time5;

Node head = new Node("Paul", null);

Node tail = head.addNodeAfter("saul");

tail = tail.addNodeAfter("mauls");

tail = tail.addNodeAfter("sauls");

Node head1 = new Node("john", null);

Node tail1 = head1.addNodeAfter("nathan");

tail = head.addNodeAfter("bill");

tail = head.addNodeAfter("ryan");

LinkedSequence list1 = new LinkedSequence(head);

LinkedSequence list2 = new LinkedSequence(head1);

LinkedSequence list3;

//cloning list2 to a blank list.

list3 = list2.clone();

tail1 = tail1.addNodeAfter("nick");

tail1 = tail1.addNodeAfter("aaron");

//displaying the lists

list1.displayList();

System.out.println(" ");

list1.addNodeAfter("bill");

list1.displayList();

System.out.println(" ");

list1.addNodeAfter("bob");

System.out.println(" ");

list1.displayList();

System.out.println(" ");

list1.addAll(list2);

System.out.println(" ");

list1.displayList();

System.out.println(" ");

list3.displayList();

System.out.println(" ");

//concatenating the lists.

LinkedSequence.concatanate(list3, list1).displayList();

long l1;

l1 = System.nanoTime();

time = l1;

//creating large linked lists with rectangle objects

Random ran = new Random();

Rectangle rec = new Rectangle(ran.nextInt(30) + 1, ran.nextInt(30) + 1);

Node rhead = new Node(rec, null);

LinkedSequence seq = new LinkedSequence(rhead);

seq.setCursor(rhead);

Node rtail = new Node(null, null);

seq.setTail(rtail);

for (int i = 0; i < size; i++) {

Rectangle rec1 = new Rectangle(ran.nextInt(30) + 1, ran.nextInt(30) + 1);

rtail = rhead.addNodeAfter(rec1);

seq.advance();

}

//completing step 5 with 10000 size list

time1 = System.nanoTime();

System.out.println("Step 5 took " + (time1 - time) / Math.pow(10, 9));

System.out.println(Node.listPosition(rhead, size).toString());

size = 1000000;

time2 = System.nanoTime();

for (int i = 0; i < size; i++) {

Rectangle rec1 = new Rectangle(ran.nextInt(30) + 1, ran.nextInt(30) + 1);

rtail = rhead.addNodeAfter(rec1);

seq.advance();

}

//completeing step 6 with 1000000 size list

time3 = System.nanoTime();

System.out.println("Step 6 took " + (time3 - time2) / Math.pow(10, 9));

System.out.println(Node.listPosition(rhead, size).toString());

time4 = System.nanoTime();

size = 10000000;

for (int i = 0; i < size; i++) {

Rectangle rec1 = new Rectangle(ran.nextInt(30) + 1, ran.nextInt(30) + 1);

rtail = rhead.addNodeAfter(rec1);

seq.advance();

}

//completeing final step.

time5 = System.nanoTime();

System.out.println("Step 7 took " + (time5 - time4) / Math.pow(10, 9));

System.out.println(Node.listPosition(head, size).toString());

int s = 10000000;

Rectangle[] rect = new Rectangle[s];

for (int i =0;i < size;i++){

rect[i] = seq.getCurrent();

seq.advance();

}

Rectangle target = new Rectangle(17,17);

System.out.println(seq.getCurrent());

System.out.println(counting(rect,target));

}

public static String counting(Rectangle[] boxes, Rectangle target) {

long count = 0;

long occur = 0;

for (int i = 1; i < boxes.length; i++) {

if (boxes[i].getLength() == boxes[i].getWidth()) {

count++;

}

}

for (int g = 0; g < boxes.length; g++) {

if (boxes[g] == target) {

occur++;

}

}

return count + " " + occur;

}

}

public class Node

{

private T data;

private Node link;

Node(T initialData, Node initialLink){

this.data = initialData;

this.link = initialLink;

}

public Node addNodeAfter(T element){

link = new Node(element,link);

return link;

}

public void removeNodeAfter(Node head){

int x =0;

Node cursor;

cursor = head;

for (x = 1; x < listLength(head); x++) {

cursor = cursor.link;

}

cursor.data = null;

}

/*

*/

public String toString(int dum) {

String s1 = "";

String s2 = "";

if (data == null) {

s1 = "data: dummy";

} else {

s1 = "data: " + data.toString();

}

if (link == null) {

s2 = "link: null in tail";

} else {

s2 = "link: " + link.toString();

}

return s1 + " " + s2;

}

public String toString(){

String message = data.toString();

return message;

}

public static Node listCopy(Node source){

Node copyHead;

Node copyTail;

if(source == null){

return null;

}

copyHead = new Node(source.getData(), null);

copyTail = copyHead;

while(source.link != null){

source = source.link;

copyTail.addNodeAfter(source.data);

copyTail = copyTail.link;

}

return source;

}

public static Object listPosition(Node head, long position){

Node cursor;

int i = 1;

if(position <= 0){

throw new IllegalArgumentException("position is not positive.");

}

cursor = head;

for (i = 1; (i < position)&&(cursor != null); i++) {

cursor = cursor.link;

}

return cursor.getData();

}

public static int listLength(Node head){

Node cursor;

int answer = 0;

for(cursor = head; cursor != null; cursor = cursor.link){

answer++;

}

return answer;

}

public static Node getTail(Node source){

int i;

Node cursor;

cursor = source;

for(i=1; i < listLength(source); i++){

cursor = cursor.link;

}

return cursor;

}

public void setLink(Node newLink){

link = newLink;

}

public void setData(T newData){

data = newData;

}

public T getData() {

return data;

}

public Node getLink() {

return link;

}

}

public class LinkedSequence implements Cloneable{

private Node head, tail, cursor, precursor;

private Node dummy = new Node(null,null);

private int manyNodes = 0;

public Node getHead() {

return head;

}

public void setHead(Node head) {

this.head = head;

}

public Node getTail() {

return tail;

}

public void setTail(Node tail) {

this.tail = tail;

}

public Node getCursor() {

return cursor;

}

public void setCursor(Node cursor) {

this.cursor = cursor;

}

public Node getPrecursor() {

return precursor;

}

public void setPrecursor(Node precursor) {

this.precursor = precursor;

}

public Node getDummy() {

return dummy;

}

public void setDummy(Node dummy) {

this.dummy = dummy;

}

public int getManyNodes() {

return manyNodes;

}

public void setManyNodes(int manyNodes) {

this.manyNodes = manyNodes;

}

public LinkedSequence(Node h){

this.head = h;

head = tail;

dummy.setLink(head);

}

public Node addNodeAfter(T data){

if (head == null) {

precursor = dummy;

head = dummy.addNodeAfter(data);

return cursor = head;

} else if (cursor == null) {

precursor = tail;

cursor = tail.addNodeAfter(data);

return cursor = tail;

} else {

precursor = cursor;

return cursor = cursor.addNodeAfter(data);

}

}

public Node addBefore(T data)

{

if (precursor == null) {

head = new Node(data, head);

return head = cursor;

} else {

precursor.setLink(dummy.addNodeAfter(data));

return cursor = precursor.getLink();

}

}//end addBefore method

public void addAll(LinkedSequence added)

{

if (added == null || added.head == null) {

return;

}

if (head == null) {

this.head = added.head;

this.tail = added.tail;

} else {

this.tail.setLink(added.head);

this.tail = added.tail;

}

}//end addAll method

public void advance()

{

if (isCurrent() && cursor != tail) {

precursor = cursor;

cursor = cursor.getLink();

} else if (!isCurrent()) {

precursor = dummy;

cursor = head;

} else {

precursor = tail;

cursor = null;

}

}//end else statement

public Node start()

{

cursor = head;

return null;

}

@Override

public LinkedSequence clone()

{

LinkedSequence copy = null;

try {

copy = (LinkedSequence) super.clone();

} catch (CloneNotSupportedException ex) {

System.out.println("CloneNotSupportedException");

}

copy.head = Node.listCopy(head);

copy.tail = Node.getTail(head);

return copy;

}

public static LinkedSequence concatanate(LinkedSequence s1, LinkedSequence s2)

{

LinkedSequence s3 = new LinkedSequence(null);

if((s1 == null)||(s2 == null))

{

throw new NullPointerException("Can not have an empty list.");

}

else

{

s3.head = Node.listCopy(s1.head);

s3.tail = Node.getTail(s2.head);

return s3;

}

}

public Node removeCurrent(Node head)

{

if (!isCurrent()){

return null;

}

if (cursor == null);

{

cursor.setLink(cursor.getLink().getLink());

}

return head;

}

public void displayList(){

System.out.println(head.toString());

}

public String displayList(int count){

String indentation = "";

for (int i = 1;i

indentation += " ";

}

return "data: " + head + " " + indentation +"link: " + cursor; // Final value of toString method

}

// added methid to help with the advance method and remove current, checks current value

public boolean isCurrent() {

if (cursor != null) {

return false;

} else {

return true;

}

}

public T getCurrent(){

if (!isCurrent()|| cursor == null){

return cursor.getData();

}

else {

return cursor.getData();

}

}

}

public class Rectangle {

private int width;

private int length;

public Rectangle(int w, int l){

this.length = l;

this.width = w;

}

/**

*

* @param Rect1

* @param Rect2

* @return

*/

public boolean equals(Rectangle Rect1,Rectangle Rect2){

if (Rect1.length == Rect2.length){

if (Rect1.width==Rect2.width){

return true;

}

}

return false;

}

@Override

public String toString(){

String message = "";

message = "width = " + width + " Length = " + length;

return message;

}

public int getWidth() {

return width;

}

public void setWidth(int width) {

this.width = width;

}

public int getLength() {

return length;

}

public void setLength(int length) {

this.length = length;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems For Advanced Applications 15th International Conference Dasfaa 2010 International Workshops Gdm Benchmarx Mcis Snsmw Diew Udm Tsukuba Japan April 2010 Revised Selected Papers Lncs 6193

Authors: Masatoshi Yoshikawa ,Xiaofeng Meng ,Takayuki Yumoto ,Qiang Ma ,Lifeng Sun ,Chiemi Watanabe

2010th Edition

3642145884, 978-3642145889

More Books

Students also viewed these Databases questions