JP-1
Print “Hello World”
Code:-
class Simple
{
public static void main(String args[])
{
System.out.println("Hello world");
}
}
Output:-
Write a java program to display table of a number
Code:-
class Simple
{
public static void main(String args[])
{
int val=1;
int i;
System.out.println("Table of 2");
for(i=1;i<11;i++)
{
val=2*i;
System.out.println(val);
}
}
}
Output:-
Create a new class with name demo
Code:-
class demo
{
void Table(int x)
{
int val=1;
int i;
System.out.println("Table of "+x);
for(i=1;i<11;i++)
{
val=x*i;
System.out.println(val);
}
}
}
class Simple
{
public static void main(String args[])
{
demo i1=new demo();
i1.Table(6);
}
}
Output:-
Practical 1A
Write a java code to create a class and copy constructor
Code:-
public class Fruit
{
private double fprice;
private String fname;
Fruit(double fPrice,String fName)
{
fprice=fPrice;
fname=fName;
}
Fruit(Fruit fruit)
{
System.out.println("\nAfter invoking Copy Constructer:\n");
fprice=fruit.fprice;
fname=fruit.fname;
}
double showPrice()
{
return fprice;
}
String showName()
{
return fname;
}
public static void main(String args[])
{
Fruit f1=new Fruit(450,"Mango");
System.out.println("Name of the First Fruit: "+f1.showName());
System.out.println("Price of the First Fruit: "+f1.showPrice());
Fruit f2=new Fruit(f1);
System.out.println("Name of the Second Fruit: "+f2.showName());
System.out.println("Price of the Second Fruit: "+f2.showPrice());
}
}
Output:-
Write a java program to demonstrate Constructer Overloading
Code:-
class Box
{
double width, height, depth;
Box(double w,double h,double d)
{
width=w;
height=h;
depth=d;
}
Box()
{
width=height=depth=0;
}
Box(double len)
{
width=height=depth=len;
}
double volume()
{
return width*height*depth;
}
}
public class Test
{
public static void main(String args[])
{
Box mybox1=new Box(10,20,15);
Box mybox2=new Box();
Box mycube=new Box(7);
double vol;
vol=mybox1.volume();
System.out.println("Volume of mybox1: "+vol);
vol=mybox2.volume();
System.out.println("Volume of mybox2: "+vol);
vol=mycube.volume();
System.out.println("Volume of mycube: "+vol);
}
}
Output:-
Write a java program for default constructor
Code:-
class Bike11
{
Bike11()
{
System.out.println("Bike is created");
}
public static void main(String args[])
{
Bike11 b=new Bike11();
}
}
Output:-
Practical 1B
Write a program to implement method overloading
Code:-
class Adder
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
class TestOverloading
{
public static void main(String args[])
{
System.out.println(Adder.add(11,11));
System.out.println(Adder.add(11,11,11));
}
}
Output:
Practical 1C
Write a program to implement static methods
Code:
class Calculate
{
static int cube(int x)
{
return x*x*x;
}
public static void main(String args[])
{
int result=Calculate.cube(5);
System.out.print(result);
}
}
Output:
Write java program to illustrate static variables
Code:
> With static keyword
class Counter
{
static int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
> Without static variable
class Counter
{
int count=0;
Counter()
{
count++;
System.out.println(count);
}
public static void main(String args[])
{
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
Comments
Post a Comment