What is the difference between enterprise java and java?
With Java Platorm EE (Enterprise Edition) SDK (software development kit) you have some additional things that are installed on your computer like Glassfish server. They are not included in Java Platform SE SDK.
Difference between import and extends in java?
Import brings all the stuff of a file/API where you have imported it and increases redundancy while inheritance reduces redundancy, increase efficiency of code & makes it manageable. And most important point to remember is that, inheritance is a design followed over the whole project to archive polymorphism.
Thanks & Regards:
Vaibhav Singh
Write a program that input a positive integer and prints a triangle using for loop?
write a program that reads in the size of the side of square and then pints a hollow square of that size out of asterisks and blanks?
What is the use of string buffer class?
Strings are extremely useful but at the same time resource intensive too. In programs where numerous strings are used which need to be altered every now and then, it is advisable to use the StringBuffer or the StringBuilder class.
The java.lang.StringBuffer and java.lang.StringBuilder classes should be used when you have to make a lot of modifications to strings of characters. As we discussed in the previous chapter, String objects are immutable, so if you choose to do a lot of manipulations with String objects, you will end up with a lot of abandoned String objects in the String pool. On the other hand, objects of type StringBuffer and StringBuilder can be modified over and over again without leaving behind a great list of discarded String objects.
Writ a program in c to display day of the week using in switch case?
/* write a program to print Days of Week using switch-case structure */
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter Day of weak as Number 1 to 7 ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n MONDAY ");
case 2:
printf("\n TUESDAY");
case 3:
printf("\n WEDNESDAY");
case 4:
printf("\n THURSDAY");
case 5:
printf("\n FRIDAY");
case 6:
printf("\n SATURDAY");
case 7:
printf("\n SUNDAY");
default :
printf("\n no operation is required");
}
getch();
}
What is abstraction method in java?
Abstraction in Java or Object oriented programming is a way to segregate implementation from interface and one of the five fundamentals along with Encapsulation, Inheritance, Polymorphism, Class and Object. Abstraction in Java is achieved by using interface and abstract class in Java.
How can we use script tag in java?
to implement javascript embed the statements between <script> n </script>,,,
and include any scripting language.
eg:<script LANGUAGE="javascript">
block of codes
</script>
What is import.java.swing in java?
You tell Java where to look for classes by importing them. javax.swing is a package (collection of classes) which provides prebuilt classes for creating a graphical user interface.
How do you write an algorithm to find the number of permutations or combinations?
Permutations and combinations are two separate things. Although we often use them interchangeably in English, we need a more precise definition in mathematics, such that ABC and CBA are regarded as being two different permutations of the same combination. In other words, the order of the elements is important in a permutation but is completely irrelevant in a combination.
First we have to define what it means to create a combination or permutation. Typically we have a set from which we must make a subset. The number of elements in the set is typically defined using the variable n while the number of elements in the subset is r. Thus we can formally define a permutation mathematically using the function P(n,r) and a combination as C(n,r).
We must also consider whether elements may be repeated within a combination or a permutation. For instance, when selecting numbers for a lottery, no number may be repeated in any combination but in a 4-digit combination lock, any digit may be repeated in a permutation.
Note that a combination lock is really a permutation lock in mathematics and is the perverse way of remembering the mathematical difference between a combination and a permutation.
Thus we have 4 possible variations to cater for. In order of difficulty, they are:
Let's deal with them one at a time.
1. Permutations with repetition
To calculate P(n,r) with repetitions, for every selection, r, there are always n possibilities, thus we have n^r permutations.
In a 3-digit combination lock, each digit has ten possibilities, 0 through 9, so there are 10^3=10x10x10=1000 permutations. This stands to reason because the permutations form all of the numeric values from 000 through to 999, which is 1000 different values.
In C, we can write this function as:
unsigned long long permutations (unsigned long long n, unsigned long long r) {
return pow(n,r); /* use standard library function */
}
2. Permutations without repetition
To calculate P(n,r) without repetitions we must reduce the set by one element each time we make a selection. If we go back to our 3-digit combination lock, we have 10 choices for the first digit which leaves 9 choices for the next and 8 for the next. So instead of 10x10x10=1000 permutations we only have 10x9x8=720 permutations.
Although fairly simple to work out in this case, we need a formula that is generalised to cater for all cases, just as n^r works for all permutations with repetitions.
We can see that 10x9x8 is the initial product of 10! (factorial 10) which is 10x9x8x7x6x5x4x3x2x1. So we need a formula that ignores everything after the 8. The portion after the 8 is 7x6x5x4x3x2x1 which is 7! and we can calculate that from (n-r)!=(10-3)!=7!
Having determined the portion we need to ignore, the rules of multiplication and division state that if we multiply by x and subsequently divide by x, then the two x's must cancel each other out. Thus we get:
10!/7!=(10x9x8x7!)/7!=10x9x8=720
Using formal notation, P(n,r) without repetition is therefore n!/(n-r)!
In C, we must first write a function to calculate factorials:
unsigned long long factorial (unsigned long long n) {
return (n>1)?factorial(n-1):1; /* recursive function */
}
With that in place, we can now write a function to handle permutations without repetitions:
unsigned long long permutations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/factorial(n-r);
}
3. Combinations without repetition
C(n,r) without repetition is simply an extension of P(n,r) without repetition. Every combination of r has r! permutations, so if we divide P(n,r) by r! we will get C(n,r). Expressing this formally, C(n,r) without repetition is n!/((n-r)!r!)
Going back to our 3-digits from 10, there are 10!/((10-3)!3!)=10!/(7!3!)=(10x9x8x7!)/(7!3!)=(10x9x8)/3!=720/6=120 combinations without repetition.
Using the factorial function shown above, we can write a C function to handle combinations without repetition:
unsigned long long combinations_norep (unsigned long long n, unsigned long long r) {
return factorial(n)/(factorial(n-r)*factorial(r));
}
4. Combinations with repetition
Combinations with repetition is the hardest concept to wrap your head around.
Going back to our 3-digits from 10, let's begin enumerating all the combinations so we can verify the answer at the end. We start by enumerating all those that combinations that begin with a 0:
000, 001, 002, 003, 004, 005, 006, 007, 008, 009
011, 012, 013, 014, 015, 016, 017, 018, 019
022, 023, 024, 025, 026, 027, 028, 029
033, 034, 035, 036, 037, 038, 039
044, 045, 046, 047, 048, 049
055, 056, 057, 058, 059
066, 067, 068, 069
077, 078, 079
088, 089
099
Note that there is no 010 because it is a permutation of 001. Similarly with 021 which is a permutation of 012. As a result of this, each row has one less combination than the one above. Thus there are 10+9+8+7+6+5+4+3+2+1=55 combinations.
If we now enumerate all those that begin with a 1, we see a similar pattern emerges:
111, 112, 113, 114, 115, 116, 117, 118, 119
122, 123, 124, 125, 126, 127, 128, 129
133, 134, 135, 136, 137, 138, 139
144, 145, 146, 147, 148, 149
155, 156, 157, 158, 159
166, 167, 168, 169
177, 178, 179
188, 189
199
This time we have 9+8+7+6+5+4+3+2+1=45 combinations.
Following the same logic, the next section must have 8+7+6+5+4+3+2+1=36 combinations, followed by 28, 21, 15, 10, 6, 3 and finally 1. Thus there are 220 combinations in total.
The formula to work this out is quite complex, however it becomes simpler when we look at the problem in a different way. Suppose we have 10 boxes and each box holds at least 3 of the same digit. We can number these boxes 0 through 9 according to those digits. Let us also suppose that we can only move in one direction, from box 0 to box 9, and we must stop at every box along the way. This means we must make 9 transitions from one box to the next.
While we along the row, we carry a tray with 3 slots. Whenever we stop at a box (including box 0 where we start from) we can either pick a number from the box or we can move onto the next box. if we pick a number, we place it in the first slot. We can then pick another or we can move on. When we have filled all the slots, we simply move on until we reach box 9. If we reach box 9 and still have slots available, we must pick as many 9s as we need to fill the remaining slots.
It probably sounds far more complex than it really is. By imagining a selection being done this way we can create a convenient binary notation. For instance, if we say that 1 means pick a number and 0 means move onto the next box, the sequence 101010000000 would tell us we selected the combination 123 while the sequence 000000000111 tells us we selected 999. Every combination is therefore reduced to 12-bit value containing exactly three 1s and nine 0s, and it is these specific combinations we are actually looking for.
C(n,r) with repetition is formally expressed as (r+n-1)!/(r!(n-1)!)
If we plug in the actual numbers we find:
=(3+10-1)!/(3!(10-1)!)
=12!/(3!9!)
=(12x11x10x9!)/(3!9!)
=(12x11x10)/3!
=1320/(3x2x1)
=1320/6
=220 combinations with repetition.
This type of problem might be expressed in other ways. For example, how many different ways can we fill a box with 100 sweets from 30 different sweets. C(n,r) is C(30,100) thus we find:
=(100+30-1)!/(100!(30-1)!)
=129!/(100!29!)
=(129x128x127x...x101x100!)/(100!29!)
=(129x128x127x...x101)/29!
=5.3302324527079900778691094496787e+59/8,841,761,993,739,701,954,543,616,000,000
=60,284,731,216,266,553,294,577,246,880 combinations with repetition.
In C we can use the following function in conjunction with the factorial function shown earlier:
unsigned long long combinations (unsigned long long n, unsigned long long r) {
return factorial(n+r-1)/(factorial(r)*factorial(n-1));
}
We might also have similar problems with an additional restriction. For instance, we might be asked to select 100 sweets from 30 different sweets selecting at least 1 of each type. This reduces the number of slots to 100-30=70 but we have the same number of transitions, so we get:
=(70+30-1)!/(100!(30-1)!)
=99!/(70!29!)
=(99x98x97x...x71x70!)/(70!29!)
=(99x98x97x...x71)/29!
=7.7910971370578048745872324992773e+55/8,841,761,993,739,701,954,543,616,000,000
=8,811,701,946,483,283,447,189,128 combinations with repetition.
To accommodate this caveat, we can use the following function instead:
unsigned long long combinations2 (unsigned long long n, unsigned long long r) {
return factorial(n-1)/(factorial(r)*factorial(n-1));
}
What are the types of non primitive in java?
There are 8 primitive data types: byte, short, int, long, float, double, boolean, char
any other data type in Java can be considered non primitive data type.
Ex: String
String data type can also be known as the primitive data type because it is already provided by the Java language, since it is not a data type but its a class, but as we know that we can use the classes as types for the variables/instance variables, as we use while creating an object of any class, hence the classes that we create and use them as a datatype are known as non-primitive datatype....
for any query, mail me on engineer.sooraj@gmail.com or call me on +92-331-350-6956.....
What is function overriding in Java?
Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.
List types of contructors in JAVA?
Overloaded Constructors
Overloading a constructor means typing in multiple versions of the constructor, each having a different argument list, like the following examples:
class Car {
Car() { }
Car(String s) { }
}
The preceding Car class has two overloaded constructors, one that takes a string, and one with no arguments. Because there's no code in the no-arg version, it's actually identical to the default constructor the compiler supplies, but remember-since there's already a constructor in this class (the one that takes a string), the compiler won't supply a default constructor. If you want a no-arg constructor to overload the with-args version you already have, you're going to have to type it yourself, just as in the Car example.
Overloading a constructor is typically used to provide alternate ways for clients to instantiate objects of your class. For example, if a client knows the Car name, they can pass that to a Car constructor that takes a string. But if they don't know the name, the client can call the no-arg constructor and that constructor can supply a default name. Here's what it looks like:
1. public class Car {
2. String name;
3. Car(String name) {
4. this.name = name;
5. }
6.
7. Car() {
8. this(makeRandomName());
9. }
10.
11. static String makeRandomName() {
12. int x = (int) (Math.random() * 5);
13. String name = new String[] {"Ferrari", "Lamborghini",
"Rover", "Spyker",
"Lotus"}[x];
14. return name;
15. }
16.
17. public static void main (String [] args) {
18. Car a = new Car();
19. System.out.println(a.name);
20. Car b = new Car("Proton");
21. System.out.println(b.name);
22. }
23. }
Running the code four times produces this output:
% java Car
Lotus
Proton
% java Car
Ferrari
Proton
% java Car
Rover
Proton
% java Car
Ferrari
Proton
There's a lot going on in the preceding code. Figure 2-7 shows the call stack for constructor invocations when a constructor is overloaded. Take a look at the call stack, and then let's walk through the code straight from the top.
• Line 2 Declare a String instance variable name.
• Lines 3-5 Constructor that takes a String, and assigns it to instance variable name.
• Line 7. Assume every Car needs a name, but the client (calling code) might not always know what the name should be, so you'll assign a random name. The no-arg constructor generates a name by invoking the makeRandomName() method.
• Line 8 The no-arg constructor invokes its own overloaded constructor that takes a String, in effect calling it the same way it would be called if client code were doing a new to instantiate an object, passing it a String for the name. The overloaded invocation uses the keyword this, but uses it as though it were a method name, this(). So line 8 is simply calling the constructor on line 3, passing it a randomly selected String rather than a client-code chosen name.
• Line 11 Notice that the makeRandomName() method is marked static! That's because you cannot invoke an instance (in other words, nonstatic) method (or access an instance variable) until after the super constructor has run. And since the super constructor will be invoked from the constructor on line 3, rather than from the one on line 7, line 8 can use only a static method to generate the name. If we wanted all Cars not specifically named by the caller to have the same default name, say, "Ford," then line 8 could have read this("Ford"); rather than calling a method that returns a string with the randomly chosen name.
• Line 12 This doesn't have anything to do with constructors, but since we're all here to learn...it generates a random integer between 0 and 4.
• Line 13 We're creating a new String object (just a single String instance), but we want the string to be selected randomly from a list. Except we don't have the list, so we need to make it. So in that one line of code we
1. Declare a String variable, name.
2. Create a String array (anonymously-we don't assign the array itself to anything).
3. Retrieve the string at index [x] (x being the random number generated on line 12) of the newly created String array.
4. Assign the string retrieved from the array to the declared instance variable name. We could have made it much easier to read if we'd just written
5. String[] nameList = {"Ferrari", "Lamborghini", "Rover", "Spyker","Lotus"};
6. String name = nameList[x];
• Line 18 We're invoking the no-arg version of the constructor (causing a random name from the list to be passed to the other constructor).
• Line 20 We're invoking the overloaded constructor that takes a string representing the name.
The key point to get from this code example is in line 8. Rather than calling super(), we're calling this(), and this() always means a call to another constructor in the same class. OK, fine, but what happens after the call to this()? Sooner or later the super() constructor gets called, right? Yes indeed. A call to this() just means you're delaying the inevitable. Some constructor, somewhere, must make the call to super().
What is the purpose of method overriding?
Assuming class A has a method named getXXX() and class B is a sub class of class A. Now, if we write a method with the same name getXXX() in class B, with exactly the same signature as class A, it is called overriding a method.
The method getXXX() in class A becomes the overridden method.
import java.util.Scanner;
public class NumberSystem
{
public void displayConversion()
{
Scanner input = new Scanner(System.in);
System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal",
"Binary", "Octal", "Hexadecimal");
for ( int i = 1; i <= 256; i++ )
{
String binary = Integer.toBinaryString(i);
String octal = Integer.toOctalString(i);
String hexadecimal = Integer.toHexString(i);
System.out.format("%-20d%-20s%-20s%-20s\n", i,
binary, octal, hexadecimal);
}
}
// returns a string representation of the decimal number in binary
public String toBinaryString( int dec )
{
String binary = " ";
while (dec >= 1 )
{
int value = dec % 2;
binary = value + binary;
dec /= 2;
}
return binary;
}
//returns a string representation of the number in octal
public String toOctalString( int dec )
{
String octal = " ";
while ( dec >= 1 )
{
int value = dec % 8;
octal = value + octal;
dec /= 8;
}
return octal;
}
public String toHexString( int dec )
{
String hexadecimal = " ";
while ( dec >= 1 )
{
int value = dec % 16;
switch (value)
{
case 10:
hexadecimal = "A" + hexadecimal;
break;
case 11:
hexadecimal = "B" + hexadecimal;
break;
case 12:
hexadecimal = "C" + hexadecimal;
break;
case 13:
hexadecimal = "D" + hexadecimal;
break;
case 14:
hexadecimal = "E" + hexadecimal;
break;
case 15:
hexadecimal = "F" + hexadecimal;
break;
default:
hexadecimal = value + hexadecimal;
break;
}
dec /= 16;
}
return hexadecimal;
}
public static void main( String args[])
{
NumberSystem apps = new NumberSystem();
apps.displayConversion();
}
}
What does Java use instead of ASCII?
ASCII and Java are 2 totally different things. ASCII is a naming convention where a certain letter, number, or punctuation mark is a specific keyboard code (Carriage Return, CR, is code 31, Line Feed 14, Capital A 96). Java is a programming language that handles text in multiple formats as needed, Unicode, EBDIC, ASCII. The two are not intertwined.
In Which Language JVM is written?
As there are more than one JVM implementations, there is not a single language used to implement them all. Off the top of my head, I can list JVMs that are implemented in C, C++, Java (yes, a JVM in Java), Objective C, and even one in Javascript (don't ask why).
#include<iostream>
#include<time.h>
// return a reference to the largest of two objects of the same type
template<typename T>T& largest (T& a, T& b) {
return a<b?b:a;
}
int main() {
// seed the random number generator
srand ((unsigned) time (0));
// repeat 10 times
for (size_t loop=0; loop<10; ++loop)
{
// select two random integers
int x = rand()%10;
int y = rand()%10;
// print the result
if (x==y)
cout << x << " & " << y << " are equal" << endl;
else
cout << "The largest of " << x << " & " << y << " is " << largest (x,y) << endl;
} // end for loop
}
Explain all the control statements in java?
The if and switch statements are commonly referred to as decision statements. When we use decision statements in our program, we're asking the program to evaluate a given expression to determine which course of action to take. It decides the control flow of the program.
Write a Java program which create variable size array in Java?
import java.util.Scanner; class Addition
{
public void Addition()
{
Scanner s = new Scanner(System.in); int a[][] = new int[3][]; int b[][] = new int[3][]; int c[][] = new int[3][]; for(int i=0;i<3;i++)
{
a[i] = new int[3]; b[i] = new int[3];
c[i] = new int[3];
}
System.out.println("Enter the elements of var size array a"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j] = s.nextInt();
}
}
System.out.println("Enter the elements of var size array b"); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
b[i][j] = s.nextInt();
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j] = a[i][j]+b[i][j];
}
}
System.out.println("The resultant matrix is: "); for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
}
public class AdditionTest
{
public static void main(String args[])
{
Addition a = new Addition();
a.Addition();
}
}
Where you have to type a java program?
Notepad, or any other text editor. If you have the compiler installed, you can run the programs on the Command Prompt. I recommend blueJ because it does not require a compiler to already be downloaded though.
Who created c and c plus plus and java?
Dennis Ritche developed C for bell labs for the unix operating system http://en.wikipedia.org/wiki/C_(programming_language) Bjarne Stroustrup developed C++ http://en.wikipedia.org/wiki/C%2B%2B
Why in java string args is passed in main method?
The String[] args parameter is an array of Strings passed as parameters when you are running your application through command line in the OS. The java -jar command will pass your Strings update and notify to your public static void main() method.
To learn more about data science please visit- Learnbay.co
Why run time polymorphism is dynamic and compile time polymorphism is static?
The simple answer is that compile-time polymorphism occurs at compile time while runtime polymorphism occurs at runtime.
The actual answer is that compile-time polymorphism results in the compiler generating source code on your behalf while runtime polymorphism relies on function pointers or virtual methods to determine the next instruction at runtime.
Compile-time polymorphism therefore applies to template functions and classes since that is the only way the compiler can generate source code on your behalf. To achieve this, the runtime type for the template parameters must be fully-defined at compile time, even if those types have runtime polymorphic characteristics of their own.
Runtime polymorphism applies to virtual methods and function pointers, both of which can be used to dynamically alter the execution path of your program. Virtual methods are made possible through virtual tables, which are essentially just arrays of function pointers. Each runtime type that derives from a base class with virtual methods provides its own virtual table, thus the runtime type determines which specific function overrides will be invoked at runtime, even if the runtime type cannot be determined at compile time. In this way you can generalise your code to work with the base type but still get the expected polymorphic behaviour whenever a derived type is passed instead.