Hello Everyone This is Gautam, In this post I m Going to Solve 8 Java Programs which is related to BCA 3rd Semester Java Lab Programs Part 1.
Note: All the Programs are Tested(Compiled), if you get any types of errors than let me know. I will Reply as soon as possible.
1)Write a program to find factorial of list of number reading input as command line argument.
Answer:-
public class Facto
{
public static void main(String[] args)
{
int num,fact=1;
if(args.length==0)
{
System.out.print("No Arguments");
}
else
{
for(int i=0;i<args.length;i++)
{
num=Integer.parseInt(args[i]);
for( i=1;i<=num;i++)
{
fact=fact*i;
}
}
}
System.out.println("Factorial is "+fact);
}
}
File name save as : Facto.java
Compile : Facto.java
Run : Facto
Output:
2)Write a program to display all prime numbers between two limits.
import java.util.*;
public class Prime
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the 1st number: ");
int start=sc.nextInt();
System.out.print("Enter the 2nd number: ");
int end=sc.nextInt();
System.out.println("List of the prime number b/w "+start+" and " +end);
for(int i=start;i<=end;i++)
{
if(isPrime(i))
{
System.out.println(i);
}
}
}
public static boolean isPrime(int n)
{
if(n<=1)
{
return false;
}
for(int i=2;i<=Math.sqrt(n);i++)
{
if (n% i==0)
{
return false;
}
}
return true;
}
}
File Name Save as : Prime.java
Compile : Prime.java
Run : Prime
Output:
3)Write a program to sort list of elements in ascending and descending order
and show the exception handling.
Answer:
import java.util.Scanner;
public class Ascending
{
public static void main(String[] args)
{
int n, temp;
Scanner sc = new Scanner(System.in);
System.out.print("Enter of elements you want in array: ");
n = sc.nextInt();
int a[] = new int[n]; // Array Creation
System.out.print("Enter all the elements: ");
for (int i = 0; i < n; i++)
{
a[i] = sc.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j]) //------------>For descending Order Change > to <
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order: ");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
File Name Save as : Ascending.java
Compile : Ascending .java
Run: Ascending
Output :
4)Write a program to implement Rhombus pattern reading the limit form user.
Answer :
import java.util.Scanner;
public class Rombuss
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int size = 0;
Character c ;
System.out.println("Enter size of the Rhombus : ");
size = sc.nextInt();
System.out.println("Which character you want to use : ");
c = sc.next().charAt(0);
for (int row = 0;row < size ; row++)
{
for (int space = size - row ; space < size ; space ++)
{
System.out.print(" ");
}
//print the character
for (int i = 0 ; i<size ; i++)
{
System.out.print(c);
}
//add a newline
System.out.println();
}
}
}
File Name Save as: Rombuss.java
Compile : Rombuss.java
Run : Rombuss
Output :
5. Write a program to implement all string operations.
Answer:
public class Allstring
{
public static void main(String[] args)
{
String str1="Java";
String str2="Java project";
System.out.println("Str1 length is= "+str1.length());
System.out.println("Str2 length is= "+str2.length());
System.out.println("The concatenation is:"+str1.concat(""+str2));
System.out.println("The first character of "+str1+"is:"+str1.charAt(0));
System.out.println("The uppercase of "+str1+"is: "+str1.toUpperCase());
System.out.println("The Lowercase of"+str1+"is: "+str1.toLowerCase());
System.out.println("The 'c' occurs at position at str2: "+str1.indexOf('c'));
System.out.println("Replacing 'J'with 'p' in str1: "+str1.replace('j','p'));
}
}
File Name Save as: Allstring.java
Compile : Allstring.java
Run : Allstring
Output :
6. Write a program to find area of geometrical figures using method.
Answer :
import java.util.Scanner;
class Geo
{
void choise()
{
Scanner in=new Scanner(System.in);
int ch;
System.out.printf("1.Circle\n2.Triangle\n3.Square\n4.Rectangle\n\n\tEnter your choise 1-4:-\n");
ch=in.nextInt();
if(ch==1)
cir();
else if(ch==2)
tri();
else if(ch==3)
sq();
else
rect();
}
void cir()
{
Scanner in=new Scanner(System.in);
float r;
System.out.print("Enter the radius of circle : ");
r=in.nextFloat();
float a=3.14f*r*r;
System.out.print("Area of circle is : "+a);
}
void tri()
{
Scanner in=new Scanner(System.in);
float a,b,c;
System.out.print("Enter the three side of Triangle: ");
a=in.nextFloat();
b=in.nextFloat();
c=in.nextFloat();
float s=(a+b+c)/2;
double ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.print("Area of Triangle is : "+ar);
}
void sq()
{
Scanner in=new Scanner(System.in);
float a;
System.out.print("Enter the Side of square:");
a=in.nextFloat();
float ar=a*a;
System.out.print("Area of Square is : "+ar);
}
void rect()
{
Scanner in=new Scanner(System.in);
float l,b;
System.out.print("Enter the lenght and breadth of rectangle: ");
l=in.nextFloat();
b=in.nextFloat();
float ar=l*b;
System.out.print("Area of REctangle is : "+ar);
}
}
public class Geom
{
public static void main(String[] args)
{
Geo s=new Geo();
s.choise();
}
}
File Name save as : Geom.java
Compile : Geom.java
Run : Geom
Output :
7. Write a program to implement constructor overloading by passing different
number of parameter of different types.
Answer:
class StudentData
{
private int stuID;
private String stuName;
private int stuAge;
StudentData()
{
//Default constructor
stuID = 100;
stuName = "New Student";
stuAge = 18;
}
StudentData(int num1, String str, int num2)
{
//Parameterized constructor
stuID = num1;
stuName = str;
stuAge = num2;
}
//Getter and setter methods
public int getStuID()
{
return stuID;
}
public void setStuID(int stuID)
{
this.stuID = stuID;
}
public String getStuName()
{
return stuName;
}
public void setStuName(String stuName)
{
this.stuName = stuName;
}
public int getStuAge()
{
return stuAge;
}
public void setStuAge(int stuAge)
{
this.stuAge = stuAge;
}
}
public class Student
{
public static void main(String[] args)
{
/* Students s1=new Students();
s1.get_details();
s1.show_detail();*/
//This object creation would call the default constructor
StudentData myobj = new StudentData();
System.out.println("Student Name is: "+myobj.getStuName());
System.out.println("Student Age is: "+myobj.getStuAge());
System.out.println("Student ID is: "+myobj.getStuID());
/*This object creation would call the parameterized
* constructor StudentData(int, String, int)*/
StudentData myobj2 = new StudentData(83, "Gautam", 20);
System.out.println("Student Name is: "+myobj2.getStuName());
System.out.println("Student Age is: "+myobj2.getStuAge());
System.out.println("Student ID is: "+myobj2.getStuID());
}
}
File Name Save as : Student.java
Compile : Student.java
Run : Student
Output :
8. Write a program to calculate bonus for different departments using method
overriding.
Answer:-
import java.util.*;
abstract class Dept
{
double bp;
Dept(double bpay)
{
bp=bpay;
}
void disp()
{
System.out.println("Basic pay="+bp);
}
abstract double bonus();
}
class sales extends Dept
{
sales(double bpay)
{
super(bpay);
}
public double bonus()
{
return(0.20*bp);
}
}
class marketing extends Dept
{
marketing(double bpay)
{
super(bpay);
}
public double bonus()
{
return(0.30*bp);
}
}
class hr extends Dept
{
hr(double bpay)
{
super(bpay);
}
public double bonus()
{
return(0.50*bp);
}
}
public class Depts
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your basic pay");
double bp=sc.nextDouble();
sales s=new sales(bp);
s.disp();
System.out.println("bonus for sales dept=" +s.bonus());
marketing m=new marketing(bp);
m.disp();
System.out.println("bonus for marketing dept=" +m.bonus());
hr h=new hr(bp);
h.disp();
System.out.println("bonus for hr dept=" +h.bonus());
}
}
File Name Save as : Depts.java
Compile : Depts.java
Run : Depts
Output:
Note :- Please Follow me, Here i will upload java lab programs Part 2 As soon as possible . guys please 🙏 Follow me and share this page with your friends.
Social media link:
Follow me on Instagram:- Click Here
THANKS FOR VISITING THIS BLOG PAGE 💖.
1 Comments
If it’s helpful then let me know. Please 🙏🏻🙏🏻🙏🏻
ReplyDelete