Calculating taxes using specific values - java

I am having trouble understanding how to make my program calculate these tax numbers correctly
If Single and taxable income is over
but not over
the tax is
of the amount over
$0
$8,000 10% $0
$8,000 $32,000 $800 + 15% $8,000
$32,000
$4,400 + 25% $32,000
If married and taxable income is over but not over the tax is of the amount over
$0 $16,000 10% $0
$16,000 $64,000 $1,600 + 15% $16,000
$64,000
$8,800 + 25% $64,000
You should prompt the user for their filing status and their taxable income. The user should enter 'S' for single filers and 'M' for married filers.
An example would be if the taxpayer is single and earns $10,000. They would pay $800 plus 15% of 2000 (10,000 - 8,000).
this is what I have so far
public class Income_tax
{
public static final int SINGLE = 1;
public static final int MARRIED = 2;
private static final double RATE1 = 0.10;
private static final double RATE1_SINGLE_LIMIT = 8000;
private static final double RATE1_MARRIED_LIMIT = 16000;
private static final double RATE2_SINGLE_LIMIT = 32000;
private static final double RATE2_MARRIED_LIMIT = 64000;
private static final double RATE3_SINGLE_LIMIT = 60000;
private static final double RATE3_MARRIED_LIMIT = 150000;
private static final double RATE2 = 0.15;
private static final double RATE3 = 0.25;
private double income;
private int status;
public Income_tax(double anIncome, int aStatus)
{
income = anIncome;
status = aStatus;
}
public double getTax()
{
double tax1 = 0;
double tax2 = 0;
if (status == SINGLE)
{
if (income <= RATE1_SINGLE_LIMIT)
{
tax1 = RATE1 * income;
}
else
{
tax1 = RATE1 * RATE1_SINGLE_LIMIT;
tax2 = RATE2 * (income - RATE1_SINGLE_LIMIT);
}
}
if (income <= RATE3_SINGLE_LIMIT)
{
tax1 = RATE3 * income;
if (income <= RATE1_MARRIED_LIMIT)
{
tax1 = RATE1 * income;
}
if (income <= RATE3_MARRIED_LIMIT)
{
tax1 = RATE3 * income;
}
else
{
tax1 = RATE1 * RATE1_MARRIED_LIMIT;
tax2 = RATE2 * (income - RATE1_MARRIED_LIMIT);
}
if (status == SINGLE)
{
if (income <= RATE2_SINGLE_LIMIT)
{
tax1 = RATE2 * income;
}
else
{
tax1 = RATE1 * RATE2_SINGLE_LIMIT;
tax2 = RATE1 * (income - RATE2_SINGLE_LIMIT);
}
}
else
{
if (income <= RATE2_MARRIED_LIMIT)
{
tax1 = RATE2 * income;
}
else
{
tax1 = RATE2 * RATE2_MARRIED_LIMIT;
tax2 = RATE1 * (income - RATE2_MARRIED_LIMIT);
}
}
return tax1 + tax2;
}
return tax2 + tax1;
}
}
my calculator class is
import java.util.Scanner;
public class TaxCalculator {
#SuppressWarnings("resource")
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Please enter your anual income [per year]: ");
double income = in.nextDouble();
System.out.print("Please enter S for Single and M for Married (S/M) ");
String input = in.next();
int status;
if (input.equals("M"))
{
status = Income_tax.MARRIED;
}
else
{
status = Income_tax.SINGLE;
}
Income_tax aTaxReturn = new Income_tax(income, status);
System.out.println(" Your tax is: "
+ aTaxReturn.getTax());
}
}

I'm not really sure why you are calling 'aTaxReturn' as an Object in your main method, that seems a bit unnecessary. You could just call it as a regular method.
One way that I would do it is have 2 seperate methods, one for single and one for married.
Then in each separate method you can break down the tax calculations, so for example:
public class Income_tax {
private static final double RATE1 = 0.10;
private static final double RATE1_SINGLE_LIMIT = 8000;
private static final double RATE1_MARRIED_LIMIT = 16000;
private static final double RATE2_SINGLE_LIMIT = 32000;
private static final double RATE2_MARRIED_LIMIT = 64000;
private static final double RATE3_SINGLE_LIMIT = 60000;
private static final double RATE3_MARRIED_LIMIT = 150000;
private static final double RATE2 = 0.15;
private static final double RATE3 = 0.25;
//private double income; Don't need these since you're not making an object
//private int status; Don't need these since you're not making an object
//Default constructor since you don't need to make an object
public Income_tax() {
}
public double getTaxSingle(double income) {
//Different if statements for tax calculations
}
public double getTaxMarried(double income) {
//Different if statements for tax calculations
}
}
Then in the main method you can call it like this:
public static double getTaxRateSingle(double income) {
return Income_tax.getTaxSingle(income);
}
public static double getTaxRateMarried(double income) {
return Income_tax.getTaxMarried(income);
}
This way you don't need to create unnecessary objects and you can minimize the code while maintaining simplicity.
One more tip: You should brush up on Java naming conventions

Related

Variables that are used for output data (In this case account balance) aren't changing when I call methods to alter them

