Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java code please! Not an example, please post a correct solution (commented if p

ID: 3689191 • Letter: J

Question

Java code please! Not an example, please post a correct solution (commented if possible) Thank you!

Write a program that heap sorts an arbitrary number of words or sentences from the least number of F’s (ignoring case) in the word to the most number of F’s in the word. Write a frontend in that will take in each word or phrase, and then sort and prints them.

Example Dialog:

Welcome to the F word sorter!

Enter words or phrases and it will sort from least number of F’s to the most number of F’s. Enter “Sort” to finish and sort.

FUN

Fluffy

Fife Five Dollar Foot Long For Five Dollars

FfffFFffuuu! Farmer Fred Fiddles for Food

After Friday fear the final. Be afraid friend.

Sort

The sorted phrases are

FUN

Fife

Fluffy

Five Dollar Foot Long For Five Dollars

Farmer Fred Fiddles for Food

After Friday fear the final. Be afraid friend.

FfffFFffuuu!

Explanation / Answer

############ HepaSort.java ##################

/* Class HeapSort */
public class HeapSort
{  
    private static int N;
  
    /* function to count number of Fs */
    public static int countF(String s){
       int count = 0;
       for(int i=0; i<s.length(); i++)
           if(s.charAt(i) == 'F' || s.charAt(i) == 'f')
               count++;
       return count;
    }
    /* Sort Function */
    public static void sort(String arr[])
    {     
        heapify(arr);      
        for (int i = N; i > 0; i--)
        {
            swap(arr,0, i);
            N = N-1;
            maxheap(arr, 0);
        }
    }   
    /* Function to build a heap */
    public static void heapify(String arr[])
    {
        N = arr.length-1;
        for (int i = N/2; i >= 0; i--)
            maxheap(arr, i);      
    }
    /* Function to swap largest element in heap */      
    public static void maxheap(String arr[], int i)
    {
        int left = 2*i ;
        int right = 2*i + 1;
        int max = i;
        if (left <= N && countF(arr[left]) > countF(arr[i]))
            max = left;
        if (right <= N && countF(arr[right]) > countF(arr[max]))      
            max = right;

        if (max != i)
        {
            swap(arr, i, max);
            maxheap(arr, max);
        }
    }  
    /* Function to swap two numbers in an array */
    public static void swap(String arr[], int i, int j)
    {
        String tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }  
  
}

########## Test Class #################

import java.util.Scanner;

public class HeapSortTest {
  
   /* Main method */
    public static void main(String[] args)
    {
        Scanner scan = new Scanner( System.in );      
        System.out.println("Heap Sort Test ");
        int n, i;  
        /* Accept number of elements */
        System.out.println("Enter number of string elements");
        n = scan.nextInt();  
        /* Make array of n elements */
        String arr[] = new String[n];
        /* Accept elements */
        System.out.println(" Enter "+ n +" String elements");
        for (i = 0; i < n; i++)
            arr[i] = scan.nextLine();
        /* Call method sort */
        HeapSort.sort(arr);
        /* Print sorted Array */
        System.out.println(" Elements after sorting ");      
        for (i = 0; i < n; i++)
            System.out.println(arr[i]);          
        System.out.println();          
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote