Java Lab
Java Lab
class Addsub
{
int add(int a,int b)
{
return(a+b);
}
int sub(int a,int b)
{
return(a-b);
}
}
class Muldiv extends Addsub
{
int mul(int a,int b)
{
return(a*b);
}
int div(int a,int b)
{
return(a/b);
}
}
class Mathoperation
{
public static void main(String args[])
{
Muldiv m=new Muldiv();
System.out.println("sum="+m.add(5,7));
System.out.println("difference="+m.sub(20,20));
System.out.println("product="+m.mul(3,4));
System.out.println("division="+m.div(4,2));
}
}
class Variable
{
static int a=10; //Static variable
int b=10; //Instance variable
void display()
{
int c=10; //Local varible
System.out.println("Static= "+a+"Instance= "+b+”Local= ”+c);
a++;
b++;
c++;
}
}
class Staticdemo
{
public static void main(String args[])
{
Variable v=new Variable ();
v.display();
v.display();
}
}
6.A. To find the area and circumference of the circle by
accepting the radius from the user.
import java.util.*;
class Student
{
String eno, name;
int m1,m2,m3,total;
public Student()
{}
public void getinfo()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter student details");
System.out.print("EnrolmentNo and Name");
this.eno = sc.next();
this.name = sc.next();
System.out.print("enter three Subject Marks");
this.m1 = sc.nextInt();
this.m2 = sc.nextInt();
this.m3 = sc.nextInt();
if (m1>=50&& m2>=50&&m3>=50)
this.total=m1+m2+m3;
else
this.total=0;
}
public void displayinfo()
{
System.out.println("EnrolmentNo" + eno);
System.out.println("Name: " + name);
System.out.println("m1,m2,m3: " + m1+"\t"+m2+"\t"+m3);
System.out.println("Total: " + total);
}
}
public class Studentinfo
{
public static void main(String[] args)
{
Student s[] = new Student[3];
s[0]=new Student();
s[1]=new Student();
s[2]=new Student();
for(int i=0;i<3;i++)
{
s[i].getinfo();
}
System.out.println("\n\nThe details of entered student are:");
for(int i=0;i<3;i++)
{
s[i].displayinfo();
}
}
}
8. In a college first year class are having the following
attributes Name of the class (BCA, BCom, BSc), Name of the
staff, No of the students in the class, Array of students in the
class.
Define a class called first year with above attributes and
define a suitable constructor.
Also write a method called best Student () which process a
first-year object and return the student with the highest total
mark. In the main method define a first-year object and find
the best student of this class.
import java.util.*;
class Employee
{
String name;
Date appdate;
Employee(String n,Date appdt)
{
name=n;
appdate=appdt;
}
void display()
{
System.out.println("Employee appointment date");
System.out.println(name+"
"+appdate.getDate()+"/"+appdate.getMonth()
+"/"+appdate.getYear());
}
}
class Employeedemo
{
public static void main(String args[])
{
int i,j;
Employee emp[]=new Employee[6];
emp[0]=new Employee ("likith",new Date(2000,05,22));
emp[1]=new Employee ("sanjay",new Date(2001,01,12));
emp[2]=new Employee ("subash",new Date(1999,05,20));
emp[3]=new Employee ("sandeep",new Date(2010,05,30));
emp[4]=new Employee ("gowtham",new Date(2005,11,20));
emp[5]=new Employee ("bharath",new Date(2003,11,21));
System.out.println("list of employees");
for( i=0;i<emp.length;i++)
emp[i].display();
for(i=0;i<emp.length;i++)
{
for(j=0;j<emp.length;j++)
{
if(emp[i].appdate.before(emp[j].appdate))
{
Employee t=emp[i];
emp[i]=emp[j];
emp[j]=t;
}
}
}
System.out.println(" ");
System.out.println("list of employees seniority wise");
for(i=0;i<emp.length;i++)
emp[i].display();
}
}
10. Create a package ‘student. Fulltime. BCA ‘in your current
working directory
a. Create a default class student in the above package with
the following attributes: Name, age, sex.
b. Have methods for storing as well as displaying
package student.fulltime.bca;
class Student
{
String name;
int age;
char gender;
void getdata(String n,int a,char g)
{
name=n;
age=a;
gender=g;
}
void display()
{
System.out.println("name="+name+"\nage="+age+"\
ngender="+gender);
}
public static void main(String args[])
{
Student s=new Student();
s.getdata("likitha",19,'F');
s.display();
}
}