Program for dequeue in data structure?
Display function in de queue
void display()
{
int i;
if((front == -1) (front==rear+1))
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
for(i=front; i<=rear; i++)
printf("\t%d",queue[i]);
}
}
Write a test program that prints a 3 by 3 matrix?
4.(Displaying matrix of 0s and 1s ) write a method that displays by n by n matrix using the following header : Public static void printMatrix(int n) Each element is o or 1,which is generated randomely. Write a test program that prints a 3 by 3 matrix that may look like this: 010 000 111 4.(Displaying matrix of 0s and 1s ) write a method that displays by n by n matrix using the following header : Public static void printMatrix(int n) Each element is o or 1,which is generated randomely. Write a test program that prints a 3 by 3 matrix that may look like this: 010 000 111
Explain the meaning of abstract keyword in relation to classes and methods?
The abstract keyword is used to denote abstract classes and abstract methods in Java.
An abstract method is a method which is defined, but not implemented:
public abstract void doStuff();
An abstract class is a class which contains one or more abstract methods*, and which cannot be instantiated directly. All subclasses of an abstract class must implement the abstract methods.
* Note that abstract classes can include no abstract methods, but this rather defeats the purpose of using an abstract class.
The quintessential example of an abstract class is the Shape class.
// Definition of our abstract class
public abstract class Shape {
// Notice how we can actually declare and implement variables and methods.
// This is what differentiates between an abstract class and an interface.
// The location of this shape
private double x,y;
// Change our location - all subclasses will have this by default
public void moveTo(final double newX, final double newY) {
x = newX;
y = newY;
}
// Definitions of our abstract classes.
// All classes which extend from Shape must implement these.
public abstract double getArea();
public abstract double getPerimiter();
}
// Definition of our concrete example class
public class Rectangle extends Shape {
// Beyond the x,y location of Shape, Rectangle must have width and height
private double width, height;
// Implement abstract methods
public double getArea() {
return width * height;
}
public double getPerimiter() {
return width + width + height + height;
}
}
Can you have a final abstract class?
No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.
Combining them would lead to an unusable class, so the compiler will not let this happen.
class firstprog
{
public static void main (String args[])
{
System.out.pritnln("My first Program in Java ");
}
}
Simple Java Programe
Write the above program in Ms dos's edit or Notepad of Windows save it with "firstprog.java" run the commands given below in command prompt of Ms Dos to get the output Remamber Java is case sensative so mentain capital and small letter.
1. javac firstprog.java
2. java firstprog
What is an abstract interface?
An abstract interface is an interface which has a one-to-many relation - providing multiple paths from a single location to multiple locations.
You can also write desktop programs. You can use Java for almost anything.
You can also write desktop programs. You can use Java for almost anything.
You can also write desktop programs. You can use Java for almost anything.
You can also write desktop programs. You can use Java for almost anything.
What is relationship between string and char data type?
The char type store only one symbol of data (letter, digit, space, tab and so on). The string type is able to keep virtually data of any length. Also string type is based on arrays and uses reference type working with memory.
How java runs in command prompt?
The Java executable comes with a showversion flag, so typing:
java -showversion
Will display something like:
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) 64-Bit Server VM (build 14.2-b01, mixed mode)
Note: This seems to be bugged on my machine. After printing out the above information, it also prints out the rest of the command line switches. If this happens to you, you may have to scroll up to see the important information.
Write a program that accepts a word in uppercase and prints it in lowercase?
There are two functions in ctype.h: toupper() and tolower(). First one to make character into upper case and the second one for making lower case.
Here is a small program converting inputted character into lower case:
#include
#include
int main() {
char ch;
printf("Enter the char: ");
scanf("%c", &ch);
printf("Char in lowercase: %c\n", tolower(ch));
return 0;
}
Testing:
Enter the char: G
Char in lowercase: g
Enter the char: S
Char in lowercase: s
What operator can be used to compare 2 values in java?
One possible way (although much less efficient than using the operators directly) is by using BigInteger:
int a = 5, b = 7;
int sum = BigInteger.valueOf(a).add(BigInteger.valueOf(b)).intValue();
However, BigInteger.add() might use arithmetic operators in its own calculations; they are simply hidden from the programmer's view.
Thread can be created in two ways either by extending Thread or implementing runnable interface
Examples:
public class A extends Thread { } public class A implements Runnable { } By using both the methods we can create a Java Thread. The disadvantage of extending the Thread class is that, you cannot extend the features of any other class. If we extend the Thread class we cannot extend any other class whose features we may require in this class.
So it is usually advisable to implement the Runnable Interface so that we can extend the required features and at the same time use the Thread functionality.
How many classes are there in java?
there is one method only. go to command prompt and type this to know the methods.
D:\java>javap java.awt.event.ActionListener
Compiled from "ActionListener.java"
public interface java.awt.event.ActionListener extends java.util.EventListener{
public abstract void actionPerformed(java.awt.event.ActionEvent);
}
What are Class Constructor and Primitive data types?
class constructor is a function which has the same name as the class name and has no return type.
primitive data types are the fundamental data types which are independent.
eg:int,char,float etc..............
How do you write a Program in java to cheak a number is automorphic number?
Source Code ::
import java.io.*;
class automorphic
{
protected static void main()throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number: ");
int a=Integer.parseInt(in.readLine()),b=a,c=0,e=a*a;
while(b>0)
{
c++;
b/=10;
}
double d=Math.pow(10,c-1);
if(a==e%d)
System.out.println("Automorphic number!!");
else
System.out.println("Not an Automorphic number!!");
}}
How do you code to inherit from an interface in java?
We have been using references to the term "Implementing an Interface" throughout the preceding chapters and we havent yet actually dug deep into this topic. As you might remember, an Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior.
When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not)
Now, you might stop me and ask, what if I implement an interface and opt not to write code for a method that I am supposed to? The answer is simple. The compiler wouldn't let you do that. You cannot successfully implement an interface without providing method implementation for all the methods declared inside the interface. This is how the java system ensures that when someone knows a certain method name in an interface and has an instance of a class that implements it, can actually call that method without fear that the method isn't implemented inside the class.
Assuming an interface, Convertible, with two methods: openHood(), and setOpenHoodFactor(), the following class will compile:
public class Ball implements Convertible { // Keyword 'implements'
public void openHood() { }
public void setOpenHoodFactor(int bf) { }
}
Ok, I know what you are thinking now. "This has got to be the worst implementation class that you have seen". Though it compiles and runs as well, it is actually doing nothing… the interface contract guarantees that the class implementing it will have a method of a particular name but it never guaranteed a good implementation. In other words, the compiler does not bother whether you have code inside your method or not. All it cares is if you have methods of the matching names as in the interface. That's all…
Implementation classes must adhere to the same rules for method implementation as a class extending an abstract class. In order to be a legal implementation class, a nonabstract implementation class must do the following:
• Provide concrete (nonabstract) implementations for all methods from the declared interface.
• Follow all the rules for legal overrides.
• Declare no checked exceptions on implementation methods other than those declared by the interface method, or subclasses of those declared by the interface method.
• Maintain the signature of the interface method, and maintain the same return type (or a subtype).
• It does not have to declare the exceptions declared in the interface method declaration.
Why is quick sort and merge sort called stable algorithms?
Quick sort is not stable, but stable versions do exist. This comes at a cost in performance, however.
A stable sort maintains the order of equal elements. That is, equal elements remain in the same order they were input. An unstable sort may change the order. In some cases, the order of equal elements is of no consequence, but when two elements with different values have the same sort key, then order can be important.
Explain different applications of Java programming language?
The Java programming language can be used to create many application. Some are:
1. Web based applications
2. Stand alone GUI based applications
3. Stand alone command window based applications
4. Games
5. Applets
etc...
Why we use IntegerParseInt in java?
Parsing is very important since the input from the user is not in the form of ints but in a String, therefore, you have to parse the String containing the number into a primitive data type.
i.e.
String num = "49";
int realNum = Integer.parseInt(num); // puts 49 into realNum;
A "null loop" is typically a loop which has no code in the loop body.
for(int i = 0; i < 57; ++i) {
}
How will you Write a c program to find the kth smallest element in an array in c?
//This is for kth largest element. (So this is for n-k smallest element) //Sudipta Kundu [Wipro Technologies] #include <stdio.h> //Input: array with index range [first, last)
//Output: new index of the pivot. An element in the middle is chosen to be a pivot. Then the array's elements are
//placed in such way that all elements <= pivot are to the left and all elements >= pivot are to the right.
int positionPivot(int* array, int first, int last); //Input: array with index range [first, last) and integer K (first <= K < last)
//Output: array whose Kth element (i.e. array[K]) has the "correct" position. More precisely,
//array[first ... K - 1] <= array[K] <= array[K + 1 ... last - 1]
void positionKthElement(int* array, int first, int last, int k); int main() {
int array[] = {7,1,8,3,1,9,4,8};
int i;
for (i = 0; i < 8; i++) {
positionKthElement(array, 0, sizeof(array) / sizeof(array[0]),i);
printf("%d is at position %d\n", array[i], i);
}
return 0;
}
int positionPivot(int* array, int first, int last) {
if (first last)
return first; int tmp = (first + last) / 2;
int pivot = array[tmp];
int movingUp = first + 1;
int movingDown = last - 1;
array[tmp] = array[first];
array[first] = pivot;
while (movingUp <= movingDown) {
while (movingUp <= movingDown && array[movingUp] < pivot)
++movingUp;
while (pivot < array[movingDown])
--movingDown;
if (movingUp <= movingDown) {
tmp = array[movingUp];
array[movingUp] = array[movingDown];
array[movingDown] = tmp;
++movingUp;
--movingDown;
}
}
array[first] = array[movingDown];
array[movingDown] = pivot;
return movingDown;
} void positionKthElement(int* array, int first, int last, int k) {
int index;
while ((index = positionPivot(array, first, last)) != k) {
if (k < index)
last = index;
else
first = index + 1;
}
}
Why is it necessary to use indentation and space in java statement?
The Java compiler doesn't need this, and in fact doesn't care. It is important to improve the readibility of the program for human programmers. Even with correct indentation, interpreting a computer program can be confusing. Without proper indentation and spacing, it will be much, much worse.
Implementation of stack using recursion?
public void reverse(Stack st)
{
int m = (int)st.Pop();
if (st.Count != 1)
reverse(st);
Push(st , m);
}
public void Push(Stack st , int a)
{
int m = (int)st.Pop();
if (st.Count != 0)
Push(st , a);
else
st.Push(a);
st.Push(m);
}
What is the use of overloading in java?