Q.
1 Write a Java program to demonstrate data types and control
structures.
Program:
class Demo {
public static void main(String[] args) {
int a = 10;
char c = 'A';
if(a > 5)
[Link]("a is greater than 5");
switch(c){case 'A': [Link]("Character is A");}
for(int i=1;i<=3;i++) [Link](i);
}
}
Output:
a is greater than 5
Character is A
1
2
3
Q.2 Write a Java program to calculate factorial using recursion
and iteration.
Program:
class Factorial {
static int factRec(int n){return n==1?1:n*factRec(n-1);}
public static void main(String[] args){
int n=5,fact=1;
for(int i=1;i<=n;i++) fact*=i;
[Link](factRec(n));
[Link](fact);
}
}
Output:
120
120
Q.3 Write a Java program to find the second largest element in an
array.
Program:
class SecondLargest {
public static void main(String[] args){
int[] a={10,20,30,40};
int max=a[0], second=a[0];
for(int i:a){
if(i>max){second=max; max=i;}
else if(i>second && i!=max) second=i;
}
[Link](second);
}
}
Output:
30
Q.4 Write a Java program to check whether a string is palindrome.
Program:
class Palindrome {
public static void main(String[] args){
String s="madam", r="";
for(int i=[Link]()-1;i>=0;i--) r+=[Link](i);
if([Link](r)) [Link]("Palindrome");
}
}
Output:
Palindrome
Q.9 Create a simple Java GUI using AWT with a Button that
changes background color when clicked.
Program:
import [Link].*;
import [Link].*;
class ColorChange extends Frame implements ActionListener{
Button b;
ColorChange(){
b=new Button("Click"); add(b);
[Link](this);
setSize(200,200); setVisible(true);
}
public void actionPerformed(ActionEvent e){ setBackground([Link]); }
public static void main(String[] args){ new ColorChange(); }
}
Output:
A frame window appears with a button labeled 'Click'.
On clicking the button, the background color changes to yellow.
Q.17 Create a web page that displays a table using rowspan and
colspan.
Program:
<html>
<body>
<table border="1">
<tr><th>Name</th><th colspan="2">Marks</th></tr>
<tr><td rowspan="2">Aashish</td><td>Java</td><td>90</td></tr>
<tr><td>CN</td><td>85</td></tr>
</table>
</body>
</html>
Output:
A table is displayed on the web page.
The Name cell spans two rows and Marks spans two columns.