answersLogoWhite

0

📱

Java Programming

The Java programming language was released in 1995 as a core component of the Java platform of Sun Microsystems. It is a general-purpose, class-based, object-oriented language that is widely used in application software and web applications.

5,203 Questions

Advantages of static keyword in java?

When we want that a class member should be used independently of any object of that class, we have to precede that member's declaration with the keyword static .
When a member is declared as static, it can be acccessed without creationg an object of the class and without referring to the object.That static is , a static member belons to a class as a whole and not to any one instance/object of that class.
The static member is shared by all objects of thatclass.

What is a class in programming?

class is summation of data member and data types(basically known as instance variable of a class). making a class is basically an encapsulation under various access specifiers on which its object accessibility depends. these data functions can be accessed inside or outside the class with the help of object(class variable) of a class. an object defined in another class(i.e. outside the original class) can access its own class members which r not declared under private mode but can access public members and it is derived then can also access protected data members or data types also.

Difference between Java and the C plus plus?

Classes are the basic unit of code in Java, whereas the basic unit of code in C++ are functions.

All objects in Java descend from a common class (Object), which allows more generic code. While this can also be (manually) implemented within C++, generic programming is best achieved using templates and concepts.

All methods in Java (including destructors) are virtual by default. In C++, methods must be explicitly declared virtual if they are expected to be overridden, or explicitly declared final if they must not be overridden.

Java does not permit operator overloading, thus we cannot operate upon objects as intuitively as we can in C++.

Benefits of inheritance in Java programming?

Advantages of Inheritance

1) Code Re-usability

2) consumes less time

3) Without changing the super class we can add some more methods in the super class by inheriting it in the derived class

What are range of character data type in c plus plus?

The range of character data types in C++ is the set of characters in the host's character set.

It is inappropriate to consider the numerical range of such things as characters, because that depends on the particular codeset involved, such as ASCII, EBCDIC, UNICODE, KANJI, etc. Doing that leads to non-portable code, and lazy programming practices. Do not write code that depends on the collating sequence of the character set, or the numerical difference between two characters.

Type char can be signed or unsigned, the value range is -128..127 or 0..255 respectively.

Where was the Java programming language invented?

James Gosling, PhD (born May 19, 1955 near Calgary, Alberta, Canada), along with other engineers and scientists, invented Java. Gosling is a famous software developer and is actually best known as the father of the Java programming language.

How do you create a concrete method inside abstract class?

The same way you create a concrete method in a concrete class. When a class is abstract, it can contain abstract methods. That doesn't mean that all methods must be abstract. Hope this helps.

What are micro-encapsulation?

These are small capsules of nano materials inside it,which when contacted with the body,splits up an soothes the skin . It can be used as revitilizer for various purposes. It is one of the effective technique used.

What is need for function overloading in Java?

To overload a method in java, you only needs to write a method with the same name of other in your class but with diferente kind or number of parameters. For example:

class MyClass{

void printNumber(int x){

System.out.println(x);

}

void printNumber(int x, int y){

System.out.println(x+y);

}

}

the type of the value that your method return could change if you want.

What are the examples of polymorphic viruses?

1260 is a computer virus developed by Mark Washburn in 1989. This virus used a form of polymorphic encryption in it's code which randomized the algorithm used by the virus but kept the core function of it intact. Polymorphic code does this by infecting files with an encrypted copy of itself, then varying it's signature by randomizing its decryption algorithm. Then it repeats. Hope this helped, here's some relevant info on the subject:

http://en.wikipedia.org/wiki/1260_(computer_virus)

http://en.wikipedia.org/wiki/Mark_Washburn

http://en.wikipedia.org/wiki/Timeline_of_computer_viruses_and_worms

--

Answered by Alix Saunders

How Convert decimal to binary using stack?

Decimal to Binary Using a Stack

There is no need to program a computer to convert from decimal to binary because all programming languages do this by default; they are binary computers after all. That is, if we want to store the decimal value 42 in computer memory, we simply assign the literal constant 42 to some variable or constant. But the value that is actually assigned is really the binary value 00101010. Moreover, even if we were to assign the hexadecimal value 0x2A or even the octal value 052, the value will still be automatically converted to the binary value 00101010. These are all different representations of the decimal value forty-two, but 00101010 is the only way that value can be physically stored in computer memory.

