QuestionAsk Questions?

Write a program that reads numbers fromthe keyboard into an array of type int[].

You may assume that there will be 50 or fewer entries in the array .Your program allows any numbers of numbers to be entered , up to 50 numbers. The output is to be a two-column list. The first column is a list of the distinct array elements ; the second column is the count of the number of occurrences of each element. The list should be sorted on entries int the first column, largest to smallest.

for the array:

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output shoulb be:

N Count

4 2

3 3

2 2

1 4

-1 1

-12 4

>>

I need your help about this...

You may assume that there will be 50 or fewer entries in the array .Your program allows any numbers of numbers to be entered , up to 50 numbers. The output is to be a two-column list. The first column is a list of the distinct array elements ; the second column is the count of the number of occurrences of each element. The list should be sorted on entries int the first column, largest to smallest.

for the array:

-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12

the output shoulb be:

N Count

4 2

3 3

2 2

1 4

-1 1

-12 4

>>

I need your help about this...

View Answers

February 16, 2010 at 9:02 AM

Hi Medinz,

Here is my version of code. Hope it helps you.

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Scanner;

public class IntArray {

/**

* @variable hash : holds the non duplicate numbers as KEY and its count of

* duplicates as VALUE.

*

* @variable final_array : holds the sorted nun duplicate numbers to be

* printed

**/

Hashtable hash = new Hashtable();

int[] final_array;

public static void main(String[] args) {

System.out.println("How many numbers you wanna enter? : ");

Scanner s = new Scanner(System.in);

int n = s.nextInt();

int[] a = new int[n];

IntArray obj = new IntArray();

System.out.println("Enter " + n + " numbers: ");

Scanner scan = new Scanner(System.in);

for (int i = 0; i < a.length; i++) {

int num = scan.nextInt(); // Get each of the integer given as input and insert into an array

a[i] = num;

}

obj.countDuplicates(a);

obj.construtFinalArray();

obj.sort(obj.final_array);

obj.print(obj.final_array);

}

/** Prints the sorted array(given) and its duplicate count. **/

public void print(int[] arr) {

System.out.println("\nSorted numbers and its no.of occurances are: ");

System.out.println("\nNumber Count");

System.out.println("------ -----");

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i] + " " + hash.get(arr[i]));

}

}

/** Sorts the given array into Descending order. **/

public int[] sort(int[] a) {

int temp = 0;

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

if (a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

return a;

}

/**

* Counts the duplicate numbers present in the given values and places in an

* HashTable where number would as the KEY and the count of its duplicates

* as the VALUE

**/

public void countDuplicates(int[] a) {

for (int i = 0; i < a.length; i++) {

int temp = 1;

if (!(hash.containsKey(a[i]))) // If hash doesn't contains the number insert it as the key.

hash.put(a[i], temp);

else { // If hash does has the number(as KEY),increment its count(VALUE of hash) to 1.

temp = Integer.parseInt(hash.get(a[i]).toString());

hash.put(a[i], temp + 1);

}

}

}

/**

* constructs 'final_array' i.e sorted array without duplicates used to

* print

**/

public int[] construtFinalArray() {

final_array = new int[hash.size()]; // Set the final_array size equal to HashTable hash as it has non duplicated numbers

Enumeration e = hash.keys();

int temp = 0;

int i = 0;

while (e.hasMoreElements()) {

String tempString = e.nextElement().toString(); // Take the key of hash because it contains the non duplicated numbers.

temp = Integer.parseInt(tempString); // As key is of type Object and the value to be inserted in final_array to be an int value,change key to string and then convert to int by using Integer.parseInt().

final_array[i++] = temp;

}

return final_array;

}

}

Regards,

javaquest2010

Here is my version of code. Hope it helps you.

import java.util.Enumeration;

import java.util.Hashtable;

import java.util.Scanner;

public class IntArray {

/**

* @variable hash : holds the non duplicate numbers as KEY and its count of

* duplicates as VALUE.

*

* @variable final_array : holds the sorted nun duplicate numbers to be

* printed

**/

Hashtable hash = new Hashtable();

int[] final_array;

public static void main(String[] args) {

System.out.println("How many numbers you wanna enter? : ");

Scanner s = new Scanner(System.in);

int n = s.nextInt();

int[] a = new int[n];

IntArray obj = new IntArray();

System.out.println("Enter " + n + " numbers: ");

Scanner scan = new Scanner(System.in);

for (int i = 0; i < a.length; i++) {

int num = scan.nextInt(); // Get each of the integer given as input and insert into an array

a[i] = num;

}

obj.countDuplicates(a);

obj.construtFinalArray();

obj.sort(obj.final_array);

obj.print(obj.final_array);

}

/** Prints the sorted array(given) and its duplicate count. **/

public void print(int[] arr) {

System.out.println("\nSorted numbers and its no.of occurances are: ");

System.out.println("\nNumber Count");

System.out.println("------ -----");

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i] + " " + hash.get(arr[i]));

}

}

/** Sorts the given array into Descending order. **/

public int[] sort(int[] a) {

int temp = 0;

for (int i = 0; i < a.length; i++) {

for (int j = 0; j < a.length; j++) {

if (a[i] > a[j]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}

}

}

return a;

}

/**

* Counts the duplicate numbers present in the given values and places in an

* HashTable where number would as the KEY and the count of its duplicates

* as the VALUE

**/

public void countDuplicates(int[] a) {

for (int i = 0; i < a.length; i++) {

int temp = 1;

if (!(hash.containsKey(a[i]))) // If hash doesn't contains the number insert it as the key.

hash.put(a[i], temp);

else { // If hash does has the number(as KEY),increment its count(VALUE of hash) to 1.

temp = Integer.parseInt(hash.get(a[i]).toString());

hash.put(a[i], temp + 1);

}

}

}

/**

* constructs 'final_array' i.e sorted array without duplicates used to

**/

public int[] construtFinalArray() {

final_array = new int[hash.size()]; // Set the final_array size equal to HashTable hash as it has non duplicated numbers

Enumeration e = hash.keys();

int temp = 0;

int i = 0;

while (e.hasMoreElements()) {

String tempString = e.nextElement().toString(); // Take the key of hash because it contains the non duplicated numbers.

temp = Integer.parseInt(tempString); // As key is of type Object and the value to be inserted in final_array to be an int value,change key to string and then convert to int by using Integer.parseInt().

final_array[i++] = temp;

}

return final_array;

}

}

Regards,

javaquest2010

Related Tutorials/Questions & Answers:

Advertisements