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

What if a final keyword is applied to a function?

The final keyword in JAVA means that the class can no longer be derived, i.e. it cannot be used as a base class for a new child class.

If you declare a method as final, this method cannot be overridden in any of the child class that may extend this class.

How do you Write a java program to find the average of given numbers?

import java.util.Scanner;

public class Numbers

{

public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

int max = -100;

int min = 100;

int sum = 0;

System.out.println("Enter ten integers");

for(int i = 0; i < 10; i++)

{

int input = scan.nextInt();

if(input > max)//test if the number entered is larger than any previous number

max = input;

if(input < min)//test if the number entered is smaller than any previous number

min = input;

sum += input;//add the input to the sum

}

System.out.println("The maximum number entered is: " + max +

"\nThe minimum number entered is: " + min +

"\nThe average of the numbers is: " + (sum / 10));//prints out the results

}

}

What does public mean in java?

The keyword public is an access specifier. A variable or a method that is declared public is publicly accessible to any member of the project. Any class or method can freely access other public methods and variables of another class.

What are the differences between procedural programming language and declarative programming language?

In procedural programming the programs are written as a list of instructions (procedures) which are written in a sequence, and where all programming is textual. In contrast, visual programming language uses graphics, animations....to manipulat programs without using texts....VLP is much easier than PP.

How do you identify key in java program?

Private keys and their associated public-key certificates are stored in password-protected databases called keystores. A keystore can hold the keys of many potential signers. Each key in the keystore can be identified by an alias which is typically the name of the signer who owns the key. The key belonging to Rita Jones might have the alias "rita"

Is java interpreter or compiler?

Java has both a compiled and an interpreted stage.

1) The programmer writes his source codes (.java extension); a compiler will compile this to bytecode (.class extension).

2) When the end-user runs the .class program, the JVM (Java Virtual Machine) will interpret this.

Command use on how to run a java program?

You execute it the same way you would on any other OS. As long as you have the Java Runtime Environment installed and the "java" executable is in your path, from the command line you would simply run: java -cp /path/to/file/here com.some.class.to.run.Here

Is the array size is fixed after it is created?

Generally, a array is fixed in size. With some libraries, however, they are extensible, either by reallocation/copying strategies (C/C++/STL), or by linking/referencing strategies (JAVA).

What is the features an organism inherits from its parents?

well it was good that you came on the website but I don't know I was looking for the answer my self

Program in java to add two numbers?

import javax.swing.JOptionPane;

public class Addition

{

public static void main( String args[] )

{

String firstNumber =

JOptionPane.showInputDialog( "Enter first integer" );

String secondNumber =

JOptionPane.showInputDialog( "Enter second integer" );

int number1 = Integer.parseInt( firstNumber );

int number2 = Integer.parseInt( secondNumber );

int sum = number1 + number2;

JOptionPane.showMessageDialog( null, "The sum is " + sum,

"Sum of Two Integers", JOptionPane.PLAIN_MESSAGE );

}

}

What is the use of volatile keyword in java?

Transient keyword is used by java programmers for variables, if the programmer does not want to store them permanently.

In case of serialization, the fields which are kept transient, are not the part of serialization process and not serialized.

Volatile is used along with the variable name, that can change its value without informing the JVM.

The system clock values are stored in Volatile variable.

They do not inform JVM when they change their value.

Why do global variables make a program difficult to debug?

The term variables imply that those things may be changed or assigned with new value (at numerous places). This make the program harder to debug because it will be difficult to know when and where a global variable being changed and what was the consequence of that value changed.

What causes of overloading?

There could be many different causes of overloading any motor.

Here is a short list to get you started:

  • the motor's rotor bearings are seizing-up due to mechanical damage or lack of grease or oil

  • the load machinery or equipment which the motor is intended to drive has moving parts which are seizing-up due to mechanical damage or lack of grease or oil

  • more output power is being demanded of the motor than it was designed to supply - for instance too big a load of clothes has been put into the drum of a washing machine...

What is an interface class and what is an abstract class?

The term interface class does not exist in C#. If this is a term describing a class being an interface to other component (human, subsystems, etc), it is very application specific. The designer of that application should know the abstraction.

However, C# does have another type called interface. An interface is NOT a class. An interface defines the intention of some behaviors that classes may be extended it and provides the implementation. The intention, is nothing but method signatures, which defines the return data type, the method name, and any method arguments and associated data type. The implementation is the code of the method. Interface is used for separating the concern of design and implementation.

Abstract class is a class with abstract keyword. It can be just like a class without that keyword (then, why it is an abstract class?). But it may have some methods or properties defined as abstract. These abstract methods, like the method signatures of an interface, defines the intention.

The subclasses of such an abstract class would need to implement those abstract methods (providing the code).

There are more common, differences between interfaces and abstract classes, please see answer(s) of those related questions in C# category.

How do you invoke the abstract methods?

