Thursday 1 September 2016

CORE JAVA PMR 2

Question-1  Area Abstraction.


Answer:-

import java.util.Scanner;

 

abstract class calcArea {

    abstract void findTriangle(double b, double h);

    abstract void findRectangle(double l, double b);

    abstract void findSquare(double s);

    abstract void findCircle(double r);

}

 

class findArea extends calcArea {

 

    void findTriangle(double b, double h)

    {

        double area = (b*h)/2;

        System.out.println("Area of Triangle: "+area);

    }

 

    void findRectangle(double l, double b)

    {

        double area = l*b;

        System.out.println("Area of Rectangle: "+area);

    }

     

    void findSquare(double s)

    {

        double area = s*s;

        System.out.println("Area of Square: "+area);

    }

     

    void findCircle(double r)

    {

        double area = 3.14*r*r;

        System.out.println("Area of Circle: "+area);

    }

}

         

class area {

    public static void main(String args[])

    {

        double l, b, h, r, s;

        findArea area = new findArea();

        Scanner get = new Scanner(System.in);

 

        System.out.print("\nEnter Base & Vertical Height of Triangle: ");

        b = get.nextDouble();

        h = get.nextDouble();

        area.findTriangle(b, h);

 

        System.out.print("\nEnter Length & Breadth of Rectangle: ");

        l = get.nextDouble();

        b = get.nextDouble();

        area.findRectangle(l, b);

 

        System.out.print("\nEnter Side of a Square: ");

        s = get.nextDouble();

        area.findSquare(s);

 

        System.out.print("\nEnter Radius of Circle: ");

        r = get.nextDouble();

        area.findCircle(r);

    }

Question-2  Division of two Numbers.


Answer:-


package divi;

import java.util.*;

import java.lang.*;


public class Divi {


Scanner sc=new Scanner(System.in);

int n1,n2,div;


public Divi(){

        try{

    System.out.println("Enter the first number:");

    n1=sc.nextInt();

    System.out.println("Enter the second number:");

    n2=sc.nextInt();

        }catch(Exception e){

        System.out.println("Invalid input..!! "+e);

        }

       div=n1/n2;

       System.out.println("The division of two numbers is: "+div);

       System.out.println("The remainder is : "+(n1%n2));

}


    public static void main(String[] args) {

     Divi d=new Divi();

     

    }

    

}


Question-3  Store the Employee details and display it.


Answer:-

package mgr;

public class Employee {

 

    String name;

    int id;

    

    public Employee(String name, int id){

    this.name = name;

    this.id = id;

    }

    

    

    public String toString(){

    

    return name+" "+id;

    }

}



&


package mgr;
public class Manager {
 
    Employee[] ee = new Employee[5];
    public void add(Employee[] e){
     
        ee = e;
     
 
    }
    public void remove(Employee[] e){
 
    }
 
    public void print(Employee[] ep){
 
        for(Employee s:ep )
        {
     
        System.out.println(s);
        }
    }
 
    public static void main(String[] as){
        Employee e = new Employee("Lalit", 1);
        Employee e2 = new Employee("Chandan", 2);
     
        Employee[] staff ={e,e2};
 
        Manager m = new Manager();
        m.add(staff);
        m.print(staff);
     
     
     
     
     
    }
}


Question-4  Give example of For Loop.


Answer:-


public class Table

{

    public static void main(String[] args) {

        int result;

        for(int i=1; i<=10; i++)

        {

            result=i*5;

            System.out.println("5 x"+i+"="+result);

        }

    }

}

Question-5  Accept the Book Details.


Answer:-


class Book {
    String author;
    String title;
    int price;
    int pages;
    int stock;
 
    public void getDetails(String at, String tt, int pr, int pg, int st){
 
        author=at;
        title=tt;
        price=pr;
        pages=pg;
        stock=st;
    }
 
    public void showDetails(){
 
        System.out.println(" ");
        System.out.println("Books Information");
        System.out.println("==========================");
        System.out.println("Book Author: "+author);
        System.out.println("Book Title: "+title);
        System.out.println("Book Price: "+price);
        System.out.println("Number Of Pages: "+pages);
    }
}
class HardwareBook extends Book{
    String hardwareCategor;
    String publisher;
 
    public void getDetails(){
 
        super.getDetails("Javeed Dhuka", "All About PC", 120, 150, 80);
        hardwareCategor="Machine";
        publisher="Denmark";
    }
 
    public void showDetails(){
 
        System.out.println(" ");
        super.showDetails();
        System.out.println("Hardware Category: "+hardwareCategor);
        System.out.println("Publisher Name: "+publisher);
        System.out.println(" ");
    }
}
class SoftwereBook extends Book{
    String softwereName;
    String softwareVersion;
 
    public void getDetails(){
 
        super.getDetails("Javeed Dhuka ", "Windows In 24 Hours ", 245, 250, 250);
        softwereName="Windows";
        softwareVersion="8";
    }
 
    public void showDetails(){
 
        super.showDetails();
        System.out.println("Software Name: "+softwereName);
        System.out.println("Software Version: "+softwareVersion);
        System.out.println(" ");
    }
}
public class BookDemo{
    public static void main(String[] args) {
        SoftwereBook sb=new SoftwereBook();
        sb.getDetails();
        sb.showDetails();
     
        HardwareBook hb=new HardwareBook();
        hb.getDetails();
        hb.showDetails();
    }
}

Question-6  Application for Game on console app.


Answer:-

asbtract class GameConsole

{

int score;

void displayScore()

{

System.out.println("The display Score method .");

}

abstract void computeScore();

abstract void playGame();


}




class Badminton extends GameConsole

{


void playGame()

{

System.out.println(" Starting the Badminton Game");

}


void computeScore()

{

System.out.println("Calculating Score for the Badminton game");

}


}



class TableTennis extends GameConsole

{

void playGame()

{

System.out.println("Starting the table tennis game");


}


void computeScore()

{

System.out.println("Calculating the score of the table tennis game");


}


}



public  class GameDemo

{

public static void main(String []args)

{

Badminton b=new Badminton();

b.playGame();

b.computeScore();

b.displayScore();

TableTennis t=new TableTennis();

t.playGame();

t.computeScore();

t.displayScore();

}


Question-7  Check whether number is divisible by second number or not.


Answer:-



package DivisibleChecker;



public class DivisibleChecker {

    

    public static void main(String[] args) {

        

        int num1=32, num2=12;

        

        if (num1 % num2 == 0) {

            

            System.out.println("The first number is divisible by the second number");   

        }

        else

        {

            System.out.println("The first number is not divisible by the second number");

        }

    }

}

1 comment: