0

I am a beginner in java, I have tried to solve the below program, but getting error, can anyone tell me where I am doing mistake?

public class TestGlass 
    { 
       public static void main(String [] args) 
       { 
          Glass milk = new Glass(15); // 15 ounces of milk 
          Glass juice = new Glass(3); // 3 ources of juice



      milk.drink(2); 
      milk.drink(1); 

      milk.report(); 

      juice.fill(6);  // went from 3 to 9 ounces 
      juice.drink(1); // now down to 8 ounces 

      juice.report();  

      juice.spill(); 

      juice.report(); 
   } 
} 

class Glass 
{ 

     int ounce;

     public void spill()
     {
       ounce = 0;
     }


     public void drink(int x){
        ounce = ounce-x;
     }

     public void fill(int x){
        ounce = ounce+x;
     }

     public int getOunce()
     {
       return ounce;  
     }


     public void report()
     {
       int x = getOunce();
       System.out.println("Glass has " + x + " ounces");
     }

} 

Here is the error,

TestGlass.java:5: error: constructor Glass in class Glass cannot be applied to given types;
      Glass milk = new Glass(15); // 15 ounces of milk 
                   ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
TestGlass.java:6: error: constructor Glass in class Glass cannot be applied to given types;
      Glass juice = new Glass(3); // 3 ources of juice 
                    ^
  required: no arguments
  found: int
  reason: actual and formal argument lists differ in length
2 errors
7
  • Your Glass class is missing a constructor. Commented Dec 6, 2014 at 19:55
  • Do you know what is constructor? If not you should read docs.oracle.com/javase/tutorial/java/javaOO/constructors.html Commented Dec 6, 2014 at 19:56
  • Here are some helpful hints for your future work - ericlippert.com/2014/03/05/how-to-debug-small-programs Commented Dec 6, 2014 at 19:58
  • Thanks for giving me the idea about constructor, Thanks you all @ReticulatedSpline Commented Dec 6, 2014 at 20:12
  • Thanks for giving me the idea about constructor, Thanks you all @Pshemo Commented Dec 6, 2014 at 20:13

2 Answers 2

1

You need to add a constructor to Glass that accepts your ounce parameter.

class Glass {
....
    public Glass (int ounce) {
        this.ounce = ounce;
    }
....
}

The constructor is the method that is called when you use the new operator. Its job is to initialize - to create - the object instance of the class. A constructor with one or more arguments, like this one, is set up to receive values to initialize the instance variables of the class.

Notice how the error message already mentions a constructor. That is because if you do not specify your own constructor, Java adds a default constructor that receives no arguments. That default no-arg constructor was what was being called when you called new. Since you were passing arguments to a no-arg constructor, you got the error. Once you add your own constructor, the default no-arg constructor goes away. If you want to have a no-arg version as well (e.g. setting ounce to 0 or to a default value), you can bring it back by specifying it along with the one I gave you - that is you can overload the constructor (see links below).

 class Glass {
    ....
        public Glass () {
            this.ounce = 1;  
            /* In this setup, a glass always has at least 1 ounce */
            /* If you want it be 0, you could say this.ounce = 0, or */
            /* just leave everything inside {} blank, since ounce will */
            /* default to 0 anyway */
        }

        public Glass (int ounce) {
            this.ounce = ounce;
        }
    ....
    }

Calling new Glass() would call that first no-arg constructor. Calling new Glass(15) would call the second constructor, the one that takes an argument.

Here's a nice tutorial on constructors.

Here's a nice tutorial on overloading constructors.

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

3 Comments

My pleasure. Enjoy Java!
Do you have a blog? where I can follow you.
Awww thanks for compliment. No I don't have one, my StackOverflow answers are probably the closest thing I have to a blog :-)
0

You need to write a constructor for Glass that takes an argument.

class Glass { 
     int ounce;

     public Glass(int ounce) {
         this.ounce = ounce;
     }

     public void spill()
     {
       ounce = 0;
     }


     public void drink(int x){
        ounce = ounce-x;
     }

     public void fill(int x){
        ounce = ounce+x;
     }

     public int getOunce()
     {
       return ounce;  
     }


     public void report()
     {
       int x = getOunce();
       System.out.println("Glass has " + x + " ounces");
     }

} 

1 Comment

Thanks for giving me the idea about constructor, Thanks you very much. Also thnaks for solving the program.