Menu

Friday, 25 September 2015

Java concept (Stack Implementation)

Using Stack To reverse Input String


/*
 * To change this license header, choose License Headers in Project  *Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package stackimplementation;

import java.util.Scanner;

/*
  @author Adeniran Adetunji
 */

/* 
This program uses stack implementation to reverse a string e.g. Input "CAT" will output "TAC"
*/

public class StackImplementation {
   
/* Declare class variables Top: which keeps track the top of the stack, Size: the size of the stack that is predetermined, stack implementationwithan array "myStack".
*/

    int size;
    char [] myStack;
    int top;
    
    //set stack size to accomodate the string input
    
    void setSize(String n) 
   {
             
       int stackSize = n.length();
       
       size = stackSize;
       myStack = new char [size];
       top = myStack.length;
    }
    
    //push string character-by-character into stack
    void push(String myWord) 
   {
       
       if (top<=0)
       {
           System.out.println("Stack is full");
       }
       else
       {
       for (int i = 0; i<myWord.length(); i++)
       {
           
             myStack[top-1]=myWord.charAt(i);
           
           top--; //update top
       }
       
   }
   }
    
    //POP string in Last-in-first-out order from stack
     String pop()
    {
      String outString="";
      
      if (top==myStack.length)
      {
      System.out.println("Stack is Empty, Enter String to reverse");
      }
       for (int i= 0; i<myStack.length; i++) 
       {
           outString = outString+myStack[i];
           
           top++;
       }
      
     
      return outString;
    }
    

     //reverse string using push and pop methods
     String reverse(String n)
     {
         setSize(n);
         push(n);
         n = pop();
         
         return n;
         
     }
   
     /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
       String str;  //Declaratio of string to be reversed
       
       Scanner read = new Scanner(System.in);  
       
       System.out.println("Enter String Input: ");
       str = read.nextLine();  //read string from keyboard input
       
       //Declare a new stackImplementaton object
      StackImplementation reverseString = new StackImplementation();
       
       //reverse string input using stackImplementation
       str = reverseString.reverse(str);
       
       //Prints input string in reverse order   
       System.out.println(str);
          
   }
        
    }


OUTPUT:

run:

Enter String Input: 

Tunji

ijnuT

BUILD SUCCESSFUL (total time: 3 seconds)


    
Please comment or suggest better implementation: Tunji4physics2@gmail.com

Wednesday, 23 September 2015

Java Concept

Array List: Insertion and Deletion Mechanism

Code:
/*
 * This is a simple program that enable dynamic deletion and.
 * insertion of data element from and into an array object.
 */
package arraydatastructure;

import java.util.Arrays;

/**
 *
 * @author Adeniran Adetunji
 */
public class Insert_Delete_from_Array {
    
//This method delete element at position 'pos' from and array 'data'


 public int [] deleteElement (int[]data, int pos)
{
    //check if postion 'pos' is within the length of the array
    
    if(pos >= 0 && pos <data.length)
{
            
    int[] dataCopy = new int[data.length-1];
    
   
    System.arraycopy (data, 0, dataCopy, 0, (pos-1));
    
    System.arraycopy(data, pos,dataCopy, pos-1, 
                      dataCopy.length-(pos-1));
    
    data=dataCopy;
    
       
    }
    return data;
    }
     
 //Method to insert data element into an array

    public int [] InsertElement (int[]dataA,int data, int pos)
{
    
    if(pos >= 0 && pos <dataA.length)
    {
        
         int[]dataCopy= new int[dataA.length+1];
    
     //adding element to array
    
    System.arraycopy(dataA, 0, dataCopy, 0, pos-1);
    
    dataCopy[pos-1] = data;
    
    System.arraycopy(dataA, pos-1, dataCopy, pos, 
                     dataA.length-(pos-1));
    
    dataA = dataCopy;
    }
    
    return dataA;
  
}
  // Testing the methods in the 'main' method
      public static void main(String args[]) {
          
          //creating array 'a' with data 1,2,3,4,5,6,7,8,9,10
          int[]a = {1,2,3,4,5,6,7,8,9,10};
          
          //converting array 'a' to string and print
          System.out.println(Arrays.toString(a));
          
          //creating and object of the type Insert_Delete_from_Array
          
          Insert_Delete_from_Array obj = new                                   Insert_Delete_from_Array();
          
          //delet element at postion 7 from array a
          a = obj.deleteElement(a, 7);
          
          //Convert array 'a' to string and print
          System.out.println(Arrays.toString(a));
          
          //insert '21' into postiont '8' in array 'a'
          a = obj.InsertElement(a, 21, 8);
          
          System.out.println(Arrays.toString(a));
          
          
      }
}

Output:

run:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

[1, 2, 3, 4, 5, 6, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 8, 21, 9, 10]
BUILD SUCCESSFUL (total time: 9 seconds)



Saturday, 12 September 2015

Introductio to Logic and Number System Tutorial Questions

Tutorial Questions 


(a) What is the largest binary number that can be expressed with 24 bits? What are the equivalent decimal and hexadecimal number?
(b)   Determine the base/radix of the numbers in each case for the following operations to be correct:
         (i)  29 + 17 = 40    (ii) 7 + 6  = 15    (iii)  67 + 24 = 113
(c)  Covert the hexadecimal number AD54 to binary, and then convert it from binary to octal
(d) Convert the decimal number 198 to binary in two ways: (a) convert directly to binary; (b) convert first to hexadecimal and then from hexadecimal to binary. Which method is faster?
(e) Express the following numbers in decimal:

         (i)  (10110.0101)2         (ii)  (69.8)16     (ii)  (73.65)   (iv)  (1110.0101)2   (v)  (BEBE.C)16
(f) Differentiate between Analog and Digital electronic system with respect to the following concept;  i.                    Signal   ii.  Representation of value.   iii.   Technology/Technique   iv.  Response to noisev.  Uses/Application.   
(g)  Number systems are characterized by the following;
            i.   The number of independent digits used in the number system.
            ii.  The place value of different digits constituting a value in the system.
            iii.  The maximum number that can be written with a given number of digits. Explain these    characteristics with respect to the following number systems using arbitrary examples;
           1.    Decimal   2.  Octal   3.  Hexadecimal   4.  Binary.                                                                     
(h)  Determine the base/radix (number system) of the number in each case for the following operation to be correct.
i.                    29+17 = 40 
ii.                  7+6 = 15 
iii.                67+24+133
(i) Solve each of the following using comfortable number of bits subtraction problems using 2’s complement arithmetic.
        i.  011111112 - 7810 
       ii.  001100102 - 12310 
       iii.   010010012 - 11110
      iv.   (3E91)16 – (1DEF)16 
(j) Write out the expression for the Boolean function bellow;





(k)  Simplify the expression in (i), and hence implement the logic circuit for the simplified       expression.
(l) Show that;
 , using perfect induction method.


(m)  In a scenario to design a bank vault lock; there are three (3) key holders to the lock; the manager, senior bank officer, and a cashier. To open the vault, at least two (2) keys  (i.e. two key holders) must be available. If the cashier is to open the vault, the managers’ key must be available. Design a logic circuit to implement the vault lock following these procedure;
       1. Define the input and output variables.
       2. Obtain the Boolean expression for the lock
       3. Simplify the expression obtained in (ii)
       4. Design the logic circuit for the vault lock.