Skip to main content

Posts

Data Structure and Algorithms : Queue Program in Java

Program: public class queuepgm { private int maxSize; private long[] queArray; private int front; private int rear; private int nItems; public queuepgm(int s) { maxSize = s; queArray = new long[maxSize]; front = 0; rear = -1; nItems = 0; } public void enqueue(long j) { if(rear == maxSize-1) rear = -1; queArray[++rear] = j; nItems++; } public long dequeue() { long temp = queArray[front++]; if(front == maxSize) front = 0; nItems--; return temp; } public long peekFront() {     return queArray[front]; } public boolean isEmpty()       { return (nItems==0); } public boolean isFull() { return (nItems==maxSize); } public int size() { return nItems; } public static void main(String[] args) { queuepgm theQueue = new queuepgm(5); // queue holds 5 items theQueue.enqueue(10); theQueue.enqueue(20); theQueue.enqueue(30); theQueue.enqueue(40); System.out.println("Dequeue Values: FIFO"); while( !theQueue.isEmpty() ) ...

Data Structure and Algorithms : Stack Program in Java

Program public class stackpgm { private int maxSize; private long[] stackArray; private int top; public stackpgm(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; } public void push(long j) {     if(!isFull())     {       stackArray[++top] = j;     } } public long pop() {           return stackArray[top--];             } public long peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize-1); } public static void main(String[] args) { stackpgm theStack = new stackpgm(10); theStack.push(1); theStack.push(2); theStack.push(3); theStack.push(4); System.out.println("Pop up values"); while( !theStack.isEmpty() ) { long value = theStack.pop(); System.out.print(value+"\n"); } } } Output

Head and Tail Commands in Linux

Head Command Description : prints output of first part of the file by default it will print first 10 lines Syntax : head filename examples: 1.  head upcasting.java it will print first 10 lines of the file upcasting.java by default. 2. head -n 50 upcasting.java  it will print first 50 lines of the file upcasting.java. 3. head upcasting.java downcasting.java it will print first 10 line of the 2 files upcasting.java and downcasting.java. 4. head *.java it will print all the java files first 10 lines. 5. head *.java *.class it will print all the java and class files first 10 lines. Tail Command: Description : prints output of last part of the file by default it will print last 10 lines Syntax : tail filename Examples: 1. tail upcasting.java it will print last 10 lines of the file upcasting.java by default. 2.  tail -n 50 upcasting.java  it will print last 50 lines of the file upcasting.java. 3.  tail ...

Linux Terminal Commands

Install Command sudo apt-get install <package-name> Example: Installing vlc media player sudo apt-get install vlc ls Command ls -l list folders and files with read write permissions and user groups ls -a This command shows hidden files and folders ls -al ls -la Clear Command clear this command clears the screen Manual Command man <command> this command shows manual of the commands example man ls shows manual command of ls q to quit the manual screen back to terminal commands ifconfig command ifconfig command ip network address details netstat command netstat command used to show network status like ip addresses of local and foreign; port, pid, network protocols cat Command   cat  command  concatenate files and print on the standard output.

Java Program: Down casting Inheritence

Program public class downcasting {    public static void main(String[] args)     {      fordd f;      benzz b;      BMWw bm;      carr c,cc,ccc;      c=new fordd();      cc=new benzz();      ccc=new BMWw();      f=(fordd)c; // downcasting or narrowing or specialization      b=(benzz)cc;      bm=(BMWw)ccc;      f.car();      b.car();      bm.car();     } } class carr {     void car()     {         System.out.println("This super class car method");     } } class fordd extends carr {     void car()     {         System.out.println("This sub class Ford car method");     } } class benzz extends carr {      void car() ...

Java Program: Up casting Program in inheritance

Program public class upcasting {    public static void main(String[] args)     {      car c,cc,ccc,o;      o=new car();      o.car(); //no casting      c=(car)new ford(); //upcasting ford sub class to super class      c.car();      cc=(car)new benz();//upcasting ford sub class to super class      cc.car();      ccc=(car)new BMW();//upcasting ford sub class to super class            ccc.car();         } } class car {     void car()     {         System.out.println("This super class car method");     } } class ford extends car {     @Override     void car()     {         System.out.println("This sub class Ford car method");     } } class benz extends car { ...

Java Program: Inheritance Super Keyword

Program class inheritance {  public static void main(String[] args)  {   apple a=new apple();   a.disp();  } } class fruit {  String f="fruit";  public void disp()  {   System.out.println("Base Class "+f);  } } class apple extends fruit {  String f="Apple";  public void disp()  {   System.out.println("Derived Class  "+f);   super.disp();   System.out.println("Super Class "+super.f);  } } Output