0% found this document useful (0 votes)
33 views18 pages

Java Lab

Uploaded by

rajumanjula226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
33 views18 pages

Java Lab

Uploaded by

rajumanjula226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

JAVA LAB

4. Program to perform mathematical operations. Create a


class called Addsub with methods to add and subtract. Create
another class called Muldiv that extends from Addsub class to
use the member data of the super class. MulDiv should have
methods to multiply and divide A main function should access
the methods and perform the mathematical operations.

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

5. Program with class variable that is available for all instances


of a class. Use static variable declaration. Observe the changes
that occur in the object’s member variable values.

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


class Circumference
{
public static void main(String args[])
{
int radius;
double circum,area;
Scanner sc=new Scanner(System.in);
System.out.println("enter the radius of the circle");
radius=sc.nextInt();
circum=2*Math.PI*radius;
area= Math.PI *radius*radius;
System.out.println("Circumference of the circle is :"+circum);
System.out.println("Area of the circle is:"+area);
}
}
6.B. To accept a number and find whether the number is
Prime or not.

import java.util. Scanner;


class Prime
{
public static void main(String args[])
{
int num,count=0;
Scanner sc=new Scanner (System.in);
System.out.println("enter the number");
num=sc.nextInt();
for(int i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==2)
System.out.println(num+"is a prime number");
else
System.out.println(num+"is not a prime number");
}
}
7. Program to create a student class with following attributes;
Enrollment No: Name, Mark of sub1, Mark of sub2, mark of
sub3, Total Marks. Total of the three marks must be
calculated only when the student passes in all three subjects.
The pass mark for each subject is 50. If a candidate fails in any
one of the subjects his total mark must be declared as zero.
Using this condition write a constructor for this class. Write
separate functions for accepting and displaying student
details. In the main method create an array of three student
objects and display the details.

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.

public class Firstyear


{
String classname,staffname, students[];
int noofstd, marks[];
Firstyear(String cn, String sn, int nstd, String std[],int m[])
{
classname=cn;
staffname=sn;
noofstd=nstd;
students=std;
marks=m;
}
void beststudent()
{
String bstudents=this.students[0];
int hmarks=this.marks[0];
int bmarks=this.marks[0];
for(int i=1;i<noofstd;i++)
{
if(this.marks[i]>hmarks)
{
bstudents=this.students[i];
hmarks=this.marks[i];
bmarks=this.marks[i];
}
}
System.out.println("The best Student is"+bstudents+"with
highest marks="+hmarks);
}
public static void main(String args[])
{
String students[]={"asha","anusha","kiran","malini","prakash"};
int bcastdmarks[]={560,480,587,496,598};
Firstyear f=new
Firstyear("BCA","kalyan",5,students,bcastdmarks);
f.beststudent();
}
}
9. Program to define a class called employee with the name
and date of appointment.
Create ten employee objects as an array and sort them as per
their date of appointment. ie, print them as per their
seniority.

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

You might also like