Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

BCA32L-Java Programming Lab Manual-ODD Sem - 2024!25!1

Sjdj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

BCA32L-Java Programming Lab Manual-ODD Sem - 2024!25!1

Sjdj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

JSS Science and Technology University, Mysuru

Department of Computer Applications


BCA III Semester
BCA32L - Java Programming Laboratory Manual
PART-A – Simple Programs
All programs have to be created as a class under the Project with your Name
Program:1 – Display Hello World (Program / Class name: helloWorld.java)
public class helloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Program:2 – Read two numbers and display the sum of these numbers.

(Program / Class name: addTwoNums.java)


import java.util.Scanner;
public class addTwoNums {
public static void main(String args[]){
int a,b, sum=0;
Scanner inp = new Scanner(System.in);
System.out.println("Enter Two Numbers");
a=inp.nextInt();
b=inp.nextInt();
sum = a + b;
System.out.println("Sum of " + a + " and " +
b + " is: "+ sum);
}
}
Program:3 – Read a number and check whether it is an ODD or EVEN number.

(Program / Class name: checkOddOrEven.java)


import java.util.Scanner;
public class checkOddOrEven {
public static void main(String args[]){
int num;
Scanner inp = new Scanner(System.in);
System.out.println("Enter a number");
num = inp.nextInt();
if(num%2 == 0)
System.out.println("Even");
else
System.out.println("Odd");
}
}

Program:4 – Read a year and check whether it is a Leap Year or Not.

(Program / Class name: leapYear.java)


import java.util.Scanner;
public class leapYear {
public static void main(String args[]){
int year;
Scanner objyr = new Scanner (System.in);
System.out.println("Enter the year");
year = objyr.nextInt();
if(((year%4 ==0) && (year%100!=0)) || (year%400==0)){
System.out.println("LEAP YEAR");
}
else{
System.out.println("COMMON YEAR");
}
}
}
Program:5 – Java program to check the entered number is ODD or EVEN using ternary operator.

import java.util.Scanner;
public class IfElseTernaryExample {
public static void main(String args[]){
int num;
String result;
Scanner inp = new Scanner(System.in);
System.out.println("Enter a number");
num = inp.nextInt();
result=(num%2==0)?"Even Number":"Odd Number";
System.out.println(result);
}
}

Program:6 – Java program to allot grade based on the marks scored using IF-ELSE IF – ELSE

(Program / Class name: marksGrade.java)


