JAVA Model Questions with Answers    
 
1. Write a Java program to accept a number through command line and then check
    whether  it    is an Armstrong number or not.
 

class q1

{

     public static void main(String a[])

     {

           int n=Integer.parseInt(a[0]);

           int i,d,s=0,temp;

           temp=n;

           while(n>0)

           {

                d=n%10;

                s=s+d*d*d;

                n=n/10;

           }

           if(temp==s)

           {

                System.out.println("Its an Armstrong number");

           }

           else

           {

                System.out.println("Its not an Armstrong number");

           }

     }

}

 

2

Create a static function to find factorial a given number.

 
 

import java.io.*;

class sam

{

     static int fact(int n)

     {

           int f=1;

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

           {

                f=f*i;

           }

           return f;

     }

}

class q2

{

     public static void main(String a[]) throws Exception

     {

           InputStreamReader ir=new  InputStreamReader(System.in);

           BufferedReader br = new BufferedReader(ir);

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

           int n=Integer.parseInt(br.readLine());

           int f=sam.fact(n);

           System.out.println("The factorial is "+ f);

     }

}

 
     

3

Accept two strings through command line. Print length of the first string. Then print
 last character of the second string and finally check whether the two strings are equal.
 
 

class q3

{

     public static void main(String a[])

     {

           int len1=a[0].length();

           int len2=a[1].length();

           System.out.println("The length of first string is " + len1);

           System.out.println("The last character of second string is "+

           a[1].charAt(len2-1));

           if(a[0].equals(a[1]))

           {

                System.out.println("The two string are equal");

           }

           else

           {

                System.out.println("The two string are not equal");

           }

     }

}

 
     

4

Define a class named CUBE having member data: length, width and height. The member
function get_value accepts member data values. The function get_volume calculates
volume of a cube(length*width*height). Create an object of the CUBE class and pass
required dimensions to print volume of the cube.
 
 

import java.io.*;

class CUBE         

{

     int length;

     int width;

     int height;

     int volume;

     void get_value()

     {

     try{

           InputStreamReader ir =new InputStreamReader(System.in);

           BufferedReader br =new BufferedReader(ir);

           System.out.println("Enter the length ");

           length=Integer.parseInt(br.readLine());

           System.out.println("Enter the width ");

           width=Integer.parseInt(br.readLine());

           System.out.println("Enter the height ");

           height=Integer.parseInt(br.readLine());

     }

     catch(Exception e)

     {}

}

     void get_volume()

     {

           volume=length*width*height;

           System.out.println("The volume is "+volume);

     }

}

class q4

{

     public static void main(String a[])

     {

           CUBE obj=new CUBE();

           obj.get_value();

           obj.get_volume();

     }

}

 
     
  1.  

Write a program to overload the class constructor in three different forms. The first
constructor takes no parameter, the second constructor takes one parameter and the
third constructor takes an integer and float value.
 

class conover

{

     int a;

     float b;

     conover()

     {

           a=0;

           b=0;

     }

     conover(int i)

     {

           a=i;

           b=0;

     }

     conover(int j,float k)

     {

           a=j;

           b=k;

     }

     void display()

     {

           System.out.println("The value of a is "+a);

           System.out.println("The value of b is "+b);

     }

}

class q5

{

     public static void main(String a[])

     {

           conover obj1 =new conover();

           conover obj2 =new conover(10);

           conover obj3 =new conover(10,12.5f);

           obj1.display();

           obj2.display();

           obj3.display();

     }

}

 
     

6.

Define a base class VOLUME having member data: length, width and height. The member
function get_volume assigns values to the member  data. Create a sub class WEIGHT
derived from the super class VOLUME. The WEIGHT class contains member data: total_
weight and member function get_weight() which accepts the total_weight.

 

class VOLUME

{

     int length,width,height;

     void getvolume(int a,int b,int c)

     {

           length=a;

           width=b;

           height=c;

           System.out.println("The volume is "+ length*width*height);

     }

}

class WEIGHT extends VOLUME

{

     int total_weight;

     void getweight(int w)

     {

           total_weight=w;

           System.out.println("The total weight is "+total_weight);

     }

}

class q6

{

     public static void main(String a[])

