You are on page 1of 8

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga
Advanced Programming 1: Lab sheet 5

Interface

1. Program to find the area of a circle and rectangle using Interface


interface Area {
static final float pi=3.142f;
float compute(float x,float y);
}
class Rectangle implements Area {
public float compute(float x, float y) {
return(x*y);
}
}
class Circle implements Area {
public float compute(float x,float y) {
return(pi*x*x);
}
}

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));
}
}

2. Program to show that an interface can extend another.


interface A {
void meth1();
void meth2();
}
// B now includes meth1() and meth2() -- it adds meth3().
interface B extends A {
void meth3();
}
// This class must implement all of A and B
class MyClass implements B {
public void meth1() {
System.out.println("Implement meth1().");
}

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


public void meth2() {
System.out.println("Implement meth2().");
}
public void meth3() {
System.out.println("Implement meth3().");
}
}
class IFExtend {
public static void main(String arg[]) {
MyClass ob = new MyClass();
ob.meth1();
ob.meth2();
ob.meth3();
}
}

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);
}
}

class Test extends Student {


float part1, part2;
void getMarks(float m1, float m2) {
part1=m1;
part2=m2;
}
void putMarks(){
System.out.println("Marks obtained");
System.out.println("part1 : " +part1);
System.out.println("part2 : "+ part2);
}
}

interface Sports{
float sportwt=6.0f;
void putwt();
}

class Results extends Test implements Sports{


float total;
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
public void putwt() {
System.out.println("Sports wt =" + sportwt);
}
void display() {
total= part1+part2+sportwt;
putNumber();
putMarks();
putwt();
System.out.println("Total Score= "+total);
}
}

class Hybrid{
public static void main(String []args){
Results s1= new Results();
s1.getNumber(1234);
s1.getMarks(43.5f, 38.6f);
s1.display();
}
}

4. Program to generate a simple package


package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0)
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance {
public static void main(String args[]) {
Balance cust1 = new Balance("Fathima", 223.85);
Balance cust2 = new Balance("Mohammed", 150.02);
Balance cust3 = new Balance("Abdulla", -11.30);
cust1.show();
cust2.show();
cust3.show();
}
}
Javac –d . AccountBalance.java
Java MyPack.AccountBalance
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
5. Program to assign and display the values of a single dimensional array
public class ArrayDemo {
public static void main(String[] args) {
int a[]=new int[5];
a[0]=2;
a[1]=5;
a[2]=7;
a[3]=4;
a[4]=9;
System.out.println("The given array is");
for(int i=0;i<5;i++){
System.out.print(a[i]);
System.out.print("\t");
}
}
}

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");
}
}
}

7. Program accessing the values from one-dimensional array


class Single {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
System.out.println("August has " + month_days[7] + " days.");
}
}

8. Program to print a two-dimensional array.


class TwoD {
public static void main(String args[]) {

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


int twoD[][] = new int[4][5];
int i, j, k = 0;
for (i = 0; i < 4; i++)
for (j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 4; i++) {
for (j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}

9. Program to Initialize a two-dimensional array.


class TwoDIn {
public static void main(String args[]) {
int m[][] = { { 1, 1, 2, 3 }, { 1, 1, 2, 1 }, { 2, 1, 0, 2 },{ 3, 3, 3, 1 } };
int i, j;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}

10. Program to find the addition of two matrices


class AddMatrix {
static void add() throws IOException {
int a[][], b[][], c[][];
a = new int[2][2];
b = new int[2][2];
c = new int[2][2];
DataInputStream dts = new DataInputStream(System.in);
System.out.println("enter first matrix 2*2 order :");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
a[i][j] = Integer.parseInt(dts.readLine());
}
}
System.out.println("enter second matrix 2*2 order :");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
b[i][j] = Integer.parseInt(dts.readLine());
}
}
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
System.out.println("The addition of the above two matricies is :");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println("\t");
}
}

public static void main(String arg[]) throws IOException {


add();
}
}

11. Program to sort an array and search an element inside it by using sort () and
binarySearch () method
import java.util.Arrays;

public class SortSearch {


public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 2);
System.out.println("Found 2 at " + index);
}

private static void printArray(String message, int array[]) {


System.out.println(message + ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if (i != 0) {
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
}

12. Program using the Methods of String Class


import java.io.*;
class Demo {
public static void main(String arg[]) throws IOException{
Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga
int n = 5;
int m = 8;
char s = 'n';
String str = new String(" Fathima");
String str3 = new String(" Fathima ");
String str4;
System.out.println(str.equals(str3));
String str1 = str.toLowerCase();
System.out.println("The String in Lower case:" + str1);
String str2 = str1.toUpperCase();
System.out.println("The String in Upper case:" + str2);
str4 = str1.trim();
System.out.println("The String after trimming:" + str4);
System.out.println("The length of String:" + str4.length());
str2 = str1.replace('h', 'd');
System.out.println("The repalced String" + str2);
System.out.println(str1.equals(str2));
System.out.println("The character at position 3:" + str2.charAt(3));

System.out.println("The substring is:" + str2.substring(n));


System.out.println("The substring is:" + str2.substring(n, m));
System.out.println("The Index of O:" + str2.indexOf(s));
}
}

13. Program using the Methods of StringBuffer Class


import java.io.*;
import java.lang.*;

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

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like