Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help desperately

Status
Not open for further replies.

rshafer45

Programmer
Jul 10, 2003
1
US
I am a newbie in java and I need some help

class javaCalculator{

public static void main(String args[]){
float taxRate,mainAmount;
javaCalculator(){This line says semicolon expected and it doesn't like javaCalculator. How do I fix the problem
}
// accessor method
public float getTaxRate(){return taxRate;}
public float getAmount{return mainAmount;}
//mutator method
public void setTaxRate(float this.taxRate){this.taxRate=taxRate;}
public boolean addAmount(float amount){
if(amount>=0)
return true;
else
false;
}
public void delAmount(float amount){
float sub=MainAmount-amount;
}
public float add(float value1,float value2){
return value1+value2;
}
public float totalTax(){
}
 
Well...there are a couple of things I noticed...

1. is
Code:
javaCalculator
suppose to be a construtor? if so then it should look something like:
Code:
public javaCalculator

2.
Code:
public static void main(String args[])
looks to be in the wrong place.

3. the line
Code:
public void setTaxRate(float this.taxRate){this.taxRate=taxRate;}
also looks incorrect, I think it should be (having the keyword
Code:
this
is incorrect):
Code:
public void setTaxRate(float taxRate){this.taxRate=taxRate;}

4. The method
Code:
public float totalTax(){
has a return type of a float however the method is not returning a value. This will/should cause a complier error.

5. Another error that would occur is with the
Code:
addAmount[code] method, the reason is you do not have a return statement (see below in red)
it should be:
 public boolean addAmount(float amount){
            if(amount>=0)
            return true;
            else
            [b][COLOR=red]return[/color][/b] false;

6. just stuff I noticed: in your class you have no way to define [code]mainAmount
, should this variable always be zero?(or am I missing something)...

I took your code, and modified it (a little) to give you a working a example to go off (note the below code is sloppy, I did rushing...)

Am I'm not NOT a expert in JAVA...Still learning myself :)), so I hope this helps.

Code:
import java.util.*;
import java.text.*;

class javaCalculator
{
	float taxRate,mainAmount;

	public javaCalculator()
	{   }

                // accessor method
        public float getTaxRate()
        {
			return taxRate;
		}
        public float getAmount()
        {
			return mainAmount;
		}
                //mutator method
        public void setTaxRate(float taxRate)
        {
			this.taxRate=taxRate;
		}
        public boolean addAmount(float amount)
        {
            if(amount>=0)
            return true;
            else
            return false;
        }
        public void delAmount(float amount)
        {
            float sub=mainAmount-amount;
        }
        public void add(float value1,float value2)
        {
			mainAmount = value1+value2;

        }
        public float totalTax()
        {
			float totalTax = getAmount()* getTaxRate();
			return totalTax;
        }

	 public static void main(String args[])
     {
		int numberOfArgs = args.length;
		if (numberOfArgs < 1)
		{
			System.out.println(&quot;There was an error trying to process this order, you order no items.&quot;);
		}
		else
		{
			float[] shoppingBasket = new float[args.length];

			for (int a = 0; a < numberOfArgs; a++)
			{
				shoppingBasket[a] = Float.parseFloat(args[a]);
			}

			javaCalculator jC = new  javaCalculator();
			final float TAX_RATE = (float).045; //Setting Tax rate to 4.5%
			jC.setTaxRate(TAX_RATE);

			int itemsProcessed = 0;
			//float item;

			for (int i = 0; i < numberOfArgs; i++)
			{
				itemsProcessed++;
				if(jC.addAmount(shoppingBasket[i]))
				{
					;
					jC.add(jC.getAmount(), shoppingBasket[i]);
				}
				else
				{
					System.out.println(&quot;An error occured, while trying to process item number &quot; + itemsProcessed +
									&quot;. This item was not added. System will continue to process this orber.&quot;);
				}

			}
			NumberFormat formatter = NumberFormat.getCurrencyInstance();
			formatter.setMaximumFractionDigits(2);

			NumberFormat formatPercent = NumberFormat.getPercentInstance();
			formatPercent.setMaximumFractionDigits(2);

			//Printing the invoice
			System.out.println(&quot;\n****** Your Order *******&quot;);
			System.out.println(&quot;Items \t\tYour Cost&quot;);
			System.out.println(&quot;-----\t\t---------&quot;);
			for (int t = 0; t < shoppingBasket.length; t++)
			{
				System.out.println(&quot;Item &quot; + (t+1) + &quot;\t\t&quot; + formatter.format(shoppingBasket[t]));
			}
			System.out.println(&quot;\t\t------&quot;);
			System.out.println(&quot;Total Cost\t&quot; + formatter.format(jC.getAmount()));
			System.out.println(&quot;SaleTax \t&quot; + formatPercent.format(jC.getTaxRate()));
			System.out.println(&quot;Total Amount\t&quot; + formatter.format(jC.totalTax()));
			System.out.println(&quot;\t\t--------&quot;);
			System.out.println(&quot;\t\t--------&quot;);
			System.out.println(&quot;Amount due \t&quot; + formatter.format(jC.getAmount() + jC.totalTax()));

		}
	}
}

***** OUTPUT ******

C:\>java -cp .; javaCalculator 100.00 100.00 200.00

****** Your Order *******
Items Your Cost
----- ---------
Item 1 $100.00
Item 2 $100.00
Item 3 $200.00
------
Total Cost $400.00
SaleTax 4.5%
Total Amount $18.00
--------
--------
Amount due $418.00




 
&quot;5. Another error that would occur is with the addAmount method, the reason is you do not have a return statement (see below in red)
it should be:
public boolean addAmount(float amount){
if(amount>=0)
return true;
else
return false;&quot;

OR


public boolean addAmount(float amount){
return (amount>=0)}


Salih Sipahi
Software Engineer.
City of Istanbul Turkey
s.sipahi@sahinlerholding.com.tr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top