Question
I am supposed to write 3 classes to sort a random array of x elements and time how long it takes to both generate and
I am supposed to write 3 classes to sort a random array of x elements and time how long it takes to both generate and sort the elements. While I am able to execute the program, I get a runtime error, as the output of the code returns a single memory address instead of outputting the randomized array pre- and post- sort. The sorting time is also off, as it should take a much shorter time to sort the array than is output to the console. Here is my code:
Timer.java public class Timer { private double startTime; private double endTime; Timer(){ } Timer(double startTime, double endTime){ this.startTime=startTime; this.endTime=endTime; } public double getStartTime() { return startTime; } public void start() { startTime=System.currentTimeMillis(); } public double getEndTime() { return endTime; } public void stop(){ endTime=System.currentTimeMillis(); } public double getElapsedTime() { return endTime-startTime; } }
Utility.java
import java.util.*; public class Utility { public void sortArray(int[] x){ Arrays.sort(x); } }
Test.java
import java.util.*; public class Test {
public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int arrElements=sc.nextInt(); Timer arrTimer=new Timer(); Utility arrSorter=new Utility(); arrTimer.start(); int[] sortArr=genArray(arrElements); arrTimer.stop(); System.out.println("Generated "+arrElements+" elements in "+arrTimer.getElapsedTime()+" seconds."); System.out.println("Array before sorting: ["+sortArr+"]"); arrTimer.start(); arrSorter.sortArray(sortArr); arrTimer.stop(); System.out.println("Sort array: "+arrTimer.getElapsedTime()+" seconds."); System.out.println("Array after sorting: ["+sortArr+"]"); } public static int[] genArray(int x) { Random r=new Random(); int[] randArr=new int[x]; for(int i=0; i } Here is the output that I receive with this code: (Tested with input value of 100) 100 Generated 100 elements in 1.0 seconds. Array before sorting: [[I@71be98f5] Sort array: 3.0 seconds. Array after sorting: [[I@71be98f5] Please help me fix this issue! Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started