JAVA 2015 Old paper Solutions

3rd Semester

Year 2015

Group B






Q.no 11 Write a Java Program to display all the even numbers from 1 to 500

public class tu15Q11 {
    public static void main(String[] args) {
        int i;
        for(i=1;i<=500;i++){
            if(i%2==0){
            System.out.println(i);
            }
        }
    }
    }
/** * * @author bim study notes */

12. Make a thread using Runnable interface to display numbers from 1 to 20, each number should be displayed in the interval of 2 seconds.

class ThreadNum implements Runnable{
public void run(){
try{
for(int i=1;i<=20;i++){
System.out.print(i+" ");
Thread.sleep(2000);
}
}catch(InterruptedException e){
System.out.println("Thread Interrupted");
}
}
}
/** * * @author bim study notes */
public class EvenNumThreadDemo {
public static void main(String[] args) {
ThreadNum n = new ThreadNum();
Thread t = new Thread(n);
t.start();
}
}





13. Write a program that reads line of text from keyboard and write to file. Also read the content of the same file and display on monitor.

import java.io.*;
class ReadingFromKeyBoard {
   public static void main(String args[]) throws IOException {
       String str;
       BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
       FileWriter fw=null;
       System.out.println("Enter line of text and 'stop' at the end");
       try{
           fw = new FileWriter("d://janak//Helllo.txt");
/** * * @author bim study notes */
           do{
               str = br.readLine();
               if(str.compareTo ("stop")==0)break;
               str = str+"\r\n";
               fw.write(str);
           }while(str.compareTo("stop")!=0);
       }catch(IOException e){
           System.out.println("I/O Error occured");
       }
       finally{
           if(fw!=null)
           fw.close();
       }
   }
}

Q.no 14

class Rectange{
    int length,breadth,area;
    public void computeArea(int l, int b){
        length=l;
        breadth=b;
        area=length*breadth;
}
    public void displayArea(){
        System.out.println("The area is: " +area);
    }
}
public class tu15Q14 extends Rectang{
    public static void main(String[] args) {
       Rectang obj1= new Rectang();
       obj1.computeArea(10, 20);
       Rectang obj2=new Rectang();
       obj2.computeArea(34, 20);
       if(obj1.area>obj2.area){
           obj1.displayArea();
       }
       else
       {
           obj2.displayArea();
       }
    }
}
/** * * @author bim study notes */





15. Make an interface named num with two functions int add(int x, int y) and int diff(int x, int y) then make a class that implements that interface num

interface num{
    int add(int x,int y);
    int diff(int x,int y);
}
class calc implements num {
    int sum;
    int sub;
    @Override
   public  int add(int x,int y){
        sum=x+y;
        System.out.println("The sum is "+sum);
        return sum;
    }
    @Override
   public  int diff(int x,int y){
       sub=x-y;
       System.out.println("The difference is "+sub);
       return sub;
    }
    public static void main(String[] args) {
        calc obj=new calc();
        obj.add(2, 3);
        obj.diff(30, 20);
    }
}
Group C

 

16. What is an exception? Explain nested try catch with example.

An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run time error.When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore these exceptions are to be handled.
Some reasons that may cause exception to occur

  • A user has entered invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.
public class NestedTry {
public static void main(String[] args) {
int num[] = {8,16,32,64,128,256,512,1024};
int den[] = {1,0,4,0,8,0,16};
int a;
for(int i=0;i<num.length;i++)
{
try{
try{
a = num[i]/den[i];
System.out.println(a);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("No matching element :");
}
}
/** * * @author bim study notes */
catch(ArithmeticException e){
System.out.println("Division by 0: ");
}
}
}
}

17. Define package? Write down steps to create package. Explain with example
Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and namespace management.

  • Some of the existing packages in Java are −
  • java.lang− bundles the fundamental classes
  • java.io− classes for input , output functions are bundled in this package
  • Programmers can define their own packages to bundle group of classes/interfaces, etc. It is a good practice to group related classes implemented by you so that a programmer can easily determine that the classes, interfaces, enumerations, and annotations are related.

Steps to create a Java package

  • Come up with a package name.
  • Pick up a base directory.
  • Make a subdirectory from the base directory that matches your package name.
  • Place your source files into the package subdirectory.
  • Use the package statement in each source file.
  • Compile your source files from the base directory.

example

The package keywordis used to create a package in java.
packagemypack;  
publicclassSimple{  
publicstatic void main(String args[]){  
System.out.println("Welcome to package"); 
}  
}

Leave a Reply

Your email address will not be published.