Saturday, 25 February 2017

Array2D

import java.util.Scanner;

public class Runner {
static NewArray2D a = new NewArray2D();

protected static int rows;
protected static String val;
protected static int r;
protected static int c;

public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("Enter Number of records:");
rows=in.nextInt();

a.create(rows, 5);

System.out.println();

a.put(0, 0, (String) "Customers Name");
a.put(0, 1, (String) "Brand Preffered");
a.put(0, 2, (String) "Avg. Monthly shopping Bill");
a.put(0, 3, (String) "Monthly Income");
a.put(0, 4, (String) "Monthly Expenses");

for( r=1; r<rows; r++){
for( c=0; c<5; c++){

System.out.println("Enter " + c +" record");
val=in.next();

if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c, val);System.out.println();}

else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");System.out.println();}

else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}
else
{System.out.println("Invalid input");}
}
}

System.out.println(a.toString());

System.out.println();
System.out.println("Do you want to change any block?:" + " " + "(Y/N)");
char choice=in.next().charAt(0);

if(choice=='Y'|| choice=='y'){
System.out.println("Which block do you want to edit:");
System.out.println("Row: "); r=   in.nextInt();
System.out.println("Column: ");  c=   in.nextInt();
System.out.println("Value: ");   val= in.next();

if(c<2 && Character.isLetter(val.charAt(0)))
{a.put(r, c,val );System.out.println();}

else if(c<2 && Character.isDigit(val.charAt(0)))
{System.out.println("Invalid input");
System.out.println();}

else if(c>=2 && Character.isDigit(val.charAt(0)))
{a.put(r, c, val);System.out.println();}

else
{System.out.println("Invalid input");}
}

if(choice=='N' || choice=='n')
{System.exit(0);}
System.out.println(a.toString());
in.close();

}
}


public class NewArray2D {
protected int tcols;
protected int trows;
protected Object[] elem;

public NewArray2D(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();

elem=new Object[row*col];
tcols=col;
trows=row;
}

public NewArray2D() {
trows=tcols=0;
}

public void create(int row,int col) {
if(row<1||col<1)throw new IllegalArgumentException();

elem=new Object[row*col];
tcols=col;
trows=row;
}

public void checkIndex(int row,int col){
if(row<0||col<0)throw new IndexOutOfBoundsException();
}

public void put(int row,int col,Object telem){
checkIndex(row, col);
elem[row*tcols+col]=telem;return;
}

public Object get(int row,int col){
checkIndex(row, col);
return elem[row*tcols+col];
}

public Object remove(int row,int col){
checkIndex(row, col);
Object ob=elem[row*tcols+col];
elem[row*tcols+col]=null;
return ob;


}
public int nrows(){return trows;}
public int ncols(){return tcols;}

public String toString(){
StringBuffer s= new StringBuffer("[");

for(int r=0;r<trows;r++){
for(int c=0;c<tcols;c++){
if(elem[r*tcols+c]==null)s.append("_, ");
else s.append(elem[r*tcols+c].toString()+", ");
}
s.delete(s.length()-2, s.length());
s.append(" # ");
}
if(trows>0&&tcols>0)
s.delete(s.length()-2, s.length());

s.append("]");
return new String (s);
}
}

Generic Array2D


