2 Eylül 2011 Cuma

JAVA Robot Class

Nowadays, it's spectacularly popular to control something remotely. However Java Robot class isn't a new and cutting edge technology but you can do fantastic thing if you know how to use it.

Today, we are gonna build a very small app that will launch Notepad and types the text that we want.

Step 1 : let's open a new notepad file :

Runtime.getRuntime().exec("notepad.exe");

Step 2 : now type the keys:

Robot r=new Robot();
r.keyPress(KeyEvent.VK_T);


Actually that is just it but we can do some funny things. For instance lets add a waiting time so it will seem as if we are in Hollywood movies :)

Thread.sleep(500);

You can download pure and poor codes from right here :)


We will add a few extra features in near future, stay tuned and have fun..

9 Ağustos 2011 Salı

F1Racing JSF App (part 1: create database)




Let me guess, your brother-in-law is a F1 supporter and he wanted you develop a small F1Racing application that gives basic info about drivers and teams.
You visualised your app GUI and now start to design database.

As you can see, we have 3 simple tables. Lets create using sql codes:

create table drivers
(
id int not null auto_increment,
primary key(id),
driver varchar(50) not null,
point float

);
create table constructors
(
id int not null auto_increment,
primary key(id),
team varchar(50)
)

create table constructor_driver
(
id int not null auto_increment,
primary key(id),
constructor int,
driver int,
foreign key(constructor) references constructors(id),
foreign key(driver) references drivers(id)
);



now insert some data into tables.

insert into drivers (driver, point) values ('Kimi Raikkonen',50);
insert into constructors (team) values ('Ferrari');


Finally we have our tables as shown:



8 Ağustos 2011 Pazartesi

Develop a Simple JSF App (Population Calculator)





Let's say we want to develp a very simple web-based JSF app that is for calculation countries' population ratios.
Let's open a new Web Application on our favorite IDE Netbeans. I just named it as "BolgeOylamalari" in Turkish.
At beginning we should have look to our requirements and visualize GUI in our brain. We should be able to input populations to inputboxes and system calculates nd whenever result is ready they must be shown. As you can see on picture, I designed a very user-friendly one.
NOW, let the hack begin....

What we really need is to develop a ManagedBean and manipulate entities in that bean.


Here you can see our program hierarchy. Our Entity Class is Population.java, managed bean is BolgecontrollerBean.

And here we have how does it works :

Additionally here is the working url on local machine:



Finally we have source codes:



------------------------
BolgecontrollerBean.java
------------------------

@ManagedBean(name="bolgecontrollerBean")
@Dependent
public class BolgecontrollerBean {

private List populationList = new ArrayList ();
private boolean calculationFinished;

/** Creates a new instance of BolgecontrollerBean */
public BolgecontrollerBean() {

populationList.add(new Population("Turkey"));
populationList.add(new Population("Iraq"));
populationList.add(new Population("Kurdistan"));
populationList.add(new Population("Great Britain"));
populationList.add(new Population("USA"));
populationList.add(new Population("Norway"));
populationList.add(new Population("India"));
populationList.add(new Population("Israel"));
populationList.add(new Population("Pakistan"));
populationList.add(new Population("Japan"));
}

public boolean isCalculationFinished() {
return calculationFinished;
}

public void setCalculationFinished(boolean calculationFinished) {
this.calculationFinished = calculationFinished;
}

public List getPopulationList() {
return populationList;
}

public void setListe(List liste) {
this.populationList = liste;
}

public void cleanForm(){
for(Population p : populationList){
p.setPopulationCount(0);
p.setPopulationRatio(0);
}
}
public void calculateRatios(){
//let's first calculate the total population
double total = 0;
for(Population p : populationList){
total += p.getPopulationCount();
}

for(Population p : populationList){
p.setPopulationRatio(p.getPopulationCount() / total * 100);
}
setCalculationFinished(true);
}

}

---------------------
Population.java
---------------------


public class Population {

private String country;
private double populationCount;
private double populationRatio;

public Population() {
}
}

Finally you can download full source codes here. Have fun guys ! :):)

17 Temmuz 2011 Pazar

Telescoping Constructors

Lets say we have a entity class with a lot of fields. Some are mandatory while the rest are not. It would be very hard to write constructors for every combination. So we need to implement telescoping constructor pattern with Singleton. Senior developers are very familiar with this.
Here we have our class:

==============================================================
import java.util.Date;

/**
*
* @author Burhan ARAS
*/
public class Person {
//mandatory fields
private String fullname;
private String emailaddress;
//optional fields
private String address1;
private String country;
private String town;
private String gsm;
private String birthplace;
private Date birthdate;
private String gender;
private String contact;

public Person(String fullname, String emailaddress) {
this.fullname = fullname;
this.emailaddress = emailaddress;
}



public String getAddress1() {
return address1;
}

public void setAddress1(String address1) {
this.address1 = address1;
}

public Date getBirthdate() {
return birthdate;
}

public void setBirthdate(Date birthdate) {
this.birthdate = birthdate;
}

public String getBirthplace() {
return birthplace;
}

public void setBirthplace(String birthplace) {
this.birthplace = birthplace;
}

public String getContact() {
return contact;
}

public void setContact(String contact) {
this.contact = contact;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getEmailaddress() {
return emailaddress;
}

public void setEmailaddress(String emailaddress) {
this.emailaddress = emailaddress;
}

public String getFullname() {
return fullname;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getGsm() {
return gsm;
}

public void setGsm(String gsm) {
this.gsm = gsm;
}

public String getTown() {
return town;
}

public void setTown(String town) {
this.town = town;
}

@Override
public String toString() {
return "Person{" + "fullname=" + fullname + ", emailaddress=" + emailaddress + ", address1=" + address1 + ", country=" + country + ", town=" + town + ", gsm=" + gsm + ", birthplace=" + birthplace + ", birthdate=" + birthdate + ", gender=" + gender + ", contact=" + contact + '}';
}

public String toStringShort(){
return "Person{" + "fullname=" + fullname + ", emailaddress=" + emailaddress + '}';
}

public static class Builder{



private Person p;
public Builder(String fullname, String emailaddress) {
p = new Person(fullname, emailaddress);
}

public Builder contact(String str) {
p.setContact(str);
return this;
}

public Builder gsm(String str) {
p.setGsm(str);
return this;
}
public Builder email(String str) {
p.setEmailaddress(str);
return this;
}
public Builder birthplace(String str) {
p.setBirthplace(str);
return this;
}
public Builder birthdate(Date date) {
p.setBirthdate(date);
return this;
}
public Builder gender(String str) {
p.setGender(str);
return this;
}
public Builder address(String str) {
p.setAddress1(str);
return this;
}
public Builder country(String str) {
p.setCountry(str);
return this;
}
public Builder town(String str) {
p.setTown(str);
return this;
}
public Person build(){
return p;
}
}

}
======================================================================

And now, we can instancate a Person like this :

Person p = new Person.Builder("isi", "syss").contact("mail").address("adrs").country("").town("").gsm("").birthplace("").birthdate(new Date()).gender("").contact("").build();


have fun guys!!

23 Mart 2011 Çarşamba

video test