Results 1 to 2 of 2
  1. #1
    5minutes is offline Member
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Incorporating If-Else into Methods + Private Helper Methods?

    Here is my assignment:

    Write a program that:

    Prompts the user for the following information:

    Filing status : single or married
    Taxable income
    Calculates and prints

    Filing status
    Taxable income
    Federal tax
    Your program should be written using proper object-oriented design. (You may wish to review concepts presented in Lessons 6 and 7.)

    There should be a TaxPayer class that has two instance variables, one for filing status and one for income. The class should have a constructor that has two parameters which are used to initialize the instance variables.

    The class should have three public methods getIncome, getStatus, and calculateTax, and two private (helper) methods called by calculateTax, one to calculate the tax for single and the other for married filing status. (The helper methods can be called inside calculateTax by just using the name of the helper method with no object or class name in front of it. For example:

    public ... calculateTax (....)
    {
    ...
    singleTax(income); // not: TaxPayer.singleTax(income);
    ...
    }
    Your main should prompt for filing status and income, and then calculate and print the output as described in step 1. Allow main to repeat this process at least twice so that different cases can be tried out.

    When you prompt for filing status, the input will be read in as a String. Convert the first letter of the String to a char by using the method charAt. Then the relational operator == can be used to test if it represents 'single' or 'married'. Your program should be able to handle both upper and lowercase input. Here is some sample code to read user input, to extract the first character from the input, and to test for 'single' status:

    // reading user input into a String
    ConsoleIO console;
    String statusStr;
    statusStr = console.readToken();

    // extracting the first character from the String
    char status = statusStr.charAt(0);

    // testing if the character is an upper or lowercase 's'
    if (status == 'S' || status == 's')
    ...
    Example run output:

    Single
    Taxable income = $ 35,125
    Federal tax = $ 6,278.13





    And here is what I have so far


    original class:


    Java Code:
    public class TaxPayer {
    	
    	private char myS;
    	private char myM;
    	private double myIncome;
    	private double myTax;
    	
        public TaxPayer(char s, char m){
        	
        	myS = s;
        	myM = m;
        }
       
        public TaxPayer(double income, double tax){
        	
        	myIncome = income;
        	myTax = tax;
        	
        }
    
    
        
        public double calculateTaxS(char s){
    
    		if (char == s){
    			System.out.println("Filing status : Single");
    			if ((income >= 0.0) && (income <= 27050.0))
    				tax = 0.15 * income;
    			else if ((income >= 27050) && (income <= 65550.0))
    				tax = (4057.50 + 0.275) * (income - 27050.0);
    			else if ((income >= 65550) && (income <=136759.0))	
    				tax = (14645.00 + 0.305) * (income - 65550.0);
    			else if ((income >= 136750) && (income <= 297350.0))
    				tax = (36361.0 + 0.355) * (income - 136750.0);
    			else if ((income >= 297.350))
    				tax = (93374.0 + 0.391) * (income - 297.350);
    				
    				return tax;
    	}
    	
    	public double calculateTaxM(char m){
    	
    		if (char == m){
    			
    			System.out.println("Filing Status : Married");
    			
    			if ((income >= 0.0) && (income <= 45200.0))
    				tax = 0.15 * income;
    			else if ((income >= 45200.0) && (income <= 109250.0))
    				tax = (6780.00 + 0.275) * (income - 45200.0);
    			else if ((income >= 109250) && (income <= 166500.0))
    				tax = (24393.75 + 0.305) * (income - 109250.0);
    		    else if ((income >= 166500) && (income <= 297350.0))
    		    	tax = (41855.00 + 0.355) * (income - 166500.0);
    		    else if ((income >= 297350.0))
    		    	tax = (88306.00 + 0.391) * (income - 297350.0);					
    						
    			return tax;
    	}
    
    
    		
    	}

    tester class:
    Java Code:
    import chn.util.*;
    
    public class TaxTester {
    
        public static void main(String args[]) {
        	
        	ConsoleIO iris = new ConsoleIO();
        	TaxPayer status = new TaxPayer();
        	
        	char s;
        	char m;
        	String singleOrMarried;
        	double taxableIncome;
        	
        	System.out.println("Filing status: single (s) or married (m): ");
            singleOrMarried = iris.readToken();
            System.out.println("Taxable Income: $");
            taxableIncome = iris.readDouble();
            
            if (singleOrMarried = s)
               System.out.println(CalculateTaxS(s));
            else 
             	System.out.println(CalculateTaxM(m));    
        	
        	
        }
        
        
    }



    I know I've have done something wrong [didn't completely follow the instructions] but I'm not sure how to fix it. How do I separate everything into the 3 methods + 2 private helper methods?


    **btw, chn.util is sort of like util.Scanner, it reads user input

  2. #2
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Incorporating If-Else into Methods + Private Helper Methods?

    Java Code:
    String singleOrMarried;
    /* ... */
    if (singleOrMarried = s)
    Does this even compile? You probably meant == instead of =, but that won't work either; use .equals() to compare objects such as a String.

    There's no need for your calculateTaxS() and calculateTaxM() to take a char as an argument. The char is already checked in the main() method and, once you've fixed your other mistakes, the appropriate method will be called from there.

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Replies: 1
    Last Post: 09-09-2011, 07:57 PM
  3. Replies: 3
    Last Post: 05-05-2011, 04:33 PM
  4. Multiple private methods
    By java4deepak in forum New To Java
    Replies: 4
    Last Post: 01-05-2011, 02:05 PM
  5. Incorporating Methods into my program
    By Moltisanti in forum New To Java
    Replies: 16
    Last Post: 10-12-2009, 07:48 AM

Posting Permissions

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