PROG 1 : Write a java class which consists of five integer values. These integer values will be supplied through command line arguments. Provide a method which sort the integer members using bubble sort. Now take keyboard input another five integer data which will be added to those integer values in the sorted list. PROCEDURES 1. A class “sort” is created where the bubble sorting of n numbers is done by the method “Bubble()” which returns an integer type array. Another method “display()” prints the sorted array. 2. The main method is defined in the class “BubbleSort”. Here an integer type array of size 10 is declared. The first five elements of the array is given through command line arguments. These five elements are sorted by the method “Bubble()” in sort class. The rest five elements are taken from keyboard. These five elements are added to the previous sorted array and the newly created array is sorted again. 3. The display() method is called to print the sorted array. PROGRAM import java.io.*; class Sort { int temp; int[] Bubble(int a[],int n) { for(int i=0;i
a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } return a; } void display(int a[],int n) { for(int i=0;i
class BubbleSort { public static void main(String args[]) { int a[]=new int[10],i; for(i=0;i<5;i++) a[i]=Integer.parseInt(args[i]); Sort S=new Sort(); a=S.Bubble(a,5); System.out.print("\nThe sorted list is : "); S.display(a,5); DataInputStream d=new DataInputStream(System.in); try { for(i=5;i<10;i++) { System.out.print("\nEnter "+(i+1)+"th integer : "); a[i]=Integer.parseInt(d.readLine()); } a=S.Bubble(a,10); System.out.print("\nThe sorted list is : "); S.display(a,10); } catch(Exception e) { System.out.println(e); } } } OUTPUT E:\java>java BubbleSort 25 4 19 91 11 The sorted sorted list is : 4 11 19 25 91 Enter 6th integer : 1 Enter 7th integer : 13 Enter 8th integer : 98 Enter 9th integer : 36 Enter 10th integer : 14 The sorted sorted list is : 1 4 11 13 14 19 25 36 91 98
class BubbleSort { public static void main(String args[]) { int a[]=new int[10],i; for(i=0;i<5;i++) a[i]=Integer.parseInt(args[i]); Sort S=new Sort(); a=S.Bubble(a,5); System.out.print("\nThe sorted list is : "); S.display(a,5); DataInputStream d=new DataInputStream(System.in); try { for(i=5;i<10;i++) { System.out.print("\nEnter "+(i+1)+"th integer : "); a[i]=Integer.parseInt(d.readLine()); } a=S.Bubble(a,10); System.out.print("\nThe sorted list is : "); S.display(a,10); } catch(Exception e) { System.out.println(e); } } } OUTPUT E:\java>java BubbleSort 25 4 19 91 11 The sorted sorted list is : 4 11 19 25 91 Enter 6th integer : 1 Enter 7th integer : 13 Enter 8th integer : 98 Enter 9th integer : 36 Enter 10th integer : 14 The sorted sorted list is : 1 4 11 13 14 19 25 36 91 98
PROG 2 : Write a java program to illustrate the use of static data, method and 'this' keyword. Provide constructor which will initialize one integer data in the class. Now display the non-static integer data which need to be initialized through static block. PROCEDURES 1 : A class A is defined which has a static data member a,initialized the value 10 and another non-static data member b. 2 : A constructor is used to initialize these data members. 3 : Now a static method Show is defined which initializes a as 5 and prints it. 4 : Another non-static method Chval is defined & initializes a and b through parameters and print their values. 5 : The main method is defined in the class UtilityOfStaticThis. As Show is a static method so the class A can call it directly without any object. The non-static method Chval() is called through an object of A.
PROGRAM class A { static int a=10; int b; A() { a=0; b=0; }
static void Show() { a=5; //b=5; can't access non-static data System.out.println("Previous value:"+a); } void ChVal(int x,int y) {//can access both a=x; b=y; System.out.println("Changed value:"+a); System.out.println("Changed value:"+b); } } public class UtilityOfStaticThis { public static void main(String[] args) { A.Show(); A obj=new A(); obj.ChVal(6,7); } }
OUTPUT E:\java>javac UtilityOfStaticThis.java E:\java>java UtilityOfStaticThis Previous value:5 Changed value:6 Changed value:7
PROG 3 : Define two abstract method area() and volume() inside two separate interfaces. The method area returns the area of circle, the method volume returns the volume of sphere. Implements those methods using single class. Use constructor for initialization. Take keyboard input for the radius. PROCEDURES 1 : Two interfaces A & B are defined in which two abstract methods area() & volume() are declared respectively. 2 : A new class C is created which implements the class A & B. Constructor is used to initialize the data members of C. Both the abstract methods are defined here and they are described as public. 3 : The main method is defined in the class IntrDemo. Here the value of radius data member is taken from keyboard and it is passed as argument to
compute the area and volume. Finally the class displays the area and volume.
PROGRAM import java.io.*; interface A { abstract double area(); } interface B { abstract double volume(); } class C implements A,B { double r; C(double rad) { r=rad; } public double area() { return (4*3.14*r*r); } public double volume() { return ((double)4/3*3.14*r*r*r); } } class IntrDemo { public static void main(String args[]) { System.out.print("\nEnter the radius : "); DataInputStream d=new DataInputStream(System.in); try { double rad=Integer.parseInt(d.readLine()); C c=new C(rad); System.out.print("\nArea = " + c.area()); System.out.println("\n\nVolume = " + c.volume()); } catch(Exception e) { System.out.println(e); } }
} OUTPUT E:\java>javac IntrDemo.java Note: IntrDemo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java IntrDemo Enter the radius : 3 Area = 113.03999999999999 Volume = 113.03999999999998
PROG 4 : An abstract class rectangle consist of two data member length and width. The only abstract method volume() returns integer value. The implementing class adds another data member height, so that rectangle now becomes the rectangular box. Use constructor for initialization of data member which are private. Take keyboard input for data members. PROCEDURES 1 : An abstract class Rectangle is defined which contain data members length & width and a constructor to initialize the data members. An abstract method volume() is declared inside it. 2 : Another class RectangularBox inherits the class Rectangle and it uses another private data member height. A constructor is used to initialize the data member height. Super keyword is used to call the superclass constructor. The method volume() is defined here which returns integer value. 3 : The main method is defined in the AbstractDemo class. The value of data members of this class are taken as keyboard input. An object of this class is created and this object is used to compute the volume by calling the volume() method. PROGRAM import java.io.*; abstract class Rectangle { int length,width; Rectangle(int l,int w) { length=l; width=w; } abstract int volume(); } class RectangularBox extends Rectangle { private int height; RectangularBox(int l,int w,int h) { super(l,w); height=h; } int volume() { return (length*width*height); } } class AbstractDemo
{ public static void main(String args[]) { int l,w,h; DataInputStream d=new DataInputStream(System.in); try { System.out.print("\nEnter length : "); l=Integer.parseInt(d.readLine()); System.out.print("\nEnter width : "); w=Integer.parseInt(d.readLine()); System.out.print("\nEnter height : "); h=Integer.parseInt(d.readLine()); RectangularBox rectbox=new RectangularBox(l,w,h); System.out.println("\nVolume = "+rectbox.volume()); } catch(Exception e) { System.out.println(e); } } } OUTPUT E:\java>javac AbstractDemo.java Note: AbstractDemo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java AbstractDemo Enter length : 25 Enter width : 10 Enter height : 5 Volume = 1250
PROG 5 : Create a package shape. Inside the package define a class named as figure, which will compute the volume of cube, cylinder and rectangular box using overloading method. Now there is another interface named as circle, which consist of radius data member initialized to value 10 and one abstract method area which returns the area of the circle in double. now implement this interface from within the figure class. Use keyboard input for defining the value of length, width and height. Access this class figure and method defined inside it from another package.
PROCEDURES 1 : A package ‘shape’ is created. 2 : Inside this package an interface Circle is defined which contain a data member radius and an abstract method area() which returns a double value. 3 : A public class Figure is created which implements the interface Circle. Inside it the method volume() is defined to compute the volume of cube, cylinder and rectangular box by applying the method overloading technique. Another method area() is defined which returns the area of a circle. 4 : This file is saved as Figure.java in the shape directory. 5 : Now another package named newpackage is created. 6 : Inside this package the main method is defined in class PackageDemo. 7 : An object is created of the Figure class. The value of the data members are taken from keyboard. 8 : By using the object the volume & area of the cube, cylinder,circle & rectangle box is computed.
9 : this file is saved as PackageDemo.java in the newpackage directory.
PROGRAM /* This file is saved as Figure.java in a directory named as ‘shape’. */ package shape; interface Circle { double radius=10; abstract double area(double r); } public class Figure implements Circle { public double volume(double l) { return (l*l*l); } public double volume(double r,double h) { return (3.14*r*r*h); } public double volume(double l,double b,double h) { return (l*b*h); } public double area(double r) { return (3.14*r*r); } } /* This file is saved as PackageDemo.java in a directory named as ‘newpackage’. */ package newpackage; import shape.*; import java.io.*; class PackageDemo { public static void main(String args[]) { Figure f=new Figure(); DataInputStream d=new DataInputStream(System.in); try { System.out.print("Enter length of cube: "); double length=Double.parseDouble(d.readLine());
System.out.println("\nVolume of cube : " + f.volume(length)); System.out.print("\nEnter radius of cylinder : "); double radius=Double.parseDouble(d.readLine()); System.out.print("\nEnter height of cylinder : "); double height=Double.parseDouble(d.readLine()); System.out.println("\nVolume of cylinder : " + f.volume(radius,height)); System.out.print("\nEnter length of the rectangular box : "); length=Double.parseDouble(d.readLine()); System.out.print("\nEnter width of the rectangular box : "); double width=Double.parseDouble(d.readLine()); System.out.print("\nEnter height of the rectangular box : "); height=Double.parseDouble(d.readLine()); System.out.println("\nVolume of the rectangular box : " + f.volume(length,width,height)); } catch(Exception e) { System.out.println(e); } } } OUTPUT E:\>cd java\shape E:\java\shape>javac Figure.java E:\java\shape>cd.. E:\java>javac newpackage\PackageDemo.java Note: newpackage\PackageDemo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java newpackage.PackageDemo Enter length of cube: 3 Volume of cube : 27.0 Enter radius of cylinder : 2 Enter height of cylinder : 4 Volume of cylinder : 50.24 Enter length of the rectangular box : 5 Enter width of the rectangular box : 4 Enter height of the rectangular box : 3 Volume of the rectangular box : 60.0
PROG 6 : Write a class Employee where basic salary is given as keyboard input. da=20% of the basic salary & hra is 15% of the basic salary. Total salary=basic + da + hra. If total salary is greater than Rs.20000 & service experience is greater than equal 20 year then he eligible for loan. If the total salary is less than Rs.20000 & service experience is >=20 years then the person is eligible for bonus. Bonus=total salary*20%. In case, the bonus >2000, then exception for bonus will be thrown. and if the service experience is less than 20 yrs then service exception will be thrown. (N.B. Create user defined exception classes). If the person is eligible for loan but hra>5000 then that person will not get loan and user defined exception is thrown. PROCEDURES
1 : A class ExperienceException inherits from the class Exception is created. Inside it a private data member year is declared. A constructor is used to initialize this data member. A public method toString() is overloaded to print the exception. 2 : A class BonusException inherits from the class Exception is created. Inside it a private data member bonus is declared. A constructor is used to initialize this data member. A public method toString() is overloaded to print the exception. 3 : A class LoanException inherits from the class Exception is created. Inside it a private data member hra is declared. A constructor is used to initialize this data member. A public method toString() is overloaded to print the exception. 4 : A class Employee is created and the data members are initialized using constructor which throws ExperienceException object. Two methods availabilityOfLoan() & availabilityOfBonus() are defined to determine the availability of loan & bonus respectively. A method Bonus() is defined to calculate the bonus and it throws BonusException object. A method Loan() which returns string value is defined to compute the loan availability status and it throws LoanException object. 5 : The main method is defined within the class SalaryDemo. The basic salary & year of experience are taken from keyboard. An object of class Employee is created to compute the loan & bonus. If there is any exception the exception is printed by catch block.. PROGRAM import java.io.*; class ExperienceException extends Exception { private int year; ExperienceException(int yr) { year=yr; } public String toString() { return "ExperienceException:"+year+" years only"; } } class BonusException extends Exception { private double bonus; BonusException(double bon) { bonus=bon; } public String toString()
{ return "BonusException:"+bonus; } } class LoanException extends Exception { private double hra; LoanException(double h) { hra=h; } public String toString() { return "LoanException hra:"+hra; } } class Employee { double basic_sal,total_sal,hra,da; int ser_exp; Employee(double bs,int yr) throws ExperienceException { if(yr<20) throw new ExperienceException(yr); else { basic_sal=bs; ser_exp=yr; da=basic_sal*0.2; hra=basic_sal*0.15; total_sal=basic_sal+da+hra; } } int avilabilityOfLoan() { if(total_sal>20000 && ser_exp>=20) return 0; else return 1; } int avilabilityOfBonus() { if(total_sal<20000 && ser_exp>=20) return 0; else return 1; } double Bonus() throws BonusException { double bon=0; if(avilabilityOfBonus()==0) { bon=total_sal*0.2;
if(bon>2000) { throw new BonusException(bon); } } return bon; } String Loan() throws LoanException { if(avilabilityOfLoan()==0 && hra<=5000) return "\nYou can get Loan"; else throw new LoanException(hra); } } public class SalaryDemo { public static void main(String[] args) throws Exception { DataInputStream in=new DataInputStream(System.in); System.out.print("\nEnter basic salary : "); double bs=Double.parseDouble(in.readLine()); System.out.print("\nEnter service experience (in years) : "); int yr=Integer.parseInt(in.readLine()); try { Employee e=new Employee(bs,yr); System.out.println("\nBonus : "+e.Bonus()); System.out.println(e.Loan()); } catch(ExperienceException ex) { System.out.println(ex); } catch(BonusException ex) { System.out.println(ex); } catch(LoanException ex) { System.out.println(ex); } } } OUTPUT E:\java>javac SalaryDemo.java Note: SalaryDemo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java SalaryDemo
Enter basic salary : 25000 Enter service experience (in years) : 23 Bonus : 0.0 You can get Loan
PROG 7: Create two thread. One thread will print the values of 1, 2, 3, 4, 5. Another thread will print ONE, TWO, THREE, FOUR, FIVE. PROCEDURES 1 : A class NumberThread which implements the Runnable class is created. 2 : Inside this class the data members name & a Thread t is created. A String type array list of size 5 is created. 3 : A constructor is used to initialize the data members and the start() method is called to start the thread. 4 : A public method run() is overloaded to print each elements of the list and to block the thread for a specified time. 5 : The main method is defined in the class ThreadCount. In this class two string type array are declared to store the numeric and word value. Now two objects of NumberThread class is created to initialize the data members of NumberThread class. PROGRAM class NumberThread implements Runnable { String name; Thread t; String[] list=new String[5]; NumberThread(String n,String[] l) { name=n; t=new Thread(this,name); for(int i=0;i<5;i++) list[i]=l[i]; t.start(); } public void run() { try { for(int i=0;i<5;i++) { System.out.println(list[i]);
Thread.sleep(1000); } } catch(InterruptedException e) { System.out.println(e); } } } public class ThreadCount { public static void main(String[] args) { String[] l1={"1","2","3","4","5"}; String[] l2={"ONE","TWO","THREE","FOUR","FIVE"}; NumberThread nt1=new NumberThread("Numeric",l1); NumberThread nt2=new NumberThread("Words",l2); } } OUTPUT E:\java>javac ThreadCount.java E:\java>java ThreadCount 1 ONE 2 TWO 3 THREE 4 FOUR 5 FIVE
PROG 8: Take one array of ten integer data members. Now one thread will perform the addition for elements 6 to 10.Another thread will perform multiplication of 1 to 5.Take keyboard i/o for array. PROCEDURES 1. Class NewThread11 is created which implements Runnable (system defined interface) with data members a. t [Thread type] b. an integer array arr[10] with 10 elements. and methods. a. A constructor is created to initalise the array by given arguments and to initialise the Thread object t & starts it. b. public void run() method is impplemented here of the system defined interface Runnable with algorithm i> set temp=1 ii> for i=0 to 5 set temp=temp*arr[i] print the value of temp and sleep for while if there occurs any interrupt the throw InterruptedException iii> print the value of temp finally.
2. Class NewThread22 is created which implements Runnable (system defined interface) with data members a. t [thread type] b. an integer array arr[10] with 10 elements. and methods, a. A constructor is created to initalise the array by given arguments and to initialise the Thread object t & starts it. b. public void run() method is impplemented here of the system defined interface Runnable with algorithm i> set temp=1 ii> for i=5 to 10 set temp=temp+arr[i] print the value of temp and sleep for while if there occurs any interrupt the throw InterruptedException iii> print the value of temp finally. 3. Class multithread2 is created which contains main method 4. Taken input from keyboard for the array 5. T1 & T2 two objects are created of class NewThread11 & Newthread22 respectively. 6. Used T1.t.join() and T2.t.join() so that no thread cann't completed untill main thread is completed. PROGRAM import java.io.*; class NewThread11 implements Runnable { Thread t; int arr[] = new int[10]; NewThread11 (int a[]) { t=new Thread (this,"Adder"); System.out.println("Thread name : "+t); for(int i=0;i<10;i++) arr[i]=a[i]; t.start(); } public void run () { int temp=0; try { for(int i=5;i<=9;i++) { temp+=arr[i]; System.out.println(t+" : "+temp); Thread.sleep(1000); } }catch (InterruptedException e) { System.out.println("An Exception Caught : "+e); } System.out.println("Addition result : "+temp);
} } class NewThread22 implements Runnable { Thread t2; int arr[] = new int[10]; NewThread22 (int a[]) { t2 = new Thread (this,"Multiplier"); System.out.println("Thread Name : "+t2); for(int i=0;i<10;i++) arr[i]=a[i]; t2.start(); } public void run () { int temp=1; try { for(int i=0;i<5;i++) { temp*=arr[i]; System.out.println(t2+" : "+temp); Thread.sleep(1000); } }catch (InterruptedException e) { System.out.println("An Exception Caught : "+e); } System.out.println("Multiplication Result : "+temp); } } class multithread2 { public static void main (String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String input; int data[] = new int[10]; System.out.println("Enter the data of array : "); for(int i=0;i<10;i++) { System.out.print("Index "+(i+1)+" : "); input = br.readLine(); try { data[i] = Integer.parseInt(input); }catch (NumberFormatException e) { System.out.println("An Exception Caught : "+e); } } NewThread11 T1 = new NewThread11 (data); NewThread22 T2 = new NewThread22 (data); try { System.out.println("Waiting for thread to be exited ..."); T1.t.join(); T2.t2.join(); }catch (InterruptedException e) { System.out.println("An Exception Caught : "+e); } }
} OUTPUT E:\java>javac multithread2.java E:\java>java multithread2 Enter the data of array : Index 1 : 1 Index 2 : 2 Index 3 : 3 Index 4 : 4 Index 5 : 5 Index 6 : 6 Index 7 : 7 Index 8 : 8 Index 9 : 9 Index 10 : 10 Thread name : Thread[Adder,5,main] Thread[Adder,5,main] : 6 Thread Name : Thread[Multiplier,5,main] Waiting for thread to be exited ... Thread[Multiplier,5,main] : 1 Thread[Adder,5,main] : 13 Thread[Multiplier,5,main] : 2 Thread[Adder,5,main] : 21 Thread[Multiplier,5,main] : 6 Thread[Adder,5,main] : 30 Thread[Multiplier,5,main] : 24 Thread[Adder,5,main] : 40 Thread[Multiplier,5,main] : 120 Addition result : 40 Multiplication Result : 120
PROG 9: Take two string as input from the keyboard for applet "HELLO" & "World". The applet will display str1+" "+str2 & also
str2+" "+str1 in the Specified location. PROCEDURES Class Hello_world is created which extends Applet (system defined class) with data members a. str1 [String type] b. str2 [String type] and meothds, a. init() method is implemented from Applet, as public void init() to initialise background color and foreground color. b. start() method is implemented from Applet, as public void start() to take keyboard input for str1 & str2 in a GUI. c. paint() meothd is implemented from Applet, as public void paint() to print the str1 & str2 in a GUI. PROGRAM import java.applet.Applet; import java.awt.*; import javax.swing.*; /* */ public class Hello_World extends Applet { String str1; String str2; public void init() { setBackground(Color.CYAN); setForeground(Color.RED); } public void start () { str1 = JOptionPane.showInputDialog("Enter String one : "); str2 = JOptionPane.showInputDialog("Enter String two : "); } public void paint (Graphics g) { String str; str=str1+" "+str2; g.drawString(str, 50, 50); str=str2+" "+str1; g.drawString(str,50,70); } } OUTPUT E:\java>javac Hello_World.java
E:\java>appletviewer Hello_World.java
PROG 10: Take keyboard input for a student information system where student data includes name, roll and marks . Create a Department class consisting of dept_name and hod_name as data members. Provide constructor for initialization of these data members. Now if the user gives roll as input then the corresponding student marks will be displayed in grading format where 90-100 is grade 'A', 80-89 is grade 'B', 50-79 is grade 'C', and below 50 is 'FAIL'. Provide user defined exception class object that will be thrown if marks>100 & <0. Check also whether the roll no exists or not. The student class contains dept_name as reference. Now if a student is "FAIL' grade then hod_name and the corresponding department of the student will be displayed also. PROCEDURES 1 : A class Department is created and a constructor is used to initialize its data members(n,dept_name,hod). 2 : A class Student is created and a constructor is used to initialize its data members. A reference to the object of Department class is created in it. The data members dept_name and hod are taken from keyboard. The grade is calculated in the method showGrade() and hence printed. 3 : The main method is defined in the class StdInfo. Name of the student, roll and marks are given from keyboard. 4 : A roll number is taken as input from keyboard and corresponding to the roll number the grade is computed and printed. PROGRAM import java.io.*; class Department { int n; String dept_name,hod; Department(String d,String h) { dept_name=d; hod=h; } } class Student
{ String name,roll; int marks; Department D; DataInputStream in=new DataInputStream(System.in); Student(String n,String r,int m) throws Exception { name=n; roll=r; marks=m; System.out.print("Enter department name:"); String d=in.readLine(); System.out.print("Enter name of head of the department:"); String h=in.readLine(); D=new Department(d,h); } void showGrade() { if(marks>=90) System.out.println("Grade A"); else if(marks>=80) System.out.println("Grade B"); else if(marks>=50) System.out.println("Grade C"); else System.out.println("Fail, Department name:"+D.dept_name+" Head of the Department:"+D.hod); } } public class StdInfo { public static void main(String[] args) throws Exception { int i=0; DataInputStream in=new DataInputStream(System.in); System.out.print("Enter number of students:"); int n=Integer.parseInt(in.readLine()); Student[] S=new Student[n]; for(i=0;i
for(i=0;ijavac StdInfo.java Note: StdInfo.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java StdInfo Enter number of students:3 Enter name:Krishnendu Enter roll:35 Enter marks:86 Enter department name:CSE Enter name of head of the department:PND Enter name:Saurav Enter roll:37 Enter marks:95 Enter department name:CSE Enter name of head of the department:PND Enter name:Kawshal Enter roll:23 Enter marks:85 Enter department name:IT Enter name of head of the department:MM Enter roll:37 Grade A
PROG 11: Draw two concentric ci rc les using applet,and f i l l colors.(use g.setColor() function).
with dif ferent
PROCEDURES 1 : An applet box of width 400 and height 400 is created and is named as ‘ConCir.class’. 2 : A class ConCir which inherits the class Applet is created. The public paint() method is overloaded. Inside it two concentric circles with different colors and dimensions is drawn using drawOval(), setColor() & fillOval() methods. PROGRAM import java.awt.*;
import java.applet.*; /**/ public class Concir extends Applet { public void paint(Graphics g1) { g1.drawOval(20,20,200,200); g1.setColor(Color.green); g1.fillOval(20,20,200,200); g1.drawOval(70,70,100,100); g1.setColor(Color.red); g1.fillOval(70,70,100,100); } } OUTPUT E:\java>javac Concir.java E:\java>appletviewer Concir.java
PROG 12 : Implement the following structure
abstract class A { int x; A(int i){x=i;} abstract void f1(); abstract class B{ int x; B(int i){x=i;} abstract void f2(); } interface P{int x=11; interface Q{int x=13;}} Each implementing class also contains variable int x. print all x from within void f2(). PROCEDURES 1 : An abstract class A is defined. Inside it a constructor is used to initialize its data member. An abstract method f1() is declared in it. Another abstract class B is defined in this class. A constructor is defined in B to initialize B’s data member. An abstract method f2() is declared in it. 2 : An interface P is defined and inside it another interface Q is also defined. 3 : The class X inherits A and implements P is defined. 4 : The constructor of X is used to initialize the data members of the superclass A using ‘super’ keyword. Inside X the f1() method is defined. 5 : Another class Y inherits B and implements Q is defined inside X. A constructor is used to initialize thae data members of its superclass using ‘super’ keyword. The f2() method is defined which prints the data members of its class and its parent class. 5 : The main method is defined in another class StructDemo. Inside it an object of X is created and the method f1() is called through this object. PROGRAM abstract class A { int x; A(int i) { x=i; } abstract void f1(); abstract class B {
int x; B(int i) { x=i; } abstract void f2(); } } interface P { int x=11; interface Q { int x=13; } } class X extends A implements P { X(int i) { super(i); } public void f1() { Y y=new Y(2); y.f2(); } class Y extends B implements Q { Y(int i) { super(i); } public void f2() { System.out.println("Inside of f2() x:"+X.super.x); System.out.println("Inside of f2() x:"+P.x); System.out.println("Inside of f2() x:"+super.x); System.out.println("Inside of f2() x:"+Q.x); } } } public class StructDemo { public static void main(String[] args) { X x=new X(5); x.f1(); } }
OUTPUT E:\java>javac StructDemo.java E:\java>java StructDemo Inside of f2() x:5 Inside of f2() x:11 Inside of f2() x:2 Inside of f2() x:13
PROG 13 : Check whether a String is PALINDROME or not i.e. the string is same both from the front side(left to right) and reverse side(Right to left). for ex. RADAR is a palindrome. PROCEDURES 1 : A class Word is defined. Inside it a data member str of String type is declared. A constructor is used to initialize str as x. A method IsPalindrome() is defined which compares the first half of the string with the reverse of the second half of the string. 2 : If they are equal, then the string is palindrome. Otherwise the string is not palindrome. 3 : The main method is defined inside a public class Palindrome. The string is taken from keyboard. Then an object of class Word is created through which the IsPalindrome() method is called. PROGRAM import java.io.*; class Word { String str; Word(String x) { str=x; } String IsPalindrome() { int n=str.length(); int i=0; for(i=0;i
} if(i==n/2) return "The Given word is Palindrome."; else return "The Given word is not Palindrome."; } } public class Palindrome { public static void main(String[] args) throws Exception { DataInputStream in=new DataInputStream(System.in); System.out.print("Enter a word:"); String s=in.readLine(); Word w=new Word(s); System.out.println(w.IsPalindrome()); } }
OUTPUT E:\java>javac Palindrome.java Note: Palindrome.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. E:\java>java Palindrome Enter a word:radar The Given word is Palindrome. E:\java>java Palindrome Enter a word:gcect The Given word is not Palindrome.