Write the algorithm of quick sorting?
1- set pass =1
2- repeat step 3 varying j from 0 to n-1-pass
3- if the element at index j is >than the element at index j+1 swap the two element
4- increment pass by 1
5-if pass is <=n-1 go to step 2
Real world example for encapsulation and abstraction?
Java Encapsulation
Encapsulation is the concept of hiding the implementation details of a class and allowing access to the class through a public interface. For this, we need to declare the instance variables of the class as private or protected.
The client code should access only the public methods rather than accessing the data directly. Also, the methods should follow the Java Bean's naming convention of set and get.
Encapsulation makes it easy to maintain and modify code. The client code is not affected when the internal implementation of the code changes as long as the public method signatures are unchanged. For instance:
public class Employee
{
private float salary;
public float getSalary()
{
return salary;
}
public void setSalary(float salary)
{
this.salary = salary;
}
Why do we need type casting in programming?
Becuase, sometimes data is in a form that is unrecognizable to the program. Example:
int a = 2;
int b = 3;
double c = a / b;
We cast c as a double. if it was not, then the data would not have a decimal value.
Program to copy one string to another?
As usual, you should check official documentation before you ask a question like this.
string.h
// Copies num characters from source into destination.
char* strncpy (char* destination, const char* source, size_t num);
// Copies characters from source into destination.
char* strcpy (char* destination, const char* source);
Write a program in c language to reverse elements of a queue?
There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements.
However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack.
You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue.
Your queue will have the exact same elements as in the beginning, but in reverse order.
The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers.
Following is pseudocode:
Queue<Item> reverse (Queue<Item> queue) {
Stack<Item> stack;
Item item;
while (queue.remove(&item)) {
stack.push(item);
}
while(stack.pop(&item)) {
queue.add(item);
}
return queue;
}
How can i write a program converting from hexadecimal to binary in java?
First of: I can do that in a simple for loop:
public static long bin2dec(char[] input) {
long dec = 0; // return value
for (int i = 0; i < input.length; i++) // No curly braces because its just one line; IT WILL WORK, but you can add curly braces
if (input[i] - 48 > 1) return null; // same here
else dec = (dec << 1) + input[i] - 48; // and here
return dec;
}
The recursion variant of the bin2dec can crash the stack:
public static long bin2dec(char[] input) {
char[] nextInput = new char[input.length - 1]; // char-array for the next recursion
for (int i = 0; i < newInput.length; i++) nextInput[i] = input[i];
return (input[input.length - 1] - 48) + bin2dec(nexInput) << 1;
}
Strings are probably one of the most commonly used java data-types. They can hold almost anything that are enclosed within a pair of double quotes and hence are very versatile and powerful. This chapter covers the String class.
Strings Are Immutable Objects
Handling "strings" of characters is a fundamental aspect of most programming languages. In Java, each character in a string is a 16-bit Unicode character. Because Unicode characters are 16 bits, a rich, international set of characters is easily represented in Unicode.
In Java, strings are objects. Just like other objects, you can create an instance of a String with the new keyword, as follows:
String s = new String();
This line of code creates a new object of class String, and assigns it to the reference variable s. So far, String objects seem just like other objects. Now, let's give the String a value:
s = "abc";
As you might expect, the String class has about a zillion constructors, so you can use a more efficient shortcut:
String s = new String("abc");
And just because you'll use strings all the time, you can even say this:
String s = "abc";
There are some subtle differences between these options that we'll discuss later, but what they have in common is that they all create a new String object, with a value of "abc", and assign it to a reference variable s. Now let's say that you want a second reference to the String object referred to by s:
String s2 = s; //refer s2 to the same String as s
String objects seem to be behaving just like other objects, so how is it that they are Immutable? Once you have assigned a String a value, that value can never change-it's immutable, frozen solid, won't change. The good news is that while the String object is immutable, its reference variable is not, so to continue with our previous example:
s = s.concat(" efg");
// the concat() method 'appends' a literal to the end
Didn't I just say that Strings are immutable? Yes, I perfectly did. But, here the stuff within the double quotes that is passed as argument to the concat method gets appended to the end of the String s. How did this happen?
The VM took the value of String s (which was "abc"), and added or rather appended " efg" onto the end, giving us the value "abc efg". Since Strings are immutable, the VM couldn't stuff this new value into the old String referenced by s, so it created a new String object, gave it the value "abc efg", and made s refer to it. At this point in our example, we have two String objects: the first one we created, with the value "abc", and the second one with the value "abc efg". Technically there are now three String objects, because the literal argument to concat, " efg", is itself a new String object. But we have references only to "abc" (referenced by s2) and "abc efg" (referenced by s).
Note, however, that the original "abc" String didn't change; only the reference variable s was changed, so that it would refer to a different String.
To wrap up, the original variable 's' in which we had "abc" as value would be abandoned and a new value "abc efg" would get assigned to it as soon as the s.concat(" efg") line of code is executed.
Why is Java better than C programming?
Java is very popular among high schools and universities for an introductory language. Java syntax is very similar to the "classic" C and C++ languages, but Java is much more developer-friendly when it comes to error feedback and memory management.
When entire generations of programmers are introduced to the field with the same language, that language tends to become the "popular" one.
What are the differences between AWT and swing components in java?
Swing is technologically more advanced than AWT. It has much more features and functions. It has a richer set of components which can modify and change according to the library. It uses MVC which is Model View Controller Paradigm thus offering a more flexible GUI. Swing also has a built in double Buffering and at the same time is lightweight. It is also one hundred percent java based. And therefore provides icons, and also decorative and attractive borders for components and tool tips.
The only flaw is that not all swing might act like native component. Swing is required to create a Java program. This tool kit is highly complex with its customizable text package and its integrated accessibility support. Since swing is built on a 2D package it can easily enhance all animations and images. And its undo framework thus supporting innumerable editing. Thus swing definitely beats AWT in all ways.
The Java language was created by James Gosling in June 1991
What is difference between instance variable and class variable in java?
The main difference between the class variable and Instance variable is,
first time, when class is loaded in to memory, then only memory is allocated for all class variables. Usually static variables are called class variables. These variables are available throughout the execution of the application and the values are common to the class. You can access them directly without creating an object of the class.
Instance variables are normal variables declared in a class, that would get initialized when you create an instance of the class. Every instance of the class would have a copy of the variable and you need a class instance (object) to access these variables
The kalangs of java were a community of skilled forest cutters and shifting cultivators . Without their expertise , it was difficult to harvest teak and for the kings to build their palaces
What is meant by flow of execution a program?
That means to load a computer program into a computer's memory, and have the computer carry out the instructions in the program.
What is A part of a program in witch a variable may be accessed?
The part of a program in which a particular variable may be accessed is called the 'scope' of the variable.
In most cases, the scope of a variable is limited to the function within which it was created, or any function it is passed to as an argument.
You can also use global variables, which can be accessed from any part of the program and have 'global scope'. However, this is generally considered as poor programming practice, and should be used cautiously and sparingly as it tends to make code difficult to read and maintain.
Program for stop and wait protocol in c language?
/*************************************************************************************/
/* C program to implement stop and wait protocol*/
/* Download more programs at http://sourcecode4u.com/ */
/*************************************************************************************/
#include<stdio.h>
#include<conio.h>
#define MAXSIZE 100
typedef struct
{
unsigned char data[MAXSIZE];
}packet;
typedef enum{data,ack}frame_kind;
typedef struct
{
frame_kind kind;
int sq_no;
int ack;
packet info;
}frame;
typedef enum{frame_arrival}event_type;
typedef enum{true_false}boolean;
void frame_network_layer(packet *p)
{
printf("\n from network arrival");
}
void to_physical_layer(frame *f)
{
printf("\n to physical layer");
}
void wait_for_event(event_type *e)
{
printf("\n waiting for event n");
}
void sender(void)
{
frame s;
packet buffer;
event_type event;
printf("\n ***SENDER***");
frame_network_layer(&buffer);
s.info=buffer;
to_physical_layer(&s);
wait_for_event(&event);
}
void from_physical_layer(frame *f)
{
printf("from physical layer");
}
void to_network_layer(packet *p)
{
printf("\n to network layer");
}
void receiver(void)
{
frame r,s;
event_type event;
printf("\n ***RECEIVER***");
wait_for_event(&event);
from_physical_layer(&r);
to_network_layer(&r.info);
to_physical_layer(&s);
}
main()
{
sender();
receiver();
getch();
}
Which package in java is treated as default package?
java.lang defines the core Java language, without which all of Java would fail to operate. It is therefore the default package that must be used with every program that will run Java, as it contains all of the logic necessary for exception handling, threads, classes that represent primitives (and their associated logic), and so on.
Why JVM is platform independent?
by creating a jre spesific to each platform programmers can confidently write code in any platform and assume it willl also work in aany other. theirfore java is platform independent as bytecode would look the same on any platform, however will be implemented by a diffrent interpreter for each platform.
What do you call a Program that runs Java byte code instruction?
Get the JDK & Bluej from net and the rest will be done by them.
Java byte codes are stored as *.class ; where "*" represents the class name, in your hard disk.
You can download BlueJ as well as JDK from the related link.
What is meant by the 'this' keyword in Java?
The keyword this is used to refer to the current class object instance
for example: this.getName() refers to the method getName() of the current object instance. Even if there were multiple getName() methods in the parent classes the key word this instructs the JVM to call the method in the current class
What is the scope of method overloading and overriding?
Overloading happens when you have multiple methods in the current class that have the same name but different signature. The scope of method overloading is "Within the current class"
Overriding happens when your current class extends another class (the parent class) and provides implementation for a method that is already available in the parent class. The scope of method overriding too is "Within the current class"
A variable declared inside the for loop control cannot be referenced outside the loop?
Yes. A variable declared inside the loop is a local variable for the code block enclosed by the {} statements of the for loop. The variable will not be available to be used by any code outside the code block.
How is Action-Listener interface implemented?
A listener listens to what the other person is lamenting, narrating, describing graphical and telling. In Java, the action listener interface finds application in handling action events.
Discuss about the visibility control of java program?
Visibility is another term used for Acess Specifiers for java variables and objects.
One of the techniques in object-oriented programming is encapsulation. It concerns the hiding of data in a class and making them available only through its methods. In this way the chance of making accidental mistakes in changing values is minimized. Java allows you to control access to classes, methods, and fields via so-called access specifiers. The access to classes, constructors, methods and fields are regulated using access modifiers i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.
Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. A member has package or default accessibility when no accessibility modifier is specified.
Access Modifiers
1. Private
2. Protected
3. Default
4. Public
Public is the most liberal access specifier and Private is the most restrictive access specifier.
What are the differences between Java OOP and PHP OOP?
JAVA is an Object Based Programming Language.
it doesn't provide multiple inheritance and operator overloading.
while Object Oriented Lanuages provides both.
What is return a void data type?
#include<stdio.h>
int main(){
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}