Final - Basics of Java Lab Manual (BPLCK205C)
Final - Basics of Java Lab Manual (BPLCK205C)
H
Department of Computer Science & Engineering
.
H.K.E. Society’s S.L.N. College of Engineering
(Affiliated to VTU - Belagavi, Approved by AICTE – New Delhi, Accredited by NAAC)
Yeramarus Camp, Raichur – 584135
(Integrated Component)
Basics of Java Laboratory Manual
(BPLCK205C)
(CBCS Scheme)
VISION
MISSION
To provide young computing aspirants with
best practices in teaching-learning with
highly experienced faculty
To team up with industries and provide
experience on latest tools to excel in
promising technologies.
To prepare students with required
fundamentals practical exposure, social and
professional morals
Programme Outcomes (POs)
PO1: Apply knowledge of computing fundamentals, computing
specialization, mathematics and domain knowledge to provide IT
solutions.
PO2: Identify, analyze and solve IT problems using fundamental
principles of mathematics and computing sciences.
PO3: Design, Develop and evaluate software solutions to meet societal
and environmental concerns.
PO4: Conduct investigations of complex problems using research based
knowledge and methods to provide valid conclusions.
PO5: Select and apply appropriate techniques and modern tools for
complex computing activities.
PO6: Understand professional ethics, cyber regulations and
responsibilities.
PO7: Involve in life-long learning for continual development as an IT
professional.
PO8: Apply and demonstrate computing and management principles to
manage projects in multidisciplinary environments by involving in
different roles
PO9: Comprehend& write effective reports and make quality
presentations.
PO10: Understand and assess the impact of IT solutions on socio- environmental
Issues.
PO11: Work collaboratively as a member or leader in multidisciplinary
teams.
PO12: Identify potential business opportunities and innovate to create
value to the society and seize that opportunity.
CONTENTS
List of problems for which student should develop program and execute in the Laboratory.
Exp. No Title Of the Experiment
Write a JAVA program that prints all real solutions to the quadratic equation
1
ax2+bx+c=0. Read in a, b, c and use the quadratic formula.
Demonstrate the following operations and sign extension with Java programs
3
(i) << (ii) >> (iii) >>>
Create two packages P1 and P2. In package P1, create class A, class B inherited
from A, class C . In package P2, create class D inherited from class A in package
9
P1 and class E. Demonstrate working of access modifiers (private, public,
protected, default) in all these classes using JAVA.
Write a JAVA program to read two integers a and b. Compute a/b and print,
10 when b is not zero. Raise an exception when b is equal to zero. Also
demonstrate working of ArrayIndexOutOfBoundException.
Program-1:
Program: Write a java program that prints all real solutions to the quadratic
equation ax2+bx+c=0. Read in a, b, c and use the quadratic formula.
import java.io.*;
import java.util.*;
class Imaginary
{
public static void main(String ar[])
{
int a,b,c,d;
Scanner s=new Scanner(System.in);
System.out.print("The Quadratic Equation is of the form ax2+bx+c=0. \n”);
System.out.print("a = "+"\n");
a=s.nextInt();
System.out.print("b = "+"\n");
b=s.nextInt();
System.out.print("c= "+"\n");
c=s.nextInt();
System.out.println("The quadratic equation you entered is
"+a+"+x2+"+b+"+x+"+c+"=0");
System.out.print("real and imaginary roots are");
d=(b*b)-4*(a*c);
if(d>0)
{
System.out.println("Real and distinct");
double rt1=(-b+Math.sqrt(d))/(2*a);
double rt2=(-b-Math.sqrt(d))/(2*a);
System.out.print("Roots are "+rt1 +" "+rt2);
}
else if(d==0)
{
System.out.println("Real and equal");
double rt1=(-b)/(2*a);
double rt2=(-b)/(2*a);
System.out.print("Roots are "+rt1 +" "+rt2);
}
else if(d<0)
{
System.out.println("Imaginary");
}
}
}
Output :
Program-2:
Aim: Introduce the java fundamentals, operators, looping and arrays in java.
import java.io.*;
import java.util.Scanner;
public class matrixmul1{
public static void main(String args[]){
for(int i=0;i<p;i++)
{
for(int j=0;j<q;j++)
{
System.out.print(b[i][j]+" ");
}
System.out.println(" ");
}
for(int i=0;i<m;i++){
for(int j=0;j<q;j++){
c[i][j]=0;
for(int k=0;k<p;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}//end of k loop
}//end of j loop
System.out.println();//new line
}
System.out.println("The Resultant Matrix-C is:");
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println(" ");
}
}
}
}
Output:
Enter the order of the matrix-A (M*N)
Enter the value of M
2
Enter the value of N
3
Output:
6 6 6
12 12 12
18 18 18
Program-3:
Program: Demonstrate the following operations and sign extension with Java
programs (i) << (ii) >> (iii) >>>
import java.util.Scanner;
class bitwiseop
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int num = in.nextInt();
while(true)
{
System.out.println("\nEnter 1: << operator 2: >> operator 3: >>> operator
\n");
int sy = in.nextInt();
int output;
switch(sy)
{
case 1:
output = num << 2;
System.out.println("\nOutput after bitwise left shift operation for
"+num+"="+output);
break;
case 2:
output = num >> 2;
System.out.println("\nOutput after bitwise right shift operation for
"+num+"="+output);
break;
case 3:
output = num >>> 2;
System.out.println("\nOutput after bitwise Unsigned Right Shift for
"+num+"="+output);
break;
default:
System.out.println("\nYou have entered wrong operator");
return;
}
}
Suresh Patel Page 7
Basics of Java Programming Integrated Laboratory Manual [BPLCK205C]
}
}
Output:
Program-4:
import java.util.Scanner;
class sorting
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] a = new int[5];
int i, j, temp;
System.out.println("Please Enter 5 elements in the Array");
for (i = 0; i < 5; i++)
{
a[i] = in.nextInt();
}
}
}
}
Output:
Program-5: (Week-3)
Program: Create a Java class called Student with the following details as variables
within it: USN, Name, Branch, Phone. Write a Java program to create n Student
objects and print the USN, Name, Branch, and Phone of these objects with suitable
headings.
import java.util.Scanner;
public class student {
String USN;
String Name;
String branch;
String phone;
void displayRecord()
{
System.out.println(USN+" "+Name+" "+branch+" "+phone);
}
}
}
}
Output:
Practice Programs:
Program:
A. Write a program to check prime number
B.Write a program for Arithmetic calculator using switch case menu
import java.util.Scanner;
if(count==0)
System.out.println(n + " is a Prime Number.");
else
System.out.println(n + " is not a Prime Number.");
}
}
Output:
Enter a Number:
11
11 is a Prime Number.
Enter a Number:
10
10 is not a Prime Number.
import java.util.Scanner;
class calc {
case '-':
output = num1 - num2;
break;
case '*':
output = num1 * num2;
break;
case '/':
output = num1 / num2;
break;
default:
System.out.printf("You have entered wrong operator");
return;
}
System.out.println(num1+" "+operator+" "+num2+": "+output);
}
}
Output:
Enter first number:5
Enter second number:2
Enter an operator (+, -, *, /): +
5.0 + 2.0: 7.0
Program-6:
Box()
{
width = height = depth = 0;
}
Box(double len)
{
width = height = depth = len;
}
double volume()
{
return width * height * depth;
}
}
class Sum {
Output:
30
60
31.0
Program-7:
Program: Design a super class called Staff with details as StaffId, Name, Phone,
Salary. Extend this class by writing three subclasses namely Teaching (domain,
publications), Technical (skills), and Contract (period). Write a Java program to
read and display at least 3 staff objects of all three categories.
import java.util.Scanner;
class Astaff {
String StaffID, Name, Phone, Salary;
void read(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter StaffID");
StaffID = sc.nextLine();
System.out.println("Enter Name");
Name = sc.nextLine();
System.out.println("Enter Phone");
Phone = sc.nextLine();
System.out.println("Enter Salary");
Salary = sc.nextLine();
}
void disp(){
System.out.println("Details are");
System.out.println("StaffID:"+StaffID);
System.out.println("Name:"+Name);
System.out.println("Phone:"+Phone);
System.out.println("Salary:"+Salary);
}
}
void disp1(){
System.out.println("Domain:"+Domain);
System.out.println("Publications:"+Publications);
}
}
void disp2(){
System.out.println("skills:"+skills);
}
}
void disp3(){
System.out.println("period:"+period);
}
}
class pgm2 {
public static void main(String []args){
Scanner sc=new Scanner (System.in);
System.out.println("Enter the number of staff\n");
int n=sc.nextInt();
ATeaching ob[]= new ATeaching [10];
System.out.println("Enter Teaching staff details\n");
for(int i=0; i<n; i++)
ob[i]=new ATeaching();
for(int i=0; i<n;i++)
{
ob[i].read();
ob[i].read1();
System.out.println("Teaching Staff details are:\n");
ob[i].disp();
ob[i].disp1();
}
ob1[i].read();
ob1[i].read2();
System.out.println("Technical Staff details are:\n");
ob1[i].disp();
ob1[i].disp2();
}
Output:
Run-1:
1
Enter Teaching staff details
Enter StaffID
1
Enter Name
ABC
Enter Phone
123456
Enter Salary
25000
Enter Domain
CSE
Enter Publications
Pristine
Details are
StaffID:1
Suresh Patel Page 20
Basics of Java Programming Integrated Laboratory Manual [BPLCK205C]
Name:ABC
Phone:123456
Salary:25000
Domain:CSE
Publications:Pritine
Program-8:
import java.util.Scanner;
/* Program is used for simple method overriding example with dynamic method
dispatch.*/
class stude
{
public void show()
{
System.out.println("Student details.");
}
}
//main method
public static void main(String args[]){
//Super class can contain subclass object.
stude obj = new colstudent();
Ouput:
Program-9:
Program: Create two packages P1 and P2. In package P1, create class A, class B
inherited from A, class C . In package P2, create class D inherited from class A in
package P1 and class E. Demonstrate working of access modifiers (private, public,
protected, default) in all these classes using JAVA.
package demopackage;
// Class A
public class A
{
void display()
{
System.out.println("Hello");
}
}
B.java:
package demopackage;
import demopackage.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
Output:
Hello
package demopackage;
// Class A
public class A
{
private void display()
{
System.out.println("Hello");
}
}
B.java:
package demopackage;
import demopackage.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
Output:
package demopackage;
// Class A
public class A
{
protected void display()
{
System.out.println("Hello");
}
}
Suresh Patel Page 24
Basics of Java Programming Integrated Laboratory Manual [BPLCK205C]
B.java:
package demopackage;
import demopackage.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
Output:
Hello
Program-10:
Program: Write a JAVA program to read two integers a and b. Compute a/b and
print, when b is not zero. Raise an exception when b is equal to zero. Also
demonstrate working of ArrayIndexOutOfBoundException.
import java.util.Scanner;
class pgm10
{
public static void main(String[ ] args)
{
int a, b, result;
Scanner in =new Scanner(System.in);
while(true)
{
System.out.println("\nEnter 1: Divide by Zero 2: Array index out of bound 3:
Exit \n");
int sy = in.nextInt();
switch(sy)
{
case 1:
System.out.println("Divide by Zero");
System.out.println("Input two integers");
System.out.println("Enter the Value of A");
a=in.nextInt();
System.out.println("Enter the Value of B");
b=in.nextInt();
try
{
result=a/b;
System.out.println("Result="+result);
}
catch(ArithmeticException e)
{
System.out.println("exception caught: Divide by zero error"+e);
}
break;
case 2:
int arr[]=new int[5];
try
{
arr[0]=10;
arr[1]=20;
arr[2]=30;
arr[3]=40;
arr[4]=50;
Suresh Patel Page 26
Basics of Java Programming Integrated Laboratory Manual [BPLCK205C]
break;
default:
System.out.println("\nYou have entered wrong operator");
return;
}
}
}
}
Output: