Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
90 views

Java Practical

The document provides 21 code examples demonstrating various Java programming concepts: 1. Adding two numbers using command line arguments 2. Demonstrating wrapper classes for primitive data types 3. Using a for-each loop to calculate a total 4. Checking if a number is prime 5. Creating an anonymous object 6. Different types of constructors 7. Static variables and methods 8. Using the "this" keyword 9. Inheritance 10. Method overloading 11. Method overriding 12. Using the "super" keyword 13. Abstract classes 14. Interfaces 15. Packages 16. Exception handling 17. Threads 18. Lists in HTML

Uploaded by

Shivam Tenguriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views

Java Practical

The document provides 21 code examples demonstrating various Java programming concepts: 1. Adding two numbers using command line arguments 2. Demonstrating wrapper classes for primitive data types 3. Using a for-each loop to calculate a total 4. Checking if a number is prime 5. Creating an anonymous object 6. Different types of constructors 7. Static variables and methods 8. Using the "this" keyword 9. Inheritance 10. Method overloading 11. Method overriding 12. Using the "super" keyword 13. Abstract classes 14. Interfaces 15. Packages 16. Exception handling 17. Threads 18. Lists in HTML

Uploaded by

Shivam Tenguriya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1. WAP to add two numbers using command line argument.

class sum
{
public static void main(String ar[])
{
int x,y,s;

x=Integer.parseInt(ar[0]);
y=Integer.parseInt(ar[1]);

s=x+y;
System.out.println("sum of " + x + " and " + y +" is " +s);
}
}

Output:
2.WAP to show the wrapper class.

public class MyClass


{
public static void main(String[] args)
{
Integer myInt = 5;
Double myDouble = 5.99;
Character myChar = 'A';
System.out.println(myInt);
System.out.println(myDouble);
System.out.println(myChar);
}
}

Output:
3.WAP 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;
}
System.out.println("Total: "+total);
}
}

Output:
4.WAP 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)
{
System.out.println(n+" is not prime number");
}else
{
for(i=2;i<=m;i++)
{
if(n%i==0)
{
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}
}
public static void main(String args[])
{
checkPrime(1);
checkPrime(3);
checkPrime(17);
checkPrime(20);
}
}

Output:
5.WAP to demonstrate anonymous object.

import java.util.Scanner;
class Anonymous
{
void table(int n)
{
for(int i=1; i<=10; i++)
{
System.out.println(i*n);
}
}
public static void main(String[] ar)
{
int n;
Scanner sc=new Scanner(System.in);
System.out.println("enter a number");
n=sc.nextInt();
new Anonymous().table(n);
}
}

Output:
6.WAP to show different type of constructers.

*DEFAULT CONSTRUCTOR:

import java.util.*;
import java.lang.*;
import java.io.*;
class DefaultCon
{
int roll=101;
String grade="Graduate";
void display()
{
System.out.println(roll+" "+grade);
}
public static void main(String args[])
{
DefaultCon c1=new DefaultCon();
DefaultCon c2=new DefaultCon();
c1.display();
c2.display();
}
}

Output:

*PERAMETERIZED CONSTRUCTOR:
class Pcons
{
int id;
String name;
Pcons(int i,String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id+" "+name);
}
public static void main(String args[])
{
Pcons s1 = new Pcons(101,"yes");
Pcons s2 = new Pcons(102,"Aryan");
s1.display();
s2.display();
}
}

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[])
{
System.out.println("Value of num: "+num);
System.out.println("Value of mystr: "+mystr);
}
}
Output:
8. WAP to show the use of “This” keyword.

class Ths
{
int instVar;
Ths(int instVar)
{
this.instVar = instVar;
}
public static void main(String[] args)
{
Ths obj = new Ths(8);
System.out.println("obj.instVar = " + obj.instVar);
}

Output:
9.WAP for inheritence.

class Inher
{
float salary=40000;
}
class Programmer extends Inher
{
int bonus=10000;
public static void main(String args[])
{
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}

Output:
10.WAP to show method overloading.

class Over
{
public void show(char ch, int numb)
{
System.out.println ("The 'show method' is defined for the first time.");
}
public void show(int numb, char ch)
{
System.out.println ("The 'show method' is defined for the second time." );
}
}
class Main
{
public static void main (String args[] )
{
Over o1 = new Over();
o1.show('G', 62);
o1.show(46, 'S');
}
}

Output:
11. WAP to show method overriding.

class college
{
public void move()
{
System.out.println("College is open");
}
}
class univ extends college
{
public void move()
{
System.out.println("University is open too");
}
}
public class Stud
{
public static void main(String args[])
{
college a = new college();
college b = new univ();
a.move();
b.move();
}
}

Output:
12. WAP to show the use of “Super” keyword.

class Animal
{
String color="white";
}
class Dog extends Animal
{
String color="black";
void printColor()
{
System.out.println(color);
System.out.println(super.color);
}
}
class Super
{
public static void main(String args[]){
Dog d=new Dog();
d.printColor();
}
}
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()


{
System.out.println("Woof");
}
public static void main(String args[])
{
Animal obj = new Dog();
obj.sound();
}
}

Output:
14.WAP to show the use of interface

interface print
{
void printable();
}
class Interface implements print
{
public void printable()
{
System.out.println("Hello");
}
public static void main(String args[])
{
Interface obj = new Interface();
obj.printable();
}
}

Output:
15.WAP to show the use of packages.

package mypack;

class MyPackageClass
{
public static void main(String[] args)
{
System.out.println("This is my package!");
System.out.println("thanks");
}
}

Output:
16.WAP 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)
{
System.out.println(e);
}
}
}
Output:
17. WAP to show the use of thread.

class test extends Thread


{
public void run()
{
try
{
System.out.println ("Thread " + Thread.currentThread().getId() + " is running");
}catch (Exception e){
System.out.println ("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();
object.start();
}
}
}

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:
19.WAP 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:
20.WAP 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:
21.WAP to demonstrate the applet.

import java.applet.*;
import java.awt.*;

public class App extends Applet {


public void paint(Graphics g) {
g.drawString("Welcome in Java Applet.",100,50);
}
}

App.html

<HTML>
<BODY>
<div >
<APPLET CODE = "App" WIDTH = "400" HEIGHT = "200"></APPLET>
</div>
</BODY>
</HTML>

OUTPUT:
*1

*2

You might also like