# Programmierpraktikum

Claudius Gros, SS2012

Institut für theoretische Physik
Goethe-University Frankfurt a.M.

# primitive data types

• int, double, char, ... are primitive (predefined) data types
arbitrary complex data types may be defined by the user
• String  is (however) an object, like everything else
Typ Size Domain Example
boolean 1 {true, false}
short 2
int 4 [−231, 231−1] =
long8 [−263, 263−1]
float 4 [1.40239846 ⋅ 10–45, 3.40282347 ⋅ 1038]
double 8 [4.94065645841246544 ⋅ 10–324, 1.79769131486231570 ⋅ 10308]

• short / long for specialized applications only
• float not used anymore, standard is
double for floating point operations
• 32/64 bits (int/double) used for memory addressing by 32/64 bit processors,
$\rightarrow$ memory restrictions

# example: primitive data types

• casting of one data type to another will be disussed later
public class PrimitiveDataTypes {

public static void main(String[] args)
{

// --- pi with double (16 digits) and float (8 digits)
// --- Math.PI is a double constant
double double_PI = Math.PI;
float   float_PI = (float)Math.PI;    // casting double to float

System.out.printf("pi with double: %24.20f\n",double_PI);
System.out.printf("pi with  float: %24.20f\n",float_PI);
System.out.printf("%25s ^\n","");   // pointer
System.out.println(" ");            // empty line

// --- int and long integer numbers
int   int_int = 1;
long long_int = 1;

for (int i=0; i<34; i++)
{
int_int =   int_int * 2;
long_int =  long_int * 2;
System.out.printf("i+1, short, int, long: %4d %12d %12d\n",
i+1, int_int, long_int);
}

}
}


# strings

• a string (word) is an aribitrary sequence of characters
• the String class java.lang.String
has many built-in member functions for string manipulation
• String concatenation with +

public class ExampleStrings {
public static void main(String[] args)
{

// --- defining and printing
// --- toUpperCase() is a member function of the String class
String cdde = "cdde";
System.out.println("abc");
System.out.println(cdde);
System.out.println(cdde.toUpperCase());

// --- String concatenation with '+'
System.out.println("abc" + " " + cdde);

// --- substring() is a member function of the String class
String c  = "abc".substring(2,3);
String dd = cdde.substring(1,3);
System.out.println(" c: " + c);
System.out.println("dd: " + dd);

// --- valueOf() casts numbers into strings
System.out.println(String.valueOf(Math.PI));

} // end of ExampleStrings.main()
} // end of class ExampleStrings



# casting vs parsing

• here: conversion of primitive data types and strings
• later: casting of objects
import java.util.*;
import java.math.*;

public class CastingDemo {

public static void main(String args[]) {

double rr = Math.random();
String ss = String.valueOf(rr);         // converting double into string
System.out.println("a random number as a string: " + ss);
System.out.printf( "                   and back: %f\n\n",
Double.parseDouble(ss));     // parsing
//
int ii = (int)(Math.random()*100.0);    // casting double into int
System.out.printf( " random integer and string : %d %s\n",
ii, String.valueOf(ii));

}  // end of CastingDemo.main()
}    // end of CastingDemo


# arrays

• n-component array
int[] array = new int[n];

• value of array-component k
int b = array[k];

• array with predefined values
int[] array = {1, 2, 3};

• length n of array
int b = array.length;

• multi-dimensional arrays
int[][] array = new int[n][m];


# example: arrays

