java

  • Links Round Up for 08/15/10

  • Google Web Toolkit

    gwt logo

    After I got back from Google I/O, I mentioned Google Web Toolkit was something they showed off that I was very impressed by. It allows you to build complete AJAX applications using Java. In one of my grad classes this semester, we had the opportunity to use Google Web Toolkit (GWT) on a semester long project. After using it I’ve come to the conclusion that is it great, but I’m not sure I would use it for a normal web design project.

    First, here is a sample GWT project I made called “The Decider.” It allows you to add items to a list and then the program will randomly select one for you. This is all done completely asynchronously. I was able to implement it completely in Java (which I think is easier than javascript) and then build the HTML around it using GWT’s ‘Widget’ classes. Again, you’re using Java to build the interface (without using Java Swing). This takes a little getting used to, but you can pick up pretty quickly after reading GWT’s documentation. A nice thing about the widgets is they all have their own CSS class assigned to them, and there are methods that allow you to add, remove, and override CSS classes. You can also build an HTML page with your own CSS id’s and dynamically add things to them with GWT.

    GWT is great is you’re unfamiliar with web design and you want to showcase some project your doing on the web. Google’s default styles look nice and you can really focus on the functionality, especially if the scope of the project didn’t include a web based implementation. It’s also good if you want to roll out a completely asynchronous UI relatively quickly. With The Decider, I didn’t have to worry about the advanced javascript stuff, using something like Dojo or JQuery, etc. I just wrote in Java, which I am pretty comfortable with. However, if from the outset you know you want to make a web app, I’d recommend doing it the traditional way. Writing your own HTML and CSS is semantically correct and less bloated than a GWT project. But of-course, I’m a web developer first, and I just have my feet wet with GWT. I’m willing to bet with a little extra time, due to the freedom you have with GWT you can make that lightweight, semantically correct (tableless!) interface you want with the power of Java behind it. And you can probably get rid of some of the bloat before deploying your app.

    All-in-all, GWT is really powerful and I think it will be a game changer. Next semester, another grad class I’m taking will be using it and I can’t wait to really get under the hood now that I have an understanding of how things work.

  • Types of classes, part 2

    Today I will continue my series on classes in Java. To review where we are, you can read this post.

    First of all, we need to add a couple of methods to our class. There are two other types of methods in classes: reporters and mutators. A reporter does exactly that. As you might notice, our instance variables are private, meaning that only that class can see it. So if we want to know certain information about the student, we must write a method that looks at the variable in question and reports it. A mutator method changes the object in some way. So, that being said, lets write a couple of these methods.

    ………..

    public String tellName(){
    return name;
    }

    public void computeGPA(float newAvg){
    float oldAvg= gpa;
    gpa= (oldAvg+newAvg)/2;
    }

    Here, we have added two methods to our class- a reporter and a mutator. Our reporter, tellName, looks at the current value of String name and returns it. Since we are returning a value, the method must be of the type we are returning. It is public so it can be accessed from outside our class. Now, here I just wrote one for name, but to get the other values (age, gpa, status), you must write a reporter method for each variable, as it’s respective type (an age reporter would be of type int).

    Our next method is a mutator that figures out a new gpa. This is a simple one that accepts a new GPA from the client and averages the new and old one together. This method is not returning anything, just doing a calculation, so it is a void method. It is accepting a variable of type float, which we declare in the parentheses before the do work in the method. Then we perform the calculation. We declare a new variable called oldAvg that gets the value of gpa, and then we assign the new value to gpa.

    That is all there is to other types of methods. They can be as simple or as complicated as you see fit. Next time, I will talk about inheritance and extensions. Later!

  • Types of classes

    Today and over the last couple of days we have been talking about classes in Java. I’m fairly certain I understand them enough to now talk about them here. Tonight I will introduce them and continue the discussion later.

    Basically, a class “consists of a collection of types of encapsulated instance variables and types of methods, possibly with implementation of those types together with a constructor function that can be used to create objects of the class. A class is a cohesive package that consists of a particular kind of compile-time metadata. A Class describes the rules by which objects behave; these objects are referred to as “instances” of that class. …” In basic terms, it is the blue prints of an object. Inside of a class, you will find instance varibles with values, methods to manipulate these variables, and constructors of those objects. If we were to make a class called Student , it might look something like this:

    public class Student{

    private String name;
    private int age;
    private float gpa;
    private char status;

    public Student (String stuName, int stuAge, float stuGpa, char stuStat){
    name= stuName;
    age= stuAge;
    gpa= stuGPA
    status= stuStat

    }
    public Student(){
    name= “”;
    age= 0;
    gpa= 0.0;
    status= null;
    }

    }

    This class has 2 methods- both constructors- and 4 instance variables in it. The variables are initialized, but have no values. This is where the constructors come in. The first constructor for Student has 4 arguments sent to it. One for name, age, gpa and status. The constructor takes these 4 values sent by the client (NOT necessarily a user) and applies them to our 4 instance variables. The second constructor, which has no arguments, simply creates an object where all of them variables are set to nothing values.

    For right now we only have constructors in our class. Next time, I will talk about other possible methods for our Student class, and subclasses that might inherit out Student class. Later

    PS- to read ahead, and check out the counter class we have been working on in class, you can find it here
    I know this was a little rushed, so if you have any questions, feel free to comment them or email me.

  • Coding

    This semester I am taking CompSci II, an advanced Java programming course at my school. So I figured I might as well live up to the name of my blog and talk about it. I am not planning on talking about CS I stuff- that is, basic programming- ifs, whiles, fors etc. unless I am asked. I’d also like you to keep in mind that I am still learning the CS II stuff, so if you see something that’s wrong or it doesn’t seem like I fully understand it, feel free to correct me in the comments.
    It’s tough to kinda find a starting point for this- so I guess I can go by class. Tonight we did error handling and exceptions. There are 3 types of errors- syntax, logic, and run time. Exception handling is for run time errors, which, if the program is not coded correctly, will make the program crash. This takes away from the robustness of the program, where robustness is the property of reacting “gracefully” (as opposed to “crashing”) when abnormal conditions.
    To prevent crashes, making the program more robust, we add exception handling in the form of a try/catch statement. a try/catch statement is set up like this:

    try{
    -code with possible bugs/errors/exceptions-
    }
    catch(excetionType1 name){
    -how you want to handle the error-
    }
    catch(excetionType2 name){
    -how you want to handle the error-
    }
    finally{
    -statements executed after try and catch sections-
    }

    The way this is set up, you put code in the try section. If at any time in that code an error occurs (stated in Java as “An exception is thrown”) it will check the catch statements looking for that exception to see how to handle it. Usually, (in my experiences) the exception is simply handled with a print statement stating the error. In the future, I’m sure we will look into how to use exception handling not only to state the error, but also allow the program to keep going, or revert back to before the error. The finally statement will be executed whether or not an exception is thrown.
    You can find a list of Java Exceptions here. Once my professor posts his notes on exception handling, I will post them here. Later

    Update: Here is that link like I promised.