Commit 86885426 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

Initial commit

package exercise_qa;
public class Operators {
public static void main(String[] args) {
int i = 10;
int n = i++%5;
//This is my package definition
package java_exercises1_1_1;
* Here you should place some documentation about the meaning of the classes and its general use.
* This is a HelloWorld class.
* @author mad
public class HelloWorld {
* This is the main entry point of the application - the main function
* @param args Array of String objects: refers to the parameters of the commandline
public static void main(String[] args) {
//Prints out text to the console: "Hello World"
System.out.println("Hello World");
package java_exercises1_1_2;
public class DataTypes {
//application wide static scope
static double myMoney=1233489390;
public static void main(String[] args) {
//local variable scope
//declare a
int a;
//define b: declare and initialize with 1
int b=1;
//define string firstName
String firstName="Martin";
//define string lastName
String lastName="Deinhofer";
//define variable pi
double pi=3.14159;
//Assign a value to a
//print out the value to the console
//Explicitely cast the double variable to an int variable with data loss
System.out.println("pi="+pi+", a="+a);
//String concatenation: Print: "My name: Martin Deinhofer"
System.out.println("My name: "+firstName+" "+lastName);
//You can use myMoney variable here as well
System.out.println("I have a lot of money: "+myMoney);
package java_exercises1_1_3;
//import statements make classes in respective packages usable without fully qualified name (without as a prefix)
public class ConsoleInput {
public static void main(String[] args) {
//Without import statements we would have to write
//cascade of streams: -> InputStreamReader -> BufferedReader
//BufferedReader let's us elegantly read lines of input (and not characters) directly
BufferedReader input=new BufferedReader(new InputStreamReader(;
try {
//Read line of input
String line=input.readLine();
//You can parse it as an integer number, but if it's not numeric an exception is thrown
int number=Integer.valueOf(line);
System.out.println("Read line: "+line);
System.out.println("Read number: "+number);
//You will get to know exceptions later, just use it like this for now.
} catch (IOException e) {
//In case the console input stream cannot be opened ( an IOException is thrown
//Use exceptions to inform the user about errors and to cleanup / change control flow
//more on this later
System.out.println("The input console could not be opened");
package java_exercises1_2_1;
public class ArraysLoops {
public static void main(String[] args) {
int n=20;
//declare array of size n
int a[]=new int[n];
//initialize manually
for(int i=0;i<n;i++) {
//assign a value to each element of the array
//now loop through all elements using the Iterator interface (no manual index needed)
//this way the index cannot exceed the number of elements --> less error prone!!
for(int elem : a) {
//is value dividable by 2 without rest value??
if((elem % 2) == 0) {
package java_exercises1_2_2;
public class UserInput {
//a function can throw an exception indicating the caller that an exception might occur
//in case of the main function, the program will terminate
public static void main(String[] args) throws IOException {
//always init variables with meaningful values to prevent NullPointerException
String input="";
BufferedReader inputReader=new BufferedReader(new InputStreamReader(;
//Read line of input
//Creates a string object with value "e" and compares it's equality with the value of input
System.out.println("You wanted to exit the program, good bye");
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment