1
class Objectsmultiplecnstrctrs {

    public static void main(String args[]){

        ObjectsForMultipleConstructors engg2=new ObjectsForMultipleConstructors(1);
        ObjectsForMultipleConstructors engg3=new ObjectsForMultipleConstructors(1,2);
        ObjectsForMultipleConstructors engg=new ObjectsForMultipleConstructors(1,2,3);

    }
}

// secondary class


public class ObjectsForMultipleConstructors {
    private int hour;
    private int minute;
    private int second;


    public ObjectsForMultipleConstructors(int h){
        this.hour=h;
        System.out.printf("give one ",+hour);

    }

    public ObjectsForMultipleConstructors(int h,int m){
        System.out.printf("goddamn ",+m);
    }

    public ObjectsForMultipleConstructors(int h,int m,int s){
        System.out.println("guess");

    }
}

OUTPUT is give one goddamn guess

Now the thing is i have declared int hour =h and value of h I assigned in the arguments in the main class,so im expecting the value of h which i defined to be displayed next to the text (System.out.printf("goddamn ",+m);) ,,but its doing what i want it to do ,where im missing

3
  • I assume you mean not 'doing what I want', but you haven't specified that in enough detail. What are you getting instead? Compile error? Exception? Unexpected output? At the moment there is no question here. Commented Mar 25, 2012 at 9:17
  • Unless you show your main method, no-one will be able to guess what you're talking about. Commented Mar 25, 2012 at 9:22
  • @Himanshu Saxena Is the problem solved? Commented Mar 25, 2012 at 10:03

4 Answers 4

3

Why you use comma System.out.printf("give one ",+hour); ?

it must be System.out.printf("give one " + hour);

the description of printf usage is :

A convenience method to write a formatted string to this output stream using the specified format string and arguments. An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation out.format(format, args) Parameters: format A format string as described in Format string syntax args Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by the Java Virtual Machine Specification. The behaviour on a null argument depends on the conversion.

Sign up to request clarification or add additional context in comments.

1 Comment

I think he's mixing System.out.print with System.out.printf. So, it should probably be either System.out.print("give one " + hour); or System.out.printf("give one %d", hour);.
1

In order to format and print arguments to printf() you need to specify them in the pattern string, like:

System.out.printf("give one %d ", hour);

Comments

0

You need to have a format specifier as well in a printf statement

System.out.printf("give one %d ",hour);

Comments

0

I think its because you have not used %d access specifier in the method printf

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.