Question

Question 2 (80 marks)

This programming assignment is divided into two parts:

(a) You will develop a Java class called StringSummary based on the UML class diagram depicted in Figure 1

String Summary -str: String - specialChars: int -digits: int - uppers: int -lowers: int +<<constructor>> Stringsummary (Strin

A user will enter a string of sentence (Figure 2), and your program will provide a

summary of the sentence as shown in sample output Figure 3.

х Sentence Input ? Enter a sentence to classify. This is aTest, 2020 is a year - 2 is the firs Cancel OK Figure 2: Sample inpString Information i Summary: Read: this is a Test, 2020 is a year - 2 is the first digit; the last is zero! String length =

The class contains several private attributes:

• str stores the sentence input by user.

• specialChars stores number of special characters (i.e. non-digit and

non-letters: ;-!@ etc) in the input string.

• digits stores the number of digits (e.g. 0,1,2 ,etc.) in the input string.

• uppers stores the number of upper-case letters (e.g. A, B, C, etc.) in the

input string.

• lowers stores the number of lower-case letters (e.g. a, b, c etc.) in the input

string.

The constructor receives the input string from user. The other methods included:

• summarize accumulates all the information about the input sentence, e.g.

the number of digits, uppercase letters, lowercase letters, and special

characters. Hints: the method relies on other methods to get the needed

information.

• countDigits returns the number of digits in a string. It returns 0 if none.

• countUpper returns the number of upper-case letter in a string. It returns 0

if none.

• countLower returns the number of lower-case in a string. It returns 0 if none.

• countSpecialChars returns the number of special characters in a string. It

returns 0 if none.

• toString overrides the default method to return the formatted output (see

Figure 3).

• getStr an accessor (getter) method, which returns value of the variable str.

• setStr a mutator (setter) method used to control changes to variable str.

Guidelines:

• Implement the class StringSummary.

• There is no single solution to the implementation of each method. Be

creative and efficient.

• After completing the implementations, test your program using the driver

class, StringSummaryDriver (Appendix A). Update your program until

satisfy.

b) The second part of the programming assignment extends from (a). make a new

class called StringSummaryExtended. The class inherits the common properties

from StringSummary. It also included a new method known as mergeStrings

String Summary StringSummary Extended +<<constructor>> String Summary Extended (String: str) +mergeStrings (s:String) Figure

A user will enter two strings of sentences in sequence, one after another

(Figure 5). Then, your program will use the mergeStrings method to merge

/ concatenate both sentences. Finally, it displays a summary of the merged

sentence as shown in sample output Figure 6.

Sentence Input 1 х ? Enter a sentence. a year - 2 is the first digit; the last is zero! OK Cancel After entering the first se

Guidelines:

• Implement the class StringSummaryExtended.

• There is no single solution to the implementation of the method. Be creative

and efficient.

• After completing the implementations, test your program using the driver

class, StringSummaryExtendedDriver (Appendix B). Update your program

until satisfy.

