Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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