So, for an assignment that I have, I have to create a program that will figure out when two people (John/Mary) will be able to pay back a loan that they took out and then display back how long it took them in months as well as how much they have saved over time. The program consists of 4 classes, (Application, SavingsAccount(Marys account), CurrentAccount(Johns account) and LoanAccount). I have numerous methods set to do things such as add an interest rate onto the account balance every month. Problem is it works out when the loan will be paid back, but not how much they have saved in their savings/current account while doing so. The two variables that store this information (One set to 100.0 by default, the other set to 0.0) haven't changed in value when I try to print the account value at the end. Though it will print out the LoanAccount value for each month if I want it to, which leaves me clueless to what I have done wrong.
Code below:
Application:
package assignment2;
/**
*
* #author Josh
*/
public class Application {
static private int monthJohn = 0;
static private int monthMary = 0;
static private float TotalDebt = 20000.00F;
static private boolean johnFinished = false;
static private boolean maryFinished = false;
static private float johnTotalSaved = 0.00F;
static private float maryTotalSaved = 0.00F;
public static void main (String args[]){
John();
Mary();
if (johnFinished == true && maryFinished == true){
CurrentAccount amountJ = new CurrentAccount();
float savingsJ = amountJ.getBalanceJohn();
SavingsAccount amountM = new SavingsAccount();
float savingsM = amountM.getBalanceSAMary();
System.out.println("John repaid his loan after " + monthJohn + " months. His current account balance at that time was £" + savingsJ);
System.out.println("Mary repaid her loan after " + monthMary + " months. Her savings account balance at that time was £" + savingsM);
}
//Test for interest rate in CA
}
public static void Mary(){
LoanAccount Debt = new LoanAccount();
float DebtPaid = Debt.getBalanceLAMary();
while(DebtPaid <= TotalDebt){
SavingsAccount Mary = new SavingsAccount();
Mary.addInterestMary();
Mary.UtilitiesMary();
Mary.MonthlyPayMary();
Debt.addInterestMary();
Debt.takeLoanPaymentMary();
DebtPaid = Debt.getBalanceLAMary();
monthMary++;
//System.out.println("Test: " + DebtPaid + " Month: " + monthJohn);
}
maryFinished = true;
}
public static void John(){
float test = 0.0F;
LoanAccount Debt = new LoanAccount();
float DebtPaid = Debt.getBalanceLAjohn();
while(DebtPaid <= TotalDebt){
CurrentAccount John = new CurrentAccount();
John.addInterestJohn();
John.UtilitiesJohn();
John.MonthlyPayJohn();
Debt.addInterestJohn();
Debt.takeLoanPaymentJohn();
John.getBalanceJohn();
test = John.getBalanceJohn();
DebtPaid = Debt.getBalanceLAjohn();
monthJohn++;
//System.out.println("Test: " + DebtPaid + " Month: " + monthJohn + " TEST :" + test);
}
johnFinished = true;
}
}
CurrentAccount:
package assignment2;
/**
*
* #author Josh
*/
public class CurrentAccount {
//Variable declaration
private float balance = 0.00F;
private float interestRate = 0.0008F;
private float interest = 0.0F;
private float LoanPayment = 200.00F;
private float MonthlyWage = 2000.00F;
private float utilitiesJohn = 1200.00F;
public static void main (String args[]){
}
public float getBalanceJohn(){
return balance;
}
public void addInterestJohn(){
interest = interestRate * balance;
balance = balance + interest;
}
public void MonthlyPayJohn(){
balance = balance + MonthlyWage;
}
public float makeDepositJohn(){
balance = balance - LoanPayment;
return LoanPayment;
}
public void UtilitiesJohn(){
balance = balance - utilitiesJohn;
}
}
SavingsAccount:
package assignment2;
/**
*
* #author Joshua Quinn-Edwards
*/
public class SavingsAccount {
//Variable declaration
private float balance = 100.00F;
private float interestRate = 0.004F;
private float interest = 0.00F;
private float LoanPayment = 300.00F;
private float MonthlyWage = 2000.00F;
private float utilitiesMary = 1000.00F;
public static void main (String args[]){
}
public void UtilitiesMary(){
balance = balance - utilitiesMary;
}
public float getBalanceSAMary(){
return balance;
}
public void addInterestMary(){
interest = interestRate * balance;
balance = balance + interest;
}
public void MonthlyPayMary(){
balance = balance + MonthlyWage;
}
public float makeDepositMary(){
balance = balance - LoanPayment;
return LoanPayment;
}
}
LoanAccount:
package assignment2;
/**
*
* #author Josh
*/
public class LoanAccount {
//Variables
private float balanceJohn = 0.00F;
private float balanceMary = 0.00F;
private float interestRate = 0.006F;
private float interestJohn = 0.00F;
private float interestMary = 0.00F;
public static void main (String args[]){
}
public float getBalanceLAjohn (){
return balanceJohn;
}
public float getBalanceLAMary (){
return balanceMary;
}
public void takeLoanPaymentJohn(){
CurrentAccount JohnLoanPayment = new CurrentAccount();
float Payment = JohnLoanPayment.makeDepositJohn();
balanceJohn = balanceJohn + Payment;
}
void takeLoanPaymentMary(){
SavingsAccount MaryLoanPayment = new SavingsAccount();
float Payment = MaryLoanPayment.makeDepositMary();
balanceMary = balanceMary + Payment;
}
public void addInterestMary(){
interestMary = interestRate * balanceMary;
balanceMary = balanceMary + interestMary;
}
public void addInterestJohn(){
interestJohn = interestRate * balanceJohn;
balanceJohn = balanceJohn + interestJohn;
}
}
If anyone has any idea to why I keep getting the outputs:
John repaid his loan after 79 months. His current account balance at that time was £0.0
Mary repaid her loan after 57 months. Her savings account balance at that time was £100.0
rather than the actual amounts that they would have in their accounts I would appreciate it if you could explain where I have gone wrong. Thanks in advance for anyone that does give any help.

Variable from superclass is not dispalying