You cannot invoke abstract methods directly. An abstract method looks like below:

public String getName() {}

It has no code inside it and can do nothing. You cannot invoke it directly. If you want to call this method then - we must extend the class that contains this method inside our class and then provide an implementation for this method and then invoke it:

Ex:

public String getName() {

return "Anand";

}

Once you place this code inside your class, then you can invoke it anytime you want by calling the method "getName()"

What is parent class in java?

Object Class is the parent class of all classes in java.

Every class in the Java system is a descendant (direct or indirect) of the Object class.

What does uncaught exception javalang error mean in a Blackberry curve 9320 and how do you fix it?

The solution to this problem is simple...

You should go to Options > Advance Options (Applications) > BB Key > Edit Permisions > Set to default.

The error will go away, BUT, you will lose your previous app permissionss and they won't have access to specific functions.

Can a class in java extend both abstract class and concrete class?

An Abstract class is similar to an interface. You cannot instantiate them, but you can extend them. Any class that extends the abstract class has to provide the implementation to the abstract methods. Hence these classes can be used as a skeleton to similar classes where some common functionality may be required. Such functionality can also be embedded into these classes. Unlike interfaces, abstract classes can have method code also. So they are very useful.

Write a Java program to count 1 to 1000 numbers using 10 threads?

// need a class to do customized counting
// (this can be a local class)
final class CounterThread extends Thread {


private final int rangeStart;
private final int rangeEnd;
private final int[] nums;
private int count;

private CounterThread(final int rangeStart, final int rangeEnd, final int[] nums) {
super();

this.rangeStart = rangeStart;
this.rangeEnd = rangeEnd;
this.nums = nums;

count = 0;

}

public void run() {
// actual counting process
for (int i = rangeStart; i <= rangeEnd; ++i) {
count += nums[i];

}

}

}

// define the number of ints we want to add up
final int ARRAY_SIZE = 1000;
// define the number of threads to use
final int NUM_THREADS = 10;
// define the number of ints each thread will add
final int NUMS_PER_ARRAY = ARRAY_SIZE / NUM_THREADS;

// NOTE: if ARRAY_SIZE is not evenly divisible by NUM_THREADS, then the
// code will need some tweaking to get the correct answer

int[] nums = new int[ARRAY_SIZE]; // assuming this is filled with numbers to count

// create our array of worker threads
CounterThread[] threads = new CounterThread[NUM_THREADS];
for (int i = 0; i < threads.length; ++i) {

// create the new thread which will count the given range
threads[i] = new CounterThread((i * NUMS_PER_ARRAY), (((i + 1) * NUMS_PER_ARRAY) - 1), nums);
// start the thread
threads[i].start();

}

// wait for all threads to finish
for (int i = 0; i < threads.length; ++i) {

threads[i].join();

}

// add up the individual counts from each thread
int totalCount = 0;
for (int i = 0; i < threads.length; ++i) {

totalCount += threads[i].count;

}

System.out.println(totalCount);

Write a program to print all the ASCII values and their equivalent characters using a while loop?

#include<stdio.h>

#include<conio.h>

void main()

{

int a=1;

while(a<=255)

{

printf("%d=%c",a,a );

a++;

}

getch();

}

Can you unistall java?

I don't know how it is done in Windows, but in Linux based systems you can remove Java using the "apt-get remove" command.

Is java dangerous?

Java itself is 100% safe to have installed on your computer. Programs written in Java, however, may still harm your computer. Always look for user reviews before running ANY unknown program on your computer.

Is overloading done at compile time or run time?

Overloading will be done at compile time itself.

Proof: If you try to narrow down the access modifier for a method from public in the parent class to private in the child class while overloading, the compiler will not let you do it.

What is vector and array processing?

An important part of IDL is the ability to work with data that is organized as vectors and arrays. We will first describe vectors and arrays and then show some tools for constructing them. Finally, we will demonstrate some of their uses. In addition to arrays of numbers, which we will describe here, there are also arrays of strings, structures and objects. You can look up information about them in IDL help if you are eager to find out about them now.

Arrays are especially important in signal image processing. We will find that images are just arrays of values with one value for each pixel. Vectors are important in the representation of one-dimensional functions such as functions of time or one spatial dimension.

Vectors

A vector is a list of data items, and can be constructed with an assignment statement like

A=[1.2, 3.1, 4.6, 5.7]

Note that square brackets are used to contain the list. Try typing a list like that shown above and then using HELP,A to get information about the vector.

Vectors can contain any of the number types that are accepted by IDL. When the assignment is made all of the numbers are converted to one type. The type that is used is determined by the data type hierarchy. Try typing

A=[1, 3B, 4L, 5.7] & HELP,A

IDL responds that A is an array of type FLOAT of length 4. If you were to type

A=[1, 3B, 4L, 5] & HELP,A

