Friday 12 August 2011

JAVA - BANGALORE UNIVERSITY

I am currently handling java programming for my BCA students. Apart from the classroom setting, i feel this is another forum for my students to interact with me and get their doubts clarified. Many students feel shy to ask questions in the classroom and keep quiet even if they do not understand a topic. So i am trying to write on certain specific topics which I am sure lot of students would still be having doubts. Please go through the topics, and if you want me to answer any particular doubts you have, feel free to post about them and i will explain it for you here. HAPPY LEARNING!!!!!!.

COMMAND LINE ARGUMENTS

ARRAYS IN JAVA





APPLET PROGRAMMING

JAVA QUESTION BANK



COMMAND LINE ARGUMENTS

Command line argument is a topic which we practically start our learning of java, but i am sure still there are students who are not clear about this. Here goes the explanation. Read it more than once if you are still not clear, you will get it.

Execution in a java program starts from the main method which we write in a class. When we write any method in java, you can pass parameters to it and the method can also return a value. Since "main" is also a method, parameters can also be passed to it but the only thing is that those parameters must be passed from the command line, when you invoke the java interpreter for executing the class file.

For example, if a class file called test is executed by the java interpreter, and command line arguments are passed to it, the invocation would like this ,

c:\>java test one two three

In the above example, three command line arguments are passed and they are the string one, two and three respectively. The complete program to handle these command line arguments would go like this,

class test
{
public static void main(String args[])
{
/* Arguments sent from the command line are stored in the args array.
Length of the args array would be equal to the no of arguments passed.
Following code simply prints the array elements */

for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}


POINTS TO BE NOTED WHILE USING COMMAND LINE ARGUMENTS

1. Understand very clearly the data type of args. It is a array of strings. That is why it is declared as String args[]. args is just a variable name given to the array of strings which stores the command line arguments. It can be called anything, x,y or z.
2. Any data can be sent using command line arguments. But they are always copied into the string array only. So if any number is passed and should be treated like a number, we should convert it into a int data type using appropriate methods in java. (Like parseInt method called with the Integer class to convert the String into primitive int data type).
3. One of the useful applications of command line arguments would be to work with files, let us say. Suppose i am writing a class called copy which works like any other copy program. copy a source file to a destination file. The two file names can be given as command line arguments instead of accepting it from the user.

c:\>java copy file1 file2

Here file1 and file2 are command line arguments and there are statements in the copy class to copy file1 into file2.

APPLET PROGRAMMING



Applets are small java programs that are primarily used in internet computing. They are a piece of code which is downloaded by the browser and run at the client side. Applets can be used for doing any activity which we want to do. Say, for example, developing a user registration form, running a game program, a paint application, an EMI calculator etc.

Every applet that we write has to be extended from the built in class called Applet. This class is available in the applet package and so that should be imported. An example program for an applet would include an import statement for importing the applet package.

import java.applet.*;
import java.awt.*;

The awt package must be imported because it contains the Graphics class which is going to used in applet programming. Because of that reason, awt package also must be imported.

Let us say, we want to write a applet called testapp then the class definition for it should go like this,

public class testapp extends Applet
{
// Enclose the code for the applet
}

There are many methods executed in the life cycle of applet and they are

1. public void init()

2. public void start()

3. public void stop()

4. public void destroy()

init()

This method contains any initialisation code.

start()

This method is executed once init() finishes its execution. When a web page which contains the applet gets its focus, this method will be executed. So in the life cycle of an applet, this method could be executed more than once.

stop()

This is executed when a web page containing an applet loses its focus.

destroy()

This is the last method to be executed in the applet's life cycle and this will be executed only once.

Apart from all these methods, we should be familiar with one more method which would be frequently used in our learning of applet and it is the paint mehtod.

The signature for this method is ,
public void paint(Graphics g) { }
This method takes a Graphics object as its parameter. The Graphics class has lot of methods for drawing mamy different types of shapes, from simple to complex.
The following is a program for printing a "Hello world "

import java.applet.*;
import java.awt.*;
public class testapp extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello world",100,100);
}
}

Parameter passing in applet

Just like how, values can be passed to the main method using command line arguments, we can also pass arguments to an applet. The arguments are passed using the PARAM tag using a name and value attribute. In the init method of the applet the parameter's values have to be captured and worked further upon. Let me first write the java program to capture the arguments. ;


import java.awt.*;
import java.applet.*
public class testparam
{
String str=" ";
public void init()
{
str=getParameter("name");
repaint();
}
public void paint(Graphics g)
{
g.drawString(str,200,200);
}
}

The second part is to write the html code which has the applet embedded and also the param tag for passing the parameters.

< html >
< body >
< applet code="testparam" width=400 height=400 >
< param name="name" value="JAVA" >
< body >
< html >

JAVA QUESTION BANK



The question bank comprises of PART A and PART B questions. PART A questions are 1 mark or 3 mark questions. PART B comprises of 7 mark questions.

PART A


PART B


PART A

