(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:
- public char charAt(int index)
- public String concat(String s)
- public int length()
- public String replace(char old, char new)
- public String substring(int begin)/ public String substring(int begin, int end)
- public String toLowerCase()
- 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