简体   繁体   中英

I need help linking my stack Class into my TestBed class

import java.util.Random;

public class TestBed {

public static void main(String a[]) {
    // creating the array
    int[] array = new int[100];
    Random random = new Random();
    // changing the variable of my clone array for reference
    int[] arr = cloneArray(array);

    for (int i1 = 0; i1 < 100; i1++)
        array[i1] = random.nextInt(100) + 1;
    // print out of bubble sort before and after the sort
    System.out
            .println("***********************Bubble Sort           ****************************");
    arr = cloneArray(array);
    System.out.println("Values Before the sort:\n");
    printArray(arr);
    System.out.println();
    bubble_srt(arr);
    System.out.print("Values after the sort:\n");
    printArray(arr);
    // print out of selection sort before and after the sort
    System.out.println();
    System.out
            .println("********************Selection Sort*****************************");
    System.out.println(" Selection Sort\n\n");
    arr = cloneArray(array);
    System.out.println("Values Before the sort:\n");
    printArray(arr);
    System.out.println();
    selection_srt(arr);
    System.out.print("Values after the sort:\n");
    printArray(arr);

    System.out.println();

    Stack stack = new Stack();



}




public static int[] buildArray(int bound) {
    return null;
}

// clone array as a data type
public static int[] cloneArray(int[] data) {
    return (int[]) data.clone();
}

// print array as a data type
public static void printArray(int[] data) {

    // while there are still numbers left in the array print out the next
    // value

    for (int i = 0; i < data.length; i++)
        System.out.print(data[i] + " ");

}

// bubble syntax
public static void bubble_srt(int a[]) {
    int t = 0;
    for (int i = 0; i < a.length; i++) {
        for (int j = 1; j < (a.length - i); j++) {
            if (a[j - 1] > a[j]) {
                t = a[j - 1];
                a[j - 1] = a[j];
                a[j] = t;
            }
        }
    }
}

// selection syntax
public static void selection_srt(int array[]) {
    for (int x = 0; x < array.length; x++) {
        int index_of_min = x;
        for (int y = x; y < array.length; y++) {
            if (array[index_of_min] > array[y]) {
                index_of_min = y;
            }
        }
        int temp = array[x];
        array[x] = array[index_of_min];
        array[index_of_min] = temp;
    }
}


}

From here i have to link my stack class but i don't know how to get the 2 classes together correctly im new to programming and my school threw me into a java class that was to high for me and now im stuck 5 weeks in.

public class Stack {
Node top;
int size;

public Stack() {
    top = null;
    size = 0;
}

public int pop() {
    if (top != null) {
        int item = top.data;
        top = top.next;
        size--;
        return item;
    }
    return -1;
}

public void push(int data) {
    Node t = new Node(data);
    t.next = this.top;
    this.top = t;
    size++;
}

public boolean isEmpty() {
    return size <= 0;
}

public int getSize() {
    return size;
}

public int peek() {
    return top.data;
}

public void printStack() {
    Node n = this.top;
    int pos = this.getSize();
    while (pos > 0) {
        System.out.println("Position: " + pos + " Element: " + n.data);
        if (pos > 0) {
            n = n.next;
        }
        pos--;

    }
}
}

class Node {
public int data;
public Node next;

Node(int d) {
    data = d;
    next = null;
}

public int getData() {
    return data;`enter code here`
}`enter code here`

{
    Stack s = new Stack();
    s.push(9);
    s.push(2);
    s.push(7);
    s.push(3);
    s.push(6);
    s.push(4);
    s.push(5);
    System.out.println("Size is: " + s.getSize());
    // s.printStack();
    int size = s.getSize();
    for (int i = 0; i < size; i++) {
        System.out.print(s.pop() + " ");
    }

}
}

At the beginning of Stack class file, create a package name, eg:

package com.example.testcode;

Then in your TestBed class file, import your Stack class from the package, eg:

import com.example.testcode.Stack;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM