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,893 people online right now. Registration is fast and FREE... Join Now!




Array list with if statements

 
Reply to this topicStart new topic

Array list with if statements

Crawler
6 Feb, 2008 - 04:16 AM
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 look at responses given and adjust the below question but it is not working in Blue J. Please guide me accordingly. Thanks in advance.

I also want to thank those who have help me with the source code for my first attempt.

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 grossPayTax;
    double sum;
    int month;
    
    
    public ArraySum (double t, double m, double s)
    {
        tax = t;
        grossPayTax = m;
        sum = s;
    }
      
    public static void main (String [] args)
        {
            double [] monthlyPay = new double [11];
            for (double Pay=1; Pay<12; Pay++)
            {
                if (month == 1)
                {
                    tax = 0.0;
                }
            
                else  if (month == 2)
                {
                    tax =  0.1;
                }
                tax = t * grossPayTax;
                sum = sum + (t * grossPayTax );
                System.out.println("error");
            }
        }
    }
~edit: added code tags PB

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

chuck87
RE: Array List With If Statements
6 Feb, 2008 - 04:59 AM
Post #2

D.I.C Head
**

Joined: 7 Sep, 2007
Posts: 65


My Contributions
Welcome to the community Crawler
first of all you should use the code tags when you post code

after a brief look to your code I would like to point you a few things

1)You have declared four instance variables but there is no constructor method.Either add a constructor method named like the name of your class like this
CODE

public ArraySum(argsuments if needed)
{
     //code if needed
}

In this case you have to change the name of the function you have already written

You don't have to write a constructor method.However if you want the instance variables to be used properly you have to create an object that is the same type with your class.You can write inside main the following
CODE

ArraySum ar =new ArraySum();

The object must be created either way


2)In the function you have calculating the sum there is no return.You want some tob returned so at the bottom of your function add
CODE

return sum;



3)Now if you want the sum to be calculated you have to make a call to the function that calculates the sum using the object you have created.So inside main write this
CODE

sum=ar.ArraySum(arguments);
//ArraySum here is the function you have written



I hope I have helped

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

KYA
RE: Array List With If Statements
6 Feb, 2008 - 05:44 AM
Post #3

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,933



Thanked: 159 times
Dream Kudos: 1375
My Contributions
I also don't see the need to have:

CODE

tax = t;

//code

//in main

tax = 0.1;
tax = t * grosspay;

//code



An extra variable that really isn't needed. Just multiply the tax by the grossPay and add it to the grossPay like this:

CODE


grossPay += tax*grossPay;



--KYA
User is offlineProfile CardPM
+Quote Post

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

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