Compare commits

..

No commits in common. "b00e0907917340125eed5d609fcbfc280b2fa831" and "59f315cacc04497651677785999b68195c7ee934" have entirely different histories.

12 changed files with 50 additions and 198 deletions

View File

@ -1,23 +1,11 @@
package OOP.Java.lab_5
/**
* A class representing a single letter in a word.
*
* @property character a character value of a Letter.
*/
class Letter(private val character: Char) {
class Letter(val character: Char) {
override fun toString(): String {
return this.character.toString()
}
/**
* Indicates whether the two objects of class Letter are "equal".
*
* @param letter a letter to compare.
* @param ignoreCase if set to "true" will ignore the case of Letters compared.
* @return "true" if letters are equal and "false" if they aren't.
*/
fun letterEquals(letter: Letter, ignoreCase: Boolean): Boolean {
fun equals(letter: Letter, ignoreCase: Boolean): Boolean {
return this.character.toString().equals(letter.toString(), ignoreCase)
}
}

View File

@ -1,14 +1,10 @@
package OOP.Java.lab_5
/**
* A class representing a punctuation mark following a word or a sentence.
*
* @property punctuationMark a [String] value of a punctuation mark.
*/
class Punctuation(var punctuationMark: String) {
private val punctuationMarkOptions: Regex = Regex("\\p{Punct}")
init {
if (!"\\p{Punct}".toRegex().containsMatchIn(punctuationMark)) {
if (!punctuationMarkOptions.containsMatchIn(punctuationMark)) {
this.punctuationMark = ""
}
}

View File

@ -1,35 +1,36 @@
package OOP.Java.lab_5
/**
* A class representing a single sentence is a text.
*
* @property sentenceArray a [Pair] of [Array]s of [Word] and [Punctuation] objects that make up a sentence.
*
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Word] and [Punctuation] objects, a secondary one accepts a string representing the entire sentence.
*/
class Sentence(var sentenceArray: Pair<Array<Word>, Array<Punctuation>>) {
class Sentence(sentenceString: String) {
var sentenceArray = Pair<Array<Word>, Array<Punctuation>>(splitWords(sentenceString), splitPunctuation(sentenceString))
constructor(
sentenceString: String
) : this(
Pair(
sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray(),
sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
)
)
init {
// val arrayOfWords: Array<Word> = splitWords(sentenceString)
// val arrayOfPunctuations: Array<Punctuation> = splitPunctuation(sentenceString)
}
private fun splitWords(sentenceString: String): Array<Word> {
return sentenceString.split("[\\p{Punct}\\s]+".toRegex()).filter { it.isNotEmpty() }.map { word -> Word(word) }.toTypedArray()
}
private fun splitPunctuation(sentenceString: String): Array<Punctuation> {
return sentenceString.split(" ").map { word -> Punctuation(word.last().toString()) }.toTypedArray()
}
/**
* Returns an array of all [Letter] objects in a sentence.
*/
fun getAllLetters(): Array<Letter> {
var allLetters = arrayOf<Letter>()
this.sentenceArray.first.forEach { allLetters += it.letters }
/*for (i: Word in this.sentenceArray.first) {
println("${i} ${i::class.simpleName} ${i.letters}")
}*/
return allLetters
}
override fun toString(): String {
var sentence = arrayOf<String>()
this.sentenceArray.first.indices.forEach {sentence += this.sentenceArray.first[it].toString() + this.sentenceArray.second[it].toString() }
for (i in this.sentenceArray.first.indices) {
sentence += this.sentenceArray.first[i].toString() + this.sentenceArray.second[i].toString()
}
return sentence.joinToString(" ")
}
}

View File

@ -1,74 +1,53 @@
package OOP.Java.lab_5
/**
* A class representing the entire text.
*
* @property textArray a [Pair] of [Array]s of [Sentence] and [Punctuation] objects that make up a text.
*
* @constructor a primary constructor accepts a [Pair] of [Array]s of [Sentence] and [Punctuation] objects, a secondary one accepts a string representing the entire text.
*/
class Text(var textArray: Pair<Array<Sentence>, Array<Punctuation>>) {
class Text(textString: String) {
var textArray = Pair(splitSentences(textString), splitPunctuation(textString))
constructor(
textString: String
) : this(
Pair(
textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray(),
textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
)
)
private fun splitSentences(textString: String): Array<Sentence> {
return textString.split("[.!?] ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Sentence(sentence) }.toTypedArray()
}
/**
* Returns an array of all [Letter] objects in a sentence.
*/
private fun getAllLetters(): Array<Letter> {
private fun splitPunctuation(textString: String): Array<Punctuation> {
return textString.split("(?<=[.!?]) ?".toRegex()).filter { it.isNotEmpty() }.map { sentence -> Punctuation(sentence.last().toString()) }.toTypedArray()
}
fun getAllLetters(): Array<Letter> {
var allLetters = arrayOf<Letter>()
this.textArray.first.forEach { allLetters += it.getAllLetters() }
return allLetters
}
/**
* Searches for the longest palindromic substring in a given text.
*
* @return the longest palindromic substring found.
*/
fun palindromeSearch(): String {
var result = " "
var results = arrayOf<String>()
val letters = this.getAllLetters()
for (leftBoundary in letters.indices) {
for (rightBoundary in letters.lastIndex downTo leftBoundary + 1) {
val subToC = letters.sliceArray(leftBoundary..rightBoundary)
// println(subToC.joinToString(""))
if (subToC.first().letterEquals(subToC.last(), true) && this.checkReverse(subToC) && subToC.size > result.length) {
result = subToC.joinToString("")
if (subToC.first().equals(subToC.last(), true) && subToC.size >= 3 && this.checkReverse(subToC)) {
// println(subToC.joinToString(""))
results += subToC.joinToString("")
}
}
}
return result
return results.maxBy { it.length }
}
/**
* Checks if a given substring is a palindrome.
*
* Is not case-sensitive.
*
* @param substring an [Array] of [Letter] objects representing a substring.
*
* @return "true" is a substring given is a palindrome, "false" if it isn't.
*/
private fun checkReverse(substring: Array<Letter>): Boolean {
var leftBoundary = 0
var rightBoundary = substring.lastIndex
var result = false
val correction = substring.size % 2
var correction = 0
if (substring.size % 2 != 0) {
correction = 1
}
while (leftBoundary < substring.size / 2 && rightBoundary >= substring.size / 2 + correction) {
leftBoundary++
rightBoundary--
result = substring[leftBoundary].letterEquals(substring[rightBoundary], true)
result = substring[leftBoundary].equals(substring[rightBoundary], true)
}
return result
@ -77,7 +56,9 @@ class Text(var textArray: Pair<Array<Sentence>, Array<Punctuation>>) {
override fun toString(): String {
var text = arrayOf<String>()
this.textArray.first.indices.forEach { text += this.textArray.first[it].toString() + this.textArray.second[it].toString() }
for (i in this.textArray.first.indices) {
text += this.textArray.first[i].toString() + this.textArray.second[i].toString()
}
return text.joinToString(" ")
}
}

View File

@ -1,21 +1,12 @@
package OOP.Java.lab_5
/**
* A class representing a single word is a sentence.
*
* @property letters an array of [Letter] objects that make up a word.
*
* @constructor a primary constructor accepts an array of [Letter] objects, a secondary one accepts a string representing the entire word.
*/
class Word(var letters: Array<Letter>) {
override fun toString(): String {
val wordString = StringBuilder()
this.letters.forEach { wordString.append(it) }
return wordString.toString()
}
constructor(
word: String
) : this((word.toCharArray().map { letter -> Letter(letter) }).toTypedArray())

View File

@ -1,19 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
}
fun totalDuration(): Duration {
var totalDuration: Duration = Duration.ZERO
this.tracks.forEach { totalDuration += it.duration }
return totalDuration
}
override fun toString(): String {
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
}
}

View File

@ -1,7 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class DancePop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Dance-pop"
}

View File

@ -1,7 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class ElectroPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Electropop"
}

View File

@ -1,7 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class Interlude(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Interlude"
}

View File

@ -1,7 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class SynthPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Synth-pop"
}

View File

@ -1,15 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration
open class Track(val numberInAlbum: Int, val trackName: String, val feature: String?, val duration: Duration) {
open val style = ""
override fun toString(): String {
return if (this.feature == null) {
"#${this.numberInAlbum}: ${this.trackName} (${this.duration})"
} else {
"#${this.numberInAlbum}: ${this.trackName} ft. ${this.feature} (${this.duration})"
}
}
}

View File

@ -1,43 +0,0 @@
package OOP.Java.lab_6
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
fun main() {
val chromatica = Album(
"Chromatica",
"Lady Gaga",
arrayOf(
Interlude(1, "Chromatica I", null, 1.minutes),
SynthPop(2, "Alice", null, 2.minutes + 57.seconds),
DancePop(3, "Stupid Love", null, 3.minutes + 13.seconds),
DancePop(4, "Rain On Me", "Ariana Grande", 3.minutes + 2.seconds),
SynthPop(5, "Free Woman", null, 3.minutes + 11.seconds),
ElectroPop(6, "Fun Tonight", null, 2.minutes + 53.seconds),
Interlude(7, "Chromatica II", null, 41.seconds),
SynthPop(8, "911", null, 2.minutes + 52.seconds),
ElectroPop(9, "Plastic Doll", null, 3.minutes + 41.seconds),
DancePop(10, "Sour Candy", "Blackpink", 2.minutes + 37.seconds),
DancePop(11, "Enigma", null, 2.minutes + 59.seconds),
SynthPop(12, "Replay", null, 3.minutes + 6.seconds),
Interlude(13, "Chromatica III", null, 27.seconds),
ElectroPop(14, "Sine From Above", "Elton John", 4.minutes + 4.seconds),
SynthPop(15, "1000 Doves", null, 3.minutes + 35.seconds),
DancePop(16, "Babylon", null, 2.minutes + 41.seconds)
)
)
println("$chromatica\n")
chromatica.tracks.sortBy { it.style }
println("Album sorted by musical style:\n$chromatica\n")
val durationRange = 1.minutes.. 3.minutes + 30.seconds
println(
"${chromatica.albumName} tracks in a duration range ($durationRange):\n${chromatica.tracksInDurationRange(durationRange).joinToString("\n")}"
)
}