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

Conditional statement inside a conditional loop?

int i = 100;

while(i > 0) { // Conditional loop

--i;

if((i % 2) == 0) { // Conditional statement inside a conditional loop

System.out.println(i + " is even.");

}

}

Write a program to find the number of and sum of and sum of all integer greater than 100 and less than 200 that are divisible by 7?

This is a homework assignment. write your homework assignments for you because you have been given them so that you learn the skills you will need in life.

However, if while doing this assignment you have specific things you need help to understand WikiAnswers will be happy to answer these specific questions for you.

How do you make struts action class thread safe?

1.Only Use Local Variables

§The most important principle that aids in thread-safe coding is to use only local variables, not instance variables , in your Action class. Local variables are created on a stack that is assigned (by your JVM) to each request thread, so there is no need to worry about sharing them. An Action can be factored into several local methods, so long as all variables needed are passed as method parameters. This assures thread safety, as the JVM handles such variables internally using the call stack which is associated with a single Thread.

2.Conserve Resources

As a general rule, allocating scarce resources and keeping them across requests from the same user (in the user's session) can cause scalability problems. For example, if your application uses JDBC and you allocate a separate JDBC connection for every user, you are probably going to run in some scalability issues when your site suddenly shows up on Slashdot. You should strive to use pools and release resources (such as database connections) prior to forwarding

Why java is a strictly type language?

C is not a strongly-typed language, it is weakly-typed. This means that it is possible to work around the type system. That is, types can be explicitly or implicitly converted to other types. A strongly-typed language simply wouldn't allow this. However, C is statically-typed, which means that values are bound to types at compile time, rather than at runtime as they are in dynamically-typed languages.

Does Every class has a toString method and an equals method inherited from the Object class?

Yes - in Java, every class has this method, which is inherited from the Object class. Often, the inherited method does nothing particularly useful, but you can override it with your own implementation.

Use the in operator to find the number and name of each customer that placed an order on October 23 2010?

select customer.customer_num, customer_name from customer, orders where orders.order_date in ('23-OCT-10');

What is the implication of catching all the exceptions with the type Exception?

though catching exceptions is a good practice inside java code, catching all exceptions of the type exception is not the best way to go. Specific exceptions need to be caught instead of the generic Exception being caught. Also, different types of exceptions need to be handled separately.

C code to implement the the first fit best fit worst fit algorithm?

#include<stdio.h>

#include<malloc.h>

/*

filename:firstfit.c

description:this program implements first fit algorithm

Author:Anupam Biswas,NIT Allahabad,India

Date:29.08.2011

*/

#define maxp 10 //maxp:maximum no of processes

#define maxmsz 100 //maxmsz:maximum memory size

struct memory{

int bsz;//bsz:block size

int bs;//bs:block start

int be;//be:block end

int ap;//ap:allocated process

int flag;//for allocated or not

struct memory*link;

}*start,*trav,*prev;

int pq[maxp];//pq:process queue

int f=-1,r=-1;

void enqueue(int ps)

{

/*ps:process size*/

if((r+1)%maxp==f)

{

printf("\nqueue is full, no new process can be loaded");

}

else

{

r=(r+1)%maxp;

pq[r]=ps;

}

}

int dequeue()

{

int ps;

if(f==r)

{

printf("queue empty,no process left\n");

return -1;

}

else

{

f=(f+1)%maxp;

ps=pq[f];

return ps;

}

}

void first_fit(int ps,int pid)

{

/*pid:process id*/

struct memory*block;

block=(struct memory*)malloc(sizeof(struct memory));

trav=start;

/*search the memory block large enough*/

while(trav!=NULL)

{

if(trav->flag==0 && trav->bsz>=ps)

{

break;

}

prev=trav;

trav=trav->link;

}

if(trav==NULL)

{

printf("No large enough block is found\n");

}

else /*if enough memory is found*/

{

dequeue();//remove process from queue

block->bsz=ps;

block->bs=trav->bs;

block->be=trav->bs+ps-1; //anus size defininition

block->ap=pid;

block->flag=1;

block->link=trav;

trav->bsz=trav->bsz-ps;

trav->bs=block->be+1;

if(trav==start)/*if first block is large enough*/

{

start=block;

}

else

{

prev->link=block;

}

}

}

void dealocate_memory(int pid)

{

trav=start;

while(trav!=NULL && trav->ap!=pid)

{

trav=trav->link;

}

if(trav==NULL)

{

printf("This process is not in memory at all\n");

}

else

{

if(trav->link->flag==0)

{

trav->bsz=trav->bsz+trav->link->bsz; //anus link definition

trav->be=trav->link->be;

prev=trav->link;

trav->link=prev->link;

prev->link=NULL;

free(prev);

}

trav->ap=-1;

trav->flag=0;

}

}

void show_mem_status()

{

int c=1,free=0,aloc=0;

trav=start;

printf("Memory Status is::\n");

printf("Block BSZ BS BE Flag process\n");

while(trav!=NULL)

{

printf("%d %d %d %d %d %d\n",c++,trav->bsz,trav->bs,trav->be,trav->flag,trav->ap);

if(trav->flag==0)

{

free=free+trav->bsz;

}

else

{

aloc=aloc+trav->bsz;

}

trav=trav->link;

}

printf("Free memory= %d\n",free);

printf("Allocated memory= %d\n",aloc);

}

int main()

{

int ch,ps;

char cc;

/*the size of the total memory size is defined i.e. largest block or hole*/

/*......................................................................*/

start=(struct memory*)malloc(sizeof(struct memory));

start->bsz=maxmsz;

start->bs=0;

start->be=maxmsz;

start->ap=-1;

start->flag=0;

start->link=NULL;

/*......................................................................*/

while(1)

{

printf("\n\t1. Enter process in queue\n");

printf("\t2. Allocate memory to process from queue\n");

printf("\t3. Show memory staus\n");

printf("\t4. Deallocate memory of processor\n");

printf("\t5. Exit\n");

printf("Enter your choice: ");

scanf("%d",&ch);

switch(ch)

{

case 1:

do

{

printf("Enter the size of the process: ");

scanf("%d",&ps);

enqueue(ps);

printf("Do you want to enter another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 2:

do

{

ps=pq[f+1];

if(ps!=-1)

{

first_fit(ps,f+1);

show_mem_status();

}

printf("Do you want to allocate mem to another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 3:

show_mem_status();

break;

case 4:

do

{

printf("Enter the process Id: ");

scanf("%d",&ps);

dealocate_memory(ps);

show_mem_status();

printf("Do you want to enter another process(y/n)?: ");

scanf("%c",&cc);

scanf("%c",&cc);

}

while(cc=='y');

break;

case 5:

break;

default:

printf("\nEnter valid choice!!\n");

}

if(ch==5)

break;

}

}

Write a program to demonstrate constructor overloading?

Constructor overloading is similar to method overloading. You can overload constructors by changing the parameter list in the class definition.

Here is an example

public class Box{

private int height;

private int width;

private int breadth;

// First constructor with no parameters

Box(){

height = 0;

width = 0;

breadth = 0;

}

// This is the second overloaded constructor

Box(int h,int w,int b){

height = h;

width = w;

breadth = h;

}

public void display(){

System.out.println("Height: "+height+" Width: "+width+" Breadth: "+breadth);

}

public static void main(String args[]){

Box obj = new Box(1,2,3);

obj.display();

}

}

Who created the java?

James Gosling and group of Sun Engineers...

How do you resolve name-space collision?

Import only one of the packages containing the classes with the same name. Use the other class by typing out its full namespace.

How do you do write an annotation?

You take the document you are annotating and add your own notes that further explain the document using critical thinking.

What is contain inside the jar file?

.jar files contain byte-code compiled to run on the Java Virtual Machine.

How do you find the HCF of all the numbers in an array java?

I guess you mean the highest common factor. Use a loop. Start assuming that the hcf is the first number in the array. Call this "result". Find the hcf of "result" and the second element, assign this to "result". Find the hcf of "result" and the third element, and copy this back to result, etc.

When does an object get destroyed in programming?

I agree with @Hilmar. You can never predict when exactly the object will be destroyed. But, you can definitely predict when an object is ready to be destroyed.

Difference between the two can be easily explained using an example (I'm using JAVA here):

Employee emp = new Employee("Dexter");

This statement creates an object of Employee (with name Dexter) which resides in the heap.

As long as you are using the reference "emp" for this object , the object will stay there. Its indestructible. Garbage Collector cannot even touch it.

Now lets say, after a few lines of code, you are executing:

emp = new Employee("RITA");

This creates a new object of Employee (with name RITA), but notice that the reference "emp" no longer points to our old object with the name Dexter. Hence the object of Employee with the name Dexter has no references associated with it.

And this makes it eligible for the Garbage Collection, and whenever the next round of Garbage Collection happens, the mighty GarbageCollector will not leave this chance to eat up the Employee (with the name Dexter).