Monday, 22 July 2013

Display a String message in Android using Toast Method

Toast method is used to display messages in Android.
It can only display String Variables.
To display any Integer variable value convert the Integer into String.

 Example    
   int x=10; 
  String a=""+ x;

Toast Method Syntax:

Toast.makeText("classname.this",a,2000).show();

It will display String 'a' for 2secs.

Friday, 19 July 2013

Number Picker Example in Android

Setting Up a Number Picker in Android

    final NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1);

        np.setMaxValue(20);

        np.setMinValue(1);

        np.setWrapSelectorWheel(true);

        int value =np.getValue();                 // get the selected value  //write this on button click

Passing Values from one page to other page in android

Passing Values from  one activity to other activity in android


Use Bundle to accomplish this task





1st activity
final Bundle bundle = new Bundle();
bundle.putString("data", name);
i.putExtras(bundle);  //before starting 2nd activity  i.e startActivity(i);

2nd Activity
final Bundle bundle = new Bundle();
Bundle bundle = getIntent().getExtras();
  String name = bundle.getString("data");

Tuesday, 11 June 2013

Shell Script to accept a number n from user and display summation of the following series. 1! + 2! + 3! + … + n!

Code:
echo -n "Enter the number : "
read x
i=1
sum=0
while test $i -le $x
do
        fact=1
        a=1
        while test $a -le $i
        do
                let fact=$fact*$a
                let a=$a+1
        done
        let sum=$sum+$fact
        let i=$i+1
done

let i=2
echo -n "1!"
while test $i -le $x
do
        echo -n " + $i !"
        let i=$i+1
done
echo " = $sum"

Output:
[ty2011330@localhost ~]$ sh j08.sh
Enter the number : 4
1! + 2 ! + 3 ! + 4 ! = 33

Shell Script to accept a number n from user and display the squares of all numbers from 1 to n

Code:
echo -n "Enter the number : "
read n
c=1
a=1
while test $c -le $n
do
        let a=$c*$c
        echo "$c square = $a"
        let c=c+1
done


Output:
[ty2011330@localhost ~]$ sh j09.sh
Enter the number : 5
1 square = 1
2 square = 4
3 square = 9
4 square = 16
5 square = 25

Shell Script to accept 5 subject marks of a student and display whether the student got first class, second class, third class or Student failed .

Code:
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let per=$m1+$m2+$m3+$m4+$m5
let per=per/5
echo "Percentage of students is $per %"
if [ $per -lt 35 ]
then
        echo "Student Failed."
elif [ $per -ge 35 -a $per -lt 45 ]
then
        echo "Student passed with third class."
elif [ $per -ge 45 -a $per -lt 60 ]
then
        echo "Student passed with second class."
elif [ $per -ge 60 -a $per -lt 75 ]
then
        echo "Student passed with first class."
fi
Output:
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
20 15 32 14 34
Percentage of students is 23 %
Student Failed.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
35 45 35 36 40
Percentage of students is 38 %
Student passed with third class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
50 55 45 56 60
Percentage of students is 53 %
Student passed with second class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
60 78 89 96 47
Percentage of students is 74 %
Student passed with first class

Shell Script to accept a number from user and print the day of the week using case … in

Code:
echo -n "Enter the day number : "
read num
case $num in
        1) echo "Sunday";;
        2) echo "Monday";;
        3) echo "Tuesday";;
        4) echo "Wednesday";;
        5) echo "Thursday";;
        6) echo "Friday";;
        7) echo "Saturday";;
        *) echo "Enter any number between 1 to 7."
esac


Output:
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 1
Sunday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 3
Tuesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 2
Monday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 4
Wednesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 5
Thursday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 6
Friday