Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
embedded_systems_public
Java-Exercises-1-Basics
Commits
86885426
Commit
86885426
authored
Sep 19, 2017
by
Martin Deinhofer
Browse files
Initial commit
parents
Changes
7
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
86885426
/.project
/.settings
/bin
/.classpath
src/exercise_qa/Operators.java
0 → 100644
View file @
86885426
package
exercise_qa
;
public
class
Operators
{
public
static
void
main
(
String
[]
args
)
{
int
i
=
10
;
int
n
=
i
++%
5
;
}
}
src/java_exercises1_1_1/HelloWorld.java
0 → 100644
View file @
86885426
//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"
);
}
}
src/java_exercises1_1_2/DataTypes.java
0 → 100644
View file @
86885426
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
a
=
10
;
//print out the value to the console
System
.
out
.
println
(
"a="
+
a
);
b
=
a
+
a
;
System
.
out
.
println
(
"b="
+
b
);
//Explicitely cast the double variable to an int variable with data loss
a
=(
int
)
pi
;
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
);
}
}
src/java_exercises1_1_3/ConsoleInput.java
0 → 100644
View file @
86885426
package
java_exercises1_1_3
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
//import statements make classes in respective packages usable without fully qualified name (without java.io. as a prefix)
import
java.io.InputStreamReader
;
public
class
ConsoleInput
{
public
static
void
main
(
String
[]
args
)
{
//Without import statements we would have to write java.io.Reader
//cascade of streams: System.in -> InputStreamReader -> BufferedReader
//BufferedReader let's us elegantly read lines of input (and not characters) directly
BufferedReader
input
=
new
BufferedReader
(
new
InputStreamReader
(
System
.
in
));
try
{
System
.
out
.
print
(
">"
);
//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 (System.in) an IOException is thrown
e
.
printStackTrace
();
//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"
);
}
}
}
src/java_exercises1_2_1/ArraysLoops.java
0 → 100644
View file @
86885426
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
a
[
i
]=
i
;
System
.
out
.
println
(
"a["
+
i
+
"]="
+
a
[
i
]);
}
//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
)
{
System
.
out
.
println
(
"elem="
+
elem
);
}
}
}
}
src/java_exercises1_2_2/UserInput.java
0 → 100644
View file @
86885426
package
java_exercises1_2_2
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
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
(
System
.
in
));
do
{
System
.
out
.
print
(
">"
);
//Read line of input
input
=
inputReader
.
readLine
();
//Creates a string object with value "e" and compares it's equality with the value of input
}
while
(!
"e"
.
equals
(
input
));
System
.
out
.
println
(
"You wanted to exit the program, good bye"
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment