Aim: Code:: Program 1
Aim: Code:: Program 1
System.out.println("Welcome to Java");
class rev
int x=s.nextInt();
int res=0,rem;
while(x>0)
rem=(x%10);
x=(x/10);
res=(res*10+rem);
System.out.print(res);
CODE:
class pattern
int i=0,j=0;
for(i=0;i<=3;i++)
for(j=0;j<=i;j++)
System.out.print("*");
System.out.println();
CODE:
class pattern2
int i=1,j,k,l;
for(i=1;i<=5;i++)
for(j=1;j<=5-i;j++)
System.out.print(" ");
for(k=1;k<=5-j;k++)
System.out.print("*");
System.out.println();
}}}
CODE:
class pattern3
int i=1,j,k,l;
for(i=1;i<=5;i++)
for(k=1;k<=5-j;k++) {System.out.print("*");}
for(l=1;l<=k-2;l++) {System.out.print("*");}
System.out.println();}}}
CODE:
import java.util.Scanner;
class student
String name;
int rollno;
String branch;
float m1,m2,m3,perc;
void setdata()
System.out.println("Enter name");
name=s.nextLine();
rollno=s.nextInt();
m1=s.nextFloat();
m2=s.nextFloat();
m3=s.nextFloat();
}
void percentage()
{perc=(m1+m2+m3)/3;}
void allotbranch()
{branch="computer sciences";}
{branch="electronics";}
{branch="electrical";}
{branch="mechanical";}
{branch="civil";}
void getdata()
class studentdetails{
public static void main(String args[])
s1.setdata();
s1.percentage();
s1.allotbranch();
s1.getdata();}}
CODE:
import java.util.Scanner;
class person
String pname;
int page;
String phone;
void getdata()
pname=s.next();
phone=s.next();
System.out.println("enter age");
page=s.nextInt();
void putdata()
int sid;
int m1,m2,m3;
float per;
void getdata()
super.getdata();
sid=s.nextInt();
m1=s.nextInt();
m2=s.nextInt();
m3=s.nextInt();
void percentage()
float total;
total=(float)(m1+m2+m3)/300;
per=total*100;
void putdata()
{
super.putdata();
System.out.println("m1" + m1);
System.out.println("m3"+ m3);
int eid;
String branch;
int nop;
void getdata()
super.getdata();
branch = s.next();
nop=s.nextInt();
eid=s.nextInt();
void putdata()
{
super.putdata();
class inheritance
float max,maxe;
int maxid,i,maxide,ke=0,kes=0;
for(int i1=0;i1<2;i1++)
s[i1]=new student();
for(int i1=0;i1<2;i1++)
e[i1]=new employee();
for(i=0;i<2;i++)
s[i].getdata();
s[i].percentage();
}
max=s[0].per;
maxid=s[0].sid;
for(i=0;i<2;i++)
if(max<=s[i].per)
max=s[i].per;
maxid=s[i].sid;
kes=i;
s[kes].sid=maxid;
s[kes].putdata();
for(i=0;i<2;i++)
e[i].getdata();
maxe=e[0].nop;
maxide=e[0].eid;
int k=0;
for(i=0;i<2;i++)
if(maxe<=e[i].nop)
{
maxe=e[i].nop;
maxide=e[i].eid;
ke=i;
e[ke].eid=maxide;
e[ke].putdata();
}
PROGRAM 8:
AIM- Create class employee and student which inherit features of class person and
write a program to calculate topper and employee of the year.
CODING-
import java.util.*;
class person
{ String name,address;
int age,pno;
void input()
name=s.nextLine();
address=s.nextLine();
age=s.nextInt();
pno=s.nextInt();
void display()
{ System.out.println("Name= "+name);
System.out.println("Address= "+address);
System.out.println("Age= "+age);
{ int rno,year;
int[] m=new int[5];
String branch;
double percent;
void input()
{ super.input();
rno=s.nextInt();
branch=s.next();
year=s.nextInt();
for(int i=0;i<5;i++)
m[i]=s.nextInt();
void per()
{ int sum=0;
for(int i=0;i<5;i++)
sum=sum+m[i];
percent=sum/5;
void display()
{ super.display();
System.out.println("rollno= "+rno);
System.out.println("branch= "+branch);
System.out.println("year= "+year);
System.out.println("marks= ");
for(int i=0;i<5;i++)
System.out.println(m[i]);
System.out.println("percentage= "+percent);
}
{ int empid,no_of_publication;
String b;
void input()
{ super.input();
empid=s.nextInt();
System.out.println("enter no of publications");
no_of_publication=s.nextInt();
b=s.next();
void display()
{ super.display();
System.out.println("branch= "+b);
class Main
int i,j=0;
double temp;
for(i=0;i<3;i++)
{ s[i]=new student();
e[i]=new employee();
s[i].input();
s[i].per();
e[i].input();
double p=s[0].percent;
for(i=0;i<3;i++)
{ if(p<s[i].percent)
{ p=s[i].percent;
j=i;
s[j].display();
int a=e[0].no_of_publication;
for(i=0;i<3;i++)
{ if(a<e[i].no_of_publication)
{ a=e[i].no_of_publication;
j=i;
e[j].display();
}
DESCRIPTION- The mechanism of driving a new class from an old one is called
inheritance. The old class is known as base class or super class or parent class and the
new one is called the subclass or derived class or child class. The inheritance allows
subclasses to inherit all the variables and methods of their parent classes. Inheritance
may take different forms:
CODING-
a.java
package p1;
public class a
int def=4;
class b extends a
{ void display()
System.out.println("pro= "+pro);
System.out.println("pub= "+pub);
System.out.println("def= "+def);
class c
{ void display()
{ a a1=new a();
System.out.println("pro= "+a1.pro);
System.out.println("pub= "+a1.pub);
System.out.println("def= "+a1.def);
}
}
pack.java
package p1;
class pack
{ System.out.println("27/8/2010");
System.out.println("MONIKA VERMA");
System.out.println("2K8-MRCE-CS-031");
b b1=new b();
c c1=new c();
b1.display();
c1.display();
pack1.java
package p2;
import p1.*;
{ void display()
System.out.println("pro= "+pro);
System.out.println("pub= "+pub);
}
class c
{ void display()
{ a a2=new a();
System.out.println("pub= "+a2.pub);
class pack1
b b1=new b();
c c1=new c();
b1.display();
c1.display();
DESCRIPTION- A variable declared as public has the widest possible visibility and
is accessible everywhere. When no access modifier is specified the member defaults to
a limited version of public accessibility known as default access. The default access
field makes fields visible only in same package, but not in other packages. The visibility
level of a protected field lies in between the public access and default access. That is,
the protected modifier makes the fields visible not only to all classes and subclasses in
the same package but also to subclasses in other packages. The non-subclasses in other
packages cannot access the protected members. Private members are accessible only
within their own class. They cannot be inherited by subclass and therefore not
accessible in subclasses.
PROGRAM 10:
AIM- Write a program to study access modifiers in case of inheritance.
CODING-
class a
int def=4;
void display()
{ System.out.println("base class");
System.out.println("pri="+pri);
System.out.println("pro="+pro);
System.out.println("pub="+pub);
System.out.println("def="+def);
class b extends a
{ int x=5;
void display()
{ super.display();
System.out.println("derived class");
System.out.println("pro="+pro);
System.out.println("pub="+pub);
System.out.println("def="+def);
System.out.println("x="+x);
}
}
class c
{ int y=6;
void display()
{ System.out.println("another class");
a abc=new a();
System.out.println("pro="+abc.pro);
System.out.println("pub="+abc.pub);
System.out.println("def="+abc.def);
System.out.println("y="+y);
class demo
b ob=new b();
c ob1=new c();
ob.display();
ob1.display();
DESCRIPTION- A variable declared as public has the widest possible visibility and
is accessible everywhere. When no access modifier is specified the member defaults to
a limited version of public accessibility known as default access. The default access
field makes fields visible only in same package, but not in other packages. The visibility
level of a protected field lies in between the public access and default access. That is,
the protected modifier makes the fields visible not only to all classes and subclasses in
the same package but also to subclasses in other packages. The non-subclasses in other
packages cannot access the protected members. Private members are accessible only
within their own class. They cannot be inherited by subclass and therefore not
accessible in subclasses.
PROGRAM 11:
AIM- Define a class item and find the item with maximum and minimum price.
CODING-
import java.util.*;
class Item
int id;
String name;
float price;
void getdata()
id=s.nextInt();
name=s.next();
price=s.nextFloat();
void putdata()
class Itemmain
{
public static void main(String []args)
int a=0,b=0;
float great,min;
for(int j=0;j<3;j++)
i[j]=new Item();
i[j].getdata();
great=i[0].price;
min=i[0].price;
for(int j=1;j<3;j++)
great=i[j].price;
a=j;
if(min>i[j].price)
min=i[j].price;
b=j;
i[a].putdata();
CODING-
import java.applet.*;
import java.awt.*;
System.out.println("IN INIT");
System.out.println("IN START");
System.out.println("IN STOP");
System.out.println("IN DESTROY");
}
PROGRAM 13:
AIM- Create a hut using applet.
CODING-
import java.applet.*;
import java.awt.*;
System.out.println("IN INIT");
g.setColor(Color.red);
g.drawRect(50,200,100,100);
g.drawRect(150,200,200,100);
g.drawLine(50,200,100,100);
g.drawLine(100,100,150,200);
g.drawLine(100,100,300,100);
g.drawLine(300,100,350,200);
g.drawRoundRect(75,225,50,75,10,20);
g.drawRect(225,225,50,50);
g.drawLine(250,225,250,275);
g.drawLine(225,250,275,250); }}
DESCRIPTION:
DrawLine (int x1,int y1,int x2,int y2)
CODING-
import java.applet.*;
import java.awt.*;
g.setColor(Color.yellow);
g.fillArc(150,150,100,100,0,360);
g.setColor(Color.black);
g.fillArc(180,170,10,10,0,360);
g.fillArc(220,170,10,10,0,360);
g.setColor(Color.red);
g.drawArc(170,180,70,45,360,-180);
}
PROGRAM 15:
AIM- Create a Hello using applet.
CODING-
import java.applet.*;
import java.awt.*;
System.out.println("IN INIT");
System.out.println("IN START");
g.setColor(Color.red);
g.setFont(a);
g.drawString("HELLO",100,100); }}