Raptor
Raptor

Reputation: 11

Learning a Constructor - No output

I am a newbie and learning methods and constructors. Questions as follow

  1. When I run the below program in Eclipse, I am prompted an error that I can declare main method as static.
public class Mouse 
      {

        double version;

        String model;

        Mouse(double v, String m)
        {
            version = v;
            model = m;
        }

        void display()
        {
            System.out.printf("Version is ", +version);

            System.out.printf("Model is ",model);
        }

      }
     class DemoMouse
     {

          public static void main(String[] args) {
        {
            Mouse m = new Mouse(5.1, "Logitech");
            m.display();
            }
     }
  1. When I am forced to remove static from main and make DemoMouse as static, I dont get the full output.I get out put is Version is Model is. I expect the out put as
*Version is 2.4 
Model is Logitech*

    package test;

    public class Mouse
     {
        double version;

        String model;

        Mouse(double v, String m)
        {
            version = v;
            model = m;
        }

        void display()
        {
            System.out.printf("Version is ", +version);

            System.out.printf("Model is ",model);
        }
  }

    static class DemoMouse
    {

        public void main(String[] args) {
            {
                Mouse m = new Mouse(5.1, "Logitech");
                m.display();
        }
}

Upvotes: 1

Views: 124

Answers (3)

maneeshrapelly
maneeshrapelly

Reputation: 1

Make the class which is having main method as public. Make below change in your first example.

public class DemoMouse
{
    public static void main(String[] args) {
    {
        Mouse m = new Mouse(5.1, "Logitech");
        m.display();
    }
}

Upvotes: 0

Proph3cy
Proph3cy

Reputation: 173

1. You need to declare your main-method static. Reason for that is, that you need an instance of a class to use non-static methods. For further in-depth explanation look at the comment "howlger" gave.

2. In your constructor, as well as in your display-method, you should use "this.version" and "this.model". This leads to the value-allocation to be allocated to the to be constructed instance of the class.

3.You should use System.out.println to display your data. "println" says nothing else, than print the String you hand over and add a linefeed to the whole thing (next line).

printf is a bit more complicated. Explanation why nothing is printed with the way you are using the printf is to be found in the reference:

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.

So basically the printf method you are trying to use normally gets 2 arguments, a format in which you want to print, and the arguments which you want to print. Since you define the format as a plain String (no "%s" or the like), there wont be place for any argument to be placed, any argument you give the method is ignored, since the number of format definitions < number of arguments.

Upvotes: 0

Krishna146
Krishna146

Reputation: 11

Use System.out.println Instead of System.out.printf

but if you want to use printf then you have to add %f for double and %s for string otherwise variables wont be printed. In this case

System.out.printf("Model is %.2f",version); //where %.2f restricts double to 2 decimal points
System.out.printf("model is %s", model);

Upvotes: 1

Related Questions