QuestionQuestion

Problem 1. (Line, Word, and Character Counts) Write a program WC that prints the number of lines, words, and characters in the input read from standard input. The program should take an optional command-line argument, which can be -l, -w, or -c. If the argument is specified, the program should print only the line, word, or character count, and if the argument is not specified, the program should print all the counts. If the argument is not valid (eg, -z), the program should print Unknown argument: -z. Hint: Use the trim(), split(), and length() methods from String.
$ java WC < lorem.txt
3 161 1092
$ java WC -w < lorem.txt 161
$ java WC -z < lorem.txt Unknown argument: -z

Problem 2. (Pascal’s Triangle) Pascal’s triangle is a triangular array of n+1 rows, with each row containing the coefficients for the expansion of (a + b)i for 0 ≤ i ≤ n. Here is Pascal’s triangle for n = 5.
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
The construction of the triangle proceeds as follows: In row 0, the topmost row, the entry is 1. Then, to construct the i elements in row i, add the number above and to the left with the number above of a given position to find the new value to place in that position. If either the number to the left or above is not present, substitute a 1 in its place.
Implement the static method pascal() in Pascal that takes an integer argument n and returns the values in rows zero to n of Pascal’s triangle as a lower-triangular matrix, ie, the elements above the main diagonal are all zeros.
$ java Pascal 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

public static void main(String[] args) {
      
       // get standard input
       Scanner stdin = new Scanner(System.in);
      
       // set all counter
       int c = 0;
       int l = 0;
       int w = 0;
       String line;
      
       // read each line
       while (stdin.hasNext()) {            
            line = stdin.nextLine();
            // count number of bytes in this line
            c += line.getBytes().length;
            // but off heading and tailing space
            line = line.trim();
            
            // increase number of line
            l++;...
$50.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats