Friday, 29 April 2016

Order in Initialization

Do you know what the following piece of code will print?


class MyClass {
MyClass() {
System.out.println("MyClass");
}
MyClass(String id) {
System.out.println("MyClass " + id);
}
}
class Parent {
// static String id = "Parent";
// MyClass instance1 = new MyClass(id);
MyClass instance1 = new MyClass();
Parent() {
System.out.println("Parent");
}
}
public class Test extends Parent {
// static String id = "Test";
// MyClass instance2 = new MyClass(id);
MyClass instance2 = new MyClass();
Test(String name) {
System.out.println("Test");
}
public static void main(String[] args) {
new Test("Cristina");
}
}


The answer is:

MyClass
Parent
MyClass
Test

Now can you tell which instance (instance1 or instance2) prints the first "MyClass" string?
 
Uncomment the commented lines and see for yourself!

http://stackoverflow.com/questions/14805547/are-fields-initialized-before-constructor-code-is-run-in-java

http://stackoverflow.com/questions/17806342/order-of-constructor-calls-in-multilevel-inheritance-in-java

Tuesday, 26 April 2016

Java EE Servlets

Introduction

I've been taking a short break before moving on to new challenges. With this occasion I'm tidying several older personal projects. Among these, my Haiku Gallery.

I resumed the review from watching again "Intro to Java. Unit 14. Intro to Java EE. GlassFish. Servlets." by Yakov Fain:



After reading Lesson 26 of Java Programming 24-Hour Trainer I wrote the vote servlet, adapted the client side of my application, packed everything in a WAR file using Eclipse IDE, and uploaded the archive to the Azure Cloud. As illustrated below.

The Client Side of VoteMyHaiku App

<div>
<h2>Windy Afternoon</h2>
<p>Windy afternoon</p>
<p>Your smile</p>
<p>On a stranger's face</p>
<form action=http://votemyhaiku.azurewebsites.net/VoteMyHaiku/vote method=Get>
<input type=Submit name="smile" value="Like" class="my_button"/>
</form>
</div>

The Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String button1 = request.getParameter("smile");
String button2 = request.getParameter("wait");
if (button1 != null) {
out.println("<html><body>");
out.println("<h1>Smile!!!</h1>");
out.println("</body></html>");
}
else if (button2 != null){
out.println("<html><body>");
out.println("<h1>Wait!</h1>");
out.println("</body></html>");
}
}

Vote My Haiku in the Azure Cloud:

http://votemyhaiku.azurewebsites.net/

Conclusions

1. I love Visual Studio Online "Monaco"!
2. Adam Bien is right: Java EE is easier than Javascript.

Homework (to self)

1. Warming up: Respond with beautiful HTML from the Vote Server Servlet.
2. Advanced: Persist votes in an SQL database. Re-order haiku pieces dynamically, based on voting results, and create a 'Top 3' section on the page.

Friday, 1 April 2016

Angular 2 Random Quote Generator, Step 1

I started coding freeCodeCamp's project titled 'Build a Random Quote Machine'.

Here's the first iteration:

// import does not work in Codepen
// using const instead
const {Component, Inject} = ng.core;
const {bootstrap} = ng.platform.browser;
const {HTTP_PROVIDERS, Http, Response} = ng.http;
@Component({
selector: 'my-app',
template: '<h1>{{ quote }}</h1>'
})
class QuoteComponent {
quote: string;
constructor(@Inject(Http) http) {
http.get('http://cfierbin.github.io/assets/data/myQuotes.json')
.subscribe((response: Response) => this.quote = response.text());
}
}
bootstrap(QuoteComponent, [HTTP_PROVIDERS]);
view raw quotesStep1.js hosted with ❤ by GitHub
My textbook for learning Angular 2 is Angular 2 Development with TypeScript, by Yakov Fain and Anton Moiseev.

P.S. Today is April Fool's Day. Stay foolish! (Steve Jobs)

References