Showing posts with label SHORTCUTS. Show all posts
Showing posts with label SHORTCUTS. Show all posts

Saturday, February 2, 2013

Packages and Interfaces programs

                                                          Packages and Interfaces
 
13. Write a java program that illustrates the following:
(a) Creation of simple package.
(b) Accessing a package.
(c) Implementing interfaces.

Packages are a feature of Java that helps you to organize and structure your classes and their relationships to one another. A package is a grouping of related types providing access protection and name space management. Note that types refer to classes, interfaces and others. The types that are part of the Java platform are members of various packages that bundle classes by function: fundamental classes are in java.lang, classes for reading and writing (input and output) are in java.io, and so on. You can put your types in packages too.
For example, consider the following package specification:

package MyPack;

In order for a program to find MyPack, we use one of the two alternatives:

1.Program is executed from a directory immediately above MyPack, or

2.CLASSPATH must be set to include the path to MyPack.
SET CLASSPATH =C:\MyPack\
We create a directory, MyPack below the current development directory, put the .class files into the MyPack directory, and then execute the program from the development directory.

Program (a): A simple package example
 
package MyPack; // A simple package
class Balance
{
String name;
double bal;
Balance(String s, double b) // constructor
{ name = s;
bal = b;
}
void show() // method
  if(bal < 0)
        System.out.print("-->> ");
   System.out.println(name + ": Rs" + bal);
}
}
class AccountBalance
{
public static void main(String args[])
Balance current[] = new Balance[3];
current[0] = new Balance("R. Lepakshi", 15230.50);
current[1] = new Balance("K. Siva Kumar", 350.75);
current[2] = new Balance("Michael Jackson", -120.30);
for(int i = 0; i < 3; i++) current[i].show();
}
}

Let the current development directory be C:\java

Create (make directory: md) MyPack directory as follows:

C:\java>md MyPack
Edit the AccountBalance.java program and compile it from current development directory as follows:

C:\java>edit AccountBalance.java

C:\java>javac AccountBalance.java

Then, copy Balance.class and AccountBalance.class files from the directory C:\java to the directory C:\java\MyPack

Execute the AccountBalance class, using the following command line:
C:\java>java MyPack.AccountBalance

AccountBalance is now part of the package MyPack. This means that it cannot be executed by itself. That is, we cannot use this command line:
C:\java>java AccountBalance

AccountBalance must be qualified with its package name.

In Java, an interface is a reference type, similar to a class that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated - they can only be implemented by classes or extended by other interfaces.
Here is an example of an interface definition (defining an interface is similar to creating a new class). Program 13(b) declares an interface called Items which contains four methods. Note that the method signatures have no braces and are terminated with a semicolon.

Program (b): Defining an interface Items

interface Items
   boolean equal(); // test whether two numbers are equal
  int add(); // sum of two numbers
  int max(); // maximum of two numbers
  void display(); // print two numbers
}

To declare a class that implements an interface, you include an implements clause in the class declaration. Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. Here is an example class Client that implements the Items interface.

Program (c): Implementing an interface Items
 
class Client implements Items
{
int a, b;
Client( int i, int j) // constructor
{ a = i; b = j; }
public boolean equal()
{ return (a == b); }
public int add()
{ return(a + b); }
public int max()
{ if( a > b ) return a;
else return b;
}
public void display()

{
  System.out.println(a + " " + b);
 }
public int multiply() // non-interface method
    return(a * b); }
}

Program (d): Testing a class which implements an interface

class ClientDemo
{
 public static void main(String[] srgs)
{
  Client ob = new Client(5, 7);
 ob.display();
if( ob.equal())
  System.out.println("Two numbers are equal");
else
  System.out.println("Two numbers are not equal");
  System.out.println("Sum of two numbers is " + ob.add());
  System.out.println("Max of two numbers is " + ob.max());
 System.out.println("Product of two numbers is " + ob.multiply());
}
}

Output of this program is:
 
5 7

Two numbers are not equal
Sum of two numbers is 12
Max of two numbers is 7
Product of two numbers is 35

Friday, January 11, 2013

