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

- Write a class WordCounter with a main method. Give WordCounter a method public

ID: 3670836 • Letter: #

Question

- Write a class WordCounter with a main method. Give WordCounter a method

public static int countWords(String string)

which returns the number of words in the argument String, string. Define a word as any set of characters separated by spaces. For example, countWords(“I can’t get no satisfaction”) should return 5.

In the main method, prompt the user to input a line of text, scan the line from System.in using Scanner’s nextLine() method into a variable named input. Then print input at the command line, call countWords with the argument input, and then print out the number of words in input.

- Convert the code that computes the prime numbers into a method findPrimes. It should accept an integer n as an argument and return an array (or an ArrayList if you prefer) with all the primes from 2 to n.

Your main method should call findPrimes() and then print out all the prime numbers up to that integer. For example, when the user enters 30, the program should print something close to this format:

Primes from 1 to 30: 2 3 5 7 11 13 17 19 23 29

Explanation / Answer

Find the word count is very simple:

we have a method split in strings that and tokenizer also useful to find the word count.

hereis the example

import java.util.*;
class WordCounter
{
public static void main(String args[])
{

Scanner s=new Scanner(System.in);

public void wordCounter(){
String st=s.nextLine();


String words[]=st.trim().split(" ");

System.out.println("No. of words "+word.length);
}

}
}

-----------------------------------------

import java.util.Scanner;
class PrimeNum
{
   public static void main(String[] args)
   {
       int n,p;
       Scanner s=new Scanner(System.in);
       System.out.println(“Enter upto which number prime numbers are needed”);
       n=s.nextInt();
       for(int i=2;i<n;i++)
       {
           p=0;
           for(int j=2;j<i;j++)
           {
               if(i%j==0)
               p=1;
           }
           if(p==0)
               System.out.println(i);
       }
       ArrayList al = new ArrayList();
       al.add(p);
       System.out.println(al);
   }
}