Header Ads Widget

Ticker

6/recent/ticker-posts

Practical 4 Java

Inheritance and interface

4-(1)

class Cricket having data members name, age and member methods display() and setdata(). class Match inherits Cricket and has data members no_of_odi, no_of_test. Create an array of 5 objects of class Match. Provide all the required data through command line and display the information… 

package com.company;

import java.util.Scanner;

public class match {
    int no_of_odi;
    int no_of_test;
    match()
    {
        takeinput();
    }
    void printbase ()
    {
        System.out.println(" odis played "+no_of_odi +" test played "+ no_of_test);
    }
    void takeinput()
    {
        Scanner sc=new Scanner(System.in);
        no_of_odi=sc.nextInt();
        no_of_test= sc.nextInt();
    }
}
import java.util.Scanner;
public class cricket extends match {
    String name;
    int age;
    cricket ()
    {
        super();
        takeinputa();
    }
    void takeinputa()
    {
        Scanner sc=new Scanner(System.in);
        name=sc.nextLine();
        age= sc.nextInt();
    }

    void printall ()
    {
        printbase();
        System.out.println(" name is "+name +" age is "+age);
    }
}
public class Main {

    public static void main(String[] args) {
        cricket[] m=new cricket[5];
        match c;

        for(int i=0;i<5;i++)
        {
            m[i]=new cricket();
        }
        for(int i=0;i<5;i++)
        {
            m[i].printall();
        }

    }
}

4-(2)

Define a class Cripher with following data Field: String plainText; int key Functions: Cipher(String plaintext,int key) abstract String Encryption( ) abstract String Decryption( ) Derived two classes Substitution_Cipher and Caesar_Cipher override Encyption() and Decyption() Method. in substitute cipher every character of string is replace with another character. For example. In this method you will replace the letters using the following scheme. Plain Text: a b c d e f g h i j k l m n o p q r s t u v w x y z Cipher Text: q a z w s x e d c r f v t g b y h n u j m i k o l p So if string consist of letter “gcet” then encrypted string will be ”ezsj” and decrypt it to get original string

package com.company;

public class cipher
{
    String plaintext;
    int key;
    cipher()
    {}

    cipher(String s,int k)
    {
        plaintext=s;
        key=k;
    }

    void printplaintext ()
    {
        System.out.println(plaintext);
    }
    void encryption ()
    {
        System.out.println("this is going to overriden so i am not writing much");
    }
    void decryption ()
    {
        System.out.println("this is going to overriden so i am not writing much");
    }

}
public class Substitution_Cipher extends  cipher
{
    StringBuffer encryptedstring;

    Substitution_Cipher(String s,int k) {
        super(s, k);
    }

    void encryption ()
    {
        encryptedstring=new StringBuffer();
        for(int i=0;i<plaintext.length();i++)
        {
            char c= plaintext.charAt(i);
            c+=key ;
            encryptedstring.append(c);
        }
        System.out.println("encrypted string "+encryptedstring);
    }
}
public class Caesar_Cipher extends Substitution_Cipher
{
    StringBuffer decryptedstring;

    Caesar_Cipher(String s,int k) {
        super(s,k);
    }


    void decryption ()
    {
        decryptedstring=new StringBuffer();
        for(int i=0;i<encryptedstring.length();i++)
        {
            char c= encryptedstring.charAt(i);
            c-=key;
            decryptedstring.append(c);
        }
        System.out.println("encrypted string "+decryptedstring);
    }
}
public class Main {

    public static void main(String[] args) {
        cipher c=new cipher();
        Caesar_Cipher s=new Caesar_Cipher("vedant.k.raval@gmail.com",7);
        s.encryption();
        c=s;
        c.decryption();
        c.encryption();
        s.decryption();
    }
}

4-(3)