I'm having a problem which I cannot fathom why. I have program I'm making where it takes a few inputs and calculates pay, tax, and final pay.
Everything is working except the final pay.
this calculates the final pay
import java.util.*;
public class Payroll extends Pay
{
public double calc_payroll()
{
super.calc_payroll();
super.tax();
netPay = grossPay - (grossPay * (tax/100));
return netPay;
}
}
this calculates pay and tax
import java.util.*;
public class Pay
{
private float hoursWrkd;
private float rate;
private int hoursStr;
float grossPay;
int tax;
float netPay;
public double calc_payroll()
{
grossPay = getHoursWrkd()*getRate();
return grossPay;
}
public double tax()
{
if (grossPay <= 399.99)
{
tax = 7;
}
else if (grossPay >= 400.00 && grossPay <= 899.99)
{
tax = 11;
}
else if (grossPay <= 900.00)
{
tax = 15;
}
return tax;
}
//Get & Set for hours worked
public float getHoursWrkd()
{
return hoursWrkd;
}
public void setHoursWrkd(float hoursWrkd)
{
this.hoursWrkd = hoursWrkd;
}
//Get & Set for Rate
public float getRate()
{
return rate;
}
public void setRate(float rate) {
this.rate = rate;
}
//Get & Set for hours straight
public int getHoursStr()
{
return hoursStr;
}
public void setHoursStr(int hoursStr)
{
this.hoursStr = hoursStr;
}
}
and this displays all
public class CalPayroll extends Pay
{
public void displayInfo()
{
super.calc_payroll();
super.tax();
Payroll colio = new Payroll();
colio.calc_payroll();
NumberFormat dollars = NumberFormat.getCurrencyInstance();
System.out.println("Gross Pay is : " + dollars.format(grossPay));
System.out.println("Tax is : " + tax + "%");
System.out.println("Net Pay is : " + dollars.format(netPay));
}
i have more files but those are the ones that just take the input, and call the other files.
The math is correct, however when i try to call the netPay variable and format it, it dosn't display any ammount. With grosspay it works. However my teacher said were supposed to pass grosspay into tax so it can use it, im not sure if that would fix it.
PLease help.
You try to display the netPay from CalPayroll, but this class never computes this value. When you do
Payroll colio = new Payroll();
colio.calc_payroll();
You’re probably expecting the netPay to be calculated in CalPayrool, but you’re actually calculating a separate value in a separate object, which has no effect on the current object.

Semicolon expected error in method header

I know that in method headers you aren't supposed to end it with a semicolon; however, all my method headers display the same error: ; expected. This is for the end of the header as well as between two parameters. How would I fix this?
import java.util.Scanner;
import java.text.DecimalFormat;
// This program will calculate the cost of someone's order at a coffee shop with applied possible discounts and tax
public class CoffeeShopWithMethods
{
public static void main (String [] args)
{
double cost = 0;
double discount = 0;
// Scanner allows user to enter values
Scanner user_input = new Scanner(System.in);
String username;
System.out.print("\nEnter your username: ");
username = user_input.next( );
System.out.print ("\nWelcome to Casey's Classic Coffee, " + username + "! \n");
//call methods
displayMenu();
displayOutput(cost, discount, Discounted_cost, tax, Total_cost);
System.out.println("\nThank you " + username + "! Have a nice day!");
}
//outputs the menu to the screen
public static void displayMenu()
{
System.out.println ("\n\tItem\t\tCost\n\t1. Coffee\t$1.50\n\t2. Latte\t$3.50\n\t3. Cappuccino\t$3.25\n\t4. Espresso\t$2.00");
}
//prompts the user to enter item number, returns user input
public static int getItemNumber(int number) //error: ; expected
{
int number;
Scanner number = new Scanner(System.in);
System.out.print ("\nPlease enter the desired item number: ");
number = user_input.nextInt();
return number;
}
//prompts user to enter quantity, returns user input
public static int getQuantity (int quantity) //error: ; expected
{
System.out.print ("\nPlease enter the quantity: ");
quantity = user_input.nextInt();
return quanity;
}
//takes the item number and quantity and returns the subtotal
public static double computeSubTotal (double cost) //error: ; expected
{
int number = getItemNumber(number);
int quantity = getQuantity(quantity);
// Used final double in order to make coffee shop items constant
final double COFFEE = 1.50;
final double LATTE = 3.50;
final double CAPPUCCINO = 3.25;
final double ESPRESSO = 2.00;
double cost = 0;
if (number == 1)
cost = quantity * COFFEE;
else if (number == 2)
cost = quantity * LATTE;
else if (number == 3)
cost = quantity * CAPPUCCINO;
else if (number == 4)
cost = quantity * ESPRESSO;
}
//takes the subtotal and returns true if the user earned a discount; otherwise, returns false
public static boolean discountCheck (double cost) //error: ; expected
{
boolean status;
if (cost >= 10)
{
status = true;
}
else if (cost < 10)
{
status = false;
}
return status;
}
//takes the subtotal and returns the dollar amount of the discount earned by the user
public static double computeDiscount (double cost, double discount) //error: ; expected
{
if (discountCheck() == true)
{
discount = cost * 0.10;
}
else if (discountCheck() != true)
{
discount = 0;
}
return discount;
}
//takes the subtotal and the discount amount and returns the price after the discount is applied
public static double computePriceAfterDiscount (double cost, double discount) //error: ; expected
{
double discount = 0;
double Discounted_cost = 0;
Discounted_cost = cost - discount;
return Discounted_cost;
}
//takes the prices after the discount is applied and tax rate and returns the tax amount
public static double computeTax(double Discounted_cost) //error: ; expected
{
tax = Discounted_cost * 0.07;
return tax;
}
//takes the price after the discount is applied and the tax amount and returns the final total
public static double computeTotal(double Discounted_cost, double tax) //says ; expected
{
Total_cost = Discounted_cost + tax;
return Total_cost;
}
//takes the subtotal, discount amount, price after discount, tax, and final total and displays all the lines of output to the user
public static void displayOutput(double cost, double discount, double Discounted_cost, double tax, double Total_cost) //says ; expected at the end of method header
{
//call methods
double cost = computeSubTotal(cost);
double discount = computeDiscount(cost, discount);
double Discounted_cost = computePriceAfterDiscount(cost, discount);
double tax = computeTax(Discounted_cost);
double Total_cost = computeTotal(Discounted_cost, tax);
System.out.printf ("\nTotal before discount and tax: $%.2f\n ", cost);
System.out.printf("\nCalculated discount: $%.2f\n", discount);
System.out.printf("\nTotal after special discount: $%.2f\n", Discounted_cost);
System.out.printf("\nTax: $%.2f\n", tax);
System.out.printf ("\nTotal cost: $%.2f\n", Total_cost);
}
} //error:reached end of the file while parsing
1)You are using the variables with out declaring:
for eg: compare this snippet with your code snippet.
public static double computeTotal(double Discounted_cost, double tax)
{
double Total_cost = Discounted_cost + tax;
return Total_cost;
}
2)You are invoking undefined methods.
for eg:
you are calling discountCheck() but you have defined like this.
and you have not initialized local variables before using
public static boolean discountCheck (double cost){
boolean status;
if (cost >= 10)
{
status = true;
}
else if (cost < 10)
{
status = false;
}
return status;
}
in the above method status should be initialized.
3) You are declaring duplicate variables that are already available to the methods via parameters.
see the code defined by you here:
public static void displayOutput(double cost, double discount, double Discounted_cost, double tax, double Total_cost)
{
//call methods
double cost = computeSubTotal(cost);
double discount = computeDiscount(cost, discount);
double Discounted_cost = computePriceAfterDiscount(cost, discount);
double tax = computeTax(Discounted_cost);
double Total_cost = computeTotal(Discounted_cost, tax);
System.out.printf ("\nTotal before discount and tax: $%.2f\n ", cost);
System.out.printf("\nCalculated discount: $%.2f\n", discount);
System.out.printf("\nTotal after special discount: $%.2f\n", Discounted_cost);
System.out.printf("\nTax: $%.2f\n", tax);
System.out.printf ("\nTotal cost: $%.2f\n", Total_cost);
}
I would start by extracting your MenuItem(s) into an enum like,
static enum MenuItem {
COFFEE("Coffee", 1.5), LATTE("Latte", 3.5), CAPPUCINO("Cappuccino",
3.25), ESPRESSO("Espresso", 2);
MenuItem(String name, double cost) {
this.name = name;
this.cost = cost;
}
double cost;
String name;
public String toString() {
return String.format("%s $%.2f", name, cost);
}
}
Then your compiler errors were mainly due to declaring duplicate local variable names. I was able to fix the compiler errors and produce something that looks like what you want with,
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter your username: ");
String username = scan.nextLine();
System.out.printf("Welcome to Casey's Classic Coffee, %s!%n", username);
displayMenu();
displayOutput(scan);
System.out.printf("Thank you %s! Have a nice day!", username);
}
// outputs the menu to the screen
public static void displayMenu() {
MenuItem[] items = MenuItem.values();
for (int i = 0; i < items.length; i++) {
System.out.printf("%d %s%n", i + 1, items[i]);
}
}
public static int getItemNumber(Scanner scan) {
System.out.println("Please enter the desired item number: ");
return scan.nextInt();
}
public static int getQuantity(Scanner scan) {
System.out.println("Please enter the quantity: ");
return scan.nextInt();
}
public static double computeSubTotal(Scanner scan) {
int number = getItemNumber(scan);
int quantity = getQuantity(scan);
return quantity * MenuItem.values()[number - 1].cost;
}
public static boolean discountCheck(double cost) {
return (cost >= 10);
}
public static double computeDiscount(double cost) {
if (discountCheck(cost)) {
return cost * 0.10;
}
return 0;
}
public static double computePriceAfterDiscount(double cost, double discount) {
return cost - discount;
}
public static double computeTax(double Discounted_cost) {
return Discounted_cost * 0.07;
}
public static double computeTotal(double Discounted_cost, double tax) {
return Discounted_cost + tax;
}
public static void displayOutput(Scanner scan) {
double cost = computeSubTotal(scan);
double discount = computeDiscount(cost);
double Discounted_cost = computePriceAfterDiscount(cost, discount);
double tax = computeTax(Discounted_cost);
double Total_cost = computeTotal(Discounted_cost, tax);
System.out.printf("Total before discount and tax: $%.2f%n", cost);
System.out.printf("Calculated discount: $%.2f%n", discount);
System.out.printf("Total after special discount: $%.2f%n",
Discounted_cost);
System.out.printf("Tax: $%.2f%n", tax);
System.out.printf("Total cost: $%.2f%n", Total_cost);
}
Here is your entire code corrected and working: http://ideone.com/ta0R21
I really recommend redesigning this. I suggest making use of global variable in some instances.
like the Scanner object. Instead of initializing a new Scanner each method call, use a global
one to handle the entire job

