0% found this document useful (0 votes)
25 views

Java Programs

The document contains multiple Java programs demonstrating different OOP concepts like classes, objects, methods, constructors, method overloading, arrays etc. It includes programs to create student and item classes, reverse a number, use array of objects, employee class, method overloading and parameterized/default constructors.

Uploaded by

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

Java Programs

The document contains multiple Java programs demonstrating different OOP concepts like classes, objects, methods, constructors, method overloading, arrays etc. It includes programs to create student and item classes, reverse a number, use array of objects, employee class, method overloading and parameterized/default constructors.

Uploaded by

Aditya Borle
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Programs

/*Create clasas student having methods getmarks and disp. The getmarks should accept
rno,name and marks of student anddisp should print the same */
import java.io.*;

import java.util.*;

class student

String name;

int rno,marks;

Scanner sc= new Scanner(System.in);

void getdata()

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

name=sc.nextLine();

System.out.println("Enter rno: ");

rno=sc.nextInt();

System.out.println("Enter marks: ");

marks=sc.nextInt();

void disp()

System.out.println("\nRollno: "+rno);

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

System.out.println("Marks : "+marks);

class ExTestl

public static void main(String args[])

student s=new student();


s.getdata();

s.disp();

/*Define a class Item having data member code and price. Accept data for one object and
display it*/
import java.io.*;

import java.util.*;

class item

String code;

int price;

Scanner sc= new Scanner(System.in);

void getdata()

System.out.print("Enter code : ");

code=sc.nextLine();

System.out.print("Enter price: ");

price=sc.nextInt();

void disp()

System.out.println("\nCode: "+code);

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

class itemEx

public static void main(String args[])

{
item obj= new item();

obj.getdata();

obj.disp();

/* Define a class having one 3-digit number as a data member .Intilize and display reverse
of that number */
import java.io.*;
import java.util.*;

class test
{
int no,rem;
void setval()
{

no=123;
}
void rev()
{
System.out.println("reverse is: ");

while(no>0)
{
rem=no%10;
System.out.print(rem);
no=no/10;

}
}
}
class ExTest2
{
public static void main(String args[])
{
test obj= new test();

obj.setval();
obj.rev();
}
}

/*Create Class student having methods getmarks and disp. The getmarks should accept
rno, name and marks of student and disp should print the same. Create array of objects
and execute the task for 5 students.*/
import java.io.*;
import java.util.*;
class student
{

String name;
int rno,marks;
Scanner sc= new Scanner(System.in);
void getdata()
{

System.out.print("\nEnter name,roll no and marks: ");


name=sc.next();
rno=sc.nextInt();
marks=sc.nextInt();
}

void disp()
{
System.out.println("\nRollno:"+rno);
System.out.println("Name:" +name);
System.out.println("Marks:"+marks);
}
}
class Test3

{
public static void main(String args[])
{
int i;
student[] obj;

obj=new student[5];
for(i=0;i<5;i++)
{
obj[i]=new student();
}

for(i=0;i<5;i++)
{
obj[i].getdata();
obj[i].disp();
}

}
}

/*Define a class “Employee” with data members empid,name and salaray. Accept data for
five objects using Array of objects and print it*/
import java.io.*;
import java.util.*;
class employee
{
String name;
int empid,sal;
Scanner sc=new Scanner(System.in);
void getdata()
{
System.out.println("\n Enter employee name, id and salary: ");
name=sc.nextLine();
empid=sc.nextInt();
sal=sc.nextInt();
}
void display()
{
System.out.println("\n Employee name is : "+name);
System.out.println("\n Employee id is : "+empid);
System.out.println("\n Employee salary is : "+sal);
}
}
class Demotest
{
public static void main(String args[])
{
int i;
employee[] obj;
obj =new employee[5];
for(i=0;i<5;i++)
{
obj[i]=new employee();
obj[i].getdata();
}
System.out.println("\n Records : ");
for(i=0;i<5;i++)
{
obj[i].display();
}
}
}

/*WAP of method overloading */


import java.io.*;
import java.util.*;
class area
{
double a;
void calarea(double radius)
{
a=3.14*radius*radius;
System.out.println("Area of circle : "+a);
}
void calarea(double base,double height)
{
a=0.5*base*height;
System.out.println("Area of triangle : "+a);
}
}
class Exmo
{
public static void main(String args[])
{
area obj =new area();
obj.calarea(5);
obj.calarea(8,5);
}
}
/*Create a class with 3 data members. Define a constructor to set values of first two data
members. Define a method to calculate addition of numbers and print the result.*/
import java.io.*;
import java.util.*;
class Cons
{
int nl,n2,sum;
Cons()
{
System.out.println("This is default constructor");
nl=10;
n2=5;
}
void cal()
{
sum=n1+n2;
System.out.println("Summation is "+sum);
}
}
class Consl
{
public static void main(String args[])
{
Cons obj=new Cons();
obj.cal();
}
}

/*Create a class with 3 data members. Define a parameterized constructor to set values of
first two data members. Define a method to calculate addition of numbers and print
result*/
import java.io.*;
import java.util.*;
class Constructor
{
int nl,n2,sum;
Constructor(int x, int y)
{
System.out.println("This is parameterized constructor");
nl = x;
n2 = y;
}
void cal()
{
sum=n+n2;
System.out.println("Summation is "+sum);
}
}
class Construct
{
public static void main(String args[])
{
Constructor obj=new Constructor(10,20);
obj.cal();
}
}

You might also like