import java.util.Scanner;
public class marksGrade {
public static void main(String args[]){
int marks;
System.out.println("Enter the marks scored”);
Scanner inp = new Scanner(System.in);
marks = inp.nextInt();
if(marks<50){
System.out.println("fail");
}
else if(marks>=50 && marks<60){
System.out.println("D grade");
}
else if(marks>=60 && marks<70){
System.out.println("C grade");
}
else if(marks>=70 && marks<80){
System.out.println("B grade");
}
else if(marks>=80 && marks<90){
System.out.println("A grade");
}
else if(marks>=90 && marks<100){
System.out.println("S grade");
}else{
System.out.println("Invalid!");
}
}
}

Program:7 - Write a program to find factorial of number


import java.util.Scanner;
public class factorial {
public static void main(String args[]){
int i,fact=1,num;
System.out.println("Enter Number to find its factorial");
Scanner inp = new Scanner(System.in);
num = inp.nextInt();
for(i=1;i<=num;i++){
fact=fact*i;
}
System.out.println("Factorial of "+num+" is: "+fact);
}
}

Program: 8: Find the sum of the digits of a given number


import java.util.Scanner;
public class sumofDigits{
public static void main(String args[]){
int number, digit, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
number = sc.nextInt();
while(number > 0)
{
digit = number%10;
sum = sum + digit;
number = number/10;
}
System.out.println("Sum of Digits: "+sum);
}
}

Program: 9: Swap two numbers without using a temporary variable


import java.util.Scanner;
public class swapNums {
public static void main(String args[]){
System.out.println("Enter the value of x and y");
Scanner inp = new Scanner(System.in);
/*Define variables*/
int x = inp.nextInt();
int y = inp.nextInt();
System.out.println("before swapping numbers: "+x +" "+ y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swapping: "+x +" " + y);
}
}

Program:10: accepts a name and display the name with greeting message using
Class.
import java.util.Scanner;
class greetings{
String dispMessage(String name){
return("Hello " + name);
}
}
public class GreetingName {
public static void main(String args[]){
String msg,name;
System.out.println("Enter Your Name");
Scanner inp = new Scanner(System.in);
name = inp.nextLine();
greetings grt = new greetings();
msg=grt.dispMessage(name);
System.out.println(msg);
}
}

Program:11: Programs on Simple Constructor


class Student{
String name;
String course;
public Student() {
this.name = "Raj";
this.course = "BCA";
}
public void sayHello() {
System.out.println(" My name is " + this.name + " and I am
studying " + this.course + " at JSS STU");
}
}
public class constructorExample {
public static void main(String args[]){
Student newStud = new Student();
newStud.sayHello();
}
}
Program:12: Programs on Parametrized Constructor
import java.util.Scanner;
class newStudents{
String name;
String course;
public newStudents(String myName, String myCourse) {
name = myName;
course = myCourse;
}
public void sayHello() {
System.out.println("My name is " + this.name + " and I am
studying " + this.course + " at JSS STU");
}
}
public class paramConstructor {
public static void main(String args[]){
String name, course;
System.out.println("Enter Student name and course");
Scanner inp = new Scanner(System.in);
name = inp.nextLine();
course = inp.nextLine();
newStudents newStud = new newStudents(name,course);
newStud.sayHello();
}
}

Program:13: Write a program to generate a salary for an employee using class, object,
constructors, methods and access control. Different parameters to be considered are Emp_No,
Emp_Name, Age, Basic, DA, HRA, CA, PT, IT.
import java.util.Scanner;
// Class Employee
class Employee{
int eCA,ePT;
float eDAPercent, eHRAPercent, eIncomeTax;
double eBasic, eGross,eNetSal;
// Constructor Employee
Employee(double basic, float DaPer, float HRAPer,int CA, int PT){
eBasic = basic;
eDAPercent = DaPer;
eHRAPercent = HRAPer;
eCA = CA;
ePT = PT;
}
// Method to Calculate Gross Salary
double calGrossSal(){
eGross = eBasic + ((eBasic*eDAPercent)/100) +
((eBasic*eHRAPercent)/100);
return eGross;
}
// Method to Calculate Income Tax
float calIncomeTax(double Gross){
if (Gross <= 500000.00)
eIncomeTax = 0.0f;
else if (Gross > 500000.00 && Gross <=750000.00)
eIncomeTax = (float) (Gross * 0.05);
else if (Gross > 750000.00 && Gross <= 1000000.00)
eIncomeTax = (float) (Gross * 0.10);
else if (Gross > 1000000.00 && Gross <= 1500000.00)
eIncomeTax = (float) (Gross * 0.20);
else if (Gross > 1500000.00 && Gross <= 2000000.00)
eIncomeTax = (float) (Gross * 0.30);
else if (Gross > 2000000.00 && Gross <= 3000000.00)
eIncomeTax = (float) (Gross * 0.40);
else
eIncomeTax = (float) (Gross * 0.50);
return eIncomeTax;
}
// Method to Calculate Net Salary
double getNetSal(double GrossSal, float InTax){
eNetSal = GrossSal - InTax;
return eNetSal;
}
}
public class empSalary {
public static void main(String args[]){
int empId,empAge;
float iTax,daPer,hraPer;
double gSal, nSal, eBasic;
Scanner objName = new Scanner(System.in);
Scanner objInp= new Scanner(System.in);
System.out.println("Enter the ID, Name, Age, Basic Salary, DA%
and HRA% of the Employee");
empId = objInp.nextInt();
String empName = objName.nextLine();
empAge = objInp.nextInt();
eBasic = objInp.nextDouble();
daPer = objInp.nextFloat();
hraPer = objInp.nextFloat();

Employee empObj = new Employee(eBasic,125.0f,15.0f,600,300);


gSal = empObj.calGrossSal();
iTax = empObj.calIncomeTax(gSal);
nSal = empObj.getNetSal(gSal, iTax);

System.out.println(" Salary Slip ");

System.out.println("===========================================");
System.out.println("Emp.ID: " + empId + " Emp.Name: " + empName + " Age:
" + empAge);
System.out.println("Basic Salary: " + eBasic);
System.out.println("Gross Salary: " + gSal);
System.out.println("Income Tax: " + iTax);
System.out.println("Net Salary: " + nSal);
System.out.println("=========================================");
}
}
Program 14: Demonstration of call by value
public class callbyVal {
public static void main(String[] args){
int a = 30;
int b = 45;
System.out.println("Before swapping, a = " + a + " and b = " + b);
swap(a, b);
System.out.println("After swapping, a = " + a + " and b is " + b);
}
public static void swap(int a, int b) {
int c = a;
a = b;
b = c;
}
}

Program 15: Demonstration of call by reference


public class callbyRef {
public static void main(String[] args) {
IntWrapper a = new IntWrapper(30);
IntWrapper b = new IntWrapper(45);
System.out.println("Before swapping, a = " + a.a + " and b = " + b.a);
// Invoke the swap method
swap(a, b);
System.out.println("After swapping, a = " + a.a + " and b is " + b.a);
}

public static void swap(IntWrapper a, IntWrapper b) {


IntWrapper c = new IntWrapper(a.a);
a.a = b.a;
b.a = c.a;
}
}

class IntWrapper {
public int a;
public IntWrapper(int a){ this.a = a;}
}
Program 16: Demonstration of Static and Dynamic Binding
class parent{
void studyTips(){
System.out.println("Study every day");
}
}
class children extends parent {
void studyTips(){
System.out.println("Study just before the events");
}
}
public class staticNdyanmicBinding {
public static void main(String args[]){
//Static Binding
parent obj = new parent();
obj.studyTips();
//Dynamic Binding
parent obj1 = new children();
obj1.studyTips();
}
}

Program 17: Write a program to generate a sales report for a sales executive using class, object,
constructors, methods and access control. Different parameters to be considered are
Emp_No, Emp_Name, Sales_Q1, Sales_Q2, Sales_Q3, Sales_Q4.

import java.util.Scanner;
class salesDetails{
int eNo, sale_Q1, sale_Q2,sale_Q3,sale_Q4;
String eName;
salesDetails(int eNo, String eName, int sale_Q1,int sale_Q2,int sale_Q3,int
sale_Q4){
this.eNo = eNo;
this.eName = eName;
this.sale_Q1 = sale_Q1;
this.sale_Q2 = sale_Q2;
this.sale_Q3 = sale_Q3;
this.sale_Q4 = sale_Q4;
}
protected int calTotalSale(){
int TotSales;
TotSales = this.sale_Q1 + this.sale_Q2 + this.sale_Q3 + this.sale_Q4;
return TotSales;
}
protected double calCommission(int totSales){
double sale_Comm;
if (totSales >= 5000 && totSales <10000)
sale_Comm = 500.00;
else if(totSales >= 10000 && totSales <15000)
sale_Comm = 1000.00;
else if(totSales >= 15000 && totSales <25000)
sale_Comm = 2000.00;
else if(totSales >= 25000 && totSales <40000)
sale_Comm = 3000.00;
else if(totSales >= 40000 && totSales <50000)
sale_Comm = 4000.00;
else
sale_Comm = 5000.00;
return sale_Comm;
}
}

public class salesReport {


public static void main(String args[]){
int eNo, sale_Q1,sale_Q2,sale_Q3, sale_Q4, totSales;
String eName;
double execCommission;

Scanner SalesObj = new Scanner(System.in);


Scanner SalesObj1 = new Scanner(System.in);
System.out.println("Enter the Emp.No, Name, Q1_Sales, Q2_Sales, Q3_Sales
and Q4_Sales");
eNo = SalesObj.nextInt();
eName = SalesObj1.nextLine();
sale_Q1 = SalesObj.nextInt();
sale_Q2 = SalesObj.nextInt();
sale_Q3 = SalesObj.nextInt();
sale_Q4 = SalesObj.nextInt();

salesDetails obj = new salesDetails(eNo, eName, sale_Q1, sale_Q2,


sale_Q3, sale_Q4);
totSales = obj.calTotalSale();
execCommission = obj.calCommission(totSales);

System.out.println("============== Sales Report==================");


System.out.println("EmpNo. Name Q1Sales Q2Sales Q3Sales Q4
Sales Tot Sales commission");

System.out.println("=======================================================");
System.out.println( eNo + " " + eName + " " + sale_Q1 + " " + sale_Q2 +
" " + sale_Q3 + " " + sale_Q4 + " " + totSales + " " + execCommission);
System.out.println("==========================================================");
}
}
Program 17: Write a JAVA Program to demonstrate Constructor Overloading and Method
Overloading.

a. Constructor Overloading
import java.util.Scanner;

class AddNums{
AddNums(int n1, int n2){
System.out.println("Sum of two integers is " + (n1+n2));
}
AddNums(int n1, int n2, int n3){
System.out.println("Sum of three integers is " + (n1+n2+n3));
}
AddNums(float n1, float n2, float n3){
System.out.println("Sum of three float values is " + (n1+n2+n3));
}
}

public class constructorOverloading {


public static void main(String args[]){
int num1, num2, num3;
float fnum1, fnum2, fnum3;
Scanner input = new Scanner(System.in);
System.out.println("Enter three integers");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
System.out.println("Enter three float values");
fnum1 = input.nextFloat();
fnum2 = input.nextFloat();
fnum3 = input.nextFloat();

AddNums objAdd = new AddNums(num1,num2);

AddNums objAdd1 = new AddNums(num1,num2,num3);

AddNums objAdd2 = new AddNums(fnum1,fnum2,fnum3);


}
}

b. Method Overloading

import java.util.Scanner;
class AddNumbers{
int sum(int a, int b){
return (a+b);
}
int sum(int a, int b, int c){
return (a+b+c);
}
float sum(float a, float b, float c){
return (a+b+c);
}
}

public class methodOverloading {


public static void main(String args[]){

int num1, num2, num3, sum1, sum2;


float fnum1, fnum2, fnum3, sum3;
Scanner input = new Scanner(System.in);
System.out.println("Enter three integers");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
System.out.println("Enter three float values");
fnum1 = input.nextFloat();
fnum2 = input.nextFloat();
fnum3 = input.nextFloat();

AddNumbers objSum = new AddNumbers();


sum1 = objSum.sum(num1, num2);
System.out.println("Sum of two integers is " + sum1);
sum2 = objSum.sum(num1, num2, num3);
System.out.println("Sum of three integers is " + sum2);
sum3 = objSum.sum(fnum1, fnum2, fnum3);
System.out.println("Sum of three float values is " + sum3);
}
}

Program 18: Write a JAVA Program to implement Inner class and demonstrate its Access
protection.

class outer {
int outdata = 10;
void display() {
inner inobj = new inner();
System.out.println("Accessing from outer class");
System.out.println("The value of outdata is " +outdata);
System.out.println("The value of indata is " +inobj.indata);
}

class inner {
int indata = 20;
void inmethod(){
System.out.println("Accessing from inner class");
System.out.println("The sum of indata & outdata is " +(outdata +
indata));
}
}
}

public class accessProtection {


public static void main(String args[]) {
outer outobj = new outer();
outobj.display();
outer.inner inobj1 = outobj.new inner();
inobj1.inmethod();
}
}

Program 19: Write a program in Java for String handling which performs the following:

a. Checks the capacity of StringBuffer objects.

public class stringBufferCapacity {


public static void main(String[] args) {
StringBuffer bufObj = new StringBuffer("BCA JSSSTU MYSURU");
// returns the current capacity of the String buffer i.e. 16 + 17
System.out.println("capacity = " + bufObj.capacity());
bufObj = new StringBuffer(" ");
// returns the current capacity of the String buffer i.e. 16 + 1
System.out.println("capacity = " + bufObj.capacity());
}
}

b. Reverses the contents of a string given on console and converts the resultant
string in upper case.

import java.util.Scanner;
public class revString2Upper {
public static void main(String args[]){
String strMsg, revMsg, uprMsg;
System.out.println("Enter a String in lower case");
Scanner inp = new Scanner(System.in);
strMsg = inp.nextLine();
StringBuffer strObj = new StringBuffer(strMsg);
// Converting string object to String type
revMsg = String.valueOf(strObj.reverse());
System.out.println("Reverse of the given string is " + revMsg);
System.out.println("Upper case of the given string is " +
strMsg.toUpperCase());
uprMsg = revMsg.toUpperCase();
System.out.println("Upper case of the reverse string is " + uprMsg);
}
}
c. Reads two string from console and appends the second to the first string and append the
resultant to the string “Result is: ”.

import java.util.Scanner;

public class stringConcatNAppend {


public static void main(String args[]){
String strFirst, strSecond, strConcat, strResult;
String strConst = "Result is: ";

Scanner inStr = new Scanner(System.in);


System.out.println("Enter two Strings");
strFirst = inStr.nextLine();
strSecond = inStr.nextLine();
System.out.println("First String is " + strFirst);
System.out.println("Second String is " + strSecond);
strConcat = strFirst + strSecond;
System.out.println("Concated String is " + strConcat);
StringBuilder bufObj = new StringBuilder();
strResult = String.valueOf(bufObj.append(strConst)) + " " +
strConcat;
System.out.println("Resultant String is " + strResult);

}
}

Program 20: Write a JAVA Program to demonstrate Inheritance

a. Simple or Single Inheritance


class mainCalc {
int addition(int a, int b){
return (a+b);
}
int subtraction(int a, int b){
return (a-b);
}
}
class subCalc extends mainCalc{
int multiplication(int a, int b){
return (a * b);
}
int division(int a, int b){
return (a/b);
}
}

public class simpleInheritance {


public static void main(String args[]){
int n1, n2 , result=0;
System.out.println("Enter two integers");
Scanner inp = new Scanner(System.in);
n1 = inp.nextInt();
n2 = inp.nextInt();

// Creating object for main class mainCalc which has only two
// methods available
// Result from Main Class
mainCalc objMain = new mainCalc();
result = objMain.addition(n1, n2);
System.out.println("Sum of two integers from Main Class: " +
result);

result = objMain.subtraction(n1, n2);


System.out.println("Difference two integers from Main Class: "
+ result);

// Crearing object for sub class subCalc where we can access


// both base class & sub class methods
// Result from Sub Class
subCalc objSub = new subCalc();
result = objSub.addition(n1, n2);
System.out.println("Sum of two integers thru Sub Class: " +
result);
result = objSub.subtraction(n1, n2);
System.out.println("Difference of two integers thru Sub Class:
" + result);

// Additonal opertaion mutliplictaion & Division from Sub Class


result = objSub.multiplication(n1, n2);
System.out.println("Product of two integers from Sub Class: " +
result);

result = objSub.division(n1, n2);


System.out.println("Quotient of two integers from Sub Class: "
+ result);

}
}

You might also like