Java Queries Here..

Piyush

Lanaya
are the both following codes do same work?

Code:
dog[] pets;
pets= new dog[7];

Code:
dog[] pets=new dog[7];

also
I need a good description about get and set methods
 

Piyush

Lanaya
@garbage
thanks ....will go through the article asap

one more query

consider the following
code1:
Code:
dog d1=new dog();
code2:
Code:
dog[] d=new dog[7];
dog[0]=new dog();
dog[1]=new dog();
//...and so on

what is happening in the 2 code snippets?
AFAIK
In code 1:we have created a new dog object and named it d1

In code 2:we have created an array of dog objects and assigned them the place in dog array

Is it true?Am I missing anything else?And is there any fault in the concept?
 

Garbage

God of Mistakes...
Code:
dog[] d=new dog[7];
dog[0]=new dog();
dog[1]=new dog();
//...and so on
This is wrong.

The correct code is:
Code:
dog[] d=new dog[7];
[B]d[0][/B]=new dog();
[B]d[1][/B]=new dog();
//...and so on
Other than that, you are absolutely right.

In first code, you can always access the object of Dog directly. However in second code, You can't access any Dog object directly (as you don't have their reference in a separate variable). So you will have to access the array first and then access the object (using it's index).

If you are confused by above explanation, consider, you have a bark() method in Dog class.
Now in first code, you can invoke the bark() using:
Code:
d1.bark();

while in second code, lets assume you want to invoke bark() on second object of Dog, you have to use following:
Code:
d[0].bark();
 

Piyush

Lanaya
thanks garbage
I wanted to ask something on interfaces and polymorphism
will post as soon as I get an appropriate code
 

Piyush

Lanaya
Code:
[B]class animal[/B]
{
eat();
sleep();
noise();
}

[B]class dog extends animal[/B]
{
eat()
{
//statements
}
noise()
{
//statements
}
}

[B]class lion extends animal[/B]
{
eat()
{
//statements
}
noise()
{
//statements
}
}

[B]class hippo extends animal[/B]
{
eat()
{
//statements
}
noise()
{
//statements
}
}
[B]class vet(animal a)[/B]
{
cure()
{
//statements
}
}

[B]class wildlife[/B]
{
public static void main(String[] args)
{
dog d=new dog();
lion l=new lion();
hippo h=new hippo;
vet v=new vet();
[COLOR="Red"]//what is happening after this line?[/COLOR]
v.cure(d);
v.cure(h);
}
}

and is the above code an example of polymorphism?If yes,how?
 

Prime_Coder

I'm a Wannabe Hacker
My suggestion would be the book: Core Java. Volume I, Fundamentals by Cay S. Horstmann & Gary Cornell.
Read the next volume after that.
Simultaneously looking at tutorials on the web will also help a lot.
 

Neo

.
should i learn it from an institute or i can do this at home?

PS:I know only C++,and that too not properly. Will that do?
 

Garbage

God of Mistakes...
should i learn it from an institute or i can do this at home?

PS:I know only C++,and that too not properly. Will that do?

I don't think you need to go to any institution to learn Java. Only thing you MUST do to learn Java at home is to WRITE LOTS OF CODE! :)
 

Prime_Coder

I'm a Wannabe Hacker
should i learn it from an institute or i can do this at home?

PS:I know only C++,and that too not properly. Will that do?

You can learn it from home, very well.

Don't get bothered about C++ knowledge. If you know OOP concepts from C++, it will be advantage for you(or easy, you can say).
 

dashing.sujay

Moving
Staff member
You can learn it from home, very well.

Don't get bothered about C++ knowledge. If you know OOP concepts from C++, it will be advantage for you(or easy, you can say).

PS:I know only C++,and that too not properly. Will that do?

Well if anybody doesn't knows C++ well, you can't expect him to have clear concepts of OOP either. And if anybody has confused concepts of OOP, java will be a tough task for him as Java has much better implication of OOP than C++.
 

achuthan1988

Broken In
I learnt a lot of java from head first java.It is one of those of books which keeps you interested and relaxed in the topics.It looks like a casual book but does explain stuff very well.I bought it on flipkart...
 

masterkd

Padawan
head first java is only for beginners. if you really want to know java study SCJP by Kathy Sierra..that's the best I can find but its not for beginners!!
 

Ksquare

Right off the assembly line
I have written a code to find the 10001st prime number(Project Euler, problem 7). But it refuses to work. Can someone please help?

Code:
class testing
{
 public static int testprime(int num)
 {
  int flag=0;
  for(int i=1;i<=num;i++)
     {
      if(num%i==0)
        {
         flag++;
        }
	 }   	
      if(flag>2)
         return 0;
      else
         return 1;
 }		 
 public static void main(String args[])
 {
  int i,n=0,flag=0,t,x;
  int p=2;
  while(n<=10001)
       {
	    x=n;
        t=testprime(p);
        if(t==1)
          {
           p++;
           n++;
          }
        else
          {
		   p++;
		   n=x;
		  } 
       }	
 }
}
 

Nisha Gupta

Right off the assembly line
Re: programming

wap to display in java based on the user input string where the input string is LAKSHMI
L
A A
K K K
S S S S
H H H
M M
I
 

Garbage

God of Mistakes...
Re: programming

wap to display in java based on the user input string where the input string is LAKSHMI
L
A A
K K K
S S S S
H H H
M M
I

First, you can post these programs in this thread:
*www.thinkdigit.com/forum/programming/71499-post-ur-java-queries-here.html


Secondly, please attempt to write program yourself. If it has some problems, then post it here so that we can suggest corrections. Don't expect members to write whole program for you without you trying to write it first. This is for your own good. :)
 
Top Bottom