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

Java Lab Prog

Lab programs

Uploaded by

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

Java Lab Prog

Lab programs

Uploaded by

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

Basics of JAVA programming Lab 2023-24

1. Write Java program to calculate the sum of elements in a 1-dimensional array.


class Demo
{
public static void main(String args[])
{
int a[]={1,2,3,4,5},sum=0;
for(int i=0;i<=4;i++)
sum = sum + a[i];
System.out.println("Result = "+sum);
}
}
Output:
javac Demo.java
java Demo
Result = 15

Chayashree G, Asst. Prof. Dept. of ISE 1


Basics of JAVA programming Lab 2023-24

2. Write a Java program to compute transpose of a 2-dimensional matrix.

 2D array can be defined as an array of arrays.


 The 2D array is organized as matrices which can be represented as the collection of rows and
columns.
 To output all the elements of a Two-Dimensional array, use nested for loops.
 For this two for loops are required, one to traverse the rows and another to traverse columns.

 Declaration – Syntax:

 Initialization – Syntax:

 Direct Method of Declaration: Syntax:

For example: int[][] arr = {{1, 2}, {3, 4}};


 Accessing Elements of Two-Dimensional Arrays

Chayashree G, Asst. Prof. Dept. of ISE 2


Basics of JAVA programming Lab 2023-24

 The transpose of a matrix is found by interchanging its rows into columns or columns into rows.

class Demo
{
public static void main(String args[])
{
int a[][]= new int[2][3];
a[0][0]=1; a[0][1]=2; a[0][2]=3;
a[1][0]=4; a[1][1]=5; a[1][2]=6;
System.out.println("Original Matrix:");
for(int i=0;i<2;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(a[i][j]+" ");
}
System.out.println();
}
System.out.println("Matrix Transpose:");
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
Output:
{
Orginal Matrix:
System.out.print(a[j][i]+" ");
} 123
System.out.println(); 456
}
} Matrix Transpose:
} 14
25
36

Chayashree G, Asst. Prof. Dept. of ISE 3


Basics of JAVA programming Lab 2023-24

3. Write a Java Program to display grade awarded to a student based on marks


obtained using else if ladder.
_____________________________________________ Marks Range Grade
80- 100 Distinction

The Java if statement is used to test the condition. It 60- 79 First Class
checks boolean condition: true or false. There are various types 40- 59 Second Class
of if statement in Java.
<40 Fail
a. if statement

b. if-else statement

Chayashree G, Asst. Prof. Dept. of ISE 4


Basics of JAVA programming Lab 2023-24

c. if-else-if ladder

if else if ladder in C programming is used to test a series of conditions

Chayashree G, Asst. Prof. Dept. of ISE 5


Basics of JAVA programming Lab 2023-24

sequentially.

d. Nested if: The nested if statement represents the if block within another if block.
Here, the inner if block condition executes only when outer if block condition is
true.

Chayashree G, Asst. Prof. Dept. of ISE 6


Basics of JAVA programming Lab 2023-24

================================================================class
Demo
{
public static void main(String args[])
{
int marks=45;
if(marks>100 || marks<0)
System.out.println("Invalid Marks");
else if(marks>=80 && marks<=100)
System.out.println("Distinction");
else if(marks>=69 && marks<=79)
System.out.println("First Class");
else if(marks>=40 && marks<=59)
System.out.println("Second Class");
else
System.out.println("Fail");
}
}
Output:
Second Class

4. Write a Java program to implement simple calculator using switch statement.


class Demo
{
public static void main(String args[])
{
float a = Float.parseFloat(args[0]);
char choice=args[1].charAt(0);
float b = Float.parseFloat(args[2]);
switch(choice)
{
case '+' : System.out.println(a+b);
break;

Chayashree G, Asst. Prof. Dept. of ISE 7


Basics of JAVA programming Lab 2023-24

case '-' : System.out.println(a-b);


break;
case 'x' : System.out.println(a*b);
break;
case '/' : System.out.println(a/b);
break;
case '%' : System.out.println(a%b);
break;
default : System.out.println("Invalid Choice");
}
}
}

5. Create a Java class called Student with the following details as variables within it. USN,
Name, Branch, Phone, Percentage. Write a Java program to create n Student objects and
print the USN, Name, Branch, Phone and Percentage of these objects with suitable headings.
class Student {
String usn;
String name;
String branch;
long phone;
float percentage;
Student(String usn,String name,String branch,long phone,float percentage)
{
this.usn=usn;
this.name=name;
this.branch=branch;
this.phone=phone;
this.percentage=percentage;
}
public void display()
{
System.out.println("USN:"+ usn);
System.out.println("Student Name:"+ name);
System.out.println("Student Branch:"+ branch);
System.out.println("Phone Number:"+ phone);
System.out.println("Percentage:"+ percentage);
System.out.println("----------------------------");
}
}

