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

Why is a keyword class used before a java program?

Because you are creating a class - a class in the sense of OOP.

Why potentiometer method is an accurate method for measuring potential difference?

Say 'emf' instead potential difference.

Because while measuring the same no current is drawn and just balancing the potential across with the potential drop on the balancing length of the potentiometer wire. While balancing the galvanometer shows null deflection. So no current flows through the galvanometer. A perfect balance with the potential difference (EMF) of a cell with potential drop across that particular length.

How do write a function that receives two pointers to character strings where the function concatenates the two strings and prints the new concatenated string?

#include <iostream>

#include <string>

std::string* concat_print_strings(std::string* pStr1, std::string* pStr2 )

{

std::string * strResult = new std::string( *pStr1 );

strResult->append( *pStr2 );

std::cout << strResult->c_str() << std::endl;

return( strResult ); }

int main()

{

std::string str1 = "This is a string.";

std::string str2 = " And this is another string.";

std::string* pStr = concat_print_strings( &str1, &str2 );

delete( pStr );

pStr = NULL;

return( 0 ); }

When you overload a Java method you write multiple methods?

Yes. The definition of function overloading is multiple methods with the same name, but different numbers of arguments and return types.

static int getArea(int height, int width) {

return height * width;

}

static double getArea(double height, double width) {

return height * width;

}

How can you put JavaME on your cellphone How can I use java softwares on a phone without the Java platform...?

You need the Java run-time (JRE, that is the Java Virtual Machine (JVM) and Java run-time libraries to run Java software. Most mobile phones support Java2 Micro Edition (J2ME). To run J2ME application you need one of these.

Most phones install J2ME applications in their Games folder when their web browser is pointed to a link to the application on the web. J2ME applications often consist of 2 files, ending with .jad and .jar extensions. You need a web server (a site if you want) and you have to place these 2 files on the site. Than in the phone's web browser write the link to the files as found on your server. The phone will then ask you if you want to install the application.

Some better-quality mobile devices also support transferring and installing J2ME applications using bluetooth, but most of them will refuze any .jad/.jar files received on bluetooth for being unsecure.

Java program convert the inputed words into its upper case or lower case form?

There are methods in the String class; toUppercase() and toLowerCase().

i.e.

String input = "Hello!";

String upper = input.toUpperCase(); //stores "HELLO!"

String lower = input.toLowerCase(); //stores "hello!"

-Note: these methods are NOT modifier methods therefore the original string is still "Hello!"

How do you limit object creation in java?

Hi, the answer for this question is as follows....:

class Demo

{

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

{

Test1 d = new Test1();

Test1 d1 = new Test1();

boolean b = d1.equals(d);

Test1 d2 = new Test1();

System.out.println("" + b);

}

}

class Test1

{

static int count=0;

public Test1() throws Exception

{

try

{

if(count > 1)

{

throw new Exception();

}

else

System.out.println("Hi");

count=count + 1;

}

catch(Exception e){System.out.println("Can't have more than two instances");}

}

}

regards,

Amit

What are string delimiters?

That refers to any character or group of characters used to separate different parts in a string. It might be spaces, commas, semicolons, tabs, or some other symbol.

What are java servelets?

When the internet/web was young... there was the webserver. But it didn't do much except fulfill requests for files. Mostly images and .html documents, and such.

However, someone noticed that a C program has stdin, stdout and stderr streams... So they made a file extension called .cgi (Common Gateway Interface) and told the webserver that when a request was made to this .cgi file... that it shouldn't SEND a .cgi file to the requestor... instead it should RUN the file as a program, get the program's output and sent THAT output to the browser.

Thus web apps were born.

Browser Request -> Webserver -> Stdin -> C program... ->>>

Stdout -> Webserver -> Response to user's browser.

Sun Microsystems wanted to use java for web apps. So, they made tiny classes called "Servlets" (mini-Server programs) to run instead of the C programs/Perl programs everyone else was using.

Servlets work like:

Browser Request -> Webserver -> Java -> Stdin -> java Servlet Class.. ->>>

Stdout -> Webserver -> Response to user's browser.

That's pretty much is. A Java Servlet is a program that accepts data, does some processing and then spits out results that a webbrowser would understand.

Hope this answers your question.

Draw a flowchart that will add all integers from 1 to 50?

A Sample java method that can do this sum of all numbers between 1 to 50

public int sumNumbers(){

int retVal = 0;

for(int i = 0; i <=50; i++){

retVal = retVal + i;

}

return retVal;

}

What would happen if you deleted java from my computer?

You would not be able to run JAVA code in your computer until you re-install the JVM and JDK in your machine.

How do you add texture in buttons in java?

If you are using the Button class, you can't. java.awt.Button only allows for text to be displayed. However, if you are using the javax.swing.JButton class you can make a call to JButton.setIcon(Icon) to add an image onto your button.

Example:

// Let's create a new button

JButton button = new JButton();

// Load the image in img.gif (this image may be a GIF, JPG, or PNG)

ImageIcon icon = new ImageIcon("img.gif");

// Add the image to the button

button.setIcon(icon);

In java why method defined in super class need not to defined in subclass?

In Java, or in any object oriented language such as C++, a method defined in super (parent) class does not need to be defined in a subclass, because that is the primary purpose of inheritance.

Object oriented programming allows you to define and declare a class that implements the behavior for an object. Inheritance allows you to refine, or subclass, that class by "reusing" all of the functionality of the parent class into the sub class, adding additional definition and declaration for the sub class.

If the subclass needs to change a parent class method, it can overload that method. This is called abstraction.

What is meant by string sorting in java?

string sorting means sorting the string array in a specific order

an example is given below

import java.io.*;

public class stringArray

{public static void main(String args[])throws IOException

{String A[]=new String[10];int i=0,j=0;String tmp;

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in=new BufferedReader(read);

System.out.println("enter the names");

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

A[i]=(in.readLine());

for(i=1;i<10;i++)

{

for(j=0;j<10-i;j++)

{

if(A[j].compareTo(A[j+1])>0)

{tmp=A[j];

A[j]=A[j+1];

A[j+1]=tmp;

}

}

}

System.out.println("the sorted array is ");

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

System.out.println(A[i]);

}

}

if the input is:

enter the names

kurian

avinash

thomson

satheesh

rahul

rohir

anand

basil

glen

james

the output will be :

the sorted array is

anand

avinash

basil

glen

james

kurian

rahul

rohir

satheesh

thomson

Longest common subsequence problem program in c?

#include<stdio.h>

#include<string.h>

int max(int a,int b)

{

return a>b?a:b;

}//end max()

int main()

{

char a[]="xyxxzxyzxy";

char b[]="zxzyyzxxyxxz";

int n = strlen(a);

int m = strlen(b);

int i,j;

for(i=n;i>=1;i--)

a[i] = a[i-1];

for(i=m;i>=1;i--)

b[i] = b[i-1];

int l[n+1][m+1];

printf("\n\t");

for(i=0;i<=n;i++)

{

for(j=0;j<=m;j++)

{

if(i==0 j==0)

l[i][j]=0;

else if(a[i] == b[j] )

l[i][j] = l[i-1][j-1] + 1;

else

l[i][j] = max(l[i][j-1],l[i-1][j]);

printf("%d |",l[i][j]);

}

printf("\n\t");

}

printf("Length of Longest Common Subsequence = %d\n",l[n][m]);

return 0;

}

What will be if we Write a program in java to accept the amount of money to be withdrawn and find out the number of notes of each denomination to be given by the bank?

Use a recursive function. Assume the denominations are placed in a vector of type double (highest denomination first):

// returns the highest denomination not greater than value

const double get_largest_denom (const double& value) {

const std::vector<double> denoms {100.0, 50.0, 20.0, 10.0, 5.0, 2.0, 1.0, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01};

for (auto const i : denoms) if (*i <= value) return *i;

return 0.0; // rare case!

}

void print_amounts (double value) {

if (value<=0.0) return;

double denom {get_largest_denom (value); }

unsigned count {(unsigned) (value / denom)};

double subtotal {(double) count * denom};

std::cout << count << " x " << denom " = " << subtotal << std::endl;

print_amounts (value - subtotal);

}

int main (void) {

double value {};

std::cout << "Enter the amount (#.##): ";

std::cin >> &value;

print_amounts (value);

}

Example output:

Enter the amount (#.##): 1234.56

12 x 100.0 = 1200.0

3 x 10.0 = 30.0

4 x 1.0 = 4.0

1 x 0.5 = 0.5

1 x 0.05 = 0.05

1 x 0.01 = 0.01

What is the AspectJ Project and what makes it important to society?

The AspectJ Project is an ACP programming extension for PARC for the Java language. It is important to society for developing innovative Java applications. You can learn more about AspectJ at the Wikipedia.

What is java ide?

Java Integrated Development Environment (IDE) provides an environment to Edit, compile and debug and generate java code

There are several java IDE like Eclipse, WSAD, BlueJ, JCreator etc

File Upload into Database in JSP?

<%@page language="java" session="true"

import="java.io.*,java.util.*,java.io.*,java.sql.*,javax.servlet.*"%>

<%

//to get the content type information from JSP Request Header

String contentType = request.getContentType();

if (contentType != null && contentType.indexOf("multipart/form-data") >= 0)

{

DataInputStream in = new DataInputStream(request.getInputStream());

//we are taking the length of Content type data

int formDataLength = request.getContentLength();

byte dataBytes[] = new byte[formDataLength];

int byteRead = 0;

int totalBytesRead = 0;

//this loop converting the uploaded file into byte code

while (totalBytesRead < formDataLength)

{

byteRead = in.read(dataBytes, totalBytesRead, formDataLength);

totalBytesRead += byteRead;

}

String file = new String(dataBytes);

//for saving the file name

String saveFile = file.substring(file.indexOf("filename="") + 10);

saveFile = saveFile.substring(0, saveFile.indexOf("\n"));

saveFile = saveFile.substring(saveFile.lastIndexOf("") + 1,saveFile.indexOf("""));

int lastIndex = contentType.lastIndexOf("=");

String boundary = contentType.substring(lastIndex + 1);

int pos;

//extracting the index of file

pos = file.indexOf("filename="");

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

pos = file.indexOf("\n", pos) + 1;

int boundaryLocation = file.indexOf(boundary, pos) - 4;

int startPos = file.substring(0, pos).getBytes().length;

int endPos = file.substring(0, boundaryLocation).getBytes().length;

// creating a new file with the same name and writing the content in new file

//FileOutputStream fileOut = new FileOutputStream(saveFile);

FileOutputStream fileOut = new FileOutputStream("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\incidentreportform_main\\upload"+saveFile+"");

fileOut.write(dataBytes, startPos, (endPos - startPos));

fileOut.flush();

fileOut.close();

//out.println(saveFile);

Connection con=null,con1=null;

Statement stmt=null,stmt1=null;

PreparedStatement ps=null,ps1=null;

ResultSet rs=null,rs1=null;

String sql="",sql1="",a="";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

con=DriverManager.getConnection("jdbc:odbc:inficert","inficert","inficert");

stmt=con.createStatement();

String sno=request.getParameter("parameter");

int j1=0;

try

{

sql="UPDATE department set crftitle =('"+saveFile+"') where sno='"+sno+"'";

j1=stmt.executeUpdate(sql);

if(j1!=0)

{

%>

<script type="text/javascript">

alert('Successfully Uploaded');

top.location.href = "selectlocation.jsp";

</script>

<%

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

%>

What is marshal streams?

"Marshalling" is roughly the same as "serialization", a mechanism many languages use to take classes, objects and values from a running program and convert them into a (normally binary) format for persistence (write to file or database) or for sending over a network.

A stream is a computer science concept meaning that bits are transmitted between a sender and a receiver one chunk at a time, like when sending a file over a network or writing to the hard drive.

A marshal stream would be a stream with marshalling data.

Marshalling isn't used in Javascript. The JSON serialization format can be considered a similar concept, but JSON differs from marshalling in the sense that JSON is only meant for serializing data, while marshalling in an object oriented language would also transmit the class definition and object methods.

Marshalling isn't really required in Javascript, as there is no compilation stage, and you can get the same effect by using eval() on a plain string representation.