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

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...
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.
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....
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 Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In...
A Point Class Definition A two-dimensional point may be represented by an x- and y-coordinate. In this problem, you will write a Point class which stores this information and provides useful methods to work with points. We have provided an __init__ definition for the Point class. You will note that this accepts two arguments, being the x- and y-coordinates of the point. We have also included a __repr__ method, which will provide a canonical string representation of the point. Make...
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits...
Using the simple model for representing binary floating point numbers A floating-point number is 14 bits in length. The exponent field is 5 bits. The significand field is 8 bits. The bias is 15 Represent -32.5010 in the simple model.
WITH using methods create an array with numbers
IN JAVA  Prompt 3:WITH using methods create an array with numbersint [] number = { 35, 68, 80, 34, 45, 79, 80};Display the numbers in the array using for loopDisplay the sumFind biggest element in the arrayDisplay the numers in the array with index numbersDisplay the numers using for loop in ascending order (sort)
Create a game of Connect Four using a two dimensional array (that has 6 rows and...
Create a game of Connect Four using a two dimensional array (that has 6 rows and 7 columns) as a class variable. It should be user friendly and be a two person game. It should have clear directions and be visually appealing. The code MUST have a minimum of 3 methods (other than the main method). The player should be able to choose to play again. **Code must be written in Java**
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT