Skip to main content

UNIT 5 OOP1

(Q.1) Describe an abstract class called Shape which has three subclasses: Triangle,Rectangle,Circle. Define one method area() in the abstract class and override this area() in these three subclasses to calculate for specific objects i.e.area() of Triangle subclass should calculate area of triangle etc. Same for Rectangle and Circle.


Ans. import java.lang.Math;

abstract class Shape

{

abstract void area();

double area;

}

class Triangle extends Shape

{

double b=50,h=15;

void area()

{

 area = (b*h)/2;

 System.out.println("area of Triangle -->"+area);

}

}

class Rectangle extends Shape

{

double w=70,h=20;

void area()

{

 area = w*h;

 System.out.println("area of Rectangle -->"+area);

}

}

class Circle extends Shape

{

double r=5;

void area()

{

 area = Math.PI * r * r;

 System.out.println("area of Circle -->"+area);

}

}


class Area

{

public static void main(String [] args)

{

 Triangle t= new Triangle();

 Rectangle r =new Rectangle();

 Circle c =new Circle();


 t.area();

 r.area();

 c.area();

}

}


Output. C:\Users\Digant Prajapati\Desktop>java Area

              area of Triangle -->375.0

              area of Rectangle -->1400.0

              area of Circle -->78.53981633974483


(Q.2) Write a program to demonstrate the multipath inheritance for the classes having relations As shown

in figure 2. 07 A->(B,C)->D


Ans. package multipath_inherintace; import java.io.*;

         interface A

         {

 int a=10;          intb=20;

         }

         interface B extends A

         {

         void sum();

         }

         interface  C extends A

         {

         void mul();

          }

          class D implements B,C

          {


          public void sum()

          {

          int c=a+b; 

          System.out.println("Additionn="+c);

          

          }


          public void mul()

   

          {


          int d=a*b; 

          System.out.println("Multiplication="+d);

          }

          }

          public class Multipath

          {

          public static void main(String args[])

          {

           C  d1 = new D();

           d1.mul();

          d1.sum();

          }

          }


(Q.3) Declare a class called author having author_name as private data member. Extend author class to have two sub classes called book_publication & paper_publication. Each of these classes have private member called title. Show usage of dynamic method dispatch (dynamic polymorphism) to display book or paper publications of a given author. Use command line arguments for inputting data.

Ans. 

class author{

private String name;

 author(String nm){

 name = nm;

}

void display(){

System.out.println("Author: "+name);

}

class book_pub extends author{

private String title;

book_pub(String tt){

 super("");

title = tt;

}

void display(){

System.out.println("Book: "+title);

}

}

class paper_pub extends author{

private String title;

 paper_pub(String tt){

super("");

title = tt;

}

void display(){

System.out.println("Paper: "+title);

}

}

class one{

public static void main(String args[]){

author o1 = new author(args[0]);

book_pub o2 = new book_pub(args[1]);

paper_pub o3 = new paper_pub(args[2]);

author r;

r = o1;

r.display();

r = o2;

r.display();

r = o3;

r.display();

}

}

Output. C:\Users\Digant Prajapati\Desktop>java publication Dp

press "1" to display book author names

press "2" to display book title names

press "3" to display paper publication names

2

Books name of given author are....

------------------------------------------------

java

c lang

Oopc


(Q.4) Differentiate String with StringBuffer class. List out the methods available with String class and explain any five with proper JAVA code in detail.

Ans. Since String is immutable in Java, whenever we do String manipulation like concatenation, substring etc, it generates a new String and discards the older String for garbage collection.



  • Methods of String Class:
  1. public char charAt(int index)
  2. public String concat(String s)
  3. public int length()
  4. public String replace(char old, char new)
  5. public String substring(int begin)/ public String substring(int begin, int end)
  6. public String toLowerCase()
  7. public String toUpperCase()
Public char charAt(int index)

public class CharAtExample {
   public static void main(String args[]) {
String str = "Welcome to string handling tutorial";
//This will return the first char of the string
char ch1 = str.charAt(0);
//This will return the 6th char of the string
char ch2 = str.charAt(5);
//This will return the 12th char of the string
char ch3 = str.charAt(11);
//This will return the 21st char of the string
char ch4 = str.charAt(20);
System.out.println("Character at 0 index is: "+ch1);
System.out.println("Character at 5th index is: "+ch2);
System.out.println("Character at 11th index is: "+ch3);
System.out.println("Character at 20th index is: "+ch4);
   }
}

Output:-
Character at 0 index is: W
Character at 5th index is: m
Character at 11th index is: s
Character at 20th index is: n
public String concat(String s)
public class ConcatenationExample {
   public static void main(String args[]) {
       //One way of doing concatenation
       String str1 = "Welcome";
       str1 = str1.concat(" to ");
       str1 = str1.concat(" String handling ");
       System.out.println(str1);

       //Other way of doing concatenation in one line
       String str2 = "This";
       str2 = str2.concat(" is").concat(" just a").concat(" String");
       System.out.println(str2);
   }
}

Output:-
Welcome to  String handling 
This is just a String

public int length()
public class StringLengthExample {

