Tuesday, September 20, 2011

Sequence Diagram

illustrate the objects that participate in a use case and the messages that pass between them over time for one use case
  • can be a generic sequence diagram that shows all possible scenarios for a use case,
  • but usually each analyst develop a set of instance sequence diagrams, each of which depicts a single scenario within the use case.

*Remember that a scenario is a single executable path through a use case.


Example :

Facebook User Authentication in a Web Application

An example of sequence diagram which shows how Facebook (FB) user could be authenticated in a web application to allow access to his/her FB resources. Facebook uses OAuth 2.0 protocol framework which enables web application (called "client"), which is usually not the FB resource owner but is acting on the FB user's behalf, to request access to resources controlled by the FB user and hosted by the FB server. Instead of using the FB user credentials to access protected resources, the web application obtains an access token.

Web application should be registered by Facebook to have an application ID (client_id) and secret (client_secret). When request to some protected Facebook resources is received, web browser ("user agent") is redirected to Facebook's authorization server with application ID and the URL the user should be redirected back to after the authorization process.

User receives back Request for Permission form. If the user authorizes the application to get his/her data, Facebook authorization server redirects back to the URI that was specified before together with authorization code ("verification string"). The authorization code can be exchanged by web application for an OAuth access token.





Sequence diagram example - Facebook User Authentication in a Web Application.

If web application obtains the access token for a FB user, it can perform authorized requests on behalf of that FB user by including the access token in the Facebook Graph API requests. If the user did not authorize web application, Facebook issues redirect request to the URI specified before, and adds the error_reason parameter to notify the web application that authorization request was denied.

Adapter from http://www.uml-diagrams.org/sequence-diagrams-examples.html

Sunday, September 18, 2011

next() and nextLine()

When I was a newbie programmer, I used to be frustrated a lot every time I used the Scanner class. At the time, it seemed like it behaved erratically and no matter how much debugging I did, it was always wrong.

The mistake I was making was that I failed to realize that whenever I used a .next call, like nextInt or nextDouble, the Scanner would stay on the same line. If then, I wanted to scan a new input that was on a different line, the Scanner will appear to skip it. What happens is that the Scanner is actually scanning the invisible newline character!

So, whenever you want to Scan numbers with a .next() call, make sure you add an empty .nextLine() at the end so that the Scanner moves to the correct position.




import java.util.Scanner;

class myClass {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
double grade=0;
String name=null;
System.out.print("Please enter the grade: ");
grade = scn.nextDouble(); //Could also be scn.next(), scn.nextInt(), and so on
//Add an empy .nextLine() to skip the invisible newline character
scn.nextLine(); //Try commenting this line out with '//' to see what happens
System.out.print("Please enter name: ");
name = scn.nextLine();
System.out.println("Name: "+name+", grade: "+grade);
}
}


Sample run without the empty scn.nextLine:


Please enter the grade: 76
Please enter name: Name: , grade: 76.0


Sample run with the empty scn.nextLine:


Please enter the grade: 100
Please enter name: Rommel
Name: Rommel, grade: 100.0



From: http://rommelrico.com/java-scanner-not-working-difference-between-next-and-nextline/

Dialog


import javax.swing.JOptionPane;
public class FirstDialog
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "First dialog");
System.exit(0);
}
}

// The number indicate the return value

// 0. Display an ERROR message.
JOptionPane.showMessageDialog(null, "Invalid Data", "My Message Box",
JOptionPane.ERROR_MESSAGE);
// 1. Display an information message.
JOptionPane.showMessageDialog(null, "Information Message", "Information",
JOptionPane.INFORMATION_MESSAGE);
// 2. Display a warning message.
JOptionPane.showMessageDialog(null, "Warning Message", "Warning",
JOptionPane.WARNING_MESSAGE);
// 3. Display a question message.
JOptionPane.showMessageDialog(null, "Question Message", "Question",
JOptionPane.QUESTION_MESSAGE);
// -1. Display a plain message.
JOptionPane.showMessageDialog(null, "Plain Message", "Message",
JOptionPane.PLAIN_MESSAGE);

Saturday, September 17, 2011

Access Modifier

Protected members of class:

may be directly accessed by methods of the same class or methods of a subclass. Protected members may be accessed by methods of any class that are in the same package.

Java provides a third access specification, protected.

A protected member’s access is somewhere between private and public.

Using protected instead of private makes some tasks easier.

However, any class that is derived from the class, or is in the same package, has unrestricted access to the protected member.

It is always better to make all fields private and then provide public methods for accessing those fields.

If no access specifier for a class member is provided, the class member is given package access by default.

Any method in the same package may access the member.

Access Modifier

Accessible to a subclass inside the same package?

Accessible to all other classes inside the same package?

default
(no modifier)

Yes

Yes

Public

Yes

Yes

Protected

Yes

Yes

Private

No

No

Access Modifier

Accessible to a subclass outside the package?

Accessible to all other classes outside the package?

default
(no modifier)

No

No

Public

Yes

Yes

Protected

Yes

No

Private

No

No

Default modifier = package?

Center Frame


import java.awt.*;

public class CenterFrame // a utility class
{
public static Point getPosition(int frameWidth, int frameHeight)
{
// frame size is width * height
// returns a Point holding the coordinates of
// the upper left-hand corner of the (centered) frame

Toolkit toolkit = Toolkit.getDefaultToolkit(); //a static method of the Toolkit class
Dimension dimensions =toolkit.getScreenSize(); //Get Dimension from the Screen Size
int x = (dimensions.width - frameWidth)/2; // x coordinate of upper left corner
int y = (dimensions.height - frameHeight)/2; // y coordinate of upper left corner
return (new Point(x,y)); // return coordinates as a Point object
}
}




Testing 123

import java.awt.*; public class CenterFrame // a utility class { public static Point getPosition(int frameWidth, int frameHeight) { // frame size is width * height // returns a Point holding the coordinates of // the upper left-hand corner of the (centered) frame Toolkit toolkit = Toolkit.getDefaultToolkit(); Dimension dimensions =toolkit.getScreenSize(); int x = (dimensions.width - frameWidth)/2; // x coordinate of upper left corner int y = (dimensions.height - frameHeight)/2; // y coordinate of upper left corner return (new Point(x,y)); // return coordinates as a Point object } }