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

VS CODE PRODUCT KEY

Visul Studio 2013 Ultimate Key BWG7X-J98B3-W34RT-33B3R-JVYW9 https://www.microsoft.com/en-us/download/details.aspx?id=55992 https://go.microsoft.com/fwlink/?linkid=841310 https://www.microsoft.com/en-us/download/details.aspx?id=35747 https://downloads.i-theses.com/?task=downloads&groupid=9&id=110 Product Year Version Product Keys Visual Studio 2022 2022 Enterprise: VHF9H-NXBBB-638P6-6JHCY-88JWH Professional: TD244-P4NB7-YQ6XK-Y8MMM-YWV2J Visual Studio 2020 2020 HG8FD-DR6F8-T7G9Y-H80UI-9NUB8 YV7T8-R6C8R-C6VT9-7GBHY-80NUJ T6RX5-EZXCR-6VT7B-Y80NU-HBGVF CDX5X-7E5CR-86VT7-9BY9V-7TC8R Visual Studio 2019 2019 16.x Professional: NYWVH-HT4XC-R2WYW-9Y3CM-X4V3Y Enterprise: BF8Y8-GN2QH-T84XB-QVY3B-RC4DF Visual Studio 2017 2017 15.x Test Professional: VG622-NKFP4-GTWPH-XB2JJ-JFHVF Professional: KBJFW-NXHK6-W4WJM-CRMQB-G3CDH 4F3PR-NFKDB-8HFP7-9WXGY-K77T7 Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF N2VYX-9VR2K-T733M-MWD9X-KQCDF Visual Studio 2015 2015 14.x Profession
   (MI) – PRACTICAL LIST 1 Write an 8085 assembly language program for exchanging two 8-bit numbers stored in memory locations 2050h and 2051h. 2 Write an 8085 assembly language program to add two 8-bit numbers stored in memory locations 2050h and 2051h. Store result in location 2052h 3 Write an 8085 assembly language program to subtract two 8-bit numbers stored in memory location 2050h from 2051h.Store result in location 2052h. 4 Write an 8085 assembly language program to add two 16-bit numbers stored in memory. 5 Write an 8085 assembly language program to add two decimal numbers using DAA instruction. 6 Write an 8085 assembly language program to find the minimum from two 8-bit numbers. 7 Write an 8085 assembly language program to get the minimum from block of N 8-bit numbers. 8 Write an 8085 assembly language program to add block of 8-bit numbers. 9 Write an 8085 assembly language program to copy block of five numbers starting from memory location 2001h to location starting from 3001