This document contains 4 Java programming assignments demonstrating various OOP concepts:
1. Implementing wrapper classes and their methods to compare and perform operations on Integer and Float objects.
2. Implementing inheritance by overriding methods in a subclass.
3. Implementing multilevel inheritance with access controls applied to data members and methods.
4. Implementing interfaces by defining an interface with a method and having different classes implement the interface by providing their own implementation of the method.
This document contains 4 Java programming assignments demonstrating various OOP concepts:
1. Implementing wrapper classes and their methods to compare and perform operations on Integer and Float objects.
2. Implementing inheritance by overriding methods in a subclass.
3. Implementing multilevel inheritance with access controls applied to data members and methods.
4. Implementing interfaces by defining an interface with a method and having different classes implement the interface by providing their own implementation of the method.
This document contains 4 Java programming assignments demonstrating various OOP concepts:
1. Implementing wrapper classes and their methods to compare and perform operations on Integer and Float objects.
2. Implementing inheritance by overriding methods in a subclass.
3. Implementing multilevel inheritance with access controls applied to data members and methods.
4. Implementing interfaces by defining an interface with a method and having different classes implement the interface by providing their own implementation of the method.
1. Write a Java Program to implement Wrapper classes and their methods.
Code class WrapperTest { public static void main (String args[]) { Integer A = new Integer (100); Integer B = new Integer ("98"); Integer C = new Integer (99);
System.out.println("Using compareTo() A and B: " + A.compareTo(B));
System.out.println("Using compareTo() A and C: " + A.compareTo(C));
System.out.println("Using equals() A and B: " + A.equals(B));
System.out.println("Using equals() A and C: " + A.equals(C)); Float f1 = new Float("9.45f"); Float f2 = new Float("1.43f"); Float f3 = new Float(48.5f); System.out.println("Using compare() f1 and f2: " +Float.compare(f1,f2)); System.out.println("Using compare() f1 and f3: " +Float.compare(f1,f3));
Float f = A.floatValue() + f1;
System.out.println("Addition of intObj1 and f1: "+ A +"+" +f1+"=" +f );
Integer int1 = Integer.valueOf("76490");
Integer int2 = Integer.valueOf("101001", 4);
Integer int3 = Integer.valueOf("D", 16);
System.out.println("Value of int1 Object: "+ int1); System.out.println("Value of int2 Object: "+ int2); System.out.println("Value of int3 Object: "+ int3); System.out.println("Hex value of int1: " + Integer.toHexString(int1)); System.out.println("Binary Value of int2: "+ Integer.toBinaryString(int2)); } } OUTPUT
2. Write a Java Program to implement inheritance and demonstrate use of
method overriding. Code class A { void display() { System.out.println("This is from class A "); } } class B extends A { void display() { System.out.println("This is from class B "); } } class AB { public static void main(String arg[]) { B obj=new B(); obj.display(); } } Output
3. Write a Java Program to implement multilevel inheritance by applying
various access controls to its data members and methods. * Code import java.io.DataInputStream; class Student { public int rollno; public String name; DataInputStream dis=new DataInputStream(System.in); public void getrollno() { try { System.out.println("Enter rollno "); rollno=Integer.parseInt(dis.readLine()); System.out.println("Enter name "); name=dis.readLine(); } catch(Exception e){ } } void putrollno() { System.out.println("Roll No ="+rollno); System.out.println("Name ="+name); } } class Marks extends Student { protected int m1,m2,m3; void getmarks() { try { System.out.println("Enter marks :"); m1=Integer.parseInt(dis.readLine()); m2=Integer.parseInt(dis.readLine()); m3=Integer.parseInt(dis.readLine()); } catch(Exception e) { } } void putmarks() { System.out.println("m1="+m1); System.out.println("m2="+m2); System.out.println("m3="+m3); } }
class Result extends Marks
{ private float total; void compute_display() { total=m1+m2+m3; System.out.println("Total marks :" +total); } } class MultilevelDemo { public static void main(String arg[]) { Result r=new Result(); r.getrollno(); r.getmarks(); r.putrollno(); r.putmarks(); r.compute_display(); } }
Output
4. Write a Java program to demonstrate use of implementing interfaces.
Code import java.lang.*; interface Area { final static float pi=3.14F; float compute(float x,float y); }
class rectangle implements Area
{ public float compute(float x,float y) { return(pi*x*y); } } class circle implements Area { public float compute(float x,float y) { return(pi*x*x); } } class interfacedemo { public static void main(String a[]) { rectangle rect=new rectangle(); circle cir=new circle(); Area A; A=rect; System.out.println("Area of rectangle="+A.compute(10,20)); A=cir; System.out.println("Area of circle="+A.compute(30,0)); } }