Programming in Java
Supervision 1
Foreword If you comment your code, so will I!
If any of the following is unclear, please let me know.
Functional translation
Here are 2 very simple programs, written in a pseudo-code for a functional language. N.B. [a] means a list of type a
func :: [Int] -> Int
func [] = 1
func (h:t) = h * func t
func1 :: [Int] -> Int
func1 [] = 0
func1 (h:t) = h + func1 t
func2 :: [Int] -> Int
func2 xs = foldl1 max xs
N.B. max – standard binary operator that returns the maximum of two integers
foldl1 (-) [4,3,6] = -5
foldr1 (-) [4,3,6] = 7
i. The first method should be a translation of “func”
ii. The second method should be a translation of “func1”
iii. The third method should be a translation of “func2”
iv. Add to the main method, code that prints to “std out” the return values of func, func1 and func2 when invoked on the array instantiated in part a. above.
Object orientated programming theory
Explain what is meant by “abstraction” in the context of object orientated programming, giving reasons for and examples of its use. (2 or 3 paragraphs)
Explain what is meant by “encapsulation” in the context of object orientated programming, giving reasons for and examples of its use. (2 or 3 paragraphs)
Explain “subsumption” and “casting” within an object orientated context. Give examples. (Don’t spend too long on this) (2 paragraphs)
Implementing a useful class
Here is a program that rotates an acceleration vector from the co-ordinate system of a sensor it was measured by, into a global co-ordinate system. This is done through multiplication using a rotation matrix, also measured by the sensor. It also calculates whether the rotation matrix is singular, just for fun!
public
class MatrixTestHarness
{
public static void main (String[] args)
{
double[][] orientations = {{-0.043536, 0.016970,
-0.998908},{0.837222, -0.544945, -0.045747},{-0.545127, -0.838300, 0.009518}};
double[] accelerations = {-5.578592, -8.606017, 0.154298};
Matrix
rotationSG = new Matrix(orientations); // Rotation
matrix for sensor->global co-ord system
Matrix
aS = new Matrix(accelerations, 3); // Acceleration
column vector (with, therefore, 3 rows) in sensor co-ord system
Matrix
aG = rotationSG.times(aS); // Acceleration column
vector in global co-ord system
double GaccX = aG.get(0,0);
double GaccY = aG.get(1,0);
double GaccZ = aG.get(2,0);
System.out.println("Acceleration vector in global
coordinate system is: (" + GaccX + ", " + GaccY + ", "
+ GaccZ + ")");
System.out.print("By the way, the orientation matrix is
");
if (!rotationSG.singular()) {System.out.print("not
");}
System.out.println("singular");
}
}
Write the class Matrix, so that the above program works. The above test harness is provided. Place the two source files MatrixTestHarness.java and Matrix.java in the same directory to compile.
Detective programming
It is often the case when you program, that you want to do something that someone else has already done. It is also often the case that various generous people share various bits of code so that after trawling the internet for a while, you find the .jar file that contains everything you were about to spend a couple of days writing yourself! This has actually happened within the java language to a certain extent.
However, this does mean you have to develop the skills of quickly understanding whether a set of various classes do in fact meet your requirements and then working out how to use them.
Bearing in mind the question above, is it wished to be able to specify an arbitrary number of sensor co-ordinate systems and transform an acceleration vector into the global co-ordinate system using each of them, in order to compare different sensors.
Complete the provided class Processor in order to make the Filter class test program work. You should make use of the Vector, Iterator and Matrix classes.
Inheritance
Following on from the last part of the second question on “Object orientated programming theory”, fill in the code in InheritanceTest.java so that the following output is obtained:
a
b
c
d
e
You are not allowed to declare any other variables, but you are allowed to instantiate classes A, B and C. The files are provided.
public
class InheritanceTest
{
static A a = new A();
public static void main(String[] args)
{
//Write
code here
//Stop
writing code here
}
}
public
class A
{
public char c =
'a';
public void
say()
{
System.out.println(c);
}
public void
whisper()
{
System.out.println('d');
}
}
public
class B extends A
{
public B()
{
c = 'b';
}
public char d =
'c';
public void
shout()
{
System.out.println(d);
}
public void
whisper()
{
System.out.println('d');
}
}
public
class C extends A
{
public char c =
'c';
public char d =
'b';
public void
shout()
{
System.out.println(d);
}
public void
whisper()
{
System.out.println('e');
}
}
Vocab test
What do the following keywords and operators do?
Good technique
Coding with good technique vastly improves the clarity of your thought and programs, which helps in their writing and debugging. Suggest sensible conventions or guidelines for the following parts of a program, with respect to ideas such as case, indentation, white space, etc.
1. Local variable names
2. Constant variable names
3. Member names
4. Method names
5. Loops
6. Conditionals