SQL

Saturday, 30 January 2016

struct and vector in C++





hasan@lucid:~/Desktop/cpp/struct$ g++ -std=c++11 structVectror.cpp
hasan@lucid:~/Desktop/cpp/struct$ ./a.out
Joseph
18
Cashier
Tom
23
Sales man

Struct in C++

This example shows a simple use of struct



hasan@lucid:~/Desktop/cpp/struct$ g++ -std=c++11 struct.cpp
hasan@lucid:~/Desktop/cpp/struct$ ./a.out
Joseph
18
Cashier


Friday, 29 January 2016

Encrypt a String using Vector in C++

This application will Encrypt any message as an reverse format. As an example:
a --> z
b --> y

.....

Capital Letter was not in consideration. You have the idea to do it :D

It is always a good idea to see if the idea is working or not. So let's try the methodology first. If it works then we will start coding.



hasan@lucid:~/Desktop/cpp/vector$g++ -std=c++14 encrypt.cpp
hasan@lucid:~/Desktop/cpp/vector$ ./a.out
a-->z
b-->y
c-->x
d-->w
e-->v
f-->u
g-->t
h-->s
i-->r
j-->q
k-->p
l-->o
m-->n
n-->m
o-->l
p-->k
q-->j
r-->i
s-->h
t-->g
u-->f
v-->e
w-->d
x-->c
y-->b
z-->a


Now we can proceed to actual code to encrypt a message or string.




hasan@lucid:~/Desktop/cpp/vector$g++ -std=c++11 encrypt.cpp
hasan@lucid:~/Desktop/cpp/vector$ ./a.out
--- --- -- -----
blf ziv zm rwrlg

vector and resize vector in C++

This Example will demonstrate C++11 or above vector and how to use it. This following creates a vector of fixed size and calculate the sum. vector_size.cpp is the c++ file. You may also rename it to vector_size.cxx


hasan@lucid:~/Desktop/cpp/vector$ g++ -std=c++11 vector_size.cpp
hasan@lucid:~/Desktop/cpp/vector$ ./a.out
Entera value
1.1
Entera value
2.4
Entera value
3.5

sum = 7



This example will demonstrate how to resize the previous vector we created and calculate the sum.



hasan@lucid:~/Desktop/cpp/vector$ g++ -std=c++11 vector_size.cpp
hasan@lucid:~/Desktop/cpp/vector$ ./a.out
Number of values to Sum?
3
Entera value
1.1
Entera value
2.2
Entera value
3.3

6.6
hasan@lucid:~/Desktop/cpp/vector$


Saturday, 23 January 2016

GUI 5 Java JTextField and JPasswordField








GUI 4 JLabel and Icon

A JLabel object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default.


You can also specify the position of the text relative to the image. By default, text is on the trailing edge of the image, with the text and image vertically aligned

A label's leading and trailing edge are determined from the value of its ComponentOrientation property. At present, the default ComponentOrientation setting maps the leading edge to left and the trailing edge to right.

Finally, you can use the setIconTextGap method to specify how many pixels should appear between the text and the image. The default is 4 pixels.





The lebels and icon position will be changed as you change the window size.You need to use gridLayout() function to position the labels.

Friday, 22 January 2016

GUI 3 Java Layout Manager and Absolute Positioning

There are two approach to put button on a frame's content pane.

  • Using Layout Manager
  • Using Absolute Positioning

If you like to use absolute position then you must use 

setLayout(null);
setBounds(X screen, Y screen, Button Width, Button Height)

Absolute Positioning:






Layout Manager:

When you use Layout Manager like FlowLayout Manager then GUI objects are placed Top to Bottom ,from left to right. X and Y coordinates plus button size automatically created.





GUI 2

This example wil extend the previous example. It will use Container class to change the Background of the frame. In order to change the Background we need to access the content pane of a frame. A frame's content pane is the designated are which does not include title, menu bar and the border of the frame. It is the area where we can display content like text, image etc. We access the content pane of a frame by calling frames's getContentPane method and to change the background we will content panes's setBackground method.





GUI 1 Simple sub class of JFrame

This is a simple GUI application. It has no text or image attached to it. You may say this is the basic JFrame subclass which has a fixed width, height and X,Y origin.






It is a good practice to use setDefaultCloseOperation(EXIT_ON_CLOSE); Otherwise even if you close the frame window , the compiler will be still running indefinitely.

The method such setTitle, setSize , setLocation and setDefaultCloseOperation are defined on its super class. We can call those inherited method using dot (.) or withot dot (.). If we use dot then we have to use this keyword. setLocation is the x and Y coordinate of the screen.

Sunday, 17 January 2016

SQL 3 : Data Manipulation and Retrieval Operations

11. Get all combination of suppliers and parts info in a way that suppliers and parts are colocated



This is a very clean example of doing S natural join P but other things should be noticed. I made this example to make it clear to show a column can be called several time in no order.Even though I wrote FROM S, P THAT DOES NOT MEAN YOU HAVE TO FOLLOW THE SEQUENCE. P.PNO CAN COME BEFORE S.SNO.