public class Array2D <T>{
protected int trows;
protected int tcols;
protected T[] elements;


@SuppressWarnings("unchecked")
public Array2D( int rows, int cols )
{

if( rows<1 || cols<1 )
throw new IllegalArgumentException ("initialCapacity must be >= 1");

elements =(T[]) new Object[rows*cols];
trows = rows;
tcols = cols;
}

public Array2D()
{
trows = tcols = 0;
}

@SuppressWarnings("unchecked")
public void create( int rows, int cols )
{
if( rows<1 || cols<1 )
throw new IllegalArgumentException ("initialCapacity must be >= 1");

elements = (T[]) new Object[rows*cols];
trows = rows;
tcols = cols;
}

void checkIndex(int row, int col)
{
if( row < 0 || row>=trows || col < 0 || col>=tcols )
throw new IndexOutOfBoundsException ("Row: "+ row + ", Range: 0 - "+trows+"Col: "+ col + ", Range: 0 - "+tcols);
}

public void put( int row, int col, T theElement )
{
checkIndex(row, col);

elements[row*tcols+col] = theElement;
return;
}

public T get( int row, int col )
{
checkIndex(row, col);
return
(T)elements[row*tcols+col];
}


public Object remove( int row, int col )
{
checkIndex(row, col);

Object ob = elements[row*tcols+col];
elements[row*tcols+col] = null;

return ob;
}

public int nrows()
{
return trows;
}

public int ncols()
{
return tcols;
}

public String toString()
{
StringBuffer s = new StringBuffer("[");

for (int r=0; r<trows; r++) {
for (int c=0; c<tcols; c++) {
if(elements[r*tcols+c] ==null)
s.append("_, ");
else s.append(elements[r*tcols+c].toString() + ", ");
}

s.delete(s.length() -2, s.length());
s.append(" # ");
}

if(trows>0 && tcols>0)
s.delete(s.length() -2, s.length());

s.append("]");
return new String(s);
}
}

import java.util.Scanner;

public class Runner {
public static void main(String[] args) {
int i, j;

int tRows;
int tCols;

Array2D<Double> Ob1;
Array2D<Double> Ob2;
Array2D<Double> Ob3;

Scanner s = new Scanner(System.in);

System.out.print("Rows   : "); tRows = s.nextInt();
System.out.print("Columns: "); tCols = s.nextInt();

Ob1=new Array2D<Double>( tRows, tCols );
Ob2=new Array2D<Double>( tRows, tCols );
Ob3=new Array2D<Double>( tRows, tCols );

System.out.println( "--------------------" );
System.out.println( "Enter values for first matrix" );

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob1.put( i, j,  s.nextDouble() );
}
}

System.out.println("--------------------");
System.out.println( "Enter values for second matrix" );

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob2.put( i, j,  s.nextDouble() );
}
}

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
Ob3.put( i, j, Ob1.get(i,j)+ Ob2.get(i,j) );
}
}


System.out.println("---Results----");

for( i=0 ; i<tRows ; i++ )
{
for( j=0 ; j<tCols ; j++ )
{
System.out.print( Ob3.get(i,j)+"\t" );
}
System.out.println();
}

s.close();
}
}

Saturday, 18 February 2017

Guess Game

#include <stdlib.h>
#include <iostream>
#include <time.h>

using namespace std;
int main()
{
srand(time(NULL));  
int number=rand()%100;
int guess=-1;
int trycount=0;
while(guess!=number && trycount<8)

{
cout<<"Please enter a guess: ";
cin>>guess;

if(guess<number)
cout<<"Too low"<<endl;

if(guess>number)
 cout<<"Too high"<<endl;

trycount++;
}
if(guess==number)
cout<<"You guessed the number";
else
cout<<"Sorry, the number was: "<<number;
return 0;
}

Tuesday, 14 February 2017

Array Manipulation (C++)


#include <iostream>    
      using namespace std;
      int main()
{
  int array[3];
  for(int x=0; x<3; x++){
  cin>>array[x];
  }
  for (int loop=0;loop<3 ; loop++){
  cout<<array[loop];if(loop<2){cout<<",";}
 
}
return 0;
}

Sunday, 5 February 2017

Pime Number

#include<stdio.h>

int main()
{
   int n, i = 3, count, c;

   printf("Enter the number of prime numbers required\n");
   scanf("%d",&n);

   if ( n >= 1 )
   {
      printf("First %d prime numbers are :\n",n);
      printf("2\n");
   }

   for ( count = 2 ; count <= n ;  )
   {
      for ( c = 2 ; c <= i - 1 ; c++ )
      {
         if ( i%c == 0 )
            break;
      }
      if ( c == i )
      {
         printf("%d\n",i);
         count++;
      }
      i++;
   }

   return 0;
}

Thursday, 2 February 2017

Bookclub

Bookclub is available on the following link:

https://drive.google.com/open?id=0B5D2vtZ05oIsQmY5NUpoMlBmYmc

Payroll

Payroll system project is available on the following link:

https://drive.google.com/open?id=0B5D2vtZ05oIsa0ZnTVJER1RFbUU