JAVA 2017 Old Paper Solutions || BIM STUDY NOTES

3rd Semester

Year 2017

Q.no 11

public class tu17Q11 {
    public static void main(String[] args) {
        int n,i;
        System.out.println("Enter the number of numbers to enter");
        Scanner sn=new Scanner(System.in);
        n=sn.nextInt();
        System.out.println("Enter the numbers");
         int num[];
        for(i=0;i<n;i++){
          num[i]=sn.nextInt();
          if(num[i]%2==0&&num[i]%3==0){
              System.out.println(num[i]);
          }
        }
    }
}

Q.no 12

/** * * @author bim study notes */
class Student {
    int roll_no;
    public void readRoll(int r){
        roll_no=r;
    }
    public void displayRoll(){
        System.out.println(roll_no);
    }
}
class Test extends Student{
    int markJava,markWeb;
    public void readMarks(int mj,int mw){
        markJava=mj;
        markWeb=mw;
    }
    public void displayMarks(){
        System.out.println("Java:"+markJava+" "+"Web:"+markWeb);
    }
}
 class Results extends Test{
    int total;
        public void calculate(){
            total=markJava+markWeb;
        }
        public void display(){
            System.out.println("The total marks is :"+total);
        }
        public static void main(String[] args) {
        Results obj=new Results();
        obj.readRoll(1);
        obj.readMarks(49,40);
        obj.calculate();
        obj.displayRoll();
        obj.displayMarks();
        obj.display();
        }
}

Q.no 13






interface Shape{
  public void get_data(int l,int b);
  public void display_area();
}
class Rectangle implements Shape{
    int length,breadth,area;
    public void get_data(int l,int b){
         length=l;
         breadth=b;
    }
    public void display_area(){
        area=length*breadth;
        System.out.println("The area of rectangle is:"+area);
    }
}
class Square implements Shape{
    int length,breadth,area;
    public void get_data(int l,int b)
    {
        length=l;
    }
    public void display_area(){
        area=length*length;
        System.out.println("The area of Square is:"+area);
    }
}
public class tu17Q13 {
    public static void main(String[] args) {
        Rectangle obj=new Rectangle();
        obj.get_data(10, 20);
        obj.display_area();
        Square obj1=new Square();
        obj1.get_data(10, 0);
        obj1.display_area();
    }
}

Q.no 14

public class tu17Q14 {
    public static void main(String[] args) {
      String []country={"Nepal","USA","Australia","Brazil","Paragua","Canada"};
        int i;
        for(i=0;i<6;i++){
          if(country[i].endsWith("a")){
              System.out.println(country[i]);
          }
        }
    }
}

17.Explain user defined exception in java with a suitable example.






In java we can create our own exception class and throw that exception using throw keyword. These exceptions are known as user-defined or custom exceptions.

class JavaException{
   public static void main(String args[]){
  try{
       throw new MyException(2);
       // throw is used to create a new exception and throw it.
  }
 catch(MyException e){
    System.out.println(e) ;
 }
}
}
class MyException extends Exception{
   int a;
   MyException(int b) {
     a=b;
   }
   public String toString(){
     return ("Exception Number =  "+a) ;
  }
}

Leave a Reply

Your email address will not be published.