CS1112 >
From Matlab to Java
If you will learn Java next, read
Appendix C "Matlab, Java, and C" in our textbook Insight.
Below are the "Java translations" of some of the
Matlab examples from the course to get you oriented.
Project 2 Problem 1: Matlab script to Java program
cubeRoot.m →
CubeRoot.java
The Java code, also called the source code, is in the file
CubeRoot.java, i.e., the class name is "CubeRoot".
The code to be executed is contained
in a method called "main". A Java method corresponds to a
Matlab function. So a single Matlab script
(in this case cubeRoot.m)
translates to a java program with one class file
(in this case CubeRoot.java) that
contains one method called main. To execute the Java program is to
compile the file CubeRoot.java and execute its
main method.
Other differences to observe:
- Each variable must be declared: its type explicitly specified.
- The semicolon indicates the end of a statement; it is not for suppressing output.
- Simple printing with no format specification is typically done
using the method println or print
from class out in package System.
The System package is automatically available--no need to import.
- Braces,
{ }
, are used to enclose a block of Java code instead of
the keyword end in Matlab. See the while-loop,
the main method block, and the class block for example.
- Common arithmetic built-in functions are in the Math class. E.g,
Math.pow(x,y) in Java is
x^y
in Matlab.
- A single-line comment is preceded by
//
;
a multi-line comment block is enclosed with /*
and */
like this:
/* comment line 1
comment line 2
*/
Project 2 Problem 1 modified: Matlab function to Java static
method
cubeRoot.m as above →
CubeRootVariation.java
A cube root function, called myCubeRoot, is implemented to
return the estimated cube root. In the class CubeRootVariation,
method myCubeRoot computes and returns the cube root while
method main contains the code that calls method myCubeRoot
several times with various arguments. These methods are static
methods--methods that can be called without an object of the class.
The code in this file, as well as that in CubeRoot.java
of the previous
example, is non-object-oriented.
Other differences to observe:
- The type of each parameter in the method header must be specified.
- Each method has a return type.
Method myCubeRoot has
the return type double in the method header; therefore it ends
with a return statement that returns a value of type double.
Method main has
the return type void in the method header, meaning that it does not return
anything; therefore method main does not have a return statement.
A method in Java may return only one thing.
- The for-loop syntax is quite different from that in Matlab.
Project 6 stained glass image: OOP
p6sol.zip →
stainedGlassJ.zip
The object-oriented concepts used are the same between Matlab and Java!
The differences you see are mainly due to syntax and
the setup necessary in Java
for producing graphics and reading an image.
The same OO design involving three classes is used: StainedGlassImage
,
Tile
, and Vertex
.
Observe these differences:
- In an instance method, there is not a parameter that refers to the instance
itself. This is in contrast to the self-reference parameter (that we often
named
self
) that is always the first parameter of a Matlab instance
method. In Java, there is an implicit reference this
, which
refers to the instance itself. See instance method perturb
in class
Vertex
for example. The method header does not include a
parameter to the object itself; yet inside the method you see the keyword
this
used to reference the object itself.
- In class
Tile
, the package awt
is imported for
doing graphics in the instance method draw
.
- In class
StainedGlassImage
, several packages are imported to handle
graphics (drawing and painting),
the window for displaying graphics (JPanel
), and
jpeg image files.
- To instantiate an object, the keyword
new
is used in addition
to calling the constructor.
- In class
StainedGlassImage
, we implement an instance method
imread
to do what Matlab's built-in imread
does.
- In class
StainedGlassImage
, the main
method
contains the code to be executed in order to instantiate the
StainedGlassImage
object, i.e., to produce the stained glass image
graphics. Notice that the main
method is static
,
i.e., an object is not necessary in order to call main
; this has
to be true since main
is the starting point of a Java program.