Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,892 people online right now. Registration is fast and FREE... Join Now!




Arraylist with if Statements

 
Reply to this topicStart new topic

Arraylist with if Statements

Crawler
5 Feb, 2008 - 10:53 PM
Post #1

New D.I.C Head
*

Joined: 5 Feb, 2008
Posts: 2

Hi I am Crawler. Please help me with the question below. I have also included my attempted answer below after the question. Please guide me accordingly. Thanks in advance.



Question 1. Create a function which is supposed to return the sum of the monthly tax calculated on monthly gross pay values. The input to this function will be an array with the 1st to 12th elements, representing the gross pay values for the 1st to 12th months respectively. The rules applicable to the calculation of the tax for each month is as follows: “In the first month, Tax is 0, and in the second month , tax is 10% of the Gross pay. Otherwise tax is 25% of the gross pay.”

Answer came up with!!!

CODE

import java.lang.*;

public class ArraySum
{
    double tax;
    double monthlyPay;
    
      
    public static void main (String [] args)
        {
            double [] monthlyPay = new double [11];
            for (double Pay=1; Pay<12; Pay++)
            {
                if ArraySum Pay = 1 tax = double 0.1
                else t = 0.25;
                Tax = t * monthlyPay;
                sum = sum + (t * monthlyPay);
                System.out.println("error");
            }
        }
    }
~edit: code tags added PB

This post has been edited by PennyBoki: 6 Feb, 2008 - 12:12 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Arraylist With If Statements
5 Feb, 2008 - 11:44 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,933



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Please put your code in code tags [] to make it more readable. also at first glance this needs parentheses:


CODE


if ArraySum Pay = 1 tax = double 0.1
else t = 0.25;
Tax = t * monthlyPay;
sum = sum + (t * monthlyPay);
System.out.println("error");


Why are you if checking ArraySum? It is the name of your class and I don't see any variable array being declared of that name. Also, you should only have one statement being evaluated in an if statement or have multiple ones connected with && ! || etc....

I envision the following:

CODE


//code
if (month == 1){
     grossPayTax = 0;
}
else if (month == 2){
     grossPayTax = 0.1;
}

//so on and so forth and then have it calculate


That should get you in the right direction for your program.

--KYA

This post has been edited by KYA: 5 Feb, 2008 - 11:45 PM
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Arraylist With If Statements
5 Feb, 2008 - 11:51 PM
Post #3

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
I've barely skimmed over your code but wow.. okay first
QUOTE

CODE

double monthlyPay;
and
double [] monthlyPay = new double [11];

This just needs to be:
double[] monthlyPay = new double[11];


Now...
QUOTE

CODE

for (double Pay=1; Pay<12; Pay++)
{
if ArraySum Pay = 1 tax = double 0.1
else t = 0.25;
Tax = t * monthlyPay;
sum = sum + (t * monthlyPay);
System.out.println("error");
}

If statements that check if something equals something are formatted as follows:
CODE

if(something == whatYourCheckingForItToEqual)
/*the "==" is what you use to say equal to,
the code "something = whatYourCheckingForItToEqual" makes: something change its value to whatYourCheckingForItToEqual*/
{
       doWhatIsInHere
}else
{
       doWhatIsInHere
}

So with your code:
CODE

for (double Pay=1; Pay<12; Pay++)
{
if (ArraySum Pay = 1)
{
   tax = double 0.1
}else
{
   t = 0.25;
}
Tax = t * monthlyPay;
sum = sum + (t * monthlyPay);
System.out.println("error");
}

Okay now:
QUOTE

CODE

Tax = t * monthlyPay;
sum = sum + (t * monthlyPay);

1.)Tax, t, and tax are not the same thing. You must keep your capitals and variable name the same.
2.) You must initialize sum before making it equal something.
So you change it to:
CODE

tax = tax * monthly pay;
double sum = sum + tax;



Now take what I've said and see where you get... hopefully you will get lesserrors...

@KYA

Lol, you finished before I did.
Didn't give him/her much though biggrin.gif.
I think that our thought are conflicting and are gonna make him/her more confused xD
Lets hope for the best and cross our fingers tongue.gif
User is offlineProfile CardPM
+Quote Post

KYA
RE: Arraylist With If Statements
5 Feb, 2008 - 11:53 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,933



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Ah whoops. smile.gif I always find that I learn more when I sift through the problem rather then be given a bunch of information.


I think it's good that we gave a few options, then they can choose which path (or both, or a combination) to take. Tax is so wonderful tongue.gif, it can be expressed so many ways. smile.gif

--KYA
User is offlineProfile CardPM
+Quote Post

DillonSalsman
RE: Arraylist With If Statements
6 Feb, 2008 - 12:02 AM
Post #5

D.I.C Head
Group Icon

Joined: 30 Oct, 2007
Posts: 72


Dream Kudos: 50
My Contributions
Lol
QUOTE

Tax is so wonderful tongue.gif, it can be expressed so many ways. smile.gif

TurboTax == lovable erotic monkey touching;

But yeah, I'll check back in morning and see where he/she gets tongue.gif
If they even check back for a reply.. Crawler is offline now.

This post has been edited by DillonSalsman: 6 Feb, 2008 - 08:39 AM
User is offlineProfile CardPM
+Quote Post

potator
RE: Arraylist With If Statements
6 Feb, 2008 - 06:37 PM
Post #6

D.I.C Head
Group Icon

Joined: 2 Dec, 2007
Posts: 78



Thanked: 1 times
Dream Kudos: 175
My Contributions
I can't tell what your code does, so I wrote my own:
CODE

//assume the double[] money contains 12 elements from 0 to 11 representing the monthly pay
public double getTax(double[] money){
    double sum = 0;
    double taxSum;
    for(int x = 0; x < 12; x++){
        sum += money[x];
        if(x == 1)
            taxSum += 0.1 * sum;
        else if(x > 1)
            taxSum += 0.25 * sum;
    }
    return taxSum;
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month