Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Method Mixup

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Method Mixup

    Alright so I have to write this code this way as a method with a question input for my homework.
    The assignment is make a print tax table method that will print a tax table from 50000 to 60000 tax income range at $50 a row.

    The only thing that I cant figure is the computation part where I println and the math is not computing properly. Also the compiler did not like my attempt to increment by 50.

    How do I increment by 50 in the while loop?
    What is wrong with my println statement?
    Why cant I use the identifiers in the println statement?

    import java.util.Scanner;
     
    public class ComputeTax {
    	public static void main(String[] args) {
    		//Create a Scanner
    		Scanner input = new Scanner(System.in);
     
    		//Prompt the user to enter filing status
    		System.out.print(
    			"Would You like to print a Tax table of 50000 to 60000 1-Yes, 2-N");
    		int status = input.nextInt();
     
    		double taxableIncome=0;
     
    		if (status == 1) {
    			printTable(status, taxableIncome);
    		}
    		else {
    			System.out.println("Come back when you want to print in the 50k to 60k range!");
    			System.exit(0);
    		}
     
     
    public static double printTable(int status, double taxableIncome) {
    		double taxableIncome = 50000;
    		double endingJob = 60000;
     
    		System.out.println("Taxable\tSingle\tMarried\tMarried\tHead of");
    		System.out.println("Income\tFiler\tJoint\tSeparate\ta House");
     
    		while (taxableIncome <= endingJob) {
    			System.out.println(taxableIncome + "\t" + (6000 * 0.10 + (27950 - 6000) * 0.15 + (taxableIncome - 27950) * 0.27)) + "\t" + (12000 * 0.10 + (46700 - 12000) * 0.15 + (taxableIncome - 46700) * 0.27) + "\t" + (6000 * 0.10 + (23350 - 6000) * 0.15 + (taxableIncome - 23350) * 0.27) + "\t" + (10000 * 0.10 + (37450 - 6000) * 0.15 + (taxableIncome - 37450) * 0.27) + "\n";
    			taxableIncome + 50;
    		}
    	}
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method Mixup

    Hmmmm....taxable income will be 0 when you call that method printTable.

    You forgot a bracket to end your main method.

    public static double printTable(int status, double taxableIncome) {
    		double taxableIncome = 50000;
    		double endingJob = 60000;
     
    		System.out.println("Taxable\tSingle\tMarried\tMarried\tHead of");
    		System.out.println("Income\tFiler\tJoint\tSeparate\ta House");
     
    		while (taxableIncome <= endingJob) {
    			System.out.println(taxableIncome + "\t" + (6000 * 0.10 + (27950 - 6000) * 0.15 + (taxableIncome - 27950) * 0.27)) + "\t" + (12000 * 0.10 + (46700 - 12000) * 0.15 + (taxableIncome - 46700) * 0.27) + "\t" + (6000 * 0.10 + (23350 - 6000) * 0.15 + (taxableIncome - 23350) * 0.27) + "\t" + (10000 * 0.10 + (37450 - 6000) * 0.15 + (taxableIncome - 37450) * 0.27) + "\n";
    			taxableIncome + 50;
    		}
    	}

    This never returns anything, but it should return a double.

    It's also usually best not to have a local variable with the same name and type as a parameter variable.

    taxableIncome = taxableIncome + 50; will increase it by 50 for each iteration of the while loop.
    Last edited by javapenguin; November 6th, 2010 at 05:36 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method Mixup

    You should have your find a way to later set your taxable income to in your method to return it, otherwise taxable income will be 0.

    Also, you don't have an ending ) on your println statement in your method.

    While status will be defined when you call the method, it appears that it need not be passed at all.

    If the status isn't 1, it never even calls the method.

    You should have it be

    public static void printTable

    Void means you're not returning anything.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method Mixup

    public class ComputeTax {
    	public static void main(String[] args) {
    		//Create a Scanner
    		Scanner input = new Scanner(System.in);
     
    		//Prompt the user to enter filing status
    		System.out.print(
    			"Would You like to print a Tax table of 50000 to 60000 1-Yes, 2-N");
    		int status = input.nextInt();
     
    		// double taxableIncome=0;
     
    		if (status == 1) {
    			printTable();
    		}
    		else {
    			System.out.println("Come back when you want to print in the 50k to 60k range!");
    			System.exit(0);
    		}
    }
     
     
    public static void printTable() {
    		double taxableIncome = 50000;
    		double endingJob = 60000;
     
    		System.out.println("Taxable\tSingle\tMarried\tMarried\tHead of");
    		System.out.println("Income\tFiler\tJoint\tSeparate\ta House");
     
    		while (taxableIncome <= endingJob) {
    			System.out.println(taxableIncome + "\t" + (6000 * 0.10 + (27950 - 6000) * 0.15 + (taxableIncome - 27950) * 0.27)) + "\t" + (12000 * 0.10 + (46700 - 12000) * 0.15 + (taxableIncome - 46700) * 0.27) + "\t" + (6000 * 0.10 + (23350 - 6000) * 0.15 + (taxableIncome - 23350) * 0.27) + "\t" + (10000 * 0.10 + (37450 - 6000) * 0.15 + (taxableIncome - 37450) * 0.27) + "\n");
    			taxableIncome  = taxableIncome + 50;
    		}
    	}
    }

  5. #5
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Mixup

    The incrementing solution seemed obvious after i looked at it to increment a = a + 50;
    I guess it became closed properly like I was instructed to do. Now it comes back with class errors.

    /tmp/jc_29720/ComputeTax.java:24: class, interface, or enum expected
    public static void printTable(int status, double taxableIncome) {
    ^
    /tmp/jc_29720/ComputeTax.java:26: class, interface, or enum expected
    double endingJob = 60000.0;
    ^
    /tmp/jc_29720/ComputeTax.java:28: class, interface, or enum expected
    System.out.println("Taxable\tSingle\tMarried\tMarr ied\tHead of");
    ^
    /tmp/jc_29720/ComputeTax.java:29: class, interface, or enum expected
    System.out.println("Income\tFiler\tJoint\tSeparate \ta House");
    ^
    /tmp/jc_29720/ComputeTax.java:31: class, interface, or enum expected
    while (taxableIncome <= endingJob) {
    ^
    /tmp/jc_29720/ComputeTax.java:33: class, interface, or enum expected
    double taxableIncome = taxableIncome + 50;
    ^
    /tmp/jc_29720/ComputeTax.java:34: class, interface, or enum expected
    }
    ^


      public class ComputeTax {
        public static void main(String[] args) {
            //Create a Scanner
            Scanner input = new Scanner(System.in);
     
            //Prompt the user to enter filing status
            System.out.print(
                "Would You like to print a Tax table of 50000 to 60000 1-Yes, 2-N");
            int status = input.nextInt();
     
            // double taxableIncome=0;
     
            if (status == 1) {
                printTable();
            }
            else {
                System.out.println("Come back when you want to print in the 50k to 60k range!");
                System.exit(0);
            }
       }
    }
     
     
    public static void printTable(int status, double taxableIncome) {
            double taxableIncome = 50000;
            double endingJob = 60000;
     
            System.out.println("Taxable\tSingle\tMarried\tMarried\tHead of");
            System.out.println("Income\tFiler\tJoint\tSeparate\ta House");
     
            while (taxableIncome <= endingJob) {
                System.out.println(taxableIncome + "\t" + (6000 * 0.10 + (27950 - 6000) * 0.15 + (taxableIncome - 27950) * 0.27)) + "\t" + (12000 * 0.10 + (46700 - 12000) * 0.15 + (taxableIncome - 46700) * 0.27) + "\t" + (6000 * 0.10 + (23350 - 6000) * 0.15 + (taxableIncome - 23350) * 0.27) + "\t" + (10000 * 0.10 + (37450 - 6000) * 0.15 + (taxableIncome - 37450) * 0.27) + "\n");
                taxableIncome  = taxableIncome + 50;
            }
    } 
    }

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method Mixup

    You have one too many brackets at the end of the main method. Delete one of them.

    You've inadvertantly told it to end the class there with that 3rd bracket.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Method Mixup

    Also

    double taxableIncome = 50000;

    How's it supposed to know if you're referring to this variable or the parameter of that method with the same name?

    Only pass it a parameter if you want

    printTables(1, 50000);

    and you'll need to get rid of:

    double taxableIncome = 50000;

    then.

    Also, as you can only call the method if status is 1, passing it as a parameter serves no purpose.

    Ahhhh, I see now, you've cleared it to no parameters when you called it.

    But it won't like it unless you clear the parameters from the method heading as well.

  8. #8
    Junior Member
    Join Date
    Sep 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Method Mixup

    import java.util.Scanner;
     
     public class ComputeTax2 {
        public static void main(String[] args) {
            //Create a Scanner
            Scanner input = new Scanner(System.in);
     
            //Prompt the user to enter filing status
            System.out.print(
                "Would You like to print a Tax table of 50000 to 60000 1-Yes, 2-N: ");
            int status = input.nextInt();
     
            // double taxableIncome=0;
     
            if (status == 1) {
                printTable();
            }
            else {
                System.out.println("Come back when you want to print in the 50k to 60k range!");
                System.exit(0);
            }
       }   
     
        public static void printTable() {
        	double taxableIncome = 50000;
            double endingJob = 60000;
            System.out.println("Taxable\tSingle\tMarried\tMarried\tHead of");
            System.out.println("Income\tFiler\tJoint\tSeparate\ta House");
     
            while (taxableIncome <= endingJob) {
                System.out.println(taxableIncome + "\t" + (6000 * 0.10 + (27950 - 6000) * 0.15 + (taxableIncome - 27950) * 0.27) + "\t" + (12000 * 0.10 + (46700 - 12000) * 0.15 + (taxableIncome - 46700) * 0.27) + "\t" + (6000 * 0.10 + (23350 - 6000) * 0.15 + (taxableIncome - 23350) * 0.27) + "\t" + (10000 * 0.10 + (37450 - 6000) * 0.15 + (taxableIncome - 37450) * 0.27) + "\n");
                taxableIncome  = taxableIncome + 50;
            }
        }
     }

    I cant really tell if this table actually prints the header is there a way to verify that the first 2 println statements occur?

Similar Threads

  1. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 12:18 PM

Tags for this Thread