I have an assignment that askes to w rite a program in Java that computes the nu
ID: 3759246 • Letter: I
Question
I have an assignment that askes to write a program in Java that computes the number of words in a text entered in a text field.
Use the following graphical objects:
a text field for the input text (one line of text)
labels to output the word count
a button that, when pressed, computes the word count
a button that, when pressed, clears the text field
a button that, when pressed, computes the longest word, shortest, and average word size
a button that, when pressed, prints the most frequently entered word
a panel, that includes all graphical objects listed above
Make your application run in a window (JFrame) and use a proper layout, backgrounds and colors to make all objects clearly visible.
Explanation / Answer
'Example 1: 'A program that adds two numbers without the use of subprograms. Option Explicit on Private Sub cmdAdd_Click() dim num1 As Double dim num2 As Double dim answer As Double num1 = 2 num2 = 3 answer = num1 + num2 'Display the sum of two numbers txtOutput.text = "This program displays a sentence" + vbnewline txtOutput.text = txtOutput.text + "identifying two numbers and their sum." + vbnewline txtOutput.text = txtOutput.text + "The sum of" + num1 +"and" + num2 + "is" + answer End Sub 'Example 2: A program that adds two numbers and calls a subprogram to 'display the header Option Explicit on Private Sub cmdAdd_Click() dim numl As Double dim num2 As Double dim answer As Double numl = 2 num2 = 3 answer = num1 + num2 Call ExplainPurpose txtOutput.text = txtOutput.text + "The sum of" + numl + "and" + num2 +"is" + answer End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program txtOutput.text = txtOutput.text + "This program displays a sentence" + vbnewline txtOutput.text = txtOutput.text + "identifying two numbers and their sum." End Sub 'Example 3: 'A program that calculates the sum of two numbers and calls 2 subprograms '1st sub displays header '2nd sub adds/displays the sum of 2 numbers by passing the values from the 'main program to the sub program 'AddDisplay Option Explicit on Private Sub cmdAdd_Click() dim numl As Double dim num2 As Double num1 = 2 num2 = 3 Call ExplainPurpose Call AddDisplay(num1, num2) End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program txtOutput.text = txtOutput.text +"This program displays a sentence" + vbnewline txtOutput.text = txtOutput.text +"identifying two numbers and their sum." + vbnewline End Sub Private Sub AddDisplay(byVal num1 As Double, byVal num2 As Double) 'Display numbers and their subs dim answer as Double answer = num1 + num2 txtOutput.text = txtOutput.text +"The sum of" + num1 + "and" + num2 + "is" + answer End Sub 'Example #4 'A program that calculates the sum of two numbers three times by including 'the numbers as arguments. Subprograms can be called an infinite number of 'times saving the programmer valuable time. Option Explicit on Private Sub cmdAdd_Click() 'Display the sum of two numbers with the Add subprogram called several ' times. Call ExplainPurpose Call AddDisplay(2, 3) Call AddDisplay(4, 6) Call AddDisplay(7, 8) End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program txtOutput.text = txtOutput.text +"This program displays a sentence" + vbnewline txtOutput.text = txtOutput.text +"identifying two numbers and their sum." +vbnewline End Sub Private Sub AddDisplay(num1 As Double, num2 As Double) 'Display numbers and their subs txtOutput.text = txtOutput.text + "The sum of+; num1+"and"+ num2+"is" + num1 + num2 End Sub 'Example #5 'Calculates the sum of two numbers calling 3 subprograms '1st sub explains purpose '2nd sub gets two numbers inputted by the user '3rd sub adds and displays the sum of two numbers. Option Explicit on Private Sub cmdAdd_Click() 'Calculates the sum of two numbers calling 3 subprograms dim num1 As Double dim num2 As Double Call ExplainPurpose Call GetData(num1, num2) Call AddDisplay(num1, num2) End Sub Private Sub ExplainPurpose() 'Explain the task performed by the program txtOutput.text = txtOutput.text +"This program displays a sentence" + vbnewline txtOutput.text = txtOutput.text +"identifying two numbers and their sum." + vbnewline End Sub Private Sub AddDisplay(byVal num1 As Double, byVal num2 As Double) 'Display numbers and their subs Dim answer As Double answer = num1 + num2 txtOutput.text = txtOutput.text +"The sum of"+ num1+"and"+ num2+"is"+ answer End Sub Private Sub GetData(byRef num1 As Double, byRef num2 As Double) 'Receives two numbers from the user. num1 = Convert.ToDouble(txtNum1.Text) num2 = Convert.ToDouble(txtNum2.Text) End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.