  public static void main(String[] args) {
    //declare the String object
    String str = "Hello World";
    
    //length() method of String returns the length of a String.

    int length = str.length();
    System.out.println("Length of a String is : " + length);
}
}

Output:- Length of a String is : 11

String toLowerCase()
import java.util.Locale;
public class LowerCaseExample{
   public static void main(String args[]){
       String str = new String("ABC IS NOT EQUAL TO XYZ");
       //Standard method of conversion
       System.out.println(str.toLowerCase());

       //By specifying Locale
       System.out.println(str.toLowerCase(Locale.FRANCE));
   }
}
Output:- abc is not equal to xyz
               abc is not equal to xyz

public String toUpperCase()
import java.util.Locale;
public class UpperCaseExample{
   public static void main(String args[]){
       String str = new String("this is a test string");
       //Standard method of conversion
       System.out.println(str.toUpperCase());

       //By specifying Locale
       System.out.println(str.toUpperCase(Locale.CHINA));
   }
}
Output:- THIS IS A TEST STRING
               THIS IS A TEST STRING

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Microsoft SQL Server 2019 Serial Key For All Edition

  Microsoft SQL Server 2019 Enterprise Edition HMWJ3-KY3J2-NMVD7-KG4JR-X2G8G Microsoft SQL Server 2019 Enterprise Core Edition 2C9JR-K3RNG-QD4M4-JQ2HR-8468J Microsoft SQL Server 2019 Standard Edition PMBDC-FXVM3-T777P-N4FY8-PKFF4 SQL Server2019 key   SQL Server 2019 Enterprise:HMWJ3-KY3J2-NMVD7-KG4JR-X2G8G Strandard:PMBDC-FXVM3-T777P-N4FY8-PKFF4 SQL Server 2017 Enterprise:TDKQD-PKV44-PJT4N-TCJG2-3YJ6B Enterprise Core:6GPYM-VHN83-PHDM2-Q9T2R-KBV83 Strandard:PHDV4-3VJWD-N7JVP-FGPKY-XBV89 Web:WV79P-7K6YG-T7QFN-M3WHF-37BXC SQL Server 2016 Enterprise:MDCJV-3YX8N-WG89M-KV443-G8249 Enterprise Core:TBR8B-BXC4Y-298NV-PYTBY-G3BCP Standard:B9GQY-GBG4J-282NY-QRG4X-KQBCR Web:BXJTY-X3GNH-WHTHG-8V3XK-T8243 SQL Server 2014 Business Intelligence:GJPF4-7PTW4-BB9JH-BVP6M-WFTMJ Developer:82YJF-9RP6B-YQV9M-VXQFR-YJBGX Enterprise:27HMJ-GH7P9-X2TTB-WPHQC-RG79R Enterprise Core:TJYBJ-8YGH6-QK2JJ-M9DFB-D7M9D Strandard:P7FRV-Y6X6Y-Y8C6Q-TB4QR-DMTTK Web:J9MBB-R8PMP-R8WTW-8JJRD-C6GGW

MIUI 11 roadmap revealed: What to know about MIUI 11, and when to expect it

MIUI 11  has been available in China for a short while now, but  Xiaomi  users outside its home market don’t have to wait long. The company has launched MIUI 11 in India, while also issuing a device roadmap for the update. According to a post by the  MIUI India Twitter account , the latest MIUI update will start rolling out from October 22 to October 31. This first wave will target the  Poco F1 ,  Redmi K20 ,  Redmi Y3 ,  Redmi 7 ,  Redmi Note 7 ,  Redmi Note 7S , and  Redmi Note 7 Pro . "> MIUI India {"uid":0.8039879768599738,"hostPeerName":"https://www-androidauthority-com.cdn.ampproject.org","initialGeometry":"{\"windowCoords_t\":0,\"windowCoords_r\":360,\"windowCoords_b\":645,\"windowCoords_l\":0,\"frameCoords_t\":1483,\"frameCoords_r\":330,\"frameCoords_b\":1733,\"frameCoords_l\":30,\"posCoords_t\...
Rumors for the  unveiling  of Xiaomi’s upcoming Android skin have proven true. The company will unveil MIUI 12, alongside Mi 10 Youth Edition 5G, on 27th April in its home country, China. We expect MIUI 12 beta ROMs to start rolling out for a select few phones even before the month comes to a close. Xiaomi made the announcement via an   official post   on Weibo earlier this morning. The company has shared a handful of teasers for both the MIUI 12 and Mi 10 Youth Edition 5G. There’s nothing you can guess about features debuting with the software skin. But, we have got a sneak peek at the newest addition to the Mi 10 lineup The rumor mill has been quite chatty about MIUI 12 features over the past week. We have also seen a couple of leaks recently, giving us a first look at the  new UI elements  and themes. The update is also expected to bring some parity in full-screen gestures. It’s going to  debut Android 10-like gestures , along with TÜV Rheinland cer...