tag:blogger.com,1999:blog-88125366163272662352024-03-13T05:48:13.461+05:30SuaveSnippetsI often came across broken or unhelpful snippets while working on projects. A small feature required by a project needed hours of surfing on the net. Just wanted to make things a tad simpler for you!aparnahttp://www.blogger.com/profile/14277551627936970617noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-8812536616327266235.post-124544469747146962014-09-08T00:16:00.002+05:302014-09-08T00:40:17.079+05:30Why You Need To Build Opinionated Products<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
Any product worth its salt was built for one of two reasons. The first reason is more evident and intuitive to understand - fulfilling a need. Picasa made online picture sharing and storage a piece of cake just when users around the world were struggling with limited email attachment size to send photographs to friends and family. The second bucket of products create the need in a bid to then make the product an indispensable part of their users' lives. Nobody had a sudden urge in the middle of the night to write about pickled lemons in 140 characters or less. Twitter created that need.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
<br clear="none" /></div>
</div>
<div style="text-align: justify;">
Products always start out with a vision - a plan and a purpose - yet it is fairly common to see them change course and meander off into an unintended path. And that's okay. That's okay provided the new, end-product is a derivative of your opinion, your reading of usage and your vision. Product folks often make the mistake of listening to all their users, especially ones with conflicting usage patterns, leading to pages and pages of knobs and settings. An interesting analogy can be drawn between products and rivers. As long as both power through one path, they are strong and booming. The path may deviate from the originally intended course, but if everything holds together, the river will meet the ocean at full capacity, just as the product will hit users with the same force. A product with a hundred different configurations is, however, like a river with a hundred tiny distributaries - each is weak, sluggish and in a lot of cases, may die out before meeting the ocean.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
<br clear="none" /></div>
</div>
<div style="text-align: justify;">
Non-opinionated products are riddled with settings and configurations. They do not define the 'golden path' that they want their users to follow but leave most of the of flexibility to its users. Users are expected to turn knobs and dials to reach a product state that they believe fits their need and then find value in the product. The problem with this approach is that no product is optimized for every permutation of its settings. The chosen permutation may be lacking in a lot of functionality and leaves the user wanting more. Satisfying this need leads to the introduction of more settings in the product to support the 'distributary'. It is indeed a vicious circle and no product person wants to be in that state - after all, every setting turned on or off makes for a different product, and maintaining all those products leads one down an endless rabbit hole.</div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
<br clear="none" /></div>
</div>
<div style="text-align: justify;">
Leave out the fluff. Kill all those settings. Build opinionated products. </div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
<br clear="none" /></div>
</div>
<div style="text-align: justify;">
Proponents of non-opinionated software often claim that it is arrogant for product to ignore feature requests and that software should be as flexible as possible. Here's what 37Signals, now Basecamp, had to say about that in an article:</div>
</div>
<div style="border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; line-height: 1.428571em; margin: 0px; padding: 0px;">
<blockquote class="tr_bq" style="border: 0px; line-height: 1.428571em; margin-bottom: 0.714285em; padding: 0px;">
<div style="text-align: justify;">
<span style="color: #666666;">We think that's bullshit. The best software has a vision. The best software takes sides. When someone uses software, they're not just looking for features, they're looking for an approach. They're looking for a vision. Decide what your vision is and run with it.</span></div>
</blockquote>
<div style="text-align: justify;">
<br /></div>
</div>
<div style="border: 0px; font-family: Helvetica, Arial, 'Droid Sans', sans-serif; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
Building opinionated software does not mean you are not listening to your users. It does not mean you disregard any feedback that comes your way or turn a blind eye to popular usage trends. It means listening to them to find out their biggest points of friction, focusing on the problems your product intended to solve all along and providing that must-have experience for users. </div>
<div style="border: 0px; line-height: 1.428571em; margin: 0px; padding: 0px;">
<div style="text-align: justify;">
<br clear="none" /></div>
</div>
<div style="text-align: justify;">
Saying 'no' can be hard - especially for people who live to build products for users. It's important to remember that although a lot of users will not like it, the ones who do, will love the product, will stick around the longest and be the strongest advocates you'd ever find.</div>
</div>
</div>
Appyhttp://www.blogger.com/profile/02128367003198847639noreply@blogger.com0tag:blogger.com,1999:blog-8812536616327266235.post-47427366139969065532011-11-19T10:35:00.001+05:302011-11-20T19:18:29.907+05:30JSP and MySQL Connectivity<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
I remember when I first started using Tomcat, it took me a while to make my JSP script connect to MySQL using Connector/J.<br />
<br />
1. Tomcat is a free, open-source implementation of Java Servlet and JavaServer Pages technologies developed under the Jakarta project at the Apache Software Foundation. Download and install Tomcat.<br />
<br />
2. Install MySQL.<br />
<br />
3. Create a database, say 'ProductSpec', and a table 'products' with three fields- id, name, brand.<br />
<code><br />
CREATE DATABASE `ProductSpec` ;<br />
<br />
CREATE TABLE `products` (<br />
`id` INT( 3 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,<br />
`name` VARCHAR( 50 ) NOT NULL ,<br />
`brand` VARCHAR( 50 ) NOT NULL<br />
) ENGINE = INNODB;<br />
</code>
<br />
4. I populate the the database with a few values next:<br />
<br /><code>
INSERT INTO `ProductSpec`.`products`<br />
VALUES ( 1 , 'Hard Disk', 'Seagate');<br />
<br />
INSERT INTO `ProductSpec`.`products`<br />
VALUES ( 2 , 'iPhone 3G', 'Apple');<br />
<br /></code>
5. Set up two environment variables as follows:<br />
(a) Right click on Computer, go to Properties. Click on 'Advanced System Settings on the left. A dialog box appears as shown below. Go to the 'Advanced Tab' and click on 'Environment Variables'.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-Mos5425C63o/TsdE598CvLI/AAAAAAAAAsY/tdUZ4-GTd64/s1600/SGPhoto.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="http://1.bp.blogspot.com/-Mos5425C63o/TsdE598CvLI/AAAAAAAAAsY/tdUZ4-GTd64/s320/SGPhoto.png" width="287" /></a></div>
<br />
<br />
(b)Under User Variables, add the two new variables. The first is the path to your jre folder, the second is the path to the the Tomcat folder. For instance, if your jre folder path is D:/jdk1.6.0_12/jre, add the following-<br />
<br />
Variable Name: JAVA_HOME, Variable Value: D:/jdk1.6.0_12/jre<br />
<br />
Variable Name: TOMCAT_HOME, Variable Value: C:\Program Files\Apache Software Foundation\Tomcat 7.0<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-EqQTtuxYNFg/TsdE_6umZxI/AAAAAAAAAsg/3d06XWxd3b4/s1600/SGPhoto_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="350" src="http://1.bp.blogspot.com/-EqQTtuxYNFg/TsdE_6umZxI/AAAAAAAAAsg/3d06XWxd3b4/s400/SGPhoto_1.png" width="400" /></a></div>
<br />
<br />
6. Download Connector/J at <a href="http://www.blogger.com/%22http://dev.mysql.com/downloads/connector/j/5.1.html%22%3Ehttp://dev.mysql.com/downloads/connector/j/5.1.html">"http://dev.mysql.com/downloads/connector/j/5.1.html">http://dev.mysql.com/downloads/connector/j/5.1.html</a><br />
Unzip or Untar the file, and copy the file mysql-connector-java-5.1.6-bin.jar to Tomcat/lib or Tomcat/common/lib<br />
<br />
This completes the setup! Now for the jsp program that connects to the database and retrieves and displays values. Make sure Tomcat and wamp are running!<br />
<br />
7. Here's db.jsp:<br />
<br />
<br /><code>
<%@ page import="java.sql.*" %><br />
<br />
<%<br />
<br />
String connectionURL = "jdbc:mysql://localhost:3306/ProductSpec?username=root;password=";<br />
<br />
Connection connection = null;<br />
<br />
Statement statement = null;<br />
<br />
ResultSet rs = null;<br />
<br />
%><br />
<br />
<html><body><br />
<br />
<%<br />
<br />
Class.forName("com.mysql.jdbc.Driver").newInstance();<br />
<br />
connection = DriverManager.getConnection(connectionURL, "root", "");<br />
<br />
statement = connection.createStatement();<br />
<br />
rs = statement.executeQuery("SELECT * FROM products");<br />
<br />
while (rs.next()) {<br />
<br />
out.println(rs.getString("name")+" "+rs.getString("brand")+"<br />");<br />
<br />
}<br />
<br />
rs.close();<br />
<br />
%><br />
<br />
</body></html><br /></code>
<br />
<br />
Change your database name, username and password on line 3: (Default username for wamp is root and password is blank.<br />
<br />
String connectionURL = "jdbc:mysql://localhost:3306/ProductSpec?username=root;password=";<br />
<br />
8. Add db.jsp to Tomcat\webapps\ROOT folder.<br />
<br />
9. Now go to your browser and type localhost/db.jsp<br />
If you had set a port number for Apache during installation like I had (8088), type localhost:8088/db.jsp<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://3.bp.blogspot.com/-9BIsYBhNrLs/TsdFRf2i40I/AAAAAAAAAso/4d2jsPUzP3Q/s1600/SGPhoto_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="202" src="http://3.bp.blogspot.com/-9BIsYBhNrLs/TsdFRf2i40I/AAAAAAAAAso/4d2jsPUzP3Q/s320/SGPhoto_2.png" width="320" /></a></div>
<br /></div>Appyhttp://www.blogger.com/profile/02128367003198847639noreply@blogger.com0tag:blogger.com,1999:blog-8812536616327266235.post-21178393351504175552011-09-14T10:50:00.000+05:302011-09-14T10:50:03.276+05:30Formulae in Data Structures - Quick Revision<div dir="ltr" style="text-align: left;" trbidi="on">
Most written technical tests have at least one question that goes - "How many Binary Search Trees can you form with n distinct numbers?" or "Number of internal nodes in an m-ary/k-ary tree?"<br />
And I know for a fact, most of us draw small trees in the corner of our sheets. That<br />
1. wastes precious time.<br />
2. leaves out important cases.<br />
<br />
So here's a list that is definitely not exhaustive, it'll be great if you could figure them out as you go along, helps remember. I'll keep adding more formulae as I come across them. But for now, this should do:<br />
<br />
1. To answer the first question above,<br />
<br />
No of BSTs using n numbers is <sup>2n</sup>C<sub>n </sub>/ (n+1) (i)<br />
<br />
For those of you who haven't seen this before, this gives the Catalan sequence, and the number of binary search trees can be found using the Catalan Sequence too, if you know it. For instance, if n=3, C(3) = 5 which is the number of BSTs you can form from three nodes.<br />
<div class="MsoNormal">
<o:p></o:p></div>
<div class="MsoNormal">
<o:p></o:p></div>
<br />
2. The number of Binary Trees that can be formed, however, is given by the n<sup>th </sup>catalan number * n! The formula given above (i) calculates the n<sup>th </sup>catalan number.<br />
<br />
No of Binary trees from n numbers is n! * <sup>2n</sup>C<sub>n </sub>/ (n+1)<br />
<br />
<br />
Here's a small example for the two formulae given above with n = 3 (say the nodes are 1, 2 and 3):<br />
<br />
<b><u>Binary Search Trees</u></b><br />
<b><u><br /></u></b><br />
<br />
<div class="MsoNormal">
<sup>6</sup>C<sub>3 </sub>/ 4 = 5<b style="text-decoration: underline;"><o:p></o:p></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px;"></span></div>
<pre><span>1 1 2 3 3
\ \ / \ / /
2 3 1 3 1 2
\ / \ /
3 2 2 1</span></pre>
<br />
<br />
<b><u><br /></u></b><br />
<b><u>Binary Trees</u></b><br />
<b><u><br /></u></b><br />
3! * 5 = 30<br />
<br />
<span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 1px; -webkit-border-vertical-spacing: 1px;"></span><br />
<pre><span> 1 1 2 2 3 3
/ \ / \ / \ / \ / \ / \
2 3 3 2 1 3 3 1 1 2 2 1
1 1 1 1 1 1 1 1
/ / / / \ \ \ \
2 3 2 3 2 3 2 3
/ / \ \ / / \ \
3 2 3 2 3 2 3 2
2 2 2 2 2 2 2 2
/ / / / \ \ \ \
1 3 1 3 1 3 1 3
/ / \ \ / / \ \
3 1 3 1 3 1 3 1
3 3 3 3 3 3 3 3
/ / / / \ \ \ \
2 1 2 1 2 1 2 1
/ / \ \ / / \ \
1 2 1 2 1 2 1 2 </span></pre>
<br />
<div class="MsoNormal">
<o:p></o:p></div>
</div>
Appyhttp://www.blogger.com/profile/02128367003198847639noreply@blogger.com0tag:blogger.com,1999:blog-8812536616327266235.post-85802947921403098162011-06-25T00:59:00.006+05:302011-06-25T01:15:36.636+05:30Enabling Query Cache for fast query execution in MySQL1. Open the MySQL Console<div><br />2. Enter <span style="font-style:italic;" ><b>SET GLOBAL query_cache_size = 1048576;</b></span></div><div>(Size is in KBs, 1GB = 1x1024x1024=1048576)<div><br /></div><div>3. Other Cache Related Settings includes</div><div><span class="Apple-style-span" ><b><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span><span class="Apple-style-span">SET GLOBAL query_cache_type=1;</span></b></span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(To Enable Query Cache)</div><div><span class="Apple-style-span" ><b><span class="Apple-tab-span" style="white-space:pre"><span class="Apple-tab-span" style="white-space:pre"> </span></span><span class="Apple-style-span">SET GLOBAL query_cache_limit</span>=1024;</b></span></div><div><span class="Apple-tab-span" style="white-space:pre"> </span>(Per User 1MB 1x1024)</div><div><br /></div><div>4. Caching speeds up the fetching of queries, the size must be chosen according as the RAM size</div><div><br /></div><div>5. To check their values, you can run command</div><div><span class="Apple-tab-span" style="white-space:pre"> </span><span class="Apple-style-span" ><b>SHOW VARIABLES LIKE 'query%';</b></span></div></div><div><span class="Apple-style-span"><br /></span></div><div><span class="Apple-style-span">Note: To run mysql in terminal: <span class="Apple-style-span" ><b>mysql -u root -p</b></span></span></div>vikramhttp://www.blogger.com/profile/14958441615812560164noreply@blogger.com0tag:blogger.com,1999:blog-8812536616327266235.post-76419622508745393812011-06-14T19:37:00.012+05:302011-12-03T10:46:19.929+05:30Animation Effect on your Splash/Welcome Screen on iPhone<div dir="ltr" style="text-align: left;" trbidi="on">
In this tutorial, I'll give an example of how to have your own welcome image, and add the fade and zoom effect to it. When the iPhone launches your application, it looks for an image named Default.png in the resources directory. In the following example, Default.png is being replaced by a UIImageView which zooms and then fades away. (Consider abc to be your project name)<br />
<div>
<br /></div>
<div>
1. To abcAppDelegate.h, add the function declaration</div>
<div>
<br /></div>
<div>
<div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<code>- (<span style="color: #c02d9d; font-size: 14px;">void</span>)<span style="font-size: 14px;"> welcomeScreen</span></code></div>
</div>
<div>
<br /></div>
<div>
<div>
2. To abcAppDelegate.m, add the function definition</div>
</div>
<div>
<br /></div>
<br />
<div>
<div style="font: normal normal normal 11px/normal Menlo; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">
<span class="Apple-style-span" style="font-size: small;">- (<span style="color: #c02d9d;">void</span>) welcomeScreen</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;">{</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
<span class="Apple-tab-span" style="font-size: small; white-space: pre;"> </span></div>
<div style="color: #008324; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> </span>//Welcome Screen</span></div>
<div style="color: #008324; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> </span><span style="color: #743fa4;">UIImageView</span><span style="color: black;">* welcome = [[[</span><span style="color: #743fa4;">UIImageView</span><span style="color: black;"> </span>alloc<span style="color: black;">] </span>initWithFrame<span style="color: black;">:</span>CGRectMake<span style="color: black;">(</span><span style="color: #2f2fd0;">0</span><span style="color: black;">,</span><span style="color: #2f2fd0;">0</span><span style="color: black;">,</span><span style="color: #2f2fd0;">320</span>,<span style="color: #2f2fd0;">480</span><span style="color: black;">)]</span>autorelease<span style="color: black;">];</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> welcome</span>.<span style="color: #743fa4;">image</span> = [<span style="color: #743fa4;">UIImage</span> <span style="color: #40207c;">imageNamed</span>:<span style="color: #d62c24;">@"img.png"</span>];</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> </span>[<span style="color: #4b8186;">window</span> <span style="color: #40207c;">addSubview</span>:welcome];</span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #4b8186;">window</span><span style="color: black;"> </span>bringSubviewToFront<span style="color: black;">:welcome];</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
<span class="Apple-tab-span" style="font-size: small; white-space: pre;"> </span></div>
<div style="color: #008324; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> </span>//Animation Effects (zoom and fade)</span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #743fa4;">UIView</span><span style="color: black;"> </span>beginAnimations<span style="color: black;">:</span><span style="color: #c02d9d;">nil</span><span style="color: black;"> </span>context<span style="color: black;">:</span><span style="color: #c02d9d;">nil</span><span style="color: black;">];</span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"></span><span class="Apple-style-span" style="white-space: pre;"> </span>[<span style="color: #743fa4;">UIView</span> <span style="color: #40207c;">setAnimationDuration</span>:2.0];</span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #743fa4;">UIView</span><span style="color: black;"> </span>setAnimationTransition<span style="color: black;">:</span>UIViewAnimationTransitionNone<span style="color: black;"> </span>forView<span style="color: black;">:</span><span style="color: #4b8186;">window</span><span style="color: black;"> </span>cache<span style="color: black;">:</span><span style="color: #c02d9d;">YES</span><span style="color: black;">];</span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #743fa4;">UIView</span><span style="color: black;"> </span>setAnimationDelegate<span style="color: black;">:welcome]; </span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #743fa4;">UIView</span><span style="color: black;"> </span>setAnimationDidStopSelector<span style="color: black;">:</span><span style="color: #c02d9d;">@selector</span><span style="color: black;">(</span>removeFromSuperview<span style="color: black;">)];</span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="color: black; font-size: small;"><br />
</span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="color: black; font-size: small;"> //set transparency to 0.0</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> welcome</span>.<span style="color: #743fa4;">alpha</span> = <span style="color: #2f2fd0;">0.0</span>;</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><br />
</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"> //zoom effect</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span class="Apple-style-span" style="white-space: pre;"> welcome</span>.<span style="color: #743fa4;">frame</span> = <span style="color: #40207c;">CGRectMake</span>(<span class="Apple-style-span" style="color: blue;">-60, -6</span><span style="color: #2f2fd0;">0</span>, <span style="color: #2f2fd0;">440</span>, <span style="color: #2f2fd0;">600</span>);</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
<span class="Apple-tab-span" style="font-size: small; white-space: pre;"> </span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"><span class="Apple-style-span" style="white-space: pre;"> </span>[</span><span style="color: #743fa4;">UIView</span><span style="color: black;"> </span>commitAnimations<span style="color: black;">];</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
<span class="Apple-style-span" style="font-size: small; white-space: pre;"><br />
</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;">}</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="font-size: 14px;"><br /></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px;">3</span><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: x-small;">. Call the method welcomeScreen </span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="font-size: 14px;"><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px;"><br />
</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: x-small;"><span class="Apple-style-span" style="font-family: Georgia, serif;">From </span>applicationDidFinishLaunching<span class="Apple-style-span" style="font-family: Georgia, serif;"> </span><span class="Apple-style-span" style="font-family: Georgia, serif;">method in abcAppDelegate.m, invoke welcomeScreen.</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="font-size: 14px;"><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px;"><br />
</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="font-size: 14px;"><span class="Apple-style-span" style="font-family: Georgia, serif; font-size: 16px;"></span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;">- (<span style="color: #c02d9d;">void</span>)applicationDidFinishLaunching:(<span style="color: #743fa4;">UIApplication</span> *)application { </span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
</div>
<div style="color: #4b8186; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"> [</span>window<span style="color: black;"> </span><span style="color: #40207c;">addSubview</span><span style="color: black;">:</span>viewController<span style="color: black;">.</span><span style="color: #40207c;">view</span><span style="color: black;">];</span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"> [</span><span style="color: #4b8186;">window</span><span style="color: black;"> </span>makeKeyAndVisible<span style="color: black;">];</span></span></div>
<div style="color: #40207c; font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;"><span style="color: black;"></span><span style="color: black;"> [</span><span style="color: #743fa4;">NSTimer </span>scheduledTimerWithTimeInterval<span style="color: black;">:</span><span style="color: #2f2fd0;">1.0</span><span style="color: black;">/</span><span style="color: #2f2fd0;">30. </span>target<span style="color: black;">:</span><span style="color: #c02d9d;">self </span>selector<span style="color: black;">:</span><span style="color: #c02d9d;">@selector</span><span style="color: black;">(</span><span class="Apple-style-span" style="color: #2e595d;">welcomeScreen</span><span style="color: black;">) </span>userInfo<span style="color: black;">:</span><span style="color: #c02d9d;">nil</span><span style="color: black;"> </span>repeats<span style="color: black;">:</span><span style="color: #c02d9d;">NO</span><span style="color: black;">];</span></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px; min-height: 13.0px;">
<span class="Apple-style-span" style="font-size: small;"><br />
</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-size: small;">}</span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span style="font-size: 14px;"><br /></span></div>
<div style="font: 11.0px Menlo; margin: 0.0px 0.0px 0.0px 0.0px;">
<span class="Apple-style-span" style="font-family: Georgia, serif; font-size: x-small;">And it's a wrap! Cheers!</span></div>
</div>
<span style="font-size: 14px;"><br />
</span><br />
<hr />
<span style="font-size: 14px;">
</span></div>
Appyhttp://www.blogger.com/profile/02128367003198847639noreply@blogger.com4tag:blogger.com,1999:blog-8812536616327266235.post-37615342950172551662011-06-06T12:50:00.001+05:302011-06-06T12:54:12.910+05:30JDBC and MySQL<div dir="ltr" style="text-align: left;" trbidi="on"><b><u>Steps to connect to a MySQL database from Java</u></b><br />
<b><u><br />
</u></b><br />
<b><u>1. Import</u></b><br />
<code><br />
import java.sql.*;<br />
import javax.sql.*;<br />
</code><br />
<b><u>2. Declare a Connection type in the class:</u></b><br />
<br />
Connection conn;<br />
<br />
<b><u>3. Add a method to establish connection</u></b>. Assume the name of the database is <i>GameIF</i>, and the username/password are the default values i.e username is <i>root</i> and there is no password.<br />
<br />
<code><br />
private void connect()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String dbUrl = "jdbc:mysql://localhost/GameIF";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String dbClass = "com.mysql.jdbc.Driver";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String username="root";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>String password="";<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Class.forName(dbClass).newInstance();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>conn = DriverManager.getConnection(dbUrl,username,password);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch(Exception e)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(e);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
</code><br />
<br />
<br />
<b><u>3. Add a function that tears down the connection:</u></b><br />
<br />
<code><br />
private void disconnect()<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>if (conn != null)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>conn.close();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch (Exception e) {}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
</code><br />
<br />
4. <b><u>Writing/Executing a query</u></b>: A query may be of two types:<br />
<br />
(a) One that returns a result set (e.g: SELECT query)<br />
<br />
(b) One that returns no result set (e.g: DROP, CREATE, INSERT, UPDATE)<br />
<br />
We'll see examples for both types:<br />
<br />
(a) SELECT<br />
<code><br />
connect( );<br />
<br />
String uname, steps;<br />
<br />
String query = "select * from `scores`";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Statement st = conn.createStatement();<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>ResultSet rs = st.executeQuery(query);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>while(rs.next())<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>uname = rs.getString("username");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>steps = rs.getInt("steps");<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println("Username: "+uname+"\tSteps: "+steps);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>rs.close();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>st.close();<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch(Exception e) {<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(e);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<br />
disconnect( );<br />
</code><br />
<br />
(b) INSERT<br />
<code><br />
<br />
connect( );<br />
<br />
<br />
Statement s;<br />
String query = "insert into `scores` (`game` , `username` , `steps`) values ('NUMBERJUMBLE','Tups',56)";<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>try<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>s = conn.createStatement( );<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>s.executeUpdate(query);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>s.close( );<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>catch(Exception e)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>{<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>System.out.println(e);<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>}<br />
<br />
<br />
disconnect( );<br />
</code><br />
<br />
<hr /><br />
And, we're done!<br />
However, a very common exception is encountered if you are building a standalone application:<br />
<br />
<b>java.lang.ClassNotFoundException</b><br />
<br />
To fix this:<br />
1. Download the mysql connector jar files (A straightforward google search!)<br />
2. Add the jar files (format <i>mysql-connector-java-<version>-bin.jar</i>) to the following two folders:<br />
<your jdk directory>/jre/lib<br />
<your jdk directory>/jre/lib/ext<br />
<br />
If you are using netbeans or another IDE, copy the files and restart!<br />
<br />
Cheers!</div><br />
<hr />aparnahttp://www.blogger.com/profile/14277551627936970617noreply@blogger.com1tag:blogger.com,1999:blog-8812536616327266235.post-26927816686947850022011-06-04T15:26:00.006+05:302011-06-06T12:58:49.377+05:30Add sound on Jbutton click in JavaTo add a sound on JButton click event, follow three simple steps:<br />
<div><u><b>1. Import the following:</b></u></div><code><br />
<div><div>import javax.sound.sampled.AudioInputStream;</div><div>import javax.sound.sampled.Clip;</div><div>import javax.sound.sampled.AudioSystem;</div></div></code><br />
<div><u><b>2. Add the following function to your class:</b></u></div><code><br />
<div><div>public void playSound(String soundName)</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span> </span>try<span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span> </span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span> AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile( ));</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>Clip clip = AudioSystem.getClip( );</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>clip.open(audioInputStream);</div><div><span class="Apple-tab-span" style="white-space:pre"> </span> <span class="Apple-tab-span" style="white-space:pre"> </span>clip.start( );</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span> </span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span>catch(Exception ex)</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span>{</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span>System.out.println("Error with playing sound.");</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span>ex.printStackTrace( );</div><div><span class="Apple-tab-span" style="white-space:pre"> <span class="Apple-tab-span" style="white-space:pre"> </span></span>}</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>}</div></div></code><br />
<div><b>3</b>.<b> In your actionPerformed function for the JButton, just <u>call the function with the filename as string:</u></b></div><code><br />
<div>public void actionPerformed(ActionEvent ae)</div><div>{</div><br />
<div><span class="Apple-tab-span" style="white-space:pre"> </span>//do something</div><div><span class="Apple-tab-span" style="white-space:pre"> </span></div><div><span class="Apple-tab-span" style="white-space:pre"></span><span class="Apple-tab-span" style="white-space:pre"> </span>playSound("buzzer.wav");</div><div>}</div></code><br />
<br /><br />
<hr />Appyhttp://www.blogger.com/profile/02128367003198847639noreply@blogger.com9tag:blogger.com,1999:blog-8812536616327266235.post-77198424171521823402011-06-04T11:57:00.004+05:302011-11-21T12:33:37.775+05:30About<div dir="ltr" style="text-align: left;" trbidi="on">I often came across broken or unhelpful snippets while working on projects. A small feature required by a project needed hours of surfing on the net. Just wanted to make things a tad simpler for you!</div>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-0512128662298388";
/* Suave */
google_ad_slot = "8041475789";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>aparnahttp://www.blogger.com/profile/14277551627936970617noreply@blogger.com