Year 2015
Group B
Q.no 11 Write a Java Program to display all the even numbers from 1 to 500
1 2 3 4 5 6 7 8 9 10 11 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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); } } <strong>Group C</strong> |
1 2 |
<b>16. What is an exception? Explain nested try catch with example. </b> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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
A 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
1 2 3 4 5 6 7 |
The <b>package keyword</b>is used to create a package in java. <b>package</b>mypack; <b>public</b><b>class</b>Simple{ <b>public</b><b>static</b> <b>void</b> main(String args[]){ System.out.println("Welcome to package"); } } |