Of course we may choose to store the value as a string, "42", however it is no longer a numeric value at that point, it is a character array. However, most languages will provide some library function that can convert the string representation of a numeric value into their actual numeric representation. In C, for instance, we would simply use the atoi() function. Nevertheless, it is not something we need to specifically cater for; the function does all the hard work for us, converting the string, "42", into the equivalent binary value 00101010.

However, although decimal integer values are always stored in native binary code, presenting that binary value to the user isn't as straightforward. All languages will automatically convert the binary value back to decimal for display purposes, but what it's actually doing behind the scenes is converting the binary value 00101010 into the string "42" and then printing the string. In other words, it is the reverse of the atoi() function, essentially the equivalent of the itoa() function.

Given that we don't need to program either of these conversions, it's easy to forget that these conversions are taking place. Given that humans work predominantly in decimal it is only natural that these conversions be done automatically for us, but it is a high-level concept; it is an abstraction. While there's nothing wrong in thinking at a high-level, it's important to be aware of the low-level operations that are actually taking place, because the more we know about what's really going on behind the scenes, the more easily we can exploit them. The high-level concept of converting from decimal to binary is only one such example where we can exploit the fact that the conversion to binary is already done for us behind the scenes. All we really need to do is present the result to the user.

To achieve this we need to convert the binary value to a string, in much the same way as the itoa() function converts a binary value into a decimal string, except we convert to a binary string. A stack makes this incredibly simple. We simply examine the low-order bit and push a '1' character onto the stack if the bit is set, otherwise we push a '0'. We then shift all the bits one bit to the right and repeat the process. We continue in this manner until the binary value is zero. If the number of elements on the stack is not an exact multiple of 8, we can (optionally) push additional '0' characters onto the stack for padding. Finally, we print the top-most element on the stack and then pop it off the stack, repeating until the stack is empty.

Thus if we use the value 00101010 once more, we examine the low-order bit. It is not set so we push a '0' onto the (empty) stack. We then shift the bits one place to the right, giving us 00010101. The low-order bit is now set so we push a '1'. We repeat the process, pushing a '0', then a '1', another '0' and another '1'. At this stage, the binary value is 00000000 so we are done. The stack has only six elements so we (optionally) push another two '0' characters onto the stack for padding. Now we begin popping the stack, printing the top-most element before each pop. When we are done, we will have output the character sequence "00101010".

The following code in C++ shows how this might be implemented as a function. The return value is a string containing the binary representation of the function argument, dec.

std::string dec2bin(int dec) {
std::stack s {}; initialise an empty stack of characters
while (dec) {
if (dec & 0x1) // if the low-order bit is set...
s.push ('1');
else
s.push ('0');
dec >>= 0x1; // shift-right
}
while (s.size() % 8) s.push('0'); // pad the stack to the nearest 8-bit boundary
std::string bin {}; // initialise return value (empty string)
while (!s.empty()) {
bin.append (s.top());
s.pop();
}
return bin;
}

Example usage:

int main() {
std::cout << "Enter an integer value: ";
int i;
std::cin >> i;
std::cout << "The decimal value " << i << " in binary is " << dec2bin(i) << std::endl;
}

Factorials Using a Stack

The question regarding factorials is easier to answer because the factorial algorithm is a naturally recursive algorithm, and whenever we recurse through a function we automatically use the call stack. The call stack is a bit more complex than an ordinary stack but it is a stack nonetheless.

The factorial of any positive value n (denoted n!) is the product of all integers in the closed range [1:n]. Thus factorial 6 (denoted 6!) is 1*2*3*4*5*6=720. We can easily implement this function using a recursive function as follows:

unsigned fact (unsigned n) {
if (n<2) return 1;
return n * fact (n-1);
}

Note that all recursive functions must have an end condition. Given that 0! and 1! are both 1, any positive value less than 2 represents the required end condition, where we simply return the value 1. Otherwise we return the product of the value and the factorial of the value minus 1.