12. Get the Max and Min quantity of part P1



Interesting huh? You may use AS or not does not matter. some people prefer AS just it makes easy for them to read or understand.

13. Get The part no and the total quantity for each part supplied


Above example can be thought little different way as well as follows:


14. Get city names for cities in which at least two suppliers are located.






15. Get parts no for parts which are supplied more than 2 suppliers.







16. Get suppliers numbers for supplier with status less than current maximum status in supplier table.






5. Get all pairs of supplier numbers such that the two suppliers are located in the same city




Don't be fooled with the identifiers s1 and s2. For the above example, you may also write it like this:





6








7. Find all supplier names and the total quantity supplied by them









8. Find all supplier names and the total quantity supplied by them as a desired output














Tuesday, 12 January 2016

SQL 2 : Data Manipulation and Retrieval Operations

1. Get supplier names for suppliers who supply part P2.




This is not the only way to get the result but there could be more possible way to get the answer.
The above example can be written following way as well




Interesting huh? What about more elegant way to write the above example?


I am showing them to write them different way so that readers can grab the technique easily.


2. Get supplier names for suppliers who supply at least one red part.






3. Get supplier names for suppliers who supply all parts.







4. Get suppliers names for suppliers who supplies at least those parts supplied by S2






5. Get all pairs of supplier numbers such that the two suppliers are located in the same city



Don't be fooled with the identifiers s1 and s2. For the above example, you may also write it like this:





6. Select all suppliers name who do not supply parts p2








7. Find all supplier names and the total quantity supplied by them






Another approach can be as following:





8. Find all supplier names and the total quantity supplied by them as a desired output







9. Get color and city for non Paris parts with weight greater than 10




10. For all the parts get the parts no and weight as grams



Saturday, 9 January 2016

SQL 1: Create Table, Insert row and Drop Table

First I will create 3 tables and some exercise will follow these 3 tables. I believe the best way to learn is by practising. Many people know very well theoretically but when it comes for real problems they fail. I will create


  • S for Suppliers Table 
  • P for Parts Table
  • SP fro Shipments Table

Create Table S:

SNO                  SNAME              STATUS               CITY
---                       ----------               ----------             -----------    
S1                      SMITH                    20                    LONDON
S2                      JONES                    10                    PARIS
S3                      BLAKE                   30                    PARIS
S4                      CLARK                   20                    LONDON
S5                      ADAMS                  30                    ATHENS






Now we have a table S in the Database but it will not look like above until we put row data on it.So we need to insert the values as well.

Right now so far we have all the data in the table S. To see all the data on S, we have to do a select command:


Create Table P:

PNO      PNAME      COLOR       WEIGHT CITY
---          ----------          -----           ----------   ----------
P1            NUT            RED           12      LONDON
P2            BOLT          GREEN   17      PARIS
P3            SCREW       BLUE           17      ROME
P4            SCREW       RED           14      LONDON
P5            CAN            BLUE           12      PARIS
P6            COG            RED           19      LONDON









Now to Inser data on P we have to use same command as we did previous table:



Create Table J:


JNO        JNAME       CITY
---------- ----------       ----------
J1         SORTER       PARIS
J2         DISPLAY     ROME
J3         OCR             ATHENS
J4         CONSOLE   ATHENS
J5         RAID            LONDON
J6         EDS              OSLO
J7         TAPE            LONDON

7 rows selected.







Create Table SP:



SNo      PNo     Qty
S1         P1        200
S2         P3        400
S2         P5        100
S3         P3        200
S3         P4        500
S4         P6        300
S5         P2        200
S5         P5        500
S5         P6        200
S5         P1        100
S5         P3        200
S5         P4        800


The following command will generate a table with empty row :


The table is empty now So to insert row or data we have to use Insert statement.


Above query will create one by one row in the SP table each time. But If we like to put several row together, we may follow this procedure:


Note: If you are using SQLPlus the above statement will not work.But the following will work:


SNo     PNo   JNo   Qty



Create Table SPJ:
SNO        PNO        JNO               QTY
---------- ---------- ----------           ----------
S1             P1            J1                200
S1             P1            J4                700
S2             P3            J1                400
S2             P3            J2                200
S2             P3            J3                200
S2             P3            J4                500
S2             P3            J5                600
S2             P3            J6                400
S2             P3            J7                800
S2             P5            J2                100
S3             P3            J1                200
S3             P4            J2                500
S4             P6            J3                300
S4             P6            J7                300
S5             P2            J2                200
S5             P2            J4                100
S5             P5            J5                500
S5             P5            J7                100
S5             P6            J2                200
S5             P1            J4                100
S5             P3            J4                200
S5             P4            J4                800
S5             P5            J4                400
S5             P6            J4                500





Insert record to SPJ:


To Drop a Table you have to use simply drop statement. DBMS will not give you a warning sign whether the table empty or full but it will simply drop it so be cautious to drop a table.



Think We have a database which has some table on it. The following command will show all table as a list.