Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i did a java program in ubuntu os......the correct and wrong ones r below....i compiled and run both of them but one is giving the right answer and the other one is giving wrong one.....
correct one:

Java
class ab{
void abc(){
int a,b;
a=10;
b=(a==3)?35:78;
System.out.println("b = "+b);
b=(a==10)?35:78;
System.out.println("b = "+b);
}
public static void main(String[] args){
ab obj=new ab();
obj.abc();
}
}


here the solution is :
b = 78
b = 35

the wrong one:


Java
class ab{
void ab(){
int a,b;
a=10;
b=(a==3)?35:78;
System.out.println("b = "+b);
b=(a==10)?35:78;
System.out.println("b = "+b);
}
public static void main(String[] args){
ab obj=new ab();

}
}


the solution is here is nothing when i run it.

What I have tried:

i tried both of them but i have no idea what's wrong here? in the first one why i m right and in the second why nothing is happened?
Posted
Updated 16-Dec-17 1:07am
v4
Comments
phil.o 16-Dec-17 5:15am    
You forgot to define what is your goal, as well as what is "correct" or "wrong" in your context.
Also, a debug session could bring you some clues quite quickly.

ab obj = new ab(); //the default constructor is called, not the void ab()
 
Share this answer
 
Comments
Member 13579362 16-Dec-17 5:42am    
ok so for tht my 2nd program has not any solutions???
Java
class ab{
void ab(){
int a,b;
a=10;
b=(a==3)?35:78;
System.out.println("b = "+b);
b=(a==10)?35:78;
System.out.println("b = "+b);
}
public static void main(String[] args){
ab obj=new ab(); // this will only create an object of the ab class. 

ab.ab(); // call the void method
 
}
}

See The Java™ Tutorials[^]
 
Share this answer
 
I'm not familiar with Java. I'm C# guy...
Can you try this?

class ab
{
  public ab()
 {
   int a,b;
   a = 10;
   b =(a==3)? 35: 78;
   System.out.println("b = "+b);
   b = (a==10)? 35: 78;
   System.out.println("b = "+b);
 }
 public static void main(String[] args)
 {
   ab obj = new ab();
 }
}



https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900