Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    IRS Coding Error

    Hi, I am trying to code an IRS that calculates the taxes according to one's marriage. However, although I have done the coding, I do not know how to create a main function so that I can make it run.
    Following is the code

    ---------------------------------------------------------------------

    import java.util.Scanner;

    public class IRS {

    private Scanner in;
    private int maritalStatus;
    private double income;

    public IRS(){
    maritalStatus = 1;
    income = 0;
    in = new Scanner(System.in);
    }

    public IRS(int m, double i){
    maritalStatus = m;
    income = i;
    in = new Scanner(System.in);
    }

    /**
    * Retrieves data from the keyboard. Returns true if data is valid,
    * false if any of the values are out of range.
    */
    public boolean input(){
    boolean valid = false;
    System.out.print("Enter marital status (1=single, 2=married): ");
    maritalStatus = in.nextInt();
    if(maritalStatus == 1 || maritalStatus == 2){
    valid = true;
    }
    System.out.print("Enter taxable income: ");
    income = in.nextDouble();
    if(income < 0){
    valid = false;
    }
    return valid;
    }

    /**
    * Returns amount of tax.
    * preconditions: maritalStatus is 1 or 2, income > 0
    */
    public double calcTax(){

    if(maritalStatus == 1){
    if(income <= 27050){
    return income * 0.15;
    }else if(income <= 65550){
    return (income - 27050) * 0.275 + 4057.5;
    }else if(income <= 136750){
    return (income - 65550) * 0.305 + 14645;
    }else if(income <= 297350){
    return (income - 136750) * 0.355 + 36361;
    }else{
    return (income - 297350) * 0.391 + 93374;
    }
    }else{
    if(income <= 45200){
    return income * 0.15;
    }else if(income <= 109250){
    return (income - 45200) * 0.275 + 6780;
    }else if(income <= 166500){
    return (income - 109250) * 0.305 + 24393.75;
    }else if(income <= 297350){
    return (income - 166500) * 0.355 + 41855;
    }else{
    return (income - 297350) * 0.391 + 88306;
    }
    }
    }

    public void printData(){
    System.out.print("Your Federal tax = ");
    System.out.printf("$%.2f", calcTax());
    System.out.println();
    }
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •