SQL

Wednesday, 21 May 2014

Separate integers, i.e 67890 will output 6 7 8 9 0

#include <iostream>
#include <iomanip>
using namespace std;

int quotient(int, int);
int reminder(int , int);
int main(){

int number;
int divisor = 10000;

cout << "ENter an integer between 1 - 32767 :";
cin >> number;

cout << "THe Digit in the number are :\n";

while (number >= 1){

if (number >= divisor){

cout << setw(3)<<quotient(number, divisor);
number = reminder(number, divisor);
divisor = quotient(divisor, 10);
}
else
divisor = quotient(divisor, 10);

}
cout << endl;
return 0;

}
int quotient(int a, int b){

return a / b;
}
int reminder(int a, int b){

return a % b;
}

Friday, 16 May 2014

Create Diamond C++

Excercise From C++ how to program

2.58 Write a program that prints the following diamond shape. You may use output statements that print either a single asterisk
(*) or a single blank. Maximize your use of repetition (with nested for structures) and minimize the number of output statements.

        *
      ***
    *****
  *******
*********
  *******
    *****
      ***
        *
#include 
using namespace std;
int main(){


 for (int row = 1; row <= 5; row++){
  for (int space = 1; space <= 5 - row; space++){

     cout << " ";

  }
  
  for (int asterisk = 1; asterisk <= 2 * row - 1; asterisk++){

   cout << "*";
   
  
  }

  cout << '\n';


 }


 for (int row = 4; row >=1; row--){
  for (int space = 1; space <= 5-row; space++){

   cout << " ";

  }

  for (int asterisk = 1; asterisk <=2 * row - 1; asterisk++){

   cout << "*";


  }

  cout << '\n';


 }
 cin.get();
 return 0;
}


Wednesday, 16 April 2014

Bubble sort


This Exercise has taken from Java How to Program 
The bubble sort presented in Fig. 7.10 is inefficient for large arrays. Make the following simple
modifications to improve the performance of the bubble sort:
a) After the first pass, the largest number is guaranteed to be in the highest-numbered element
of the array; after the second pass, the two highest numbers are “in place”; etc. Instead
of making nine comparisons on every pass, modify the bubble sort to make eight
comparisons on the second pass, seven on the third pass, etc


public class BubbleSort {
	public static void main (String args[]){
		
		int []array= {25,7,10,3,9,48,5,6};
		for (int j = 1; j <array.length; j ++){		
			for (int i = 0; i <array.length-j; i ++){
				int temp = array [i];
				if (array[i]> array [i+1]){
					array [i] = array [i+1];
					array [i+1]=temp;
				}
			}
		}
		for (int i = 0; i <array.length; i ++){
			System.out.printf("\t"+array[i]);
		}
	}
}

Ex: 7.10 Java How to Program


Use a one-dimensional array to solve the following problem: A company pays its salespeople on a commission basis. The salespeople receive $200 per week plus 9% of their gross sales for that week. For example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9% of $5000, or a total of $650. Write an applet (using an array of counters) that determines how many of the salespeople earned salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
import java.util.Scanner;

public class Sales {
 public static void main (String args[]){
  int [] total = new int [9];
 
  Scanner input = new Scanner (System.in);
  int range;  
  int salary = 200;
  double commission = .09; 
  
  while (input.hasNext()){
   
   double income  = salary + input.nextInt()*commission;
   range = (int)income / 100;
   if (range > 9 )
    range = 10;
   ++total [range -2];
  }
  
  
 System.out.println("Range \t \t Number"); 
  for (int i = 0 ; i < total.length-1; i++){
   System.out.printf("\n$"+(200+100*i) + "- $"+(299+100*i)+"\t \t"+total[i]+"\n");
  }
  System.out.println("$1000 and over \t \t"+total[total.length-1]); 
 }
}

Saturday, 17 March 2012

Creating A Child Class

Creating a child class is pretty simple.Just create a class and use the keyword "extend" to extend the parent class.You need to make sure that thw class that you are extending is not a FINAL class because you can not extend a FINAL class.

