Допустим, у меня есть классы : Текст, Предложение, Слово, Буква, Пунктуация. Задача: удалить в каждом слове повторяющиеся буквы. Как мне решить задачу используя эти классы? Классы я написал, НО корректно ли с точки зрения ООП?
Код:
package Lab4;
public class Lab4 {
public static void main(String[] args){
String s = "Размер или длина массива — это общее количество элементов в массиве." +
" Размер массива задаётся при создании массива и не может быть изменён в дальнейшем," +
" другими словами нельзя убрать элементы из массива или добавить их туда, но" +
" можно в существующие элементы присвоить новые значения.";
Text text = new Text(s);
//????????????????????????????????????????????????????????
}
}
class Text{
private Sentence[] sentences;
private String allText;
private int numberOfSentences;
public Text(String allText){
this.allText = allText;
sentences = new Sentence[amountOfSentences()];
}
public int amountOfSentences(){
return allText.split("\\.").length;
}
public void fillUpSentenceArray(){
for (int i = 0; i < amountOfSentences(); i++){
sentences[i] = new Sentence((allText.split("\\."))[i]);
}
}
public String getAllText() {
return allText;
}
}
class Sentence{
private Word[] words;
private Punctuation[] punctuations;
private String allSentence;
public Sentence(String allSentence){
this.allSentence = allSentence;
words = new Word[countAmountOfWords()];
punctuations = new Punctuation[countAmountOfPunct()];
}
public int countAmountOfWords(){
return allSentence.split(" ").length;
}
public int countAmountOfPunct(){
StringBuilder a = new StringBuilder();
for(int i = 0; i < allSentence.length(); i++){
if(allSentence.toCharArray()[i] == ','){
a.append(allSentence.toCharArray()[i]);
}
}
return a.length();
}
public void fillUpWordsArray(){
for(int i = 0; i < countAmountOfWords(); i++){
words[i] = new Word((allSentence.split(" "))[i]);
}
}
public void fillUpPunctArray(){
char[] charr = allSentence.toCharArray();
for(int i = 0; i < countAmountOfPunct(); i++){
if(charr[i] == ',' || charr[i] == '.' || charr[i] == '-'){
punctuations[i] = new Punctuation(charr[i]);
}
}
}
}
class Word{
private Letter[] letters;
private String allWord;
public Word(String allWord){
this.allWord = allWord;
}
public int countLengthofWord(){
return allWord.length();
}
public void fillUpLetterArray(){
for (int i = 0; i < countLengthofWord(); i++){
letters[i] = new Letter(allWord.toCharArray()[i]);
}
}
}
class Punctuation{
private char symbol;
public Punctuation(char symbol){
this.symbol = symbol;
}
public char getSymbol() {
return symbol;
}
}
class Letter{
private char letter;
public Letter(char letter){
this.letter = letter;
}
public char getLetter() {
return letter;
}
}
Сборка персонального компьютера от Artline: умный выбор для современных пользователей