The only problem with this is that the return value would normally be either 32-bit or 64-bit. A 32-bit unsigned integer is only capable of storing values in the closed range [0:4,294,967,295] so 12! is the largest factorial it can handle because 13! is 6,227,020,800. With 64-bit unsigned integers the range increases to [0:18,446,744,073,709,551,615] but 20! is the largest factorial you can handle because 21! is 51,090,942,171,709,440,000.

Using floating point types such as long long double will give more headroom to play with but, as you can hopefully appreciate, even very small numbers have extremely high factorials, so the risk of overflow cannot be avoided. Even the Microsoft Windows Scientific calculator can only handle factorials up to 3248!

There are two possible solutions to this. The first is to examine the function argument and throw an exception if it exceeds the abilities of the function:

unsigned fact (unsigned n) {
if (n<2) return 1;
if (12
return n * fact (n-1);
}

The problem with this is that you must handle the exception every time you call the function:

int main() {
try {
fact (13);
}
catch (std::range_error& e) {
std::cerr << "out of range" << std::endl;
}
}

The alternative is to create a user-defined type that is capable of exceeding the bounds imposed by the architecture, typically by using a string representation of the value. That's beyond the scope of this answer, however you can find open-source libraries for most languages that can provide this functionality if you require it. It's not a common requirement hence most languages do not include it as standard, but it makes no sense to implement your own "big integer" library when there are so many free libraries available.

Where can one find java project tutorials?

Java project tutorials can be found on Oracles homepage as well as Javas homepage. Netbeans and Eclipse also provide tutorials for setting up java projects in their specific integrated development environments.

WAP that initialize an integer and display whether it is single digit two digit three digit or more than three digits?

Please note that the answer below is the most expandable answer. By this I mean that it can be easily changed to display the output given for any number of digits in a number. There are far simpler solutions which do not require any explanation, but are useless in an evolving setting.

To answer this question, we need to look at the Math.log10 function. Ignoring the possible anomalous results of this function (check the current Java API to find out what other results are possible), we can find the number of digits in a number by using: (log10(n) + 1). If n is equal to the value 10^m for a value m, then the function will return m. That is to say, if n is equal to 10, then the result of the function will be 1, since 10^1 = 10. Likewise, if n is equal to 100, then the result of the function will be 2, since 10^2 = 100, and so forth. If you look at the numbers in between, you will find the function returns a decimal result. log10(99) = 1.9956, for example. This means that the range of answers you will get for the values [10,99] will return the range of answers from [1.0,2.0). The same can be found looking at the range of values [100,999] which return in the range [2.0,3.0). From this we find that the function log10 always returns either an integer or a fractional value which will always be at most 1.0 less than the number of digits in the number sent to it. In either case, if we simply truncate it (cast the double as an int) and add 1, we come up with our getNumDigits function.

int getNumDigits(int n) {

return ((int) Math.log10(n)) + 1;

}

And to finish up your question, we can add in another method to deal with the results.

// we're assuming the number n comes from somewhere else,

// whether hard-coded or passed as input to the program

void displayNumDigits(int n) {

switch(getNumDigits(n)) {

case 1:

System.out.println("One digit");

break;

case 2:

System.out.println("Two digits");

break;

case 3:

System.out.println("Three digits");

break;

default:

System.out.println("More than three digits");

}

}

How can you initialize an array in Java?

If you refering to Object then String[] something=new String[2]; Here you have to remember that something only allocated space for 2 String object but it did not created them yet. By default Objects are intantiated to null so if you try to print the content of an array System.out.println(something[0]);//print null System.out.println(something[0].toLowerCase()); Throws NullPointerException Couple other ways to create Arrays In java String[] something=new String[]{"Me","You"}; String[] something={"Me", "You"};

How can you save java servlet program for running?

If ur developing servlet program manually.The First thing is that u have to put servlet-api jar file should be set in classpath. The root directory is like that

create a folder name.inside the folder create one more folder name WEB-INF. Inside the WEB-INF folder create two folders one is classes and another is Lib

In the WEB-INF folder you have to put web.xml. In classes you have to put servlet.java. In Lib u have to put servlet-api.jar file. Go to command prompt then compile servlet.java .Create a war file. steps to create war file file jar cvf name.war . enter .open the browser type the bar http://localhost: portnumber enter & then apache tomcat server start.Click tomcat manager.It will ask username & password.

then submit.In the downstairs page there is a option name War file to deploy .Click on choose file.It will locate where is the war file .Select it & deploy it.click the programs then you have to type in the addresss bar what u had written in url-pattern in web.xml then enter u get the output.

Explain the concepts of constructor and destructor Do you have to declare a constructor every time you create a class?

we cover constructors and destructors for C++ objects. Constructors give a means to initializing newly created objects and can be overloaded. Destructors offer a way to perform clean up on an object just before it is destroyed; a class can have at most one destructor. Together constructors and destructors provide a method of encapsulating the lifespan of an object. Although we have not done anything useful with destructors so far, we will see that they are extremely useful as our programs get more complex.

In mathematics, an ordered list numbers is called a sequence. Sequences come in many types, but we can model a generic sequence with a class. Sequences can be of any length, but we will put a limit of 100 terms on our sequences for this first example:

The code above describes a sequence class with two constructors, a member function, and two data members. The first constructor is a default constructor, which creates a sequence with no terms in it. The second constructor takes an array of terms and a length; the terms are copied from the array to the sequence via a for-loop. The member function outputs the length of the sequence and then all of the terms-a check is made internally to assure that sequence has at least one term before outputting them. The data members are the length and an array of doubles to hold the terms.

In this main function, we create two instances of our sequence object and call Print() to output the sequences to the console window.

What are the advantages of overriding method in java class?

Any time you have a class that inherits a method from a superclass, you have the opportunity to override the method (unless, as you learned in the earlier chapters, the method is marked final). The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type. The following example demonstrates a Porsche subclass of Car overriding the Car version of the drive() method:

public class Car {

public void drive() {

System.out.println("Generic Car Driving Generically");

}

}

class Porsche extends Car {

public void drive() {

System.out.println("Porsche driving Full Throttle");

}

}

For abstract methods you inherit from a superclass, you have no choice. You must implement the method in the subclass unless the subclass is also abstract. Abstract methods must be implemented by the concrete subclass, but this is a lot like saying that the concrete subclass overrides the abstract methods of the superclass. So you could think of abstract methods as methods you're forced to override.

The Car class creator might have decided that for the purposes of polymorphism, all Car subtypes should have an drive() method defined in a unique, specific way. Polymorphically, when someone has an Car reference that refers not to an Car instance, but to an Car subclass instance, the caller should be able to invoke drive() on the Car reference, but the actual runtime object (say, a Porsche instance) will run its own specific drive() method. Marking the drive() method abstract is the Car programmer's way of saying to all subclass developers, "It doesn't make any sense for your new subtype to use a generic drive() method, so you have to come up with your own drive() method implementation!" A (non-abstract), example of using polymorphism looks like this:

public class TestCars {

public static void main (String [] args) {

Car a = new Car();

Car b = new Porsche(); //Car ref, but a Porsche object

a.drive(); // Runs the Car version of drive()

b.drive(); // Runs the Porsche version of drive()

}

}

class Car {

public void drive() {

System.out.println("Generic Car Driveing Generically");

}

}

class Porsche extends Car {

public void drive() {

System.out.println("Porsche driving Full Throttle");

}

public void brake() { }

}

In the preceding code, the test class uses a Car reference to invoke a method on a Porsche object. Remember, the compiler will allow only methods in class Car to be invoked when using a reference to a Car. The following would not be legal given the preceding code:

Car c = new Porsche();

c.brake(); // Can't invoke brake();

// Car class doesn't have that method

To reiterate, the compiler looks only at the reference type, not the instance type. Polymorphism lets you use a more abstract supertype (including an interface) reference to refer to one of its subtypes (including interface implementers).

How do you achieve abstraction in java?

Abstraction in Java is achieved using interfaces and abstract class. abstract means something which is not complete or concrete but abstraction itself is a great programming concept and allow you to write code which is more flexible to change.

Explain this keyword with example in java?

this is one of the keyword in java programming language. this can be used inside any method to refer to the current object. example: class Box{ double width; double height; double depth; Box(double w,double h,double d){ this.width=w; this.height=h; this.depth=d; }

} The significance of the keyword this would be realized in cases where we use inheritance. There may be cases where you have methods of the same name in both the current class and its parent. That is when the keyword "this" can be used to tell the JVM to invoke the method of the current class and not the parent class. this refers to the object with which the method inside a class is called. For instance, I call studentObject.getGrade(), in the getGrade() method to call my own method or variables, i.e. a private instance variable named grade, I would state: this.grade. Though inside the class any method or variable that is called without an object automattically calls the most local method or variable, i.e. I could just call grade for the above example given that I do not have a variable of the same name declared in the method. "this" is useful in those circumstances where there are more than one variable with the same name, just for example that I do have a variable named grade in my getGrade() method, to get my instance variable for my object I would simply call this.grade, thus to be safe use this for instance variables.

Static variable and static methods in java?

== == Static method cannot be overwritten because it belongs to the class and not to the Object

If you're asking about overriding or overloading, then yes. Static methods can be overridden and overloaded just like any other methods.

High-level programming language intepreted?

EZtrieve and EZtrieve Plus are examples of a high level language that can be either compiled or run interpretive.

Differences among sequential access direct access?

Let's say you have a set of 100 pieces of data, which are all names. Now, if you want to find a specific name, "Kevin", you can find it in different ways. You could either go through each of the records one after another, or you could randomly generate a number, and check if that record is "Kevin", this is potentially faster than sequential access as "Kevin" could be the last record.

How are pointer different from other variables?

A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location.

For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable.

For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p).

