II Sem Java Programs
II Sem Java Programs
1. Program to assign two integer values to x and y, using the “if ” statement the output pf the program should display a
message whether x is greater tha y or vice versa
class Big{
OUTPUT:
2. Program to list the factorial of the numbers 1 to 10 to calculate the factorial value use while loop
class Factorial{
OUTPUT:
Factorial of 1is1
Factorial of 2is2
Factorial of 3is6
Factorial of 4is24
Factorial of 5is120
Factorial of 6is720
Factorial of 7is5040
Factorial of 8is40320
Factorial of 9is362880
Factorial of 10is3628800
3. Program to add teo integers and two float numbers when no arguments are supplied . Give a default value to calculate
the sum . Use function over loading .
class Add{
int a=10,b=20;
double c=25.5,d=35.2;
OUTPUT:
Integer Sum=40
Float Sum=24.0
class Addsub{
int a=20,b=10;
void add(){
System.out.println("sum="+(a+b));
}
void sub(){
System.out.println("Difference="+(a-b));
}
}
class MulDiv extends Addsub{
void mult(){
System.out.println("Product="+(a*b));
}
void div(){
System.out.println("Division="+(a/b));
}
}
class Arithmetic{
public static void main(String args[]){
MulDiv ob1= new MulDiv();
ob1.add();
ob1.sub();
ob1.mult();
ob1.div();
}
}
OUTPUT:
sum=30
Difference=10
Product=200
Division=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 Demo{
int a=10;
static int count=0;
void increament(){
a++;
count++;
}
void display(){
System.out.println("a="+a+"count="+count);
}
}
class Staticdemo{
public static void main(String args[]){
Demo obj1=new Demo();
obj1.increament();
System.out.println("Object 1 Data");
obj1.display();
Demo obj2=new Demo();
obj2.increament();
System.out.println("Object 2 Data");
obj2.display();
}
}
OUTPUT:
Object 1 Data
a=11 count=1
Object 2 Data
a=11 count=2
6. A. Program to find the area & circumferece of the circle by accepting the radius from the user
B. To accept a number and find whether the number is prime or not.
A.
import java.io.*;
import java.util.*;
class Circle{
public static void main(String []args) throws IOException{
double radius, area, circum, pi=3.14;
DataInputStream in= new DataInputStream(System.in);
System.out.println("Enter a radius");
radius=Double.valueOf(in.readLine()).doubleValue();
area=pi*radius*radius;
circum=2*pi*radius;
System.out.println("Area of a Crircle is="+area);
System.out.println("Circumference of the circle="+circum);
}
}
OUTPUT:
Enter a radius
5
Area of a Crircle is=78.5
B.
import java.io.*;
class Prime{
public static void main(String []args) throws IOException{
int num,flag=0;
DataInputStream in = new DataInputStream(System.in);
System.out.println("Enter a number");
num=Integer.parseInt(in.readLine());
for( int i=2;i<=num/2;i++){
if(num%i==0){
flag=1;
break;
}
}
if(flag==0){
System.out.println(num+"is a prime number");
}
else{
System.out.println(num+"is not a prime number");
}
}
}
OUTPUT:
i) Enter a number
import java.io.*;
class Student{
String Sname, Sregno;
int sub1,sub2,sub3,total;
Student(String name,String regno, int m1, int m2, int m3){
Sname=name;
Sregno=regno;
sub1=m1;
sub2=m2;
sub3=m3;
}
void display(){
if(sub1<50||sub2<50||sub3<50){
total=0;
}
else{
total=sub1+sub2+sub3;
}
System.out.println("name="+Sname);
System.out.println("registor number="+Sregno);
System.out.println("subject one marks="+sub1);
System.out.println("subject two marks="+sub2);
System.out.println("subject three marks="+sub3);
System.out.println("total marks is="+total);
}
}
class Studentdemo{
public static void main(String args[])throws IOException{
String name, regno;
int i=0,m1,m2,m3;
DataInputStream in=new DataInputStream(System.in);
Student s[]= new Student[3];
System.out.println("Enter the details of 3 students");
for(i=0;i<3;i++){
System.out.println("Enter name , regno, m1,m2,m3 of "+(i+1)+"student");
name=in.readLine();
regno=in.readLine();
m1=Integer.parseInt(in.readLine());
m2=Integer.parseInt(in.readLine());
m3=Integer.parseInt(in.readLine());
s[i]=new Student(name,regno,m1,m2,m3);
}
System.out.println("The Student Details are:");
for(i=0;i<3;i++){
System.out.println("Information of Students"+(i+1));
s[i].display();
}
}
}
OUTPUT:
likhith b
0079
29
49
50
mayur jadav
0101
99
100
98
lokesh
0089
100
100
49
Information of Students1
name=likhith b
registor number=0079
Information of Students2
name=mayur jadav
registor number=0101
Information of Students3
name=lokesh
registor number=0089
8. In a college First year classes are havig the following name of the class (BCA ,B Com,BSc),name of the staff ,no of the
students in the class. Array of student in class
import java.io.*;
class College{
String cname,staffname;
int num;
College(String name, String sname, int n)
{
cname=name;
staffname=sname;
num=n;
}
void display(){
System.out.println("Class name="+cname);
System.out.println("Staff Name="+staffname);
System.out.println("number of Students="+num);
}
}
class Cdemo{
public static void main(String args[])throws IOException{
String name, sname;
int n, i=0;
DataInputStream in= new DataInputStream(System.in);
College c[]= new College[3];
System.out.println("Eter the details");
for( i=0;i<3;i++){
System.out.println("Enter the Class name, Staff name , number of Students");
name = in.readLine();
sname=in.readLine();
n=Integer.parseInt(in.readLine());
c[i]= new College(name,sname,n);
}
System.out.println("The College Details :");
for( i=0;i<2;i++){
c[i].display();
}
}
}
OUTPUT:
Eter the details
DATA STRUCTURES
REKHA MAAM
57
JAVA
RASHMI MAAM
57
MATHEMATICS
BHAVANA MAAM
57
number of Students=57
Class name=JAVA
number of Students=57
9. Define a class called first year with above attributes ad define a suitable constructor. Also write a method called best
Studet() which process a first-year object and return the student with the highest total marks. In the main method
define a first-year object and find the best student of the class.
10. import java.io.*;
11. class FirstYear{
12. String sname,sregno;
13. int sub1,sub2,sub3;
14. FirstYear(String name, String regno,int m1,int m2,int m3){
15. sname=name;
16. sregno=regno;
17. sub1=m1;
18. sub2=m2;
19. sub3=m3;
20. }
21. int beststudent(){
22. return sub1+sub2+sub3;
23. }
24. }
25. class FirstYearDemo{
26. public static void main(String []args)throws IOException{
27. String name,regno;
28. int m1,m2,m3,total=0,high=0,j=0,i=0;
29. DataInputStream in=new DataInputStream(System.in);
30. FirstYear s[]=new FirstYear[3];
31. System.out.println("Enter the Details of the Three Students :");
32. for(i=0;i<3;i++){
33. System.out.println("Enter name,regno,m1,m2,m3");
34. name=in.readLine();
35. regno=in.readLine();
36. m1=Integer.parseInt(in.readLine());
37. m2=Integer.parseInt(in.readLine());
38. m3=Integer.parseInt(in.readLine());
39. s[i]=new FirstYear(name,regno,m1,m2,m3);
40. }
41. high=s[0].beststudent();
42. for(i=0;i<3;i++){
43. total=s[i].beststudent();
44. System.out.println("Total Marks os the Student is:"+(i+1)+"is"+total);
45. if(high<total){
46. high=total;
47. j=i;
48. }
49. }
50. System.out.println("BEST STUDENT IS:");
51. System.out.println("NAME="+s[j].sname+"HIGHSCORE="+high);
52. }
53. }
OUTPUT:
Enter name,regno,m1,m2,m3
likhith
100
99
87
67
Enter name,regno,m1,m2,m3
mayur
88
89
99
100
Enter name,regno,m1,m2,m3
lokesh
100
100
100
99
NAME=lokeshHIGHSCORE=299
10. Program to define a class called employee with the name and state of appointment. Create ten employee objects
or an array and sort them as per their date of appontment i.e., print them as per the time seniorty.
11. import java.util.*;
12. import java.io.*;
13. class Emp{
14. String name,date;
15. Emp(String name,String date){
16. this.name=name;
17. this.date = date;
18. }
19. }
20. class Sort implements Comparator<Emp>{
21. public int compare(Emp a,Emp b){
22. return a.date.compareTo(b.date);
23. }
24. }
25. public class EmpSort{
26. public static void main(String args[]){
27. ArrayList<Emp> list=new ArrayList<Emp>();
28. list.add(new Emp("RAJU","2020-03-19"));
29. list.add(new Emp("SANJANA","2019-02-27"));
30. list.add(new Emp("RAM","2019-01-27"));
31. list.add(new Emp("ARUN","1998-02-26"));
32. System.out.println("BEFORE SORTING:");
33. for(Emp d:list)
34. System.out.println(d.name+"\t"+d.date);
35. Collections.sort(list,new Sort());
System.out.println("SORTED IN ASCENDING ORDER");
36. for(Emp d:list)
37. System.out.println(d.name+"\t"+d.date);
38. }
39. }
OUTPUT:
BEFORE SORTING:
RAJU 2020-03-19
SANJANA 2019-02-27
RAM 2019-01-27
ARUN 1998-02-26
SORTED IN ASCENDING ORDER
ARUN 1998-02-26
RAM 2019-01-27
SANJANA 2019-02-27
RAJU 2020-03-19
PS C:\Users\KOUSHIK B N>
Age, Sex.
package BCA;
import java.io.*;
class StudentDemo{
public static void main(String args[])throws IOException{
String name, sex;
int age;
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter Student name age and sex");
name=in.readLine();
age=Integer.parseInt(in.readLine());
sex=in.readLine();
System.out.println("DETAILS ARE:");
System.out.println("*****************");
System.out.println("name="+name);
System.out.println("age="+age);
System.out.println("sex="+sex);
}
}
OUTPUT:
LIKHITH
19
MALE
DETAILS ARE:
*****************
name=LIKHITH
age=19
sex=MALEs