Question
Add countsort() method to the following xxxxxH1.java program that reads 4 integers n, a, b and k from anyinputfile, generates n random integers and stores
Add countsort() method to the following xxxxxH1.java program that reads 4 integers n, a, b and k from anyinputfile, generates n random integers and stores them in an arr[], prints arr[] formatted k numbers per line, before and after count sorting.
1 import java.io.*;
2 import java.util.*;
3 // xxxxxH1.java program reads 4 integers n, a, b
4 // and k from anyinputfile, generates n random
5 // integers and stores them in an arr[], prints
6 // arr[] formatted k numbers per line, before and
7 // after count sorting.
8 public class xxxxxH1{
9 // class variables
10 int freq[], arr[], a, b, n, k, range;
11
12 //use a short word for System.out to save typing
13 PrintStream prt = System.out;
14
15 // class constructor
16 xxxxxH1(){
17 prt.print("\tThis Java program reads 4 integers n, a, b and k" +
18 " \tfrom anyinputfile and stores them in an arr[] and \t" +
19 "Prints arr[] formatted k numbers per line," +
20 "before and after count sorting." +
21 " \t\tTo compile: javac xxxxxH1.java " +
22 " \t\tTo execute: java xxxxxH1 < anyinputfile");
23
24 try{
25 // open file inf
26 Scanner inf = new Scanner(System.in);
27
28 n = inf.nextInt(); //read n, no. of data
29 a = inf.nextInt(); //read a, lower range
30 b = inf.nextInt(); //read b, upper range
31 k = inf.nextInt(); //read k, how many per line
32 // close file inf
33 inf.close();
34 } catch (Exception e){
35 prt.printf(" Oops! Read Exception: %s" , e);
36 }
37 // Allocate space for array
38 range = b - a + 1;
39 arr = new int[n];
40 freq = new int[range];
41 //Its a good practice to print input in program output!
42 prt.printf(" \tINPUT: n = %3d, a = %4d, b = %4d, k = %4d", n, a, b, k);
43 } // end constructor
44
45
46 private void generate(){
47 //declare variables
48 int i;
49
50 //initialize rand
51 Random rand = new Random();
52
53 //for loop to generate n random no.
54 for (i = 0; i < n ; i ++)
55 arr[i] = rand.nextInt(range) + a;
56 } // end generate
57
58 //Print arr[] formatted, k elements per line
59 private void printarr(){
60 //declare variables
61 int j;
62 for (j = 0; j < n ; j ++){
63 prt.printf("%4d ", arr[j]);
64 if ((j+1) % k == 0)
65 prt.printf(" \t");
66 } // end for
67 } // end printarr
68
69 // main program
70 public static void main(String args[]) throws Exception{
71 //create an instance of a class
72 xxxxxH1 h = new xxxxxH1();
73
74 //call generate method
75 h.generate();
76
77 System.out.printf(" \t arr[] contents before count sorting: \t");
78 //call printarr method
79 h.printarr();
80
81 //call countsort method
82 g.countsort();
83
84 System.out.printf(" \t arr[] contents after count sorting: \t");
85 //call printarr method
86 h.printarr();
87 /// MAKE SURE TO WRITE YOUR NAME IN LINE 88
88 System.out.printf(" \tAuthor: Gh. Dastghaibyfard Date: " +
89 java.time.LocalDate.now());
90 } // end main
91 }// end class xxxxxH1
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