Declare an interface called Property containing a method computePrice to compute and return the price. The interface is to be implemented by following two classes i) Bungalow and ii) Flat. Both the classes have following data members - name - constructionArea The class Bungalow has an additional data member called landArea. Define computePrice for both classes for computing total price. Use following rules for computing total price by summing up sub-costs: Page 6 of 12 Construction cost(for both classes):Rs.500/- per sq.feet Additional cost ( for Flat) : Rs. 200000/- ( for Bungalow ): Rs. 200/- per sq. feet for landArea Land cost ( only for Bungalow ): Rs. 400/- per sq. feet Define method main to show usage of method computePrice

package com.company;

public interface property {
    double computeprice();
}
public class Bunglow implements  property
{
    String name;
    double constructionarea;
    Bunglow (String name,double constructionarea)
    {
        this.name=name;
        this.constructionarea=constructionarea;
    }
    public double computeprice() {
        double price;
        price=500*constructionarea +200*constructionarea +400*constructionarea;
        return price;
    }
}
public class flat implements  property{
    String name;
    double constructionarea;
        flat(String name,double constructionarea)
        {
            this.name=name;
            this.constructionarea=constructionarea;
        }

    public double computeprice() {
        double price;
        price=500*constructionarea+200000;
        return price;
    }
}
public class Main {

    public static void main(String[] args) {
    Bunglow b1=new Bunglow("Bilipatra",200);
    flat f1=new flat("suvarnabhumi",10000);
    System.out.println(b1.name+" bunglow price is "+b1.computeprice());
    System.out.println(f1.name+" flat price is "+f1.computeprice());
    }
}

4-(4)

4. Define following classes and interfaces. public interface GeometricShape { public void describe(); } public interface TwoDShape extends GeometricShape { public double area(); } public interface ThreeDShape extends GeometricShape { public double volume(); } public class Cone implements ThreeDShape { private double radius; private double height; public Cone (double radius, double height) public double volume() public void describe() } public class Rectangle implements TwoDShape { private double width, height; public Rectangle (double width, double height) public double area() public double perimeter() public void describe() } public class Sphere implements ThreeDShape { private double radius; public Sphere (double radius) public double volume() public void describe() } Define test class to call various methods of Geometric Shape

package com.company;

public interface GeometricShape
{
     void describe();
}
public interface ThreeDShape extends GeometricShape
{
     double volume();
}
public interface TwoDShape extends GeometricShape
{
    double area();
}
public class Rectangle implements TwoDShape {
    private double width, height;
    public Rectangle (double width, double height)
    {
        this.width=width;
        this.height=height;
    }

    public double area()
    {
        double area;
        area=width*height;
        return area;
    }

    public double perimeter()
    {
        double pm;
        pm=2*(width+height);
        return pm;
    }

    public void describe()
    {
        System.out.println("width of rectangle is "+this.width);
        System.out.println("height of rectangle is "+this.height);
        System.out.println("area of rectangle is "+area());
        System.out.println("perimeter of rectangle is "+perimeter());


    }
}
public class Sphere implements ThreeDShape
{
    private double radius;
    public Sphere (double radius)
    {
        this.radius=radius;
    }


    @Override
    public void describe()
    {
           System.out.println("radius of sphere is "+this.radius);
        System.out.println("volume of sphere is "+volume());

    }

    @Override
    public double volume()
    {
        double vol;
        vol=4/3*3.14*Math.pow(radius,3);
        return vol;
    }
}
public class Cone implements ThreeDShape
{
    private double radius;
    private double height;
    public Cone (double radius, double height)
    {
        this.radius=radius;
        this.height=height;
    }

    public double volume()
    {
        double resvol=3.14*Math.pow(radius,2)*height;
        return resvol;
    }


    public void describe()
    {
        System.out.println("redius of cone is "+radius);
        System.out.println("height of cone is "+height);
        System.out.println("volume of cone is "+volume());

    }
}
public class Main {

    public static void main(String[] args)
    {
        Cone c=new Cone (5.6,7.6);
        c.describe();
        Rectangle r=new Rectangle(6.7,8.9);
        r.describe();
        Sphere s=new Sphere(4.5);
        s.describe();
}
}


Post a Comment

0 Comments