Type 4 method: parameterized with return value
1. Write a Java program to input the sales amount. Define a method
calculateCommission(double amount) that receives the sales amount as an
argument and returns the commission to the main method.
If the sales amount is more than Rs. 100000, the commission is 25% of
the amount.
Otherwise, the commission is 15% of the amount.
import java.util.Scanner;
public class CommissionCalculator
{
public static double calculateCommission(double salesAmount)
{
double commission;
if (salesAmount > 100000)
{
commission = 0.25 * salesAmount;
} else {
commission = 0.15 * salesAmount;
}
return commission;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the sales amount: Rs. ");
double salesAmount = sc.nextDouble();
double commission = calculateCommission(salesAmount);
System.out.println("The commission is: Rs. " + commission);
}
}
Variable Description
Variable Name Data Type Description
sc Scanner Scanner object used to take input from the user
salesAmount double The sales amount entered by the user
commission double The calculated commission based on the sales amount
2. Write a program in Java to input the quantity and price per unit of a product.
Define a method calculatePayableAmount(int quantity, double price) that
receives the quantity and price as arguments and returns the payable amount
after applying a discount.
If the total amount (quantity × price) is more than Rs. 3000, a discount of 12%
is applied. Otherwise, no discount is given. Display the payable amount from
the main() method.
import java.util.Scanner;
public class ProductBill
{
public static double calculatePayableAmount(int quantity, double price)
{
double amount = quantity * price;
double reduction = 0;
if (amount > 3000)
{
reduction = 0.12 * amount;
}
double payableAmount = amount - reduction;
return payableAmount;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter quantity: ");
int quantity = sc.nextInt();
System.out.print("Enter price per unit: Rs. ");
double price = sc.nextDouble();
double payable = calculatePayableAmount(quantity, price);
System.out.println("Total payable amount after reduction (if any): Rs. " +
payable);
}
}