Appendix A Listing 1: Driver class for Question 2a import javax.swing.JOptionPane; public class String SummaryDriver { publ

Appendix B Listing 2: Driver class for Question 26 import javax.swing.JOptionPane; public class String SummaryExtendedDrive

1 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

I have written the Java Program based on your requirements.

The below code works perfectly and it has no-error.

Just SImple compile & run the below code.

I have also attached the Output screenshot that I got by running the below program.

Output:

C:\Users\Public>javac StringSummaryExtendedDriver.java C:\Users\Public>java StringSummaryExtendedDriver

Sentence Input 1 ? Enter a sentence: Hi Welcome ОК Cancel

х Sentence Input 2 ? Enter a second sentence to be merged. Mr.John !!! OK Cancel

x String Information i Summary: Read: Hi Welcome Mr.John !!! String length = 21 Number of digits = 0 Number of uppercase char

***********************************************************************************************************************************

Code:

import java.io.*;
import java.util.*;

import javax.swing.JOptionPane;

class StringSummary
{
   private String str;
   private int specialChars;
   private int digits;
   private int uppers;
   private int lowers;
  
   public StringSummary(String str)
   {
       this.str=str;
   }
  
   public int countDigits(String s)
   {
       int count=0;
      
       for(int i=0;i<str.length();i++)
       {
           if(Character.isDigit(str.charAt(i)))
           {
               count++;
           }
       }
      
       return count;
   }
  
   public int countUppers(String s)
   {
       int count=0;
      
       for(int i=0;i<str.length();i++)
       {
           if(Character.isUpperCase(str.charAt(i)))
           {
               count++;
           }
       }
       return count;
   }
  
   public int countLowers(String s)
   {
       int count=0;
      
       for(int i=0;i<str.length();i++)
       {
           if(Character.isLowerCase(str.charAt(i)))
           {
               count++;
           }
       }
       return count;
   }
  
   public int countSpecialChars(String s)
   {
       int count=0;
      
       for(int i=0;i<str.length();i++)
       {
           if(Character.isLetter(str.charAt(i)))
           {
               continue;
           }
           else if(Character.isDigit(str.charAt(i)))
           {
               continue;
           }
           else
           {
               count++;
           }
       }
       return count;
   }
  
   public String toString()
   {
       String temp="Summary: \nRead: "+str+"\nString length = "+Integer.toString(str.length())+"\nNumber of digits = "+Integer.toString(countDigits(str))+"\nNumber of uppercase characters = "+Integer.toString(countUppers(str))+"\nNumber of lowercase characters = "+Integer.toString(countLowers(str))+"\nNumber of lowercase special characters = "+Integer.toString(countSpecialChars(str));
       return temp;
   }
  
   public String getStr()
   {
       return str;
   }
  
   public void setStr(String s)
   {
       this.str=s;
       return;
   }
}
  
class StringSummaryExtended extends StringSummary
{
   public StringSummaryExtended(String str)
   {
       super(str);
   }
  
   public void mergeStrings(String s)
   {
       String temp=getStr();
       temp=temp+s;
       setStr(temp);
       return;
   }
}


public class StringSummaryExtendedDriver
{
   public static void main(String[] args)
   {
String str=JOptionPane.showInputDialog(null,"Enter a sentence:","Sentence Input 1",JOptionPane.QUESTION_MESSAGE);

String str2=JOptionPane.showInputDialog(null,"Enter a second sentence to be merged.","Sentence Input 2",JOptionPane.QUESTION_MESSAGE);
  
StringSummaryExtended summary=new StringSummaryExtended(str);     
  
   summary.mergeStrings(str2);
  
  
  
   JOptionPane.showMessageDialog(null,summary.toString(),"String Information",JOptionPane.INFORMATION_MESSAGE);
  
   }
}

Add a comment
Know the answer?
Add Answer to:
Question 2 (80 marks) This programming assignment is divided into two parts: (a) You will develop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. This question is about processing strings 2.1 [4 marks] Given a string str, we can...

    2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...

  • C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪...

    C++ Programming Question: This programming assignment is intended to demonstrate your knowledge of the following: ▪ Writing a while loop ▪ Write functions and calling functions Text Processing [50 points] We would like to demonstrate our ability to control strings and use methods. There are times when a program has to search for and replace certain characters in a string with other characters. This program will look for an individual character, called the key character, inside a target string. It...

  • Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to...

    Lab 4: Java Fundamentals, Part IV In this assignment, you solve a conversion problem similar to Programming Challenge 1 of Chapter 3 of your text, page 184 (187 in Edition 5). Given one of the Roman numerals I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV as an input your program must determine and display the corresponding decimal digit 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15....

  • C programming (not c++) This programs input is a series of words. All words consist of...

    C programming (not c++) This programs input is a series of words. All words consist of only lowercase letters(a-z), no uppercase letters or digits or punctuation or other special symbols. The program reads until end-of-file, and then prints out the lowercase letters that were not seen in the input. Enter your input: the quick brown fox jumps over the lazy old dog Missing letters: enter your input: roll tide missing letters: a b c f g h j k m...

  • I was asked to write a program that when divisible by 2- reverses the order of...

    I was asked to write a program that when divisible by 2- reverses the order of the letters in a sentence when divisible by 3- changes all lowercase letter into uppercase letters in a sentence when divisible by 5 skips the vowels in a sentence this is what I have so far. my reverseMode works fine. #include <iostream> #include <string> #include<cstring> using namespace std; void reverseMode(char* str); void upperMode(char* str); void goodbyeVowels(char* str); char str[50], rstr[50]; //initializing my character arrray...

  • Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To...

    Intro to Programming in C – Large Program 1 – Character/ Number converter Assignment Purpose: To compile, build, and execute an interactive program with a simple loop, conditions, user defined functions and library functions from stdio.h and ctype.h. You will write a program that will convert characters to integers and integers to characters General Requirements • In your program you will change each letter entered by the user to both uppercase AND lowercase– o Use the function toupper in #include...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem descr...

    C++ help Format any numerical decimal values with 2 digits after the decimal point. Problem description Write the following functions as prototyped below. Do not alter the function signatures. /// Returns a copy of the string with its first character capitalized and the rest lowercased. /// @example capitalize("hELLO wORLD") returns "Hello world" std::string capitalize(const std::string& str); /// Returns a copy of the string centered in a string of length 'width'. /// Padding is done using the specified 'fillchar' (default is...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT