Question

Lab Description: Given a provided array, determine how many groups of a specified size exist. For the array [i,1,1,2,2,2,3,3,import static java.lang.System.* import java.util.Arrays; import java.util.Scanner; public class ArrayStats /linstance variabpublic class ArraystatsRunner public static void main(String args[])

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*; //importing util package to use Scanner class for inputs
class ArrayStats
{
int a[],size; //global variables. a stores the array, size stores the capacity of the array
ArrayStats() //constructor
{
size=0;
}
public void setArray() //setter function, uses Scanner class to input size and elements of the array
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the array : ");
size=sc.nextInt();
a=new int[size];
System.out.print("Enter the elements of the array separated by a space : ");
for(int i=0;i<size;i++)
a[i]=sc.nextInt();
}
public String toString() //function to convert objects of this class to a string, enabling them to be printed
{
String s="";
for(int i=0;i<size;i++)
s=s+a[i]+" ";
return s;
}
public int getNumGroupsOfSize(int size) //function to find number of groups of given size in the array
{
int cnt=0;
int i=0;
while(i<this.size) // we use this.size to refer to the global variable
{
int t=1;
while(i+1<this.size && a[i+1]==a[i]) //we use this loop to find every group and their size
{
i++;
t++;
}
if(t>=size) //if size of group more than size, they are counted
cnt++;
i++;
}
return cnt;
}
}
import java.util.*;
class ArrayStatsRunner //class to run ArrayStats
{
public static void main(String args[])
{
ArrayStats a=new ArrayStats(); //creating object of ArrayStats
a.setArray(); //calling function to input array
for(int i=1;i<=a.size;i++) //loop to find the groups
{
int t=a.getNumGroupsOfSize(i);
if(t>0)
System.out.println("Size "+i+" count == "+t);
}
}
}

Screenshot of output:

Add a comment
Know the answer?
Add Answer to:
Lab Description: Given a provided array, determine how many groups of a specified size exist. For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Only part 1, makinf the 2nd and 3rd picture of code do the same thing using...

    Only part 1, makinf the 2nd and 3rd picture of code do the same thing using array Purpose This homework is to learn the concept and usage of one-dimensional (ID) arrays. Part 1 (30 pts) Modify your HW6' (i.e., digit frequency histogram) in a new program (DigitFrequencyArray.java) to use a count array instead of 10 count variables. Your new program should be much shorter than your original code since there is no need for branching statements (if or switch). For...

  • 1.The following statement gets an element from position 4 in an array named myArray: x =...

    1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...

  • You must use an array to hold the items in the queue. In other words, one...

    You must use an array to hold the items in the queue. In other words, one of your fields should be an array for holding the items, but you may not have any other container fields (no other arrays, lists, stacks, queues, etc.) You will need at least one additional field (an int). package hw5; public class GeneralizedQueue {    /** * Creates an empty queue. */ public GeneralizedQueue() { // TODO }    /** * Checks if the queue...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

  • DESCRIPTION: You will be given a 2-D string array, called matrix. The array has an unknown...

    DESCRIPTION: You will be given a 2-D string array, called matrix. The array has an unknown number of cells filled with data. Your goal is to iterate through the 2-D array and keep a count of how many cells contain the string "Angela". INPUT: All input has been handled for you: A filled 2-D string array. PROCESSING: Determine the number of rows in the array matrix Determine the number of columns in the array matrix Use nested loops to iterate...

  • PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores...

    PROGRAM DESCRIPTION Using the given class definitions for either C++, create a minimum heap that stores integers and and implements a minimum priority queue. (Your program can be "hard coded" for integers - it does not need to use templates, generics, or polymorphism.) Your data structure must always store its internal data as a heap. Your toString function should return a string with the heap values as a comma separated list, also including the size of the heap as well....

  • Given the contents of a sorted array of size 6, shown below, show which indices of...

    Given the contents of a sorted array of size 6, shown below, show which indices of the array will be visited, and in what order, if the algorithm BinarySearch was to be performed on this array for the number 4. int theArray[] = {1, 2, 3, 6, 8, 9}; Following are example question & answer: Given the contents of a sorted array of size 7, shown below, show which indices of the array will be visited, and in what order,...

  • A function that takes three parameters: an int array, an int n that is the size...

    A function that takes three parameters: an int array, an int n that is the size of the array and an int pointer max that is to assign the maximum value in the array. The function will find and return the index of the first occurrence of the maximum value in the array and assign the maximum value to the pointer. For example, in the array {1, 2, 6, 5, 6, 4, 3, 6, 4}, the maximum value is 6...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • /** Given an int array, return an int array with duplicate ints removed if the array...

    /** Given an int array, return an int array with duplicate ints removed if the array contains duplicate values. <br> <br> removeDuplicateInts({3}) -> {3} <br> removeDuplicateInts({1, 2}) -> {1, 2} <br> removeDuplicateInts({7, 7}) -> {7} <br> removeDuplicateInts({1, 7, 1, 7, 1}) -> {1, 7} <br> removeDuplicateInts({1, 2, 3, 4, 5}) -> {1, 2, 3, 4, 5}) <br> removeDuplicateInts({1, 2, 3, 2, 4, 2, 5, 2}) -> {1, 2, 3, 4, 5} <br> @param numbers int[] an array of integers. @return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT