从类中的其他文件调用类

时间:2018-03-09 20:28:36

标签: java

我坚持我的程序的最后部分,涉及比较两个单独的排序算法。

我无法在我的主要内容中调用我的其他两个课程,我不完全确定原因。

import java.util.Random;
 import java.util.Arrays;
 import java.util.Scanner;


 // Implement two separate sorting classes
 // Compare both classes and see how they stack up
 // based off their results.

 //QuickSort + BubbleSort
public class a8main{
public static void main(String[] args)
{
Scanner userInput = new Scanner(System.in);

System.out.println("Type in array size.");
System.out.println("5,000 10,000 30,000");

int input = userInput.nextInt();

//Test smaller arrays starting at 5,000
//Test larger arrays. (30,000 or more)

byte[] c = new byte[input];
new Random().nextBytes(c);

//Begin QuickSorting + BubbleSorting
for (int j=1;j<6;j++)
{
    byte[] c1 = Arrays.copyOfRange(c, 0, 5000*j);
    long startTime = System.currentTimeMillis();
    quickSort(c1,0,5000*j-1);
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;

    byte[] c2 = Arrays.copyOfRange(c, 0, 5000*j);
    startTime = System.currentTimeMillis();
    bubbleSort(c2);
    long endTime = System.currentTimeMillis();
    long totalTime = endTime - startTime;

    System.out.println("Amount of time taken for first array "+5000*j+" elements for quickSort: "+totalTime+" miliseconds.");


    System.out.println("Amount of time taken for second array "+5000*j+" elements for bubbleSort: "+totalTime+" miliseconds.");
}

} }

我在这里有主要代码,我将包括我的其他两个课程,只是为了给你一个关于我在这里处理的内容的线索。

public class bubbleSort
 {
     static void bubbleSorter(byte[] args)
 {
  int n = args.length;
  byte temp = 0;
  for(int i=0; i < n;i++)
  {
       for(int j=1; j < (n-i); j++)
     {
          if(args[j-1] > args[j])
         {
             temp = args[j-1];
             args[j-1] = args[j];
             args[j] = temp;
        }
    }
}

} }

冒泡排序

      public class quickSort
  {
      static void quickSorter(byte[]args,int low,int high)
   {
    if (args == null || args.length == 0)
        return;

    if (low >= high)
        return;

    int center = low + (high - low) / 2;
    byte pivot = args[center];

    int i = low, j = high;
    while (i <= j)
    {
        while (args[i] < pivot)
        {
            i++;
        }
        while (args[j] > pivot)
        {
            j--;
        }


    }

    if (low < j)
        quickSorter(args, low, j);

    if (high > i)
        quickSorter(args, i, high);
}

}

和QuickSort

我做错了什么? 我只需要一些线索。

2 个答案:

答案 0 :(得分:0)

您需要按类名称调用方法,因为您将它们定义为静态方法,如此

quickSort.quickSorter(parameter1,parameter2,parameter3);
bubbleSort.bubbleSorter(parameter);

答案 1 :(得分:0)

你有一些问题。

首先,Java建议使用PascalCase作为类名;例如,BubbleSort。方法名称是像bubbleSorter这样的驼峰案例。这种方式更容易阅读。

其次,您在endTime totalTime方法中重新声明了Mainmain()。在long方法调用后,取出bubbleSorter()前的public class BubbleSort { private BubbleSort() { //This is a static class - no instantiation needed } public static void bubbleSorter(byte[] args) { int n = args.length; byte temp = 0; for (int i = 0; i < n; i++) { for (int j = 1; j < (n - i); j++) { if (args[j - 1] > args[j]) { temp = args[j - 1]; args[j - 1] = args[j]; args[j] = temp; } } } } }

第三,你应该强制你的静态类有一个私有的构造函数。如果有人试图初始化私有构造函数,有些人会抛出异常。这只能在课堂上完成,所以我选择只写一个评论,因为人们可以删除异常。

第四,调用类的静态方法时,需要包含静态方法的类。当你在这个答案的第一点时,这将更容易阅读。

这是你的课程的样子。

冒泡排序类:

public class QuickSort
{
    private QuickSort() {
        //This is a static class - no instantiation needed
    }

    public static void quickSorter(byte[]args,int low,int high)
    {
        if (args == null || args.length == 0)
            return;

        if (low >= high)
            return;

        int center = low + (high - low) / 2;
        byte pivot = args[center];

        int i = low, j = high;
        while (i <= j)
        {
            while (args[i] < pivot)
            {
                i++;
            }
            while (args[j] > pivot)
            {
                j--;
            }


        }

        if (low < j)
            quickSorter(args, low, j);

        if (high > i)
            quickSorter(args, i, high);
    }
}

快速排序类:

public class Main {

    public static void main(String[] args) {
        Scanner userInput = new Scanner(System.in);

        System.out.println("Type in array size.");
        System.out.println("5,000 10,000 30,000");

        int input = userInput.nextInt();

//Test smaller arrays starting at 5,000
//Test larger arrays. (30,000 or more)

        byte[] c = new byte[input];
        new Random().nextBytes(c);

//Begin QuickSorting + BubbleSorting
        for (int j=1;j<6;j++)
        {
            byte[] c1 = Arrays.copyOfRange(c, 0, 5000*j);
            long startTime = System.currentTimeMillis();
            QuickSort.quickSorter(c1,0,5000*j-1);
            long endTime = System.currentTimeMillis();
            long totalTime = endTime - startTime;

            byte[] c2 = Arrays.copyOfRange(c, 0, 5000*j);
            startTime = System.currentTimeMillis();
            BubbleSort.bubbleSorter(c2);
            endTime = System.currentTimeMillis();
            totalTime = endTime - startTime;

            System.out.println("Amount of time taken for first array "+5000*j+" elements for quickSort: "+totalTime+" miliseconds.");


            System.out.println("Amount of time taken for second array "+5000*j+" elements for bubbleSort: "+totalTime+" miliseconds.");
        }
    }
}

主要类别:

function dateDiff(startingDate, endingDate) {
    var startDate = new Date(new Date(startingDate).toISOString().substr(0, 10));
    if (!endingDate) {
        endingDate = new Date().toISOString().substr(0, 10);    // need date in YYYY-MM-DD format
    }
    var endDate = new Date(endingDate);
    if (startDate > endDate) {
        var swap = startDate;
        startDate = endDate;
        endDate = swap;
    }
    var startYear = startDate.getFullYear();
    var february = (startYear % 4 === 0 && startYear % 100 !== 0) || startYear % 400 === 0 ? 29 : 28;
    var daysInMonth = [31, february, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    var yearDiff = endDate.getFullYear() - startYear;
    var monthDiff = endDate.getMonth() - startDate.getMonth();
    if (monthDiff < 0) {
        yearDiff--;
        monthDiff += 12;
    }
    var dayDiff = endDate.getDate() - startDate.getDate();
    if (dayDiff < 0) {
        if (monthDiff > 0) {
            monthDiff--;
        } else {
            yearDiff--;
            monthDiff = 11;
        }
        dayDiff += daysInMonth[startDate.getMonth()];
    }

    return yearDiff + 'Y ' + monthDiff + 'M ' + dayDiff + 'D';
}
相关问题