     {

           VOLUME v = new VOLUME();

           WEIGHT w=new WEIGHT();

           v.getvolume(2,3,6);

           w.getweight(100);

     }

}

 
     

            7.  

 

Define the following:

 a)Interface

 Name              :           COMPANY

 Member data   :           

         
 
Data Name 
Value
Data type
 
  Company_name     ITDESK     String  
    Software    
 

Abstract methods:

                         accept_data(int emp_id, String emp_name, float emp_salary)

 

                         print_data()

 

           b) Class

              It has to implement the interface COMPANY

                  Name             :   Employee

              Member data   :   

         
   Data Name    Data type  
  Emp_id     int  
  Emp_name   String  
 

Member methods:

Over-ride functions of the interface to accept the employee data and then print
the details. Having finished with the above declarations, access the class object
with the interface reference.

 

 

 

interface COMPANY

{

     String company_name="ITDESK software solution";

    

     void accept_data(int emp_id, String emp_name, float emp_salary);

     void print_data();

}

class EMPLOYEE implements COMPANY

{

     int empid;

     String empname;

     float empsal;

     public void accept_data(int emp_id, String emp_name, float emp_salary)

     {

           empid=emp_id;empname=emp_name; empsal=emp_salary;

     }

     public void print_data()

     {

           System.out.println("The employee id is      "+empid);

           System.out.println("The employee name is    "+empname);

           System.out.println("The employee salary is  "+empsal);

     }

}

class q7

{

     public static void main(String a[])

     {

           COMPANY c=new EMPLOYEE();

           c.accept_data(10,"Venu",100.065f);

           c.print_data();

     }

}

 
     
  1.  

Declare an integer array of size 5. Protect the value input process from abnormal
termination in case of excess storage of values. Make use of the following:
ArrayIndexOutOfBoundsException.

 

import java.io.*;

class q8

{

     public static void main(String arg[]) throws IOException

     {

           int a[] =new int[5];

           int i=0;

           InputStreamReader ir=new InputStreamReader(System.in);

           BufferedReader br = new BufferedReader(ir);

           System.out.println("Enter array elements");

           do

           {

                try

                {

                     a[i]=Integer.parseInt(br.readLine());

                }

                catch(ArrayIndexOutOfBoundsException e)

                {

                     System.out.println("ECxecption caught");

                     break;

                }

                i++;

           }while(a[i-1]!=0);

     }

}

 
     
  1. Create a package called MyPack in the root directory and define a class BANKACCOUNT
    within the package. BANKACCOUNT contains the fields
    name and balance,
    which
    are assigned values through the constructor function. Define member function
    print_details
    to print the account information. (Note: Use Public visibility for all
    members). Coming back to the root directory, import the package in a main program
    and use the class.

 

import mypack.BANKACCOUNT;

class q9

{

     public static void main(String a[])

     {

           BANKACCOUNT b=new BANKACCOUNT("venu",1000);

           b.print_details();

     }

}

 
     
  1. Using applets create a textbox to input text matter. Create 3 radio buttons labeled
    RED, BLUE and GREEN which sets the foreground color of the text matter. Create a
    button to handle the action event that sets text-color to the selected color option.

 

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

public class q10 extends Applet implements ActionListener

{

     TextField t1;

     Checkbox c1,c2,c3;

     CheckboxGroup group;

     Button b1;

     String s;

     public void init()

     {

           t1=new TextField("",20);

           group = new CheckboxGroup();

           c1=new Checkbox("RED",group,true);

           c2=new Checkbox("BLUE",group,false);

           c3=new Checkbox("GREEN",group,false);

           b1=new Button("Click");

           setLayout(null);

           t1.setBounds(50,30,150,20);

           c1.setBounds(20,50,50,20);

           c2.setBounds(80,50,50,20);

           c3.setBounds(140,50,80,20);

           b1.setBounds(50,80,50,25);

           add(t1);

           add(c1);add(c2);add(c3);

           add(b1);

           b1.addActionListener(this);

     }

     public void actionPerformed(ActionEvent e)

     {

           if(c1.getState())

           {

                 t1.setForeground(Color.red);

           }

           else if(c2.getState())

           {

                t1.setForeground(Color.blue);

           }

           else

           {

                t1.setForeground(Color.green);

           }

     }

}