You are on page 1of 33

PROGRAM 1:

AIM: To Print “Welcome To Java” on the screen.


CODE:
class welcome

public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

System.out.println("Welcome to Java");

DESCRIPTION: We use the println function to print “Welcome to


Java” on the screen. System is a predefined class that provides access
to the system. Out is the outputstream that is connected to the
console.

println()-Display the String which is to it.


PROGRAM 2:
AIM: To take a number from user and reverse it.
CODE:
import java.util.Scanner;

class rev

public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

System.out.println("Enter the number");

Scanner s = new Scanner(System.in);

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);

DESCRIPTION: We use the nextInt function to take an integer


from the user and after reversing it we display to the user.

Scanner is a predefined class in the util pakage. We import it using


the import function.
PROGRAM 3:
AIM: To print the pattern

CODE:
class pattern

public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

int i=0,j=0;

for(i=0;i<=3;i++)

for(j=0;j<=i;j++)

System.out.print("*");

System.out.println();

DESCRIPTION: We use 2 nested loops to print the pattern. The


first loop goes from i=0 to 3 and the second loop uses j and
increments j until j gets equal to i. We print * in the inner loop and a
space in outer loop.
PROGRAM 4:
AIM: To print the pattern

CODE:
class pattern2

public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

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();

}}}

DESCRIPTION: We use 2 loops within a loop to print the pattern.


The outer loop goes from i=1 to 5. The first inner loop uses j and
increments j until j gets equal to 5-i.The second inner loop uses k and
increment k until k gets equal to 5-j.
PROGRAM 5:
AIM: To print the pattern

CODE:
class pattern3

public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

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("*");}

for(l=1;l<=k-2;l++) {System.out.print("*");}

System.out.println();}}}

DESCRIPTION: We use 3 loops within a loop to print the pattern.


The outer loop goes from i=1 to 5. The first inner loop uses j and
increments j until j gets equal to 5-i.The second inner loop uses k and
increment k until k gets equal to 5-j.The third inner loop uses I and
increments I until I gets equal to k-2.We print a space in the first
inner loop,* in the second inner loop,* in the third inner loop and
space and space and space in the outer loop.
PROGRAM 6:
AIM: Create a class student having data members name,
rollno,marks in 3 subjects and member function setdata() to get
values, getdata() to display values ,per() to calculate percentage and
allotbranch() to allot branch according to percentage.

CODE:
import java.util.Scanner;

class student

String name;

int rollno;

String branch;

float m1,m2,m3,perc;

void setdata()

Scanner s= new Scanner(System.in);

System.out.println("Enter name");

name=s.nextLine();

System.out.println("Enter roll number");

rollno=s.nextInt();

System.out.println("Enter marks in 1st subject");

m1=s.nextFloat();

System.out.println("Enter marks in 2nd subject");

m2=s.nextFloat();

System.out.println("Enter marks in 3rd subject");

m3=s.nextFloat();
}

void percentage()

{perc=(m1+m2+m3)/3;}

void allotbranch()

{ if(perc > 90)

{branch="computer sciences";}

else if(perc >85 && perc <=90)

{branch="electronics";}

else if(perc >80 && perc <=85)

{branch="electrical";}

else if(perc >85 && perc <=90)

{branch="mechanical";}

else if(perc >85 && perc <=90)

{branch="civil";}

void getdata()

System.out.println("Name is:" + name);

System.out.println("Rollno is:" + rollno);

System.out.println("Marks in 1st subject are:" + m1);

System.out.println("Marks in 2nd subject are:" + m2);

System.out.println("Marks in 3rd subject are:" + m3);

System.out.println("Percentage is:" + perc);

System.out.println("Branch alloted is:" + branch);

class studentdetails{
public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

student s1= new student();

s1.setdata();

s1.percentage();

s1.allotbranch();

s1.getdata();}}

DESCRIPTION: We create an object s1 of student and then use


setdata() to take values form user,per() to calculate percentage,
alootbranch() to allot branch and getdata() to display values to user.
PROGRAM 7:
AIM: Display Student having highest %.

CODE:
import java.util.Scanner;

class person

String pname;

int page;

String phone;

void getdata()

Scanner s=new Scanner(System.in);

System.out.println("Enter name of the person");

pname=s.next();

System.out.println("enter phone number");

phone=s.next();

System.out.println("enter age");

page=s.nextInt();

void putdata()

System.out.println("person name" + pname);

System.out.println(" phone number" +phone);

System.out.println("person's age"+ page);


}

class student extends person

int sid;

int m1,m2,m3;

float per;

void getdata()

super.getdata();

Scanner s=new Scanner(System.in);

