SSH protocol is used to exchange data between two networked devices using a secure channel. Ubuntu server machines with ssh installed on them can be reached from other computers on the network. Ubuntu's synaptic package manager easily installs ssh for your server machine. In order to reach remote ubuntu server machines with ssh installed on them, type the following command from command line of the client machine:
$ ssh remoteUserName@remoteMachineIPAddress
replace remoteUserName and remoteMachineIPAddress with appropriate values. Then you will see that it is asking for adding to the list of known hosts. write 'yes' for confirmation. Then type following from the console window and press enter, the remote machine will shutdown.
$ sudo shutdown -h now
Friday, October 9, 2009
Tuesday, September 29, 2009
equals(Object obj) and hashCode() methods in Java
Comparing two different objects is one of the mostly achieved tasks for object oriented Java developers. In order to handle this requirement, it is important to understand what makes two different objects in Java equals to each other. For this purpose java.lang.Object class includes equals(Object obj) and hashCode() methods. Main criteria for determining whether different two objects are equal to each other depends actually upon the application's business logic. So it depends on the significance of fields in your Java classes.
Implementation details for the equals(Object obj) method of java.lang.Object class involve deciding about firstly for the types of objects being compared. If two objects are of different types, then the equals(Object obj) method returns false. Else if the compared object can be casted to the current object, it becomes the time for checking the equality of fields in objects.
Another method which is helpful for object comparison in Java is the hashCode() method of java.lang.Object class. hashCode() and equals(Object obj) methods of java.lang.object class are related to each other because if two objects are equal so they are required to generate the same hash code for their common fields. It is suggested to also override hashCode() method of java.lang.Object class whenever you override equals(Object obj) method of java.lang.Object class.
If you are using eclipse for Java development, then ide generates equals(Object obj) and hashCode() methods for you automatically. In order to generate equals(Object obj) and hashCode() methods for your Java classes in eclipse ide, right click in your .java source file and choose from context menu "Source --> Generate hashCode() and equals()" so ide generates hashCode() and equals(Object obj) methods for you.
When you run this java class, console output becomes:
personOne and personTwo are equal!
personOne and personThree are not equal!
Implementation details for the equals(Object obj) method of java.lang.Object class involve deciding about firstly for the types of objects being compared. If two objects are of different types, then the equals(Object obj) method returns false. Else if the compared object can be casted to the current object, it becomes the time for checking the equality of fields in objects.
Another method which is helpful for object comparison in Java is the hashCode() method of java.lang.Object class. hashCode() and equals(Object obj) methods of java.lang.object class are related to each other because if two objects are equal so they are required to generate the same hash code for their common fields. It is suggested to also override hashCode() method of java.lang.Object class whenever you override equals(Object obj) method of java.lang.Object class.
If you are using eclipse for Java development, then ide generates equals(Object obj) and hashCode() methods for you automatically. In order to generate equals(Object obj) and hashCode() methods for your Java classes in eclipse ide, right click in your .java source file and choose from context menu "Source --> Generate hashCode() and equals()" so ide generates hashCode() and equals(Object obj) methods for you.
package util;
public class Person {
private int id;
private String name;
public Person(int id,String name){
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Person))
return false;
Person other = (Person) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static void main(String[] args) {
Person personOne = new Person(1,"Alice");
Person personTwo = new Person(1,"Alice");
Person personThree = new Person(1,"Jack");
if(personOne.equals(personTwo)){
System.out.println("personOne and personTwo are equal!");
}
if (personOne.equals(personThree)) {
System.out.println("personOne and personThree are equal!");
} else {
System.out.println("personOne and personThree are not equal!");
}
}
}
When you run this java class, console output becomes:
personOne and personTwo are equal!
personOne and personThree are not equal!
Saturday, July 25, 2009
Implementing CommandListener Interface in J2ME Applications
Classes that implement CommandListener interface, which has a single method: commandAction(Command com, Displayable dis), have the abilitiy to trace command click actions. In order to transfer command information to a listener, the listener is registered with the method setCommandListener(CommandListener listener). In the sample code at below, the command listener in this case is the form class itself which is implementing the commandAction() method.
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
public class ConfirmationPopUpForm extends Form implements CommandListener {
private Command cmdPreviousPage;
private Command cmdExit;
public ConfirmationPopUpForm(){
this("Confirmation Form");
}
public ConfirmationPopUpForm(String title){
super(title);
cmdPreviousPage = new Command("Return",Command.CANCEL, 0);
addCommand(cmdPreviousPage);
cmdExit = new Command("Exit",Command.OK, 1);
addCommand(cmdExit);
this.setCommandListener(this);
}
public void commandAction(Command cmd, Displayable display) {
if(cmd == cmdExit){
exitAction();
}
else if(cmd == cmdPreviousPage){
showPreviousFormAction();
}
}
private void showPreviousFormAction() {
MainMenuForm.show();
}
private void exitAction() {
StarterForm.show();
}
}
Subscribe to:
Posts (Atom)