Skip to content

Commit dbbb6f0

Browse files
committed
initial commit
0 parents  commit dbbb6f0

11 files changed

+311
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Java examples from Chapter 2 and Chapter 15 as described in Learning Android book, 2nd Edition by Marko and Mas:
2+
http://shop.oreilly.com/product/0636920023456.do
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.marakana.examples;
2+
3+
/*
4+
In this example, ComplexExample has a main method when executed
5+
instantiates a MsgGenerator object and then passes this object to a
6+
Thread. The Thread's process is then started and then the main thread
7+
waits till the generator object notifies that it is done (via the
8+
notifyAll()). At this point the generator's printList method is called
9+
and information about what was in the generator's list is printed out.
10+
*/
11+
public class ComplexExample {
12+
13+
public static void main(String[] args) {
14+
System.out.println("ComplexExample start");
15+
16+
MsgGenerator generator = new MsgGenerator();
17+
18+
Thread thread = new Thread(generator);
19+
thread.start();
20+
21+
try {
22+
synchronized(generator) {
23+
generator.wait();
24+
}
25+
} catch (InterruptedException ie) {
26+
System.err.println("Generator Wait Interrupted!!!");
27+
ie.printStackTrace();
28+
} finally {
29+
System.out.println("Generator Wait End");
30+
}
31+
32+
generator.printList();
33+
34+
System.out.println("ComplexExample end");
35+
}
36+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.marakana.examples;
2+
3+
public class HelloWorld {
4+
5+
public static void main(String[] args) {
6+
7+
System.out.println("Hello World");
8+
9+
}
10+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.marakana.examples;
2+
3+
import java.util.ArrayList;
4+
import java.util.Random;
5+
6+
public class MsgGenerator implements Runnable {
7+
private ArrayList<MsgInterface> list;
8+
9+
public MsgGenerator() {
10+
list = new ArrayList<MsgInterface>();
11+
}
12+
13+
public void run() {
14+
Random rand = new Random();
15+
int r = 0;
16+
ArrayList<MsgInterface> localList = new ArrayList<MsgInterface>();
17+
18+
while((r = rand.nextInt(20)) < 18) {
19+
MsgInterface msg = null;
20+
21+
switch (rand.nextInt(3)) {
22+
case 0: msg = new MsgTypeImplementation();
23+
break;
24+
case 1: msg = new MsgTypeAdditional();
25+
break;
26+
case 2: msg = new MsgTypeImplementationExtended();
27+
break;
28+
}
29+
30+
msg.setMsg("Num is: "+r);
31+
32+
localList.add(msg);
33+
}
34+
35+
synchronized(this) {
36+
list = localList;
37+
this.notifyAll();
38+
}
39+
}
40+
41+
public void printList() {
42+
ArrayList<MsgInterface> localList;
43+
synchronized (this) {
44+
localList = list;
45+
}
46+
47+
System.out.println("List Contents:");
48+
for(MsgInterface msg : localList) {
49+
System.out.println(" "+msg.getMsgType()+" msg = "+msg.getMsg());
50+
if(msg.getMsgType().equals("MsgTypeImplementationExtended")) {
51+
System.out.println(" *** Overloaded getMsg : "+
52+
((MsgTypeImplementationExtended) msg).getMsg("Special") +
53+
((MsgTypeImplementationExtended) msg).getMsg(99));
54+
}
55+
}
56+
System.out.println("List Size: "+list.size());
57+
}
58+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.marakana.examples;
2+
3+
public interface MsgInterface {
4+
void setMsg(String msg);
5+
String getMsg();
6+
String getMsgType();
7+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.marakana.examples;
2+
3+
public class MsgTypeAdditional implements MsgInterface {
4+
5+
private String msg;
6+
7+
public MsgTypeAdditional() { }
8+
9+
public MsgTypeAdditional(String msg) {
10+
setMsg(msg);
11+
}
12+
13+
public void setMsg(String msg) {
14+
this.msg = msg + " 2";
15+
}
16+
17+
public String getMsg() {
18+
return this.msg;
19+
}
20+
21+
public String getMsgType() {
22+
return "MsgTypeAdditional";
23+
}
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.marakana.examples;
2+
3+
public class MsgTypeImplementation implements MsgInterface {
4+
5+
private String msg;
6+
7+
public void setMsg(String msg) {
8+
this.msg = msg;
9+
}
10+
11+
public String getMsg() {
12+
return this.msg;
13+
}
14+
15+
public String getMsgType() {
16+
return "MsgTypeImplementation";
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.marakana.examples;
2+
3+
public class MsgTypeImplementationExtended extends MsgTypeImplementation {
4+
5+
@Override
6+
public String getMsgType() {
7+
return "MsgTypeImplementationExtended";
8+
}
9+
10+
// here we Overload the getMsg() method so we now have an
11+
// additional method that adds something to the getMsg() string
12+
13+
public String getMsg(String pre) {
14+
return pre+" "+getMsg();
15+
}
16+
17+
// and again we Overload the getMsg() method this time with a int
18+
19+
public String getMsg(int post) {
20+
return " -- "+post;
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.marakana.examples;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.URL;
6+
import java.net.HttpURLConnection;
7+
8+
public class QuickHttpExample {
9+
public static void main(String[] args) {
10+
HttpURLConnection urlConnection = null;
11+
try {
12+
URL url = new URL("http://yamba.marakana.com/api/statuses/public_timeline.json");
13+
14+
urlConnection = (HttpURLConnection) url.openConnection();
15+
16+
int statusCode = urlConnection.getResponseCode();
17+
System.out.println("Response Code: "+statusCode);
18+
19+
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
20+
21+
String textline = null;
22+
23+
while((textline = in.readLine()) != null) {
24+
System.out.println(textline);
25+
}
26+
} catch (Exception e) {
27+
e.printStackTrace();
28+
} finally {
29+
if(urlConnection != null) urlConnection.disconnect();
30+
}
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.marakana.examples;
2+
3+
/*
4+
A simple example class with a basic main method
5+
that instantiates several objects of the class,
6+
manipulates the objects, and outputs information
7+
about the object
8+
*/
9+
public class SimpleExample {
10+
11+
private int number;
12+
13+
public SimpleExample() { }
14+
15+
public void setValue(int val) {
16+
number = val;
17+
}
18+
19+
public int getNumber() {
20+
return number;
21+
}
22+
23+
public static void main(String[] args) {
24+
for(int i=0;i<10;i++) {
25+
SimpleExample example = new SimpleExample();
26+
27+
if(i/2 <= 2) {
28+
example.setValue(i);
29+
} else {
30+
example.setValue(i*10);
31+
}
32+
33+
System.out.println("SimpleExample #"+i+"'s value is "+example.getNumber());
34+
}
35+
}
36+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.marakana.examples;
2+
3+
/*
4+
A simple example class with a basic main method
5+
that instantiates several objects of the class,
6+
manipulates the objects, and outputs information
7+
about the object
8+
*/
9+
public class SimpleExampleWErrorHandling {
10+
11+
private int number;
12+
13+
public SimpleExampleWErrorHandling() { }
14+
15+
//------- ERROR HANDLING PART 1
16+
public void setValue(int val) throws Exception {
17+
if(val < 0) throw new Exception("setValue Exception- Value that is set is Negative!");
18+
number = val;
19+
}
20+
21+
public int getNumber() {
22+
return number;
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
for(int i=0;i<10;i++) {
28+
SimpleExample example = new SimpleExample();
29+
30+
if(i/2 <= 2) {
31+
try { example.setValue(i); } catch (Exception e) { e.printStackTrace(); } //------- ERROR HANDLING PART 4
32+
} else {
33+
try { example.setValue(i*10); } catch (Exception e) { e.printStackTrace(); } //------- ERROR HANDLING PART 4
34+
}
35+
36+
System.out.println("SimpleExample #"+i+"'s value is "+example.getNumber());
37+
}
38+
39+
40+
showErrorHandling(); //------- ERROR HANDLING PART 2
41+
42+
}
43+
44+
//------- ERROR HANDLING PART 3
45+
public static void showErrorHandling() {
46+
// here we show Error Handling
47+
try {
48+
System.out.println();
49+
System.out.println("SimpleExample BadValue Insert Case Start");
50+
SimpleExample example = new SimpleExample();
51+
example.setValue(-10);
52+
System.out.println("SimpleExample BadValue's value is "+example.getNumber());
53+
System.out.println("SimpleExample BadValue Insert Case End");
54+
55+
} catch (Exception e) {
56+
57+
System.err.println("SimpleExample BadValue Insert Case threw an exception");
58+
e.printStackTrace();
59+
60+
} finally {
61+
62+
System.out.println("SimpleExample BadValue Insert Case Finally Called");
63+
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)