October 9, 2011

Access Modifiers for Class members in Java

There are 3 access modifiers for class members: public, private, protected
and 4 access levels: public, private, protected, default (none of the modifiers present)

public access level
A public member (variable or method) can be accessed from another class regardless of the package in which the other class is present.
public members can be inherited by a subclass regardless of the package in which the subclass is present.

package info.icontraining.p1;
public class Test {
   public int i;           // public member variable
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;       // public members can be accessed 
                     // by code in another class that is
   }                 // present in a different package 
}

package info.icontraining.p2;

import info.icontraining.p1.Test;
public class B extends Test {
   public void m1() {
      i = 3;         // public members can be inherited
   }                 // by another class that is present
}                    // in a different package


private access level
A private member cannot be accessed by code in any class except by code in the class in which the private member is present.
private members cannot be inherited by any subclass.

package info.icontraining.p1;
public class Test {
   private int i;           // private member variable
   public void m1() {
      i = 3;           // private members can be accessed
   }                   // only by code within the class
}                      // in which it is declared

package info.icontraining.p1;
public class A {
   public void method1() {
      Test t = new Test();
      t.i = 3;           // this line will not compile
   }
}

package info.icontraining.p1;
public class B extends Test {
   public void method1() {
      i = 3;            // this line will not compile
   }
}

default access level
A default member can be accessed from another class if both classes are present in the same package.
default members can be inherited by a subclass is both classes are present in the same package.

package info.icontraining.p1;
public class Test {
   int i;              // default member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be 
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // will not compile, cannot be
   }               // inherited in another package
}

protected access level
A protected member can be accessed by another class in the same package only.
However, protected members can be inherited by a subclass present in a different package also.


package info.icontraining.p1;
public class Test {
   protected int i;              // protected member variable
}

package info.icontraining.p1;
public class A {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // can be accessed by a class
   }                   // in same package
}

package info.icontraining.p1;
public class B extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in same package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class C {
   public void m1() {
      Test t = new Test();
      t.i = 3;         // will not compile, cannot be
   }                   // accessed in another package
}

package info.icontraining.p2;
import info.icontraining.p1.Test;
public class D extends Test {
   public void m2 {
      i = 3;       // can be inherited by a class
   }               // in another package
}

October 8, 2011

Timer Interval functions in Javascript

Code Example


<html>
<head>
<script type="text/javascript">

var timerId;

var setAlerts = function() {
   timerId = setInterval("myfunction();",3000);
}

var removeAlerts = function() {
   clearInterval(timerId);
}

function myfunction() {
   alert("Hi");
}

window.onload = function() {
   document.getElementById("setButton").onclick=setAlerts;
   document.getElementById("clearButton").onclick=removeAlerts;
}

</script>
</head>
<body>

<button id="setButton">Set Interval</button><br/>
<button id="clearButton">Clear Interval</button><br/>

</body>
</html>

Javascript to set cookies and get hold of a specific cookie

Write Javascript code to store a cookie and retrieve it - also check if cookies are enabled on the client.

Solution

<html>
<head>
<script type="text/javascript">

var cookieFunction = function() {
   
if (navigator.cookieEnabled) {
var name = prompt("Enter cookie name");
      var value= prompt("Enter cookie value");
      if ((name) && (value)) {
         var date = new Date("1 Jan 2015 11:30:00");
         document.cookie= name + "="+value+"; expires="+date.toGMTString()+";";
      }
   } else {
      alert("Cookies Not enabled");
   }
}

var getCookie = function() {
   var cookies=document.cookie;
   alert(cookies);
}

var getSpecificCookie = function() {
   var c_name = prompt("Enter cookie name");
   var all_cookies = document.cookie.split( ';' );
   for (i=0;i<all_cookies.length;i++) {
      x = all_cookies[i].substr(0, all_cookies[i].indexOf("="));
      y = all_cookies[i].substr(all_cookies[i].indexOf("=")+1);
      x = x.replace(/^\s+|\s+$/g,"");
      if (x == c_name) {
        alert( unescape(y));
        return;
     }
   }

   alert("Cookie does not exist");
}

window.onload = function() {
   document.getElementById("getCookieButton").onclick=getCookie;
   document.getElementById("getSpecificCookieButton").onclick=getSpecificCookie;
   document.getElementById("diceButton").onclick=rollDice;
}

</script>
</head>
<body>
<button id="cookieButton">Set Cookie</button>
<button id="getCookieButton">Get Cookies</button>
<button id="getSpecificCookieButton">Get Specific Cookie</button>

</body>
</html>

Javascript simulation of rolling of a dice


Write Javascript code to simulate the rolling of a dice. When the user clicks a button, the result should be the simulation of a dice - a value between 1 to 6 is displayed in the String, "Your roll of the dice displayed 3"

Hint: Math.random(), document.getElementById(), innerHTML, onclick event

Solution


<html>
<head>
<script type="text/javascript">

var rollDice = function() {
   var diceElementNode = document.getElementById("diceValue");
   var diceElementNodeChildren = diceElementNode.childNodes;

   if (diceElementNodeChildren[0] != null) {
      diceElementNodeChildren[0].nodeValue = "The dice value is " + Math.ceil((Math.random() * 6));
   } else {
      var diceTextNode = document.createTextNode("");
      diceTextNode.nodeValue ="The dice value is " + Math.ceil((Math.random() * 6));
      diceElementNode.appendChild(diceTextNode);
   }
}

window.onload = function() {
   document.getElementById("diceButton").onclick=rollDice;
}

</script>
</head>
<body>
<button id="diceButton">Roll Dice</button>
<div id="diceValue"></div>

</body>
</html>