Question

In: Computer Science

Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers.

Programing in Scala language: Create a class called “Array” that implements a fixed-sized two-dimensional array of floating-point numbers. Write separate methods to get an element (given parametersrow and col), set an element (given parametersrow, col, and value), and output the matrix to the console formatted properly in rows and columns. Next, provide an immutable method to perform array addition given two same-sized array.

Solutions

Expert Solution

Code:


object MyClass {
  
//creating a 2d array of fixed size
var myArr = Array.ofDim[Double](2,2);
  
//method to set an element of teh array
def setArrayElement(row: Int, col: Int, value: Double): Unit =
{
myArr(row)(col) = value;
}
  
//method to return the element from the respective pos
def getElement(row: Int, col: Int): Double =
{
return myArr(row)(col);
}
  
//method to print the array
def printArr(arr: Array[Array[Double]]): Unit =
{
for(i<-0 to 1)
{
for(j<- 0 to 1)
{
// Accessing the values
print(arr(i)(j) + " ");
}
println()
}
}
  
//method used for array addition
def addintion(arr1: Array[Array[Double]], arr2: Array[Array[Double]]): Unit =
{
var sum: Double = 0;
for(i<-0 to 1)
{
for(j<- 0 to 1)
{
arr1(i)(j) += arr2(i)(j);
  
}
}
  
println("\nNew Array after Addition: ");
printArr(arr1);
}
  
  
  
//main method
def main(args: Array[String]) {
  
  
//set the array element
setArrayElement(0,0,10.1);
setArrayElement(0,1,12.23);
setArrayElement(1,0,39.00);
setArrayElement(1,1,44.12);
  
//get array element
println("Element at pos 0,0: " + getElement(0,0));
println("Element at pos 0,1: " + getElement(0,1));
println("Element at pos 1,0: " + getElement(1,0));
println("Element at pos 1,1: " + getElement(1,1));
  
//print the array in matrix format
println("Array in Matrix Format: ")
printArr(myArr);
  
//method to add to array of sma size
var newArr= Array(Array(12.10, 43.29),
Array(88, 10.59)) ;
  
  
addintion(myArr, newArr);
}
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------

Output:


Related Solutions

Write a Java method called printAvg that takes in two floating point numbers and prints out...
Write a Java method called printAvg that takes in two floating point numbers and prints out the average of them.
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional...
you will create a dynamic two dimensional array of mult_div_values structs (defined below). The two dimensional array will be used to store the rows and columns of a multiplication and division table. Note that the table will start at 1 instead of zero, to prevent causing a divide-by-zero error in the division table! struct mult_div_values { int mult; float div; }; The program needs to read the number of rows and columns from the user as command line arguments. You...
How to create a two-dimensional array, initializing elements in the array and access an element in...
How to create a two-dimensional array, initializing elements in the array and access an element in the array using PHP, C# and Python? Provide code examples for each of these programming languages. [10pt] PHP C# Python Create a two-dimensional array Initializing elements in the array Access an element in the array
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0.
This program is for C.Create a two-dimensional array A using random integers from 1 to 10. Create a two-dimensional array B using random integers from -10 to 0. Combine the elements of A + B to create two- dimensional array C = A + B. Display array A, B and C to the screen for comparison. (Note a[0] + b[0] = c[0], a[1] + b[1] = c[1], etc.)
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the...
A professor has constructed a 3-by-4 two-dimensional array of float numbers. This array currently contains the lab grades, quiz grades and exam grades of three students in the professor’s class. Write a C++ program that calculate the final grade for each student and save it to the last column. You program should display the following output:             Lab    Quiz      Exam       Final     Student 1          ## ## ## ## Student 2          ## ## ## ## Student 3          ## ## ## ## The...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester...
Write a Java application that implements the following: Create an abstract class called GameTester. The GameTester class includes a name for the game tester and a boolean value representing the status (full-time, part-time). Include an abstract method to determine the salary, with full-time game testers getting a base salary of $3000 and part-time game testers getting $20 per hour. Create two subclasses called FullTimeGameTester, PartTimeGameTester. Create a console application that demonstrates how to create objects of both subclasses. Allow the...
Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list...
Create a class called MovieReducerExtremes that implements MediaReducer. Implement a reducer that takes a movie list and an option ("newest" or "oldest"), then return the newest or oldest movie as appropriate.Submit both the MovieReducerExtremes and the Movie class from the first question. /////Required Output://///// Newest\n 2014 AKA Jessica Jones Action \n Oldest\n 1936 Cabaret Music \n Given Files: Movie.java public class Movie extends Media { public Movie(String name, int year, String genre) { super(name, year, genre); } public String getEra()...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT