You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Write a Scala program to sum values of a given array.
importscala.io.StdIn.{readDouble};
importscala.collection.mutable.ArrayBuffer;
objectSumArray {
defmain(args: Array[String]) = {
varlistOfNumbers=newArrayBuffer[Double]();
println("Enter numbers to add. Enter 0 to stop.");
varcurrent=1.0;
while (current !=0.0) {
try {
current = readDouble();
listOfNumbers.append(current);
} catch {
casee: NumberFormatException=> {
println("Enter only numbers.");
}
case_: Throwable=> {
println("An error occured.");
}
}
}
println(
s"The sum of all numbers provided to the array is ${listOfNumbers.sum}"
);
}
}
Output
Enter numbers to add. Enter 0 to stop.
6546
4343
343
544
4335.95485
545.343
556.3
64
9
0
The sum of all numbers provided to the array is 17286.59785
2. Write a Scala program to remove a specific element from a given array.
importscala.io.StdIn.{readLine};
importscala.collection.mutable.ArrayBuffer;
objectRemoveFromArray {
defmain(args: Array[String]) = {
varlistOfItems=newArrayBuffer[String]();
println("Enter elements to add to array. Enter 0 to stop.");
varcurrent="";
while (current !="0") {
try {
current = readLine();
if(current !="0") listOfItems.append(current);
} catch {
case_: Throwable=> {
println("An error occured.");
}
}
}
println("Element to remove?");
valtoRemove= readLine();
println(s"Original Array: $listOfItems");
println(s"Array without $toRemove: ${listOfItems.filter(s => s != toRemove)}");
}
}
Output
Enter elements to add to array. Enter 0 to stop.
dora
doraemon
mona
monaemon
dora
0
Element to remove?
dora
Original Array: ArrayBuffer(dora, doraemon, mona, monaemon, dora)
Array without dora: ArrayBuffer(doraemon, mona, monaemon)
3. Write a Scala program to rotate one element left of an given array (length 1 or more) of integers
importscala.io.StdIn.{readLine};
importscala.collection.mutable.ArrayBuffer;
objectRotateArrayLeft {
defmain(args: Array[String]) = {
varlistOfItems=newArrayBuffer[String]();
println("Enter elements to add to array. Enter 0 to stop.");
varcurrent="";
while (current !="0") {
try {
current = readLine();
if(current !="0") listOfItems.append(current);
} catch {
case_: Throwable=> {
println("An error occured.");
}
}
}
println(s"Original Array: $listOfItems");
listOfItems = listOfItems.tail :+ listOfItems.head;
println(s"Rotate left: $listOfItems");
}
}
Output
Enter elements to add to array. Enter 0 to stop.
watermelon
eats
bananas
0
Original Array: ArrayBuffer(watermelon, eats, bananas)
Rotate left: ArrayBuffer(eats, bananas, watermelon)
4. Write a Scala program to find the maximum and minimum value of an array of integers.
importscala.io.StdIn.{readInt};
importscala.collection.mutable.ArrayBuffer;
objectMinMaxArray {
defmain(args: Array[String]) = {
varlistOfNumbers=newArrayBuffer[Int]();
println("Enter numbers to add. Enter 0 to stop.");
varcurrent=1;
while (current !=0) {
try {
current = readInt();
if(current !=0) listOfNumbers.append(current);
} catch {
casee: NumberFormatException=> {
println("Enter only whole numbers.");
}
case_: Throwable=> {
println("An error occured.");
}
}
}
println(
s"The max of all numbers provided to the array is ${listOfNumbers.max}"
);
println(
s"The min of all numbers provided to the array is ${listOfNumbers.min}"
);
}
}
Output
Enter numbers to add. Enter 0 to stop.
1
3
5
7
9
0
The max of all numbers provided to the array is 9
The min of all numbers provided to the array is 1
5. Write a Scala program to find a missing number in an array of integers.
importscala.io.StdIn.{readInt};
importscala.collection.mutable.ArrayBuffer;
objectMissingNumberInArray {
defmain(args: Array[String]) = {
varlistOfNumbers=newArrayBuffer[Int]();
println("Enter numbers to add. Enter 0 to stop.");
varcurrent=1;
while (current !=0) {
try {
current = readInt();
if(current !=0) listOfNumbers.append(current);
} catch {
casee: NumberFormatException=> {
println("Enter only whole numbers.");
}
case_: Throwable=> {
println("An error occured.");
}
}
}
println(s"Original Array: $listOfNumbers");
varmissingNumbers=newArrayBuffer[Int]();
for(i <- listOfNumbers.min to listOfNumbers.max) {
if(listOfNumbers.indexOf(i) ==-1) missingNumbers.append(i);
}
println(s"Missing Numbers: $missingNumbers");
}
}
Output
Enter numbers to add. Enter 0 to stop.
1
3
5
7
9
0
Original Array: ArrayBuffer(1, 3, 5, 7, 9)
Missing Numbers: ArrayBuffer(2, 4, 6, 8)
6. Write a Scala program to print after removing duplicates from a given string.
importscala.io.StdIn.{readLine};
objectMissingNumberInArray {
defmain(args: Array[String]) = {
println("Enter string: ");
varstring= readLine();
println(s"Original String: $string");
varstringWithoutDuplicates="";
for (x <-0 until string.length()) {
if (string.indexOf(string(x)) == x) stringWithoutDuplicates += string(x);
}
println(s"String without duplicates: $stringWithoutDuplicates")
}
}
Output
Enter string:
She sells seashells by the seashore
Original String: She sells seashells by the seashore
String without duplicates: She slabytor
7. Write a Scala program to reverse every word in a given string.
Enter string:
She sells seashells by the seashore
Original String: She sells seashells by the seashore
String with reversed words: ehS slles sllehsaes yb eht erohsaes
8. Write a Scala program to compare two strings lexicographically.
importscala.io.StdIn.{readLine};
importscala.collection.mutable.ArrayBuffer;
objectCompareStrings {
defmain(args: Array[String]) = {
println("Enter first string: ");
valstring1= readLine();
println("Enter second string: ");
valstring2= readLine();
if (string1 == string2) {
println(s"\"$string1\" is equal to \"$string2\"");
} else {
if (string1.compareTo(string2) >0) {
println(s"\"$string1\" is less than \"$string2\"");
} else {
println(s"\"$string1\" is greater than \"$string2\"");
}
}
}
}
Output
Enter first string:
Pikachu
Enter second string:
Polka
"Pikachu" is greater than "Polka"
Enter first string:
Watermelon
Enter second string:
Dragonfruit
"Watermelon" is less than "Dragonfruit"
Enter first string:
Spider Lily
Enter second string:
Spider Lily
"Spider Lily" is equal to "Spider Lily"
9. Write a Scala program to replace a specified character with another character.