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

java/c++ which ever one helps. 1.Write only a recursive method that will count t

ID: 3808120 • Letter: J

Question

java/c++ which ever one helps.

1.Write only a recursive method that will count the number of times a character occurs in a given string.

The method header should be as follows:

public static int count(String str, char a)

2.

The file attached contains all 50 state prefixes in a text file. The states are list in no particular order. You are to write a method that will open the text file (you should have appropriate exception handling), read the data into an array and then sort the data appropriately.

Explanation / Answer

1) CharacterCounting.java:
_______________________

import java.util.Scanner;


public class CharacterCounting {

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("Enter String:");
       String str = sc.next();
       System.out.println("Enter Character:");
       char ch = sc.next().charAt(0);
       int result = count(str,ch);
       System.out.println(ch+" has occurred "+result+" times in "+str);
   }
   public static int count(String str,char a){
      
       if(str.length()==0) {
       return 0;
       }
       String character = Character.toString(a);
       if ((str.substring(0,1).equals(character))) {
       return 1 + count(str.substring(1),a);
       }

       return count(str.substring(1),a);
   }
}


/* Sample Input and Output */

Enter String:
lakshman
Enter Character:
a
a has occurred 2 times in lakshman

_____________________________________________________________

2) SortingStates.java :
___________________

import java.io.BufferedReader;
import java.io.FileReader;


public class SortingStates {
   public static void main(String[] args) {
       sort_states();
   }
   public static void sort_states(){
       String str[] = new String[50];
       try{
           FileReader fr = new FileReader("F:\Workspace\Chegg\states.txt");
           BufferedReader br = new BufferedReader(fr);
           String line = null;
           int k = 0;
           while((line = br.readLine())!=null){
               String a[] = line.split(" ");
               for(int i=0;i<a.length;i++)
                   str[k++] = a[i];
           }
           for(int i=0;i<str.length;i++){
               for(int j=i+1;j<str.length;j++){
                   if(str[i].compareTo(str[j])>0){
                       String temp = str[i];
                       str[i] = str[j];
                       str[j] = temp;
                   }
               }
           }
           System.out.println("State Prefixes in sorted order:");
           for(int i=0;i<str.length;i++){
               System.out.println(str[i]);
           }
       }
       catch(Exception e){
           e.printStackTrace();
       }
   }
}


states.txt :
_________


FL
GA
SC
NC
VA
MD
NY
NJ
DE
PA
CT
RI
MA
VT
NH
ME
AL
TN
KY
WV
OH
MI
MS
AR
MO
KS
NE
IN
IL
WI
MN
LA
TX
OK
IA
SD
ND
NM
CO
WY
ID
AZ
UT
NV
MT
CA
OR
WA
AL
HI

Sample Input and Output:
________________________

State Prefixes in sorted order:
AL
AL
AR
AZ
CA
CO
CT
DE
FL
GA
HI
IA
ID
IL
IN
KS
KY
LA
MA
MD
ME
MI
MN
MO
MS
MT
NC
ND
NE
NH
NJ
NM
NV
NY
OH
OK
OR
PA
RI
SC
SD
TN
TX
UT
VA
VT
WA
WI
WV
WY