hi
I have a problem regarding calling print() method through main().It asks to pass arguments but i already done so using constructors.Any help in missing statement.

public class ElectricityBill {
	public static void main(String args[]){
    current c1=new current(500,"January");
    current c2=new current(1600,"February");
    
    c1.print(double total,String month);//this is the place i wanna know,how to call print()
    
    
	}
}

class current{
	int usage;
	String month;
	double total;
	
	current(int u,String m){
		usage = u;
		month=m;
	}
	double calculate(int usage){
		double u1=12.50;
		total=usage*u1;
		return total;
	}
	void print(double total,String month){
		System.out.println("Your bill for month "+month+" is "+total);
	}
}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

then it says
Multiple markers at this line
- month cannot be resolved
- The method print(double, String) in the type current is not applicable for the arguments ()
- total cannot be resolved

hi
I have a problem regarding calling print() method through main().It asks to pass arguments but i already done so using constructors.Any help in missing statement.

public class ElectricityBill {
	public static void main(String args[]){
    current c1=new current(500,"January");
    current c2=new current(1600,"February");
    
    c1.print(double total,String month);//this is the place i wanna know,how to call print()
    
    
	}
}

class current{
	int usage;
	String month;
	double total;
	
	current(int u,String m){
		usage = u;
		month=m;
	}
	double calculate(int usage){
		double u1=12.50;
		total=usage*u1;
		return total;
	}
	void print(double total,String month){
		System.out.println("Your bill for month "+month+" is "+total);
	}
}

Declare and Initialize total and month that you are passing as argument.

double total=500;
String month = "January";
c1.print(total,month);
public class ElectricityBill {
	public static void main(String args[]){
    current c1=new current(500,"January");
    current c2=new current(1600,"February");
    
    c1.print(c1.total,c1.month);//this is the place i wanna know,how to call print()
    c1.print(c2.total,c2.month);
    
	}
}

I think you should create print method outside of the class

public class ElectricityBill {
	public static void main(String args[]){
    current c1=new current(500,"January");
    current c2=new current(1600,"February");
    
    c1.print(c1.total,c1.month);//this is the place i wanna know,how to call print()
    c1.print(c2.total,c2.month);
    
	}
}

I think you should create print method outside of the class

His print method is already in the correct place, he is just calling it incorrectly.

Since the print just prints the month and total of the class and you already have those values in your class and you call it with parameters those values, shouldn't you be doing this:

void print() {
	System.out.println("Your bill for month "+month+" is "+total);
}

Inside the class?

The print method is a pre-defined method in Java. There is the println() method and the print() method. This is a static method of the PrintWriter class. Its better if you change the name of that method and try. If you have to use print() method compulsorily then you have to override that method.

The print method is a pre-defined method in Java. There is the println() method and the print() method. This is a static method of the PrintWriter class. Its better if you change the name of that method and try. If you have to use print() method compulsorily then you have to override that method.

Completely useless information and confusing. Are you really suggesting that he overrides the method of the PrintWriter class????
Or are you saying the name is the problem? That is completely wrong.
And there is NO pre-defined print method in java.
Different classes have print methods, but there is no confusing since in order to call them, you need an instance of their classes.

System.out.println() calls the print method of the System.out PrintStream instance

Don't let this guy confuse you gaya88.

Comments
thannnk you. well said.
This article has been dead for over six months. Start a new discussion instead.