1. Expand WWW,HTML,JDK,JVM,JRE,API and AWT.
2. Explain few methods available with the Math class.
3. What are the different types of comments available in java?
4. What are the rules for naming an identifier.
5. Why is java called platform independent?
6. What is a JVM?
7. How to create symbolic constant in java?
8. Write the syntax of switch statement in java.
9. What are labelled loops?
10. What are static members?
11. What is inheritance?
12. What are the different types of inheritance?
13. What are the consequences of making a class final?
14. What is the use of making a method final?
15. What are wrapper classes in java?
16. Name some inbuilt packages in java.
17. What are the different priorities that can be assigned to a thread?
18. Name some methods used for inter-thread communication.
19. How many catch blocks can be there for a single try block?
20. Name some differences between applets and applications.
21. Write a simple html file for embedding a applet.
22. Name some methods available in the Graphics class for drawing purpose.
23. What are the attributes available with applet tag?. Explain few of them.
24. What is the difference between byte stream and character stream classes?
25. What is the purpose of pushbackinput stream class?
26. What is the purpose of piped stream class?
27. Name some methods available with inputstream class?
28. Name some methods available with outputstream class?
29. What are the different modes of opening a file using Randomaccessfile class?
29. How to draw a polygon in java?
30. Name some important events available in java.

PART B

1. Explain the features of java.
2. How java differs from c and c++.
3. Explain the general structure of a java program.
4. Explain the different types of statements in java with example.
5. Explain command line arguments with an example.
6. Explain the data types in java with an example.
7. Eplain instance variables, class variables and local variables with an example.
8. What are constructors? Explain with an example.
9. What is method overloading? Explain with an example.
10. Illustrate method overriding with an example.
11. What are abstract methods?. Explain with an example.
12. Write the steps for creating an array in java with an example.
13. Explain atleast 10 string functions in java.
14. What are interfaces? Explain with an example.
15. What are packages? Write the steps for creating a package with an example.
16. What are threads?. What are the two different ways of creating a thread?
17. Explain the lifecycle of a thread with a neat diagram.
18. Explain the five keywords of exception handling.
19. Explain the life cycle of applet with a neat diagram.
20. How to pass parameters to applets?. Explaintream
with an example.
21. Explain the hierarchy of input stream classes with a digram.
22. Explain the hierarchy of output stream classes with a neat diagram.
23. Explain the hierarchy of input stream classes with a neat diagram.
24. Explain the random access file class in java with an example.
25. What is a File class?. Explain some important methods available with this class.


ARRAYS IN JAVA




Here i am assuming that the readers will know about how arrays are handled in c and c++. The main focus is on how java handles arrays and what are the unique features in its declaration.

In c or c++, whenever we declare an array we have to mention the size of the array(i am not talking about dynamic allocation of arrays using malloc or new operator). But in java when an array is declared you just mention that it is an array by writing empty square brackets next to the name of the array.
For example, if we want to declare an integer array which will be called as arr,
the declaration would go like this,
int arr[];

The size of the array would be mentioned when memory is allocated for it.
arr=new int[5];
Memory for the array arr is allocated and arr contains the reference to the set of five contiguous memory locations.

Points to be noted:

1. Individual elements of the array are accessed by using the arrayname[indexname]syntax.
2. If we overstep ar array in java, it generates an ArrayIndexOutOfBoundsException and comes out of the program. I just cannot overemphasize the importance of staying within the array bounds in java. If you overstep, java will not keep quiet and it will mouth exceptions like the one mentioned above. So, JUST BE PREPARED..
3. In java, if we are talking about two-dimensional arrays, all the rows need not have the same column. Each one of the rows can be having a different number of columns. Such arrays are called jagged arrays. A very interesting and a different way of working with arrays. I would explain it with an example.

class jaggedarr
{
public static void main(String ar[])
{
int twodim[][];
twodim=new int[3][];
int i,j;
for(i=0;i<twodim.length;i++)
{
twodim[i]=new int[i+1];
for(j=0;j<twodim[i].length;j++)
{
twodim[i][j]=1;
System.out.print(twodim[i][j]+" ");
}
System.out.println();
}
}
}
In the above example, the array twodim is declared as one having 3 rows, but the number of columns are given based on the value of i. So, the first row has one column, second row has two columns and third row has three columns.




2 comments:

  1. HI,if you want to build your aptitude skills,then must try this site.very useful for aptitude
    http://www.kidsfront.com/competitive-exams/quantitative-aptitude-practice-test.html?

    ReplyDelete
  2. Thank you for sharing this.
    Every year NTA conducts UGC NET exam twice in a year. In 2021, NTA could conduct the exam just one occasion thanks to COVID-19 pandemic. Therefore, NTA has announced that UGC NET December 2020 exam cycle are going to be conducted within the month of May 2021.
    Important tips to crack the UGC NET exam within the first attempt. Today in this article we will discuss about How to crack UGC net exam with 3 months preparation by GURUKUL CAREER GROUP best UGC Net coaching in Chandigarh.
    UGC NET Coaching in Chandigarh

    ReplyDelete