# Object Oriented Programming

• November 20th, 2013, 02:46 PM
cruzer66
Object Oriented Programming
I wrote this OOP code which is supposed to add the total at the end. The code I wrote has a current balance of .41 cents, then it will ask for total pennies, nickels, dimes and quarters. It is supposed to add those to the current balance but it is not. I appreciate any help in advance. cruzer

Here is the private code:
public class Bank
{
private double pennies;
private double nickels;
private double dimes;
private double quarters;
private double total;

public Bank()
{
pennies = .01;
nickels = .05;
dimes = .10;
quarters = .25;

}

public void setPennies(double p)
{
if (p >= 0)
{
pennies = p;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getPennies()
{
return pennies;

}

public void setNickels(double n)
{
if (n >= 0)
{
nickels = n;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getNickels()
{
return nickels;
}

public void setDimes(double d)
{
if (d >= 0)
{
dimes = d;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}

public double getDimes()
{
return dimes;
}

public void setQuarters(double q)
{
if (q >= 0)
{
quarters = q;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}

public double getQuarters()
{
return quarters;
}

public double total()
{
double total;
total = pennies + nickels + quarters + dimes;
}
}

AND HERE IS MY MAIN CODE:

import java.util.Scanner;
import java.text.*;

public class UseBank
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
Bank e1 = new Bank();
double pennies;
double nickels;
double dimes;
double quarters;
int total;

DecimalFormat df = new DecimalFormat("#.##");
System.out.println(" Your current balance is: " + (df.format(e1.total())) );

System.out.print("How much pennies: ");
pennies = kbd.nextDouble();
e1.setPennies(pennies);

System.out.print("How much nickels: ");
nickels = kbd.nextDouble();
e1.setNickels(nickels);

System.out.print("How much dimes: ");
dimes = kbd.nextDouble();
e1.setDimes(dimes);

System.out.print("How much quarters: ");
quarters = kbd.nextDouble();
e1.setQuarters(quarters);

System.out.println("Your current balance is: " + e1.total());
}
}

Thanks again.
• November 20th, 2013, 03:17 PM
copeg
Re: Object Oriented Programming
Welcome to the forums. Please read the announcements at the top of every subforum. In it you will see how to properly format your code so that it is readable, as well as the policy on posting (your duplicate post has been locked).
• November 20th, 2013, 04:02 PM
GoodbyeWorld
Re: Object Oriented Programming
Quote:

Welcome to the forums. Please read the announcements at the top of every subforum. In it you will see how to properly format your code so that it is readable, as well as the policy on posting (your duplicate post has been locked).

Use the tags [highlight=java] your code [/highlight] .

Use [exception] Exceptions/errors [/exception] for errors and exceptions that you might get.

Your code doesn't make sense here:
Code java:

```if (p >= 0) { pennies = p; } else { System.out.println("Please enter a numer greater than zero!"); }```

You accept 0 in the if , but you tell them to enter a number greater than 0 if they enter something negative when you do the println in the else.

You seem to be doing the same thing with the nickels.

Note: Your set methods don't really rectify the issue if you enter a negative number. Hence, if you entered a negative, it would not fix it.

I think I may see something with your code that might be an issue.

I'm not sure if you want to start with one penny, one nickel, one dime, and one quarter, or if you are trying to set the values of penny, nickel, dime, quarter.

Assuming it's the latter, you'd be better off with a final constant value for each.

Code java:

```  final double PENNY = 0.01; final double NICKEL = 0.05; final double DIME = 0.10; final double QUARTER = 0.25;```

You could make your pennies, nickels, dimes, and quarters now be of type int instead of double.

To get your total, just multiply your penny count by PENNY, your nickel count by NICKEL, and the same with dimes and quarters.