Java Lab Prog
Java Lab Prog
Declaration – Syntax:
Initialization – Syntax:
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
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
c. if-else-if ladder
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.
================================================================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
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("----------------------------");
}
}
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)
{
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
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;
}
{
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 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();
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 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("--------------------------------------------");
}
}
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);
}
}
}
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);
}
}
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?