Java Programs
Java 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;
void getdata()
name=sc.nextLine();
rno=sc.nextInt();
marks=sc.nextInt();
void disp()
System.out.println("\nRollno: "+rno);
System.out.println("Name: "+name);
System.out.println("Marks : "+marks);
class ExTestl
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;
void getdata()
code=sc.nextLine();
price=sc.nextInt();
void disp()
System.out.println("\nCode: "+code);
System.out.println("Price : "+price);
class itemEx
{
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()
{
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();
}
}
}
/*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();
}
}