1. WAP to add two numbers using command line argument.
class sum
{
public static void main(String ar[])
{
int x,y,s;
x=[Link](ar[0]);
y=[Link](ar[1]);
s=x+y;
[Link]("sum of " + x + " and " + y +" is " +s);
}
}
Output:
[Link] to show the wrapper class.
public class MyClass
{
public static void main(String[] args)
{
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
[Link](myInt);
[Link](myDouble);
[Link](myChar);
}
}
Output:
[Link] to demonstrate for-each loop.
class ForEach
{
public static void main(String args[])
{
int arr[]={12,13,14,44};
int total=0;
for(int i:arr)
{
total=total+i;
}
[Link]("Total: "+total);
}
}
Output:
[Link] to check prime number
public class Prime
{
static void checkPrime(int n)
{
int i,m=0,flag=0;
m=n/2;
if(n==0||n==1)
{
[Link](n+" is not prime number");
}else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
[Link](n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { [Link](n+" is prime number"); }
}
}
public static void main(String args[])
{
checkPrime(1);
checkPrime(3);
checkPrime(17);
checkPrime(20);
}
}
Output:
[Link] to demonstrate anonymous object.
import [Link];
class Anonymous
{
void table(int n)
{
for(int i=1; i<=10; i++)
{
[Link](i*n);
}
}
public static void main(String[] ar)
{
int n;
Scanner sc=new Scanner([Link]);
[Link]("enter a number");
n=[Link]();
new Anonymous().table(n);
}
}
Output:
[Link] to show different type of constructers.
*DEFAULT CONSTRUCTOR:
import [Link].*;
import [Link].*;
import [Link].*;
class DefaultCon
{
int roll=101;
String grade="Graduate";
void display()
{
[Link](roll+" "+grade);
}
public static void main(String args[])
{
DefaultCon c1=new DefaultCon();
DefaultCon c2=new DefaultCon();
[Link]();
[Link]();
}
}
Output:
*PERAMETERIZED CONSTRUCTOR:
class Pcons
{
int id;
String name;
Pcons(int i,String n)
{
id = i;
name = n;
}
void display()
{
[Link](id+" "+name);
}
public static void main(String args[])
{
Pcons s1 = new Pcons(101,"yes");
Pcons s2 = new Pcons(102,"Aryan");
[Link]();
[Link]();
}
}
Output:
7. WAP to show static variable and static function.
class Str{
static int num;
static String mystr;
static{
num = 97;
mystr = "Static keyword in Java";
}
public static void main(String args[])
{
[Link]("Value of num: "+num);
[Link]("Value of mystr: "+mystr);
}
}
Output:
8. WAP to show the use of “This” keyword.
class Ths
{
int instVar;
Ths(int instVar)
{
[Link] = instVar;
}
public static void main(String[] args)
{
Ths obj = new Ths(8);
[Link]("[Link] = " + [Link]);
}
Output:
[Link] for inheritence.
class Inher
{
float salary=40000;
}
class Programmer extends Inher
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
[Link]("Programmer salary is:"+[Link]);
[Link]("Bonus of Programmer is:"+[Link]);
}
}
Output:
[Link] to show method overloading.
class Over
{
public void show(char ch, int numb)
{
[Link] ("The 'show method' is defined for the first time.");
}
public void show(int numb, char ch)
{
[Link] ("The 'show method' is defined for the second time." );
}
}
class Main
{
public static void main (String args[] )
{
Over o1 = new Over();
[Link]('G', 62);
[Link](46, 'S');
}
}
Output:
11. WAP to show method overriding.
class college
{
public void move()
{
[Link]("College is open");
}
}
class univ extends college
{
public void move()
{
[Link]("University is open too");
}
}
public class Stud
{
public static void main(String args[])
{
college a = new college();
college b = new univ();
[Link]();
[Link]();
}
}
Output:
12. WAP to show the use of “Super” keyword.
class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
[Link](color);
[Link]([Link]);
}
}
class Super
{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
}
}
Output:
13. WAP to show the use of abstract class.
abstract class Animal
{
public abstract void sound();
}
public class Dog extends Animal
{
public void sound()
{
[Link]("Woof");
}
public static void main(String args[])
{
Animal obj = new Dog();
[Link]();
}
}
Output:
[Link] to show the use of interface
interface print
{
void printable();
}
class Interface implements print
{
public void printable()
{
[Link]("Hello");
}
public static void main(String args[])
{
Interface obj = new Interface();
[Link]();
}
}
Output:
[Link] to show the use of packages.
package mypack;
class MyPackageClass
{
public static void main(String[] args)
{
[Link]("This is my package!");
[Link]("thanks");
}
}
Output:
[Link] to show the use of exception handling.
class Excep
{
public static void main(String args[])
{
try
{
//code that may raise exception
int data=100/0;
}catch(Exception e)
{
[Link](e);
}
}
}
Output:
17. WAP to show the use of thread.
class test extends Thread
{
public void run()
{
try
{
[Link] ("Thread " + [Link]().getId() + " is running");
}catch (Exception e){
[Link] ("Exception is caught");
}
}
}
public class Threads
{
public static void main(String[] args)
{
int n = 8;
for (int i=0; i<n; i++)
{
test object = new test();
[Link]();
}
}
}
Output:
18. WAP to demonstrate the list in HTML.
<html>
<head>
<title> list</title>
</head>
<body>
<h4>Unorder list</h4>
<ul>
<li>Deepak</li>
<li>Rishabh</li>
<li>Ayushman</li>
</ul>
<h4>Order list</h4>
<ol>
<li>Deepak</li>
<li>Rishabh</li>
<li>Ayushman</li>
</ol>
</body>
</html>
Output:
[Link] to demonstrate the forms in HTML.
<!DOCTYPE html>
<html>
<body>
<form>
Username:<br>
<input type="text" name="username">
<br>
Email id:<br>
<input type="text" name="email_id">
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output:
[Link] to demonstrate the table in HTML.
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>deepak</td>
<td>Sharma</td>
<td>20</td>
</tr>
<tr>
<td>rishabh</td>
<td>Sharma</td>
<td>22</td>
</tr>
<tr>
<td>Ayushman</td>
<td>Sisodia</td>
<td>19</td>
</tr>
</table>
</body>
</html>
Output:
[Link] to demonstrate the applet.
import [Link].*;
import [Link].*;
public class App extends Applet {
public void paint(Graphics g) {
[Link]("Welcome in Java Applet.",100,50);
}
}
[Link]
<HTML>
<BODY>
<div >
<APPLET CODE = "App" WIDTH = "400" HEIGHT = "200"></APPLET>
</div>
</BODY>
</HTML>
OUTPUT:
*1
*2