Java Lab 5
Java Lab 5
Interface
class InterfaceTest {
public static void main(String s[]) {
Circle c1=new Circle();
Rectangle r1=new Rectangle();
Area a1;
a1=c1;
System.out.println("the area of the circle is "+s1.compute(10,10));
a1=r1;
System.out.println("the area of the rectangle is is "+s1.compute(20,20));
}
}
3. Program to display the marks and total of a student by using both inheritance and
interface
class Student{
int rollnum;
void getNumber(int n) {
rollnum=n;
}
void putNumber() {
System.out.println("Roll No : "+rollnum);
}
}
interface Sports{
float sportwt=6.0f;
void putwt();
}
class Hybrid{
public static void main(String []args){
Results s1= new Results();
s1.getNumber(1234);
s1.getMarks(43.5f, 38.6f);
s1.display();
}
}
6. Program to display the given one dimentional array and sort by using sort().
import java.util.Arrays;
class Demo {
public static void main(String args[]) {
int A[] = { 5, 8, 2, 9, 1, 4, 7, 56, 15 };
System.out.println("The given array is ");
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]);
System.out.print("\t");
}
Arrays.sort(A);
System.out.println();
System.out.println("The sorted array is");
for (int i = 0; i < A.length; i++) {
System.out.print(A[i]);
System.out.print("\t");
}
}
}
11. Program to sort an array and search an element inside it by using sort () and
binarySearch () method
import java.util.Arrays;
class BufferDemo {
public static void main(String args[]) throws IOException {
StringBuffer str = new StringBuffer("Fathima");
System.out.println("\n1.Length\n2.Capacity\n3.Setlength\n4.Charat\n5.
Setcharat\n6.Append\n7.Deletecharat\n8.Substring\n9.
Substring1\n10.Insert\n11.Reverse");
System.out.println("Enter ur chioce");
DataInputStream ds = new DataInputStream(System.in);
int ch = Integer.parseInt(ds.readLine());
switch (ch) {
case 1:
System.out.println("The length of the string is:" + str.length());
break;
case 2:
System.out.println("The capacity of String:" + str.capacity());
break;
case 3:
str.setLength(15);
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
System.out.println("Set length of String:" + str.length());
break;
case 4:
System.out
.println("The character at 6th position:" + str.charAt(5));
break;
case 5:
str.setCharAt(2, 'u');
System.out.println("The string after setting position is:" + str);
break;
case 6:
System.out.println("The string after appending:"
+ str.append(" Mohammed"));
break;
case 7:
System.out.println("The string after deletion:"
+ str.deleteCharAt(5));
break;
case 8:
System.out.println("The substring is:" + str.substring(2));
break;
case 9:
System.out.println("The subsstring2 is:" + str.substring(3, 5));
break;
case 10:
System.out.println(" The string after insertion is:"
+ str.insert(6, 'm'));
break;
case 11:
System.out.println("The string after reverse is:" + str.reverse());
}
}
}
Assignment:
1. Write a program to display the marks and percentage of a student along with the name and
Roll number using multiple inheritance
2. Write a program to display the average of the elements of a given single dimensional array.
3. Write a program to display the multiplication of two matrices.
Best of luck