• arrays start always with [0]
public class ExampleArrays {
public static void main(String[] args) {

// --- array of integers
int[] values = {2, 3, 5, 7, 11, 13};
System.out.println(values[3]);     // 7

// --- array of strings
String[] colors = {"yellow", "red", "green", "blue"};
colors[0] = "black";
for (int i=0; i<colors.length; i++)
System.out.printf("This is color[%d]: %s \n", i, colors[i]);
System.out.println(" ");           // empty line

// --- splitting a string, typically used when reading from file
String inputString = "age height weight";
String[] words = inputString.split(" ");
for (int i=0; i<words.length; i++)
System.out.printf("This is words[%d]: %s \n", i, words[i]);
System.out.println(" ");           // empty line

// --- array of chars
char[] chars = "black sun".toCharArray();
for (int i=0; i<chars.length; i++)
System.out.printf("This is chars[%d]: %c \n", i, chars[i]);
}
}


# example: matrices

• matrix.length: the number of rows
• matrix[i].length: the number of elements in row[i]
• the number of elements per row may be variable (ragged matrix)
public class ExampleMatrices {
public static void main(String[] args)
{

// --- defining a 3x2 matrix (3 rows and 2 columns)
String[][] strMatrix = new String[3][2];
String[] rowZero = { "rowZero[0]", "rowZero[1]"};
String[] rowOne  = { "rowOne[0]", "rowOne[1]"};
strMatrix[0] = rowZero;             // setting row [0]
strMatrix[1] = rowOne;               // setting row [1]

// --- printing matrix
// --- note: row[2] is undefined (null)
for (int i = 0; i<strMatrix.length; i++)
for (int j = 0; j<strMatrix[i].length; j++)
System.out.printf("  strMatrix[%d][%d]:  %s\n",
i, j, strMatrix[i][j]);
System.out.println(" ");

// --- a matrix needs not to be rectangular, may be ragged
int[][] intMatrix = new int[3][];    // allocate the number of rows
int[] row_0 = {0,1,2,3,4};
int[] row_1 = {5,6,7};
int[] row_2 = {8,9};
intMatrix[0] = row_0;
intMatrix[1] = row_1;
intMatrix[2] = row_2;

for (int i = 0; i<intMatrix.length; i++)
{
System.out.printf("  strMatrix[%d]:",i);
for (int j = 0; j<intMatrix[i].length; j++)
System.out.printf(" %d",intMatrix[i][j]);
System.out.printf("\n");
}

} // end of ExampleMatrices.main()
} // end of class ExampleMatrices


# exercise: matrix operations

training suggestion
write a code for calculating the scalar product between two vectors
and for matrix multiplication

• n × m-Matrix
int[][] matrix = new int[n][m];

• value of Mij
matrix[i][j]

• initialize matrix with predefined values
int[][] matrix = new int[][] {{1, 2, 3}, {4, 5, 6}};


write a code for evaluating the trace of a given matrix

• tr(M) ≔ ∑i M ii

# arithmetic operators

• assignment and arithmetics
SymbolDescriptionExamples
=assignment int i=5;
+  -addition, subtraction int i=5+3; int i=5-3;
*  /multiplication, integer division int i=5*3; int i=9/3;
%modulo int i=14%3;
+  -unary plus/minus int i=+3; int i=-2;
+=  -=  *=   /=assignment and operation i += 5; // i = i + 5
++   --pre-/postfix increment/decrement i++; // i = i + 1

• naming of C++

# mathematical functions

### import for basic mathematics

import java.lang.Math;

• π ≈ 3.14159265358979323846
e ≈ 2.7182818284590452354, Euler's number
static final double PI;
static final double E;

• absolute value |x|
static int abs(int x);

• minimum/maximum value
static int min(int x, int y);
static int max(int x, int y);

• (arcus-, hyperbolic-) sinus/cosine/tangent
static double sin(double x);
static double cos(double x);
static double tan(double x);
static double asin(double x);
static double acos(double x);
static double atan(double x);
static double hsin(double x);
static double hcos(double x);
static double htan(double x);

• square/cubic root
static double sqrt(double x);
static double cbrt(double x);

• exponential ex
static double exp(double x);

• power xy
static double pow(double x, double y);

• logarithm (base e/10)
static double log(double a);
static double log10(double a);

• round
static double round(double a);
static long round(double a);