core java
Saturday 15 September 2012
Json example in javascript
What is JSon?
Its Java Script Object Notation
1. Light weight data interchange format2. Easy to parse compare to xml
3. Minimal
4. A Subset of javascript
Points to remember about JSon:
1. It is based on key-value
2. The json object is represents using {key:value}
3. The key must be enclose with ""
4. Examples are: {"firstName":"vikash","lastName":"Kumar"}
3. The jsonArray is represents using []
5. Example are [1,2,3,4,"vikash","kumar"]
Here is the Complete Full Example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML>
<HEAD>
<TITLE>Vikash JavaScript With JSon Example</TITLE>
</HEAD>
<BODY>
<h1>To See the Example Plese Click on the Below Button!</h1>
<input type="button" value="Hit Me!" onClick="calculate()">
</BODY>
</HTML>
<script>
var employees = {
"person" : [ {
"firstName" :"vikash",
"lastName" :"kumar",
"age" :29,
"email":"vikash.kumar@gmail.com",
"phone" : [ {
"home" :9852232900,
"personal" :9899267293,
"office" :013543215,
"mobile" :985432123
} ]
}, {
"firstName" :"Kalu",
"lastName" :"Dawn",
"age" :25,
"email":"kaludawn@gmail.com",
"phone" : [ {
"home" :9332232900,
"personal" :9834267293,
"office" :12345,
"mobile" :9853441300
} ]
}, {
"firstName" :"Babli",
"lastName" :"Singh",
"age" :29,
"email":"babli.singh@gmail.com",
"phone" : [ {
"home" :9899909000,
"personal" :9899267293,
"office" :0140799005,
"mobile" :989000309
} ]
}, {
"firstName" :"Raj",
"lastName" :"Mohan",
"age" :49,
"email":"rajmohan@gmail.com",
"phone" : [ {
"home" :8998762900,
"personal" :9899267293,
"office" :013543215,
"mobile" :985432123
} ]
}
]
}
function calculate() {
//alert(JSON.stringify(employees));
document.write("<table border='1' align='center'>");
document.write("<tr>");
document.write("<th colspan='4' bgcolor='green'>Personal Details</th>")
document.write("<th colspan='4' bgcolor='blue'>Contact Details</th>")
document.write("</tr>");
document.write("<tr>");
document.write("<th>First Name</th><th>Last Name</th><th>Age</th><th>Email</th>")
document.write("<th>Home</th><th>Personal</th><th>Office</th><th>Mobile</th>")
document.write("</tr>")
document.write("<tr bgcolor='red'>")
for ( var i = 0; i < employees.person.length; i++) {
document.write("<tr bgcolor='cyan'>")
document.write("<td>")
document.writeln(employees.person[i].firstName)
document.write("</td>")
document.write("<td>")
document.writeln(employees.person[i].lastName)
document.write("</td>")
document.write("<td>")
document.writeln(employees.person[i].age)
document.write("</td>")
document.write("<td>")
document.writeln(employees.person[i].email)
document.write("</td>")
for ( var j = 0; j < employees.person[i].phone.length; j++) {
document.write("<td>")
document.writeln(employees.person[i].phone[j].home)
document.write("</td>")
document.write("<td>")
document.writeln(employees.person[i].phone[j].personal)
document.write("</td>")
document.write("<td>")
document.writeln(employees.person[i].phone[j].office);
document.write("<td>")
document.writeln(employees.person[i].phone[j].mobile);
document.write("</td>")
document.write("</td>")
document.write("</tr>");
}
}
document.write("</table>")
}
</script>
Sunday 26 August 2012
Bubble Sorting
package com.vikash.core;
public class BubbleTest1 {
public static void main(String args[]) {
int bubbles[] = { 0,5,4,3,78,6,76};
System.out.println("Beffore Sorting:");
for(int i:bubbles)
{
System.out.print(" "+i+" ");
}
System.out.println();
int t = 0;
while(t!=bubbles.length){
for (int j = 0; j < bubbles.length - 1; j++) {
if (bubbles[j] > bubbles[j + 1]) {
t = bubbles[j];
bubbles[j] = bubbles[j + 1];
bubbles[j + 1] = t;
}
}
t+=1;
}
System.out.println("After Sorting");
for (int i : bubbles)
System.out.print(" " + i + " ");
}
}
public class BubbleTest1 {
public static void main(String args[]) {
int bubbles[] = { 0,5,4,3,78,6,76};
System.out.println("Beffore Sorting:");
for(int i:bubbles)
{
System.out.print(" "+i+" ");
}
System.out.println();
int t = 0;
while(t!=bubbles.length){
for (int j = 0; j < bubbles.length - 1; j++) {
if (bubbles[j] > bubbles[j + 1]) {
t = bubbles[j];
bubbles[j] = bubbles[j + 1];
bubbles[j + 1] = t;
}
}
t+=1;
}
System.out.println("After Sorting");
for (int i : bubbles)
System.out.print(" " + i + " ");
}
}
Inheritance
class A
{
public void show()
{
System.out.println("Super Class");
}
class B Extends A
{
public void show()
{
System.out.println("Sub Class");
}
}
class TestInheritance()
{
public static void main(String args[])
{
A a=new A();
B b=new B();
a.show();
b.show();
}
}
Note: When you change the access specifier of the overridden method in the subclass then it gives an compile time error i.e. u can't reduce the visibility.
Monday 13 August 2012
String Handling
1. Reverse a given String
package com.vikash.core;
public class ReverseStringTest {
public static void main(String args[]) {
String str = "vikash";
StringBuffer sb = new StringBuffer();
for (int i = str.length() - 1; i >= 0; i--)
sb.append(str.charAt(i));
System.out.println(sb);
}
}
package com.vikash.core;
public class ReverseStringTest {
public static void main(String args[]) {
String str = "vikash";
StringBuffer sb = new StringBuffer();
for (int i = str.length() - 1; i >= 0; i--)
sb.append(str.charAt(i));
System.out.println(sb);
}
}
Wednesday 25 January 2012
core java
/**
* This class demonstrates how to find the current logged in user on the system
* in Java.
*
*/
public class CurrentLoggedUser {
public void getCurrentLoggedInUser() {
String currentLoggedInUser = System.getProperty("user.name");
System.out.println("The Current Logged In User Is: " + currentLoggedInUser);
}
public static void main(String[] args) {
new CurrentLoggedUser().getCurrentLoggedInUser();
}
}
* This class demonstrates how to find the current logged in user on the system
* in Java.
*
*/
public class CurrentLoggedUser {
public void getCurrentLoggedInUser() {
String currentLoggedInUser = System.getProperty("user.name");
System.out.println("The Current Logged In User Is: " + currentLoggedInUser);
}
public static void main(String[] args) {
new CurrentLoggedUser().getCurrentLoggedInUser();
}
}
Subscribe to:
Posts (Atom)