Error with Income Tax program in Java using JCreator

I am getting an error that says one of the variables I have used are not initialized but upon reviewing the code I realize that I have in fact initialized the variable so I don't understand why I keep getting the error.
This is the error:
error: variable tax might not have been initialized
avgtax = (tax/sal)*100;
^
This is the code:
import java.util.*;
public class Taxable {
final static double first15=0.1;
final static double next20=0.2;
final static double over35=0.25;
final static double tax_inc=0.8;
public static void main(String[] args) {
double sal, TaxInc;
double tax, avgtax;
Scanner in = new Scanner(System.in);
System.out.printf("Enter Salary: ");
sal = in.nextDouble();
TaxInc = sal*tax_inc;
if (TaxInc > 0 && TaxInc <= 15000)
tax = TaxInc*first15;
else if (TaxInc > 15000 && TaxInc <= 35000)
tax = (15000 * first15) + ((TaxInc - 15000) * next20);
else if (TaxInc > 35000)
tax = (15000 * first15) + (20000 * next20) + ((TaxInc - 35000) * over35);
else
System.out.printf("\nInvalid Salary Figure.");
avgtax = (tax/sal)*100;
System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is:"
+ " %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
}
}
Any help would be greatly appreciated.
This has some fixes to your code so it compiles and runs as expected
public class Taxable {
final static double first15=0.1;
final static double next20=0.2;
final static double over35=0.25;
final static double tax_inc=0.8;
public static void main(String[] args) {
double sal;
double taxInc;
double tax = 0;
double avgtax;
Scanner in = new Scanner(System.in);
System.out.printf("Enter Salary: ");
sal = in.nextDouble();
taxInc = sal*tax_inc;
if (taxInc > 0 && taxInc <= 15000) tax = taxInc*first15;
else if (taxInc > 15000 && taxInc <= 35000) tax = (15000 * first15) + ((taxInc - 15000) * next20);
else if (taxInc > 35000) tax = (15000 * first15) + (20000 * next20) + ((taxInc - 35000) * over35);
else System.out.printf("\nInvalid Salary Figure.");
avgtax = (tax/sal)*100;
System.out.printf("\nAt a salary of: %3.2f\nTax to be paid is: %3.2f\nAverage Tax Rate is: %3.1f", sal, tax, avgtax);
}
}
However its not nicely structured ... I suggest reading Java naming convention and programming standards
EDIT:
Although you have accepted an answer, i suggest looking at this code sample, its written in slightly more readable way :)
import java.util.Scanner;
public class Taxable {
private final static double first15 =0.1;
private final static double next20 =0.2;
private final static double over35 =0.25;
private final static double tax_inc =0.8;
private double sal;
private double taxInc;
private double tax = 0;
private double avgtax;
public static void main(String[] args) {
System.out.printf("Enter Salary: ");
Scanner in = new Scanner(System.in);
Taxable taxable = new Taxable();
taxable.setSal(in.nextDouble());
System.out.println(taxable.calculateTax());
}
private String calculateTax(){
setTaxInc(getSal()*tax_inc);
if (getTaxInc() > 0 && getTaxInc() <= 15000){
setTax(getTaxInc()*first15);
}
else if (getTaxInc() > 15000 && getTaxInc() <= 35000){
setTax((15000 * first15) + ((getTax() - 15000) * next20));
}
else if (getTaxInc() > 35000){
setTax((15000 * first15) + (20000 * next20) + ((getTax() - 35000) * over35)) ;
}
else {
System.out.printf("\nInvalid Salary Figure.");
}
setAvgtax((getTax()/getSal())*100);
String calculation = "\n At a salary of: " + getSal() + "\n Tax to be paid is: " + getTax() + "\n Average Tax Rate is: " + getAvgtax();
return calculation;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getTaxInc() {
return taxInc;
}
public void setTaxInc(double taxInc) {
this.taxInc = taxInc;
}
public double getTax() {
return tax;
}
public void setTax(double tax) {
this.tax = tax;
}
public double getAvgtax() {
return avgtax;
}
public void setAvgtax(double avgtax) {
this.avgtax = avgtax;
}
}
Tax is not initialized and the error is due to the if loops.
If by any chance all if's fail, Tax will not get a value.
just put double Tax = 0;

Using Scanner...Error: '.class' expected

getDiscountedBill() will return final amount of the bill
if the bill is >2000, the bill receives a 15% discount
public class Discount
{
private double bill;
private double discount;
private double amt;
public static double getDiscountedBill(double bill)
{
if (bill > 2000)
{
discount = bill * .15;
amt = bill - discount;
}
return amt;
if (bill <= 2000)
{
return bill;
}
}
public void print()
{
System.out.println("Bill after discount :: ");
System.out.printf("%.2f\n", amt);
}
code in another main
public static void main( String args[] )
{
Scanner keyboard = new Scanner(System.in);
out.print("Enter the original bill amount :: ");
double amt = keyboard.nextDouble();
keyboard.getDiscountedBill(double);
keyboard.print();
error message:error: '.class' expected
keyboard.getDiscountedBill(double);
Change this statement:
keyboard.getDiscountedBill(double);
with this one:
double discuontedBill = getDiscountedBill(amt);
You're supposed to pass a value as a method argument, not to pass a type .

Categories

Resources