you would find that A is now of type LONG. The rule is to convert all of the numbers to the highest number type.

Arrays

We can think of a vector as a list that has one dimension. It is a row of data. An array is a list that is arranged in multiple dimensions. A two-dimensional array is a vector of vectors that are all of the same length.

C=[[1,2,3,4],[5,6,7,8],[7,6,5,4]] & PRINT,C

1 2 3 4

5 6 7 8

7 6 5 4

We see that A has three rows and four columns and that each row is one of the vectors in the list. An array can contain any of the IDL data types. When it is created all of the numbers are converted to the highest number type. We will describe a number of IDL functions for the construction of arrays. By using these functions one can construct arrays of up to eight dimensions.

Indexing

Column and Row Index

It is important to be able to refer to particular elements of an array. An element in column 1 and row 2 can be referred to by C[1,2]. In the above array we would find that C[1,2]=6. In IDL the column index comes before the row index.

Rows and columns are indexed beginning with the number 0. The column indexes of C are 0, 1, 2, 3 and the row indexes are 0, 1, 2. Zero-referenced indexing is a convention that may seem strange at first, but it has programming advantages.

One can refer to an entire row by using an asterisk in the column position. C[*,r] refers to the vector in row r. Try the command PRINT,C[*,2].

One can refer to an entire column by using an asterisk in the row position. C[col,*] refers to the vector in column col. Try the command PRINT,C[3,*].

A part of a row or column can be indexed by providing a list of the elements that are wanted. Try the following

row =[0,1,2] & col = [3,2,1] & PRINT,C[col,row]

Can you explain what is printed?

A contiguous segment of column or row elements can be referred to with the notation a:b. For example, C[0:2,2] refers to the elements in positions 0,1,2 in row 2. Try the command PRINT,C[0:2,2]

Array Index

Every element in an array has an array index. The array index is found by counting each element from the beginning of the array to the end row by row. An array that has four columns and three rows would have the indexes as shown below.

0 1 2 3

4 5 6 7

8 9 10 11

Any element in the array can be referred to by its array index. A list of elements in positions 0, 6 and 8 can be printed by

k=[0,6,8] & C[k]

One can find the value of array index from the row and column indexes. The number of columns, n, has to be known. Then k is simply

k = col + row*n

If k is given then the column and row indexes can be found by integer operations:

row = k/n

col = k - row*n

For an array with n=4 columns, C[6]=C[2,1]. You should try converting in both directions.

Array Creation Tools

IDL provides a number of functions that can be used to create arrays and initialize their values. The list can be found in the section "Array Creation Routines" in IDL Online Help. A subset of these routines are listed below.

BINDGEN Return a byte array with each element set to its subscript.

BYTARRCreate a byte vector or array.

FINDGEN Return a floating-point array with each element set to its subscript.

FLTARRReturn a floating-point vector or array.

INDGEN Return an integer array with each element set to its subscript.

INTARRReturn an integer vector or array.

LINDGEN Return a longword integer array with each element set to its subscript.

LONARRReturn a longword integer vector or array.

IDENTITY Return an identity array.

MAKE_ARRAYGeneral purpose array creation.

REPLICATEForm array of given dimensions filled with a value.

The functions BYTARR, FLTARR, INTARR, LONARR create arrays with of the type indicated by the first letter of the name and with their initial values set to zero. You can then enter data into them by assignment statements. For example, create an integer array of size 3x4 and set the elements of the first row to the value 3.

A=INDGEN(3,4)

A[*,0]=3

PRINT,A

The functions BINDGEN, INDGEN, FINDGEN, LINDGEN create arrays of the type indicated by the first letter of the name and with the initial values set to the array index. For example, create an array of type byte of size 3x4 and set its initial value to the index.

B=BINDGEN(3,4)

PRINT,B

0 1 2

3 4 5

6 7 8

9 10 11

The functions that generate index arrays are very useful in creating a vector of independent variables to be used in calculating and plotting a function. Suppose that you needed to plot the function (1+t2)Cos(5t) over the interval [0,3] in steps of size 0.01. You will then need a vector of 301 values t=[0, .01, .02, ..., 2.99, 3]. You certainly would not want to type all this in. The statements that will do the calculation and plot a graph are given below.

t=FINDGEN(301)/100 ;Generates the desired vector

f=(1+t^2)*cos(5*t) ;Calculates the function

plot,t,f ;Draws the graph

The IDENTITY function returns a square array of type float with the elements on the main diagonal set to 1 and all others set to zero. This function is very useful in many linear algebra computations.

I=IDENTITY(4)

PRINT,I

The REPLICATE function makes an array of the specified size in which a give value placed in all of the positions. To make a 5x2 array in which all of the elements contain the value 3.1 one can use

C=REPLICATE(3.1,5,2)

PRINT,C

The function MAKE_ARRAY is the most general array creation tool. It has many options which are best described by the online help documentation and the reference manual.