Chayashree G, Asst. Prof. Dept. of ISE 8


Basics of JAVA programming Lab 2023-24

class Stud
{
public static void main(String args[])
{
Student std1= new Student("4VV22EC001","Arun Kumar","EC",9538699292L,75.45f);
Student std2= new Student("4VV22EC002","Bharath Kumar","EC",8938699292L,85.45f);
std1.display();
std2.display();
}
}

Output:
USN:4VV22EC001
Student Name:Arun Kumar
Student Branch:EC
Phone Number:9538699292
Percentage:75.45
----------------------------
USN:4VV22EC002
Student Name:Bharath Kumar
Student Branch:EC
Phone Number:8938699292
Percentage:85.45
----------------------------

6. Write a Java program to find the area of square, rectangle and Circle using method
overloading.
class OverloadDemo {
void area(int x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)

Chayashree G, Asst. Prof. Dept. of ISE 9


Basics of JAVA programming Lab 2023-24

{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
}

class Overload {
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
javac OverloadDemo.java
$ java OverloadDemo

the area of the square is 25.0 sq units


the area of the rectangle is 132.0 sq units
the area of the circle is 19.625 sq units

7. . Design a super class called Staff with details as StaffID, Name, Phone, Salary, Extend
this class by writing three subclasses namely Teaching (domain, Publication), Technical
(skills) and Contract (Period). Write a Java Program to read display at least 3 staff
objects of all three categories.

class Staff {
String StaffID;
String Name;
String Phone;
float Salary;
}

class Teaching extends Staff


{
String Domain;
String Publications;
Teaching(String id,String sn,String mob,float sal,String br,String pub)
{
StaffID=id;
Name=sn;
Phone=mob;
Salary=sal;
Domain=br;
Publications=pub;
}
void display()
Chayashree G, Asst. Prof. Dept. of ISE 10
Basics of JAVA programming Lab 2023-24

{
System.out.println("Teaching Details");
System.out.println("-----------------------");
System.out.println("ID: "+ StaffID);
System.out.println("Name: "+ Name);
System.out.println("Phone: "+ Phone);
System.out.println("Salary: "+ Salary);
System.out.println("Domain: "+ Domain);
System.out.println("Publications: "+ Publications);
System.out.println("-----------------------");
}
}

class Technical extends Staff


{
String Skills;
Technical(String id,String sn,String mob,float sal,String sk)
{
StaffID=id;
Name=sn;
Phone=mob;
Salary=sal;
Skills=sk;
}
void display()
{
System.out.println("Technical Details");
System.out.println("-----------------------");
System.out.println("ID: "+ StaffID);
System.out.println("Name: "+ Name);
System.out.println("Phone: "+ Phone);
System.out.println("Salary: "+ Salary);
System.out.println("Skill: "+ Skills);
System.out.println("-----------------------");
}
}

class Contract extends Staff


{
int Period;
Contract(String id,String sn,String mob,float sal,int exp)
{
StaffID=id;
Name=sn;
Phone=mob;
Salary=sal;
Period=exp;
}
void display()
{
System.out.println("Contract Staff Details");
System.out.println("-----------------------");
System.out.println("ID: "+ StaffID);
System.out.println("Name: "+ Name);
System.out.println("Phone: "+ Phone);
Chayashree G, Asst. Prof. Dept. of ISE 11
Basics of JAVA programming Lab 2023-24

System.out.println("Salary: "+ Salary);


System.out.println("Experience: "+ Period);
System.out.println("-----------------------");
}
}

class Inheritance
{
public static void main(String args[])
{
Teaching t1=new Teaching("1","Arun","9856987589",40000,"CS","Sapna");
Teaching t2=new Teaching("2","Bharath","8566987589",50000,"EC","Tata McGraw Hill");
Teaching t3=new Teaching("3","Charan","6985987589",60000,"IS","Pearson");
t1.display();
t2.display();
t3.display();

Technical te1=new Technical("1","AKash","7896987589",10000,"Testing");


Technical te2=new Technical("2","Baskar","6325987589",20000,"Networking");
Technical te3=new Technical("3","Chethan","7965987589",30000,"Programming");
te1.display();
te2.display();
te3.display();

Contract c1=new Contract("1","Ashok","7416987589",20000,2);


Contract c2=new Contract("2","Bindhu","9025987589",30000,4);
Contract c3=new Contract("3","Chandu","8885987589",40000,6);
c1.display();
c2.display();
c3.display();
}
}

Chayashree G, Asst. Prof. Dept. of ISE 12


Basics of JAVA programming Lab 2023-24

8. Write a java program to compute rate of interest offered among various Banks using
method overriding.
class Bank {
float getRateOfInterest()
{
return 0.0f;
}
}

class SBI extends Bank {


float getRateOfInterest()
{
return 5.5f;
}
}

class ICICI extends Bank {


float getRateOfInterest()
{
return 6.5f;
}
}

class AXIS extends Bank {


float getRateOfInterest()
{
return 7.5f;
}
}

class MO {
public static void main(String args[])
{
SBI s = new SBI();
ICICI i = new ICICI();
AXIS a = new AXIS();
System.out.println("--------------------------------------------");
System.out.println("Rate of Interest Offered in Different Banks");
System.out.println("--------------------------------------------");
System.out.println("Rate of Interest in SBI is "+s.getRateOfInterest()+" %");
System.out.println("Rate of Interest in ICICI is "+i.getRateOfInterest()+" %");
System.out.println("Rate of Interest in AXIS is "+a.getRateOfInterest()+" %");
System.out.println("--------------------------------------------");
}
}

Chayashree G, Asst. Prof. Dept. of ISE 13


Basics of JAVA programming Lab 2023-24

9. Write a java program to read two integers a and b. Compute a/b and print result
when b is not zero. Raise an exception when b is equal to zero and demonstrate the
working of ArrayIndexOutOfBounds Exception.

class Excep {
public static void main(String args[])
{
try
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int res = a / b ;
System.out.println("Answer is : " +res);
}
catch(ArithmeticException ae)
{
System.out.println("Dividing a Number by Zero Error : "+ ae);
}
catch(ArrayIndexOutOfBoundsException aie)
{
System.out.println("Insufficient Inputs : "+ aie);
}
}
}

Chayashree G, Asst. Prof. Dept. of ISE 14


Basics of JAVA programming Lab 2023-24

10. Write a java program to sort the elements in an array of strings using compareTo()
method.

class Cmp {
public static void main(String args[])
{
String a[]={"Charan","Bharath","Darshan","Arun","Eshwar"};
System.out.println("------Before Sorting-------");
String b[];
for(String org : a)
System.out.println(org);
for(int j=1;j<a.length;j++)
{
for(int i=0;i<a.length-j;i++)
{
if(a[i].compareTo(a[i+1]) > 0)
{
String t = a[i];
a[i] = a[i+1];
a[i+1]=t;
}
}
}
System.out.println("------After Sorting-------");
for(String after : a)
System.out.println(after);
}
}

Chayashree G, Asst. Prof. Dept. of ISE 15


Basics of JAVA programming Lab 2023-24

VIVA QUESTIONS
1. Is Java Platform Independent if then how?
2. What are the top Java Features?
3. What are the various access specifiers in Java?
4. Explain public static void main (String args[]) in Java.
5. What will happen if we declare don’t declare the main as static?
6. Explain different data types in Java.
7. What is an array?
8. What are the types of an array?
9. What is the difference between int array [] and int [] array?
10. What are control statements?
11. When do we use if statement?
12. Write syntax of if-else ladder.
13. Write syntax of switch statement.
14. Explain selection statements.
15. Explain looping and jump statements.
16. Explain working of break and return statement.
17. What are the three principles of OOP?
18. What is overloading?
19. What is inheritance?
20. What are the different types of inheritance?
21. Explain extends keyword with syntax.
22. Explain super keyword usage.
23. What is overriding?
24. Write difference between overloading and overriding methods.
25. What is exception?
26. What is the difference between error and exception?
27. What is exception handling?
28. What are the different types of exceptions?
29. What are checked and unchecked exceptions?
30. What are throwable exceptions?
31. What are runtime exceptions?
32. What are the five keywords used in exception? write syntax.
33. Write syntax of multiple catch statement.
34. What are strings in Java?
35. Why string objects are immutable in Java?

Chayashree G, Asst. Prof. Dept. of ISE 16


Basics of JAVA programming Lab 2023-24

Chayashree G, Asst. Prof. Dept. of ISE 17

You might also like