Eclipse 30 Key Board Shortcuts

  • This tutorial is about 30 Eclipse keyboard shortcuts, this list is by no means complete and I will suggest you guys to share eclipse shortcuts listed other than here to make this more useful.Eclipse is most used Java development IDE and knowing Eclipse shortcuts not only improve your productivity but also makes you more efficient, you will have more time for things you like to do. 
  • Using keyboard shortcuts also helps to impress colleagues and shows that you have good hold on tools you used for Java Development. 
  • Netbeans provide sophisticated IDE environment to enable you to build, debug and run your Java application in various emulator including some advanced processing options e.g. preprocessing, setting up Exception break-point etc.
  • From that time I love to know keyboard shortcuts on IDE and other tools I used for development including Edit Plus, Microsoft Excel etc.

 Eclipse was most suited for those application given some of cool feature of eclipse e.g. Remote Debugging, Conditional Breakpoints, Exception breakpoints and Ctrl+T and Ctrl+R kind of shortcuts.

 Here I am sharing list of 30 Eclipse keyboard shortcuts which I found most useful and using in my day to day life while writing code, reading code or debugging Java application in Eclipse. 

Eclipse Keyboard Shortcuts 

 1) Ctrl + T for finding class even from jar This keyboard shortcut in Eclipse is my most used and favorite shortcut. While working with high speed trading system which has complex code I often need to find classes with just blink of eye and this eclipse keyboard shortcut is just made for that. No matter whether you have class in your application or inside any JAR, this shortcut will find it.

 2) Ctrl + R for finding any resource (file) including config xml files This is similar to above Eclipse shortcut with only difference that it can find out not only Java files but any files including xml, configs and many others, but this eclipse shortcut only finds files from your workspace and doesn’t dig at jar level. 

3) Ctrl + 1 for quick fix This is another beautiful Eclipse shortcut which can fix up any error for you in Eclipse. Whether it’s missing declaration, missing semi colon or any import related error this eclipse shortcut will help you to quickly short that out. 

4) Ctrl + Shift + o for organize imports Another Eclipse keyboard shortcut for fixing missing imports. Particularly helpful if you copy some code from other file and what to import all dependencies. 

Eclipse Shortcut for Quick Navigation In this section we will see some eclipse keyboard shortcut which helps to quickly navigate within file and between file while reading and writing code in Eclipse.

 7) Ctrl + o for quick outline going quickly to method 

  9) Alt + right and Alt + left for going back and forth while editing.

 12) Alt + Shift + W for show in package explorer

 13) Ctrl + Shift + Up and down for navigating from member to member (variables and methods) 15) Ctrl + k and Ctrl + Shift +K for find next/previous

 24) Go to a type declaration: F3, This Eclipse shortcut is very useful to see function definition very quickly. 


Eclipse Shortcut for Editing Code These Eclipse shortcuts are very helpful for editing code in Eclipse. 

5) Ctrl + / for commenting, un commenting lines and blocks 

6) Ctrl + Shift + / for commenting, un commenting lines with block comment 

8) Selecting class and pressing F4 to see its Type hierarchy 

10) Ctrl + F4 or Ctrl + w for closing current file 

11) Ctrl+Shirt+W for closing all files. 

14) Ctrl + l go to line 

16) Select text and press Ctrl + Shift + F for formatting.

 17) Ctrl + F for find, find/replace 

18) Ctrl + D to delete a line

 19) Ctrl + Q for going to last edited place


 Miscellaneous Eclipse Shortcuts These are different Eclipse keyboard shortcuts which doesn’t fit on any category but quite helpful and make life very easy while working in Eclipse. 

20) Ctrl + T for toggling between super type and subtype 

21) Go to other open editors: Ctrl + E. 

22) Move to one problem (i.e.: error, warning) to the next (or previous) in a file: Ctrl +. For next, and Ctrl +, for previous problem

 23) Hop back and forth through the files you have visited: Alt + ? and Alt + ?, respectively.

 25) CTRL+Shift+G, which searches the workspace for references to the selected method or variable 

26) Ctrl+Shift+L to view listing for all Eclipse keyboard shortcuts.

 27) Alt + Shift + j to add javadoc at any place in java source file. 

28) CTRL+SHIFT+P to find closing brace. Place the cursor at opening brace and use this.

 29) Alt+Shift+X, Q to run Ant build file using keyboard shortcuts in Eclipse.

 30) Ctrl + Shift +F for Auto formating. Please update us if anything is missing or posted wrong.. No related posts.