System.out.println(" enter student id, marks 0f 3 sub");

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()
{

System.out.println("Student id is " + sid);

super.putdata();

System.out.println("m1" + m1);

System.out.println(" m2"+ m2);

System.out.println("m3"+ m3);

System.out.println("Percentage is "+ per);

class employee extends person

int eid;

String branch;

int nop;

void getdata()

super.getdata();

Scanner s=new Scanner(System.in);

System.out.println("enter branch , number of publication and employee ID");

branch = s.next();

nop=s.nextInt();

eid=s.nextInt();

void putdata()
{

System.out.println("Employee id is " + eid);

super.putdata();

System.out.println("number of publications" + nop);

System.out.println(" Branch"+ branch);

class inheritance

public static void main( String args[])

float max,maxe;

int maxid,i,maxide,ke=0,kes=0;

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

student[] s=new student[2];

for(int i1=0;i1<2;i1++)

s[i1]=new student();

employee[] e=new employee[2];

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()

{ Scanner s=new Scanner(System.in);

System.out.println("enter person name");

name=s.nextLine();

System.out.println("enter person address");

address=s.nextLine();

System.out.println("enter person age");

age=s.nextInt();

System.out.println("enter person phone no");

pno=s.nextInt();

void display()

{ System.out.println("Name= "+name);

System.out.println("Address= "+address);

System.out.println("Age= "+age);

System.out.println("Phone no= "+pno);

class student extends person

{ int rno,year;
int[] m=new int[5];

String branch;

double percent;

void input()

{ super.input();

Scanner s=new Scanner(System.in);

System.out.println("enter rollno,branch and year of student");

rno=s.nextInt();

branch=s.next();

year=s.nextInt();

System.out.println("enter marks of 5 subjects");

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);
}

class employee extends person

{ int empid,no_of_publication;

String b;

void input()

{ super.input();

Scanner s=new Scanner(System.in);

System.out.println("enter employee id");

empid=s.nextInt();

System.out.println("enter no of publications");

no_of_publication=s.nextInt();

System.out.println("enter branch of employee");

b=s.next();

void display()

{ super.display();

System.out.println("employee id= "+empid);

System.out.println("no of publications= "+no_of_publication);

System.out.println("branch= "+b);

class Main

{ public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

int i,j=0;

double temp;

student[] s=new student[3];


employee[] e=new employee[3];

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;

System.out.println("topper is: ");

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;

System.out.println("employee of the year: ");

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:

 Single inheritance(only one super class)


 Multilevel inheritance(Derived from a derived class)
 Hierarchical inheritance(one super class, many subclasses)
PROGRAM 9:
AIM- Write a program to study access modifiers in same package and in different
package.

CODING-
a.java

package p1;

public class a

{ private int pri=1;

protected int pro=2;

public int pub=3;

int def=4;

class b extends a

{ void display()

{ //System.out.println("pri= "+pri); pri has private access in p1.a

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("pri= "+pri); pri has private access in p1.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

{ public static void main(String args[])

{ 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.*;

class b extends p1.a

{ void display()

{ //System.out.println("pri= "+pri); pri has private access in p1.a

System.out.println("pro= "+pro);

System.out.println("pub= "+pub);

//System.out.println("def= "+def); def is not public in p1.a; cannot be


accessed from outside package

}
class c

{ void display()

{ a a2=new a();

//System.out.println("pri= "+a2.pri); pri has private access in p1.a

//System.out.println("pro= "+a2.pro); pro has protected access in p1.a

System.out.println("pub= "+a2.pub);

//System.out.println("def= "+a2.def); def is not public in p1.a; cannot be


accessed from outside package

class pack1

{ public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

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

{ private int pri=1;

protected int pro=2;

public int pub=3;

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("pri="+pri); pri has private access

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("pri="+abc.pri); pri has private access

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

{ public static void main(String args[])

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

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()

Scanner s=new Scanner(System.in);

System.out.println("Enter the item id : ");

id=s.nextInt();

System.out.println("Enter the item name : ");

name=s.next();

System.out.println("Enter the item price : ");

price=s.nextFloat();

void putdata()

System.out.println("Item id : "+ id);

System.out.println("Item name : "+name);

System.out.println("Item price : "+price);

class Itemmain

{
public static void main(String []args)

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

int a=0,b=0;

float great,min;

Item[] i = new Item[3];

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++)

if(great < i[j].price)

great=i[j].price;

a=j;

if(min>i[j].price)

min=i[j].price;

b=j;

System.out.println("The item with maximum price is : ");

i[a].putdata();

System.out.println("The item with minimum price is : ");


i[b].putdata();

DESCRIPTION: We create object i1,i2,i3 of item and then use


get() to take values for i1,i2,i3 from user.Whichever item have the
maximum price its value are stored in i4 and displayed to user using
i4 . Display() to show the i4 details.
PROGRAM 12:
AIM- Create an applet cycle.

CODING-
import java.applet.*;

import java.awt.*;

//<applet code="cycle.class" height="400" width="400"></applet>

public class cycle extends Applet

public void init() //called only once

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

System.out.println("IN INIT");

public void start() //After init and maximize

System.out.println("IN START");

public void stop() //minimize and when x

System.out.println("IN STOP");

public void destroy() //when x

System.out.println("IN DESTROY");

}
PROGRAM 13:
AIM- Create a hut using applet.

CODING-
import java.applet.*;

import java.awt.*;

//<applet code="hut.class" height="400" width="400"></applet>

public class hut extends Applet

public void init()

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

System.out.println("IN INIT");

public void paint(Graphics g)

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)

Returns DrawLine for line starting at x1,y1 and ending at x2,y2.


PROGRAM 14:
AIM- Create a smiley using applet.

CODING-
import java.applet.*;

import java.awt.*;

//<applet code="smiley.class" height="400" width="400"></applet>

public class smiley extends Applet

public void init()

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

public void paint(Graphics g)

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.*;

//<applet code="abc.class" height="400" width="400"></applet>

public class abc extends Applet

public void init()

System.out.println("Adit Singhal (2k8-MRCE-CSE-003)");

System.out.println("IN INIT");

public void start()

System.out.println("IN START");

public void paint(Graphics g)

g.setColor(Color.red);

Font a=new Font("Comic Sans MS",Font.BOLD,20);

g.setFont(a);

g.drawString("HELLO",100,100); }}

You might also like