0% found this document useful (0 votes)
5 views2 pages

Lab 2

The document describes Java code for a lab exercise that creates a Paragraph object using a Word class. The main method gets text from the user, creates a Paragraph with the text, gets the text from the Paragraph, creates a Word object with the text, and calls the Word's modifiedText method. The modifiedText method takes a substring and word from the user, finds occurrences of the substring in the text, and appends the word to create a modified text which is returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Lab 2

The document describes Java code for a lab exercise that creates a Paragraph object using a Word class. The main method gets text from the user, creates a Paragraph with the text, gets the text from the Paragraph, creates a Word object with the text, and calls the Word's modifiedText method. The modifiedText method takes a substring and word from the user, finds occurrences of the substring in the text, and appends the word to create a modified text which is returned.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

5. Создайте объект класса «Paragraph»(Абзац), используя класс «Word»(Слово).

Lab2 Comur Piotr CR-214

import javax.swing.*;

public class lab2 {


public static void main(String[] args) {
String t = JOptionPane.showInputDialog("Enter text");
System.out.println("Enter the text" + t);
Paragraph paragraph = new Paragraph(t);
String str = paragraph.getText();
Word word = new Word(str);
StringBuffer answer = word.modifiedText();
System.out.print("ModifiedText: " + answer );
}

Paragraph

public class Paragraph {


String text;
public Paragraph(String text) {
this.text = text;
}

public Paragraph() {
}

public String getText() {


return text;
}
}

Word

import javax.swing.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Word extends Paragraph{

public Word(String text) {


this.text = text;
}

public StringBuffer modifiedText() {


String substring = JOptionPane.showInputDialog("Enter substring");
System.out.println("Enter substring" + substring);

String wordToAdd = JOptionPane.showInputDialog("Enter word");


System.out.println("Enter word" + wordToAdd);

Pattern pattern = Pattern.compile(substring + "\\b");


Matcher matcher = pattern.matcher(text);

StringBuffer modifiedText = new StringBuffer();

while (matcher.find()) {

matcher.appendReplacement(modifiedText, matcher.group() + " " +


wordToAdd);
}
StringBuffer newText = matcher.appendTail(modifiedText);
return newText;

}
}

You might also like