Consider:

long n = 65;

long *p;

p = &n;

Results:

Type | Name | Location | Value

long n 0xA3FF 65

long * p 0x31DF 0xA3FF

So p points to n. Now,

n = 65

&n = 0xA3FF

p = 0xA3FF

*p = 65

&p = 0x31DF

You may find yourself having to use typecasts frequently when using pointers.

Pointers are useful when passing data to functions.

For instance, consider the following function:

void add(int a, int b, int c) { c = a + b; }

The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to:

void add(int a, int b, int *c) { c = a + b; }

and call the function by passing in the location of the variable to the function:

add(a,b,&c);

then you can modify the variable itself.

Pointers are also good for working with arrays:

char *c = "Hello World";

int i=0;

while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.

Java programing in library management system program?

import java.io.*;

class lib

{

int bno,fine=10,edit,date,ret,userdt;

String auth,publi,buknam;

void get()throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\tEnter the book number:");

bno=Integer.parseInt(br.readLine());

System.out.print("\tEnter the book u want:");

buknam=br.readLine();

System.out.print("\tEnter the author's name:");

auth=br.readLine();

System.out.print("\tEnter the edition:");

edit=Integer.parseInt(br.readLine());

System.out.print("\tEnter the date of issuing:");

date=Integer.parseInt(br.readLine());

System.out.print("\tEnter the return date:");

ret=Integer.parseInt(br.readLine());

}

}

class fin extends lib

{

void get1()throws IOException

{

BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\tuser returning date:");

userdt=Integer.parseInt(br2.readLine());

if(userdt>ret+10)

{

System.out.println("\t\t PAY THE FINE AMOUNT...!!!! RS:"+fine+"/-");

}

}

void disp()

{

System.out.println("\t THANKS FOR USING OUR SERVICE...:-)...");

}

}

class libmain

{

public static void main(String ar[])throws Exception

{

BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));

int s,k=0,i;

fin f=new fin();

System.out.println("\n\t\t^*^*^*^*^*^*^* LIBRARY MANAGEMENT ^*^*^*^*^*^*^");

do

{

System.out.println("------------------MENU-------------------------------");

System.out.println("\nENTER UR CHOICE\n\t1.GONNA TAKE A BOOK\n\t2.RETURN THE BOOK\n\t3.EXIT");

s=Integer.parseInt(br1.readLine());

switch(s)

{

case 1:

f.get();

f.disp();

break;

case 2:

f.get();

f.get1();

break;

case 3:

k=1;

break;

}

}

while(k==0);

}

}

with regards

sm.muthu :-)