1 / 12

Scala Class Definition

Scala Class Definition. Java /Scala. The purpose of this tutorial is to show some of the differences between coding in Java and Scala. Defining a Scala class will be the focus.

thu
Download Presentation

Scala Class Definition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scala Class Definition

  2. Java /Scala • The purpose of this tutorial is to show some of the differences between coding in Java and Scala. • Defining a Scala class will be the focus. • The program to be used is called ExamScores which uses an array to show the differences of a class definition in Java and Scala.

  3. ExamScores in Java • ExamScores has a single constructor that takes one parameter: an array of exam scores • public ExamScores(int[]paramScores) • ExamScores methods include a couple of accessor methods • public int getScores() • public void setScores(int[] paramArray)

  4. In Scala, functions are first-class citizens. Functions can be passed as parameters Functions can be returned from functions Scala employees special functions to do common tasks so you do not have to redefine commonly used functions. Functions in Scala

  5. Java public class ExamScores{ privateint[] scores; First notice that the ExamScore class in Java is declared as “public” Then notice the declaration of the variables that the program will need to do its functions private Int[] scores Third notice the semicolons. Getting Started • Scala uses “public” by default, so, unless you want to specify something else, that can be omitted from your code • Declaration of arrays in Java require you to specify the type of data that will be entered into the array. Scala can make that determination for you. • Semicolons are optional and can also be omitted in Scala

  6. Java publicint[] scores = {84, 78, 92, 68, 88}; Scala val scores = Array(84, 78, 92, 68, 88) Declaring Arrays • There is not much difference in establishing an array in Java versus an array in scala. The main differences are in not having to declare the type of data being stored and not having to use the semicolon. • By declaring the array as “val” we can protect it from being altered. • The arrays would normally be instantiated in an outside client program but is shown here to focus on the functions performed on the arrays.

  7. /*------------------------------------------------------ a public mutator method that returns void and takes one parameter, an array of integers. It instantiates the instance variable array scores using the length of the parameter array as the size specifier. It then copies the elements from the parameter array into the scores array. Finally, it assigns false to isSorted. ------------------------------------------------------*/ setScores() Java publicvoid setScores(int[] paramArray){ scores = new int[paramArray.length]; for (int i = 0; i < scores.length; i++){ scores[i] = paramArray[i]; } isSorted = false; } This is our mutator methods that we will look at. This Java method is the definition of a function. It takes an array as an argument, then creates an new array, (the one we declared as the class array “scores”) and then copies the elements into the class array for processing. This is done to insure the integrity of the original list. Then it sets the class boolean to false. The fact that this Java method is a definition of a function is important to us in Scala because Scala specializes in functions. Unlike Java, Scala treats functions as objects. These “function” objects can be passed and received much the same way as we have passed and received the array in this Java method.

  8. /*--------------------------------------------------- a public accessor method that takes no parameters and returns an array of integers. It declares and instantiates a local array of integers using the length of the scores array as the size specifier. It then copies the elements from the scores array into the local array. Finally, it returns the local array. ---------------------------------------------------*/ getScores() Java publicint[] getScores(){ int[] localScores = newint [scores.length]; for (int i = 0; i < scores.length; i++){ localScores[i] = scores[i]; } return localScores; } This is our accessor methods that we will look at. This Java method is also a definition of a function. It creates a new array, then copies the class array elements into the new array for processing. Now we have two arrays within our Java class. Plus the array that we will pass to the class. Then it returns the copy of the new array without doing anything more.

  9. Code Duplication • Both the setScores and getScores methods in Java use a common function. That common function is a for loop to process the elements in the array. • Since both methods use the same function, this creates code duplication. • Since Scala treats “functions” as objects we can take advantage of this by assigning the function to a reference and pass the “function” instead of passing the array. But not yet…. • Scala has a surprise for us out of the gate….it will create these methods for us. Taking the passed array, “setting” it to a class array, then providing a “getter” so we can access it.

  10. //Java Code public class ExamScores{ privateint[] scores; private boolean isSorted; publicvoid setScores(int[] paramArray){ scores = newint[paramArray.length]; for (int i = 0; i < scores.length; i++){ scores[i] = paramArray[i]; } isSorted = false; } publicint[] getScores(){ int[] localScores = newint[scores.length]; for (int i = 0; i < scores.length; i++){ localScores[i] = scores[i]; } return localScores; } //Scala Code class ExamScores(val myArray: Array[Int]) That’s it! This is our Scala class definition without the boolean. The Java version required that we define the field and accessor methods for scores. In the Scala version, the parameter to the class takes care of defining the field and writing the acccessor and mutator methods. Here’s The Code So Far….

  11. What Just Happened? • Scala’s functional programming allowed us to do in 1 line of code what it took Java 13 lines! • To test if this is working you can compile the ExamScores class. • Create an array to pass into the class val scores = Array(2,3,4,5) • Create a new instance of ExamScore and pass it the “scores” array by coding: val testing = new ExamScores(scores) • Use the built in getter of our class by coding this: println(“Scala getter gets “ + testing.myArray.deep) • Also notice that we did not have to write a toString(), although the toString() created returns the address of the array, the “.deep” call takes care of printing the array. • The output: Scala getter gets Array(2,3,4,5)

  12. Scala made the ExamScores class public by default as it does with any variable that is not set otherwise by the programmer. We could have easily made it private or protected if we had chosen to by specifying it. • Run the following code and you can get a printout of the methods scala created for the ExamScores class: classOf[ExamScores].getMethods • Array[java.lang.reflect.Method] = Array(public int[] ExamScores.myArray() • public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException • public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException • public final void java.lang.Object.wait() throws java.lang.InterruptedException • public boolean java.lang.Object.equals(java.lang.Object) • public java.lang.String java.lang.Object.toString() • public native int java.lang.Object.hashCode() • public final native java.lang.Class java.lang.Object.getClass() • public final native void java.lang.Object.notify() • public final native void java.lang.Object.notifyAll())

More Related