class ChildClass extends ParentClass
{
// class definition goes here
}


The child class needs access to the file where the parent class is defined, so you need to
include the parent file before defining the child class. Since the child class depends on it,
you should normally use require_once to include the parent file.




Getter and Setter in PHP


In PHP class getter and setter methods are really useful to show the results or pull the data.When you create a value as public it is visible from everywhere.That means inside or outside the class and any one can pull the data or extend the class to change it.So here comes the private or protected privileges.set() sets the value and get() gets the value.So you can pull the data using the get().Here is a simple example:

class Employee
{
// properties defined here
protected $_name = 'John';
// methods defined here
// methods defined here
public function getName()
{
return $this->_name;
}
public function setName($name)
{
$this->_name = $name;
}

}

Now create another test.php and copy followings:
// include the class file
require_once 'Employee.php';
// create an instance of the Employee class
$employee = new Employee();
// display the $_name property
//echo "$employee->_name";
$employee->setName('Robert');
echo $employee->getName();


If you run the file you can see Robert but may be you are expecting John.This is because we have set the variable to Robert using set().

Setting default values with a constructor method

A constructor builds the object, applying default values and assigning to properties values passed to the class when an object is instantiated.
the constructor for all classes is called __construct()

The constructor works like a setter method, so any values passed to it as arguments can be
assigned to properties by using $this to refer to the current object like this:
public function __construct($value)
{
$this>property = $value;
}

Lets create a constructor with 2 properties:

// constructor
public function __construct($name, $title)
{
$this->_name = $name;
$this->_title = $title;
}

Because the value of $_name is set by the constructor, you no longer need the
setter method. So we can delete it and add a getter method to retrieve the value of the
$_title property. The Employee.php should look
like this:

class Employee
{
// properties defined here
protected $_name;
protected $_title;
// constructor
public function __construct($name, $title)
{
$this->_name = $name;
$this->_title = $title;
}
// methods defined here
public function getName()
{
return $this->_name;
}


public function getTitle()
{
return $this->_title;
}
}

the class constructor now expects two arguments. To keep the code simple, I haven’t added any code to check the type of data passed as arguments, but let’s assume that both are meant to be strings. Test the revised class by the code in test.php as follows
require_once 'employee.php';
$employee1 = new Employee('John', 'Manager');
$employee2 = new Employee('Robert', 'Cashier');
echo '$employee1 is  ' . $employee1->getName();
echo ' Title is "' . $employee1->getTitle() . '"
';
echo '$employee2 is  ' . $employee2->getName();
echo ' Title is  "' . $employee2->getTitle() . '"
';

When you run test.php, you should see something similar to



$employee1 is John Title is "Manager"
$employee2 is Robert Title is "Cashier"



Wednesday, 7 March 2012

Simple MySQL Class connection Test

This is a very simple PHP class to check MySQL connection.Please read my other blog to create the MySQL table to make things work.
define("server","localhost");
define("conn_username", "root");
define("conn_password", "root");
define("database_name", "database_1.1");


require "database.inc.php";
 
 class mysql 
 { 
     function connect() { 
      @mysql_connect(server,conn_username,conn_password); 
      @mysql_select_db(database_name); 
  } 
 
     function query($query) { 
         $result = mysql_query($query); 
         if (!$result) { 
             echo 'Could not run query: ' . mysql_error(); 
             exit; 
   } 
   else{
    return $result;
   }
     } 
     
     function showresult($result) {
      $arr = array();
    @mysql_data_seek($result);
    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
     array_push($arr, $row);
    }
    return $arr;
     }
     function end() { 
         @mysql_close(); 
     } 
 }
 
 $mysql = new mysql(); 
 $mysql->connect(); 
       // $query = $mysql->query("SELECT * FROM Employee");
 print_r($mysql->showresult(/*$query)*/$mysql->query("SELECT * FROM Employee"))); 
 $mysql->end();