0
Calculating NetRevenue with Taxes
My code is saying my method cannot be applied to given types. Could someone help me? The code I have so far is: https://code.sololearn.com/ce6ZC681er65/?ref=app
8 Respostas
+ 2
Guy Robbins
change your code for the following method to this ,
public void CalculateNetRevenue(){
int n = this.totalIncome;
int m = this.taxPercent;
this.netRevenue = n-n*m/100;
}
Your code requires you to pass two arguments for the above code as you have parameters specified for it.
+ 3
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int totalIncome = read.nextInt();
int taxPercent = read.nextInt();
//creating an Income object
Income income = new Income();
income.totalIncome = totalIncome;
income.taxPercent = taxPercent;
income.CalculateNetRevenue(totalIncome, taxPercent);
System.out.println("Net revenue: " + income.getNetRevenue());
}
}
//Please Subscribe to My Youtube Channel
//Channel Name: Fazal Tuts4U
class Income{
public int totalIncome;
public int taxPercent;
//the net revenue is private
private int netRevenue;
//complete setter method
public void CalculateNetRevenue(int totalIncome, int taxPercent){
this.netRevenue = this.totalIncome - (this.totalIncome*this.taxPercent)/100;
}
//complete getter method
public int getNetRevenue(){
return this.netRevenue;
}
}
0
I think on line 30 when you use division, it's giving you float or double, so you have to use type casting to get int
0
method is declaring as
public void CalculateNetRevenue(int n, int m){
but you call it without any arguments.
income.CalculateNetRevenue();
0
Thank you Abhay that worked. May I ask just to understand better why in this example I did not have to use parameters for my CalculateNetRevenue() method?
0
Guy Robbins
In the following piece of code ,
"""
income.totalIncome = totalIncome;
income.taxPercent = taxPercent;
income.CalculateNetRevenue();
"""
You are already assigning user input values (totalIncome and taxPercent) to class variables , so why would need parameters for a method that just won't do anything ?
See the following code ,
"""
public void CalculateNetRevenue(int n, int m){
n = totalIncome;
m = taxPercent;
"""
totalIncome and taxPercent are already assigned . Now what you think the above code is doing ? Even if you pass values to this method they won't affect the result at all. Our user input totalIncome will be assigned to variable m , so you see there is no need for those parameters !
let me know if still something doesn't makes sense.
0
Okay I think I am understanding a little bit more thank you for your feedback Abhay. I do appreciate it.
0
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int totalIncome = read.nextInt();
int taxPercent = read.nextInt();
//creating an Income object
Income income = new Income();
income.totalIncome = totalIncome;
income.taxPercent = taxPercent;
income.CalculateNetRevenue();
System.out.println("Net revenue: " + income.getNetRevenue());
}
}
class Income{
public int totalIncome;
public int taxPercent;
//the net revenue is private
//That is why we use a getter and setter method
private int netRevenue;
//complete setter method
public void CalculateNetRevenue(){
this.netRevenue = totalIncome-totalIncome*taxPercent/100;
}
//complete getter method
public int getNetRevenue(){
return netRevenue;
}
}