Question

Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

Submit Chapter7.java with four (4) public static methods as follows:

A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int.

B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1% of each other to be equal. So 2.34 and 2.35 are not equal, but 22222.34 and 22222.35 ARE considered equal.

C. Write a method called vowelCount that accepts a String as its only parameter, and returns an array int[5] containing the count of vowels a,e,i,o,u in that String, using ignoreCase. For example, vowelCount("") returns {0,0,0,0,0}, and for the call vowelCount("Bill Iverson") your method returns {0,1,2,1,0} because there is one 'e' and two 'i' vowels (ignore case) and one 'o' with zero counts for 'a' and 'u' vowels.

D. Write a method called isSquare that accepts a two-dimensional array of integers as a parameter and returns true if the number of columns in every row equals the number of rows in every column. For example, isSquare for {[2,7,6], [9,5,1], [4,3,8]} returns true because there are 3 columns and 3 rows. But isSquare {[2,7], [9,5], [4,3]} will return false. Your method must work for any dimension.

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

Problem#A:

public static int mostCommon(int []array)
    {HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
    int max=1,temp;
    for(int i=0;i<array.length;i++)
        {
            if(hm.get(array[i])!=null)
            {int count=hm.get(array[i]);
            count=count+1;
            hm.put(array[i],count);
            if(count>max)
                {max=count;
                 temp=array[i];}
            }
            else
            {hm.put(array[i],1);}
        }
        return temp;
    }

Problem#B:

static int countOccurrences(double[] list, double targetValue) {
    int count = 0;
    for (int i = 0; i < list.length; i++) {
        if (list[i] == targetValue)
            count++;
    }
}


static double mostCommon(double[] list) {
    int mostFrequentCount = 0;
    double mostFrequentValue = 0;
    for (int i = 0; i < list.length; i++) {
        double value = list[i];
        int count = countOccurrences(list, value);
        if (count > mostFrequentCount) {
            mostFrequentCount = count;
            mostFrequentValue = value;
        }
    }
    return mostFrequentValue;
}

Problem#C:

public static int VowelCount(String word) {
int count[5],i;
for (i=0;i<5;i++)
    count[i]=0;
if (word != null) {
    word = word.trim().toLowerCase();
}
if (word == null || word.length() < 1) {
    return count;
}
for (int index = 0; index < word.length(); index++) {
  
    char fred = word.charAt(index);
    if (fred == 'a')
      ++count[0];
    if (fred == 'e')
      ++count[1];
    if (fred == 'i')
      ++count[2];
    if (fred == 'o')
      ++count[3];
    if (fred == 'u')
      ++count[4];
}
System.out.println("For the word \"" + word
      + "\" there are {");
   for (i=0;i<5;i++)
       System.out.println(count[i] + ", ");
    System.out.println("} vowels");
}

Problem#D:

public boolean isSquare(int[][] dd) {
    int r = dd.length;
    int sum = 0;
    for (int i=0; i<r ; i++) {
   sum += dd[0][i];
   if (dd[i].length != r) {
        return false;
   }
    }

    int sum2 = 0;
    // column sum
    for (int i=0; i<r;i++) {
   for (int j=0; j<r; j++) {
        sum2 += dd[i][j];
   }
   if (sum2 != sum)
        return false;
   sum2 = 0;
    }
    // row sum
    for (int j=0; j<r; j++) {
   for (int i=0; i<r; i++) {      
        sum2 += dd[i][j];
   }
   if (sum2 != sum)
        return false;
   sum2 = 0;
    }
    // diagram
    int sum3 = 0;
    for (int i=0; i<r; i++) {
   sum2 += dd[i][i];
   sum3 += dd[i][r-i-1];
    }
    if (sum2 != sum)
   return false;
    if (sum3 != sum)
   return false;
  
    return true;
}

Add a comment
Know the answer?
Add Answer to:
Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...
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
  • By using Java public static methods solve following problem Write a method called vowelCount that accepts...

    By using Java public static methods solve following problem Write a method called vowelCount that accepts a String as its only parameter, and returns an array int[5] containing the count of vowels a,e,i,o,u in that String, using ignoreCase. For example, vowelCount("") returns {0,0,0,0,0}, and for the callvowelCount("Bill Iverson") your method returns {0,1,2,1,0} because there is one 'e' and two 'i' vowels (ignore case) and one 'o' with zero counts for 'a' and 'u' vowels.

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • 1. Write a static method named mode that takes an array of integers as a parameter...

    1. Write a static method named mode that takes an array of integers as a parameter and that returns the value that occurs most frequently in the array. Assume that the integers in the array appear in sorted order. For example, if a variable called list stores the following values: ist -3, 1, 4, 4, 4, 6, 7,8, 8, 8, 8, 9, 11, 11, 11, 12, 14, int 141i Then the call of mode (li array, appearing four times. st,...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  • Preferred in Java Write a method called print Array With Total that accepts a reference to...

    Preferred in Java Write a method called print Array With Total that accepts a reference to a two - dimensional array of integers and two primitive integers called number Of Rows and number OF Columns as parameters and displays the elements of two - dimensional array as a table with an extra column for the row total as the rightmost column and an extra row for the column totals as the bottom row. (Please do NOT call either definition of...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • java /* Q2 (10 pts): Write a method called method that accepts an integer parameter *...

    java /* Q2 (10 pts): Write a method called method that accepts an integer parameter * * * * and returns a sum of the first n terms of the sequence. * In other words, the method should generate the following sequence: 1 + 1/2 + 1/3 + 1/4 + ... 1/n * For example, method2(2) will return 1.5 since 1+1/2 = 1.5 * method2 (15) will return 3.3182289932289937 * You may assume that the parameter n is nonnegative. */...

  • Write a Java program that has a method called arrayAverage that accepts an arrary of numbers...

    Write a Java program that has a method called arrayAverage that accepts an arrary of numbers as an argument and returns the average of the numbers in that array. Create an array to test the code with and call the method from main to print the average to the screen. The array size will be from a user's input (use Scanner). - array size = int - avergage, temperature = double - only method is arrayAverage Output:

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
ADVERTISEMENT