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 3 of 3

Thread: Object Oriented Programming

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Location
    Las Vegas
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    return total;
    }
    }

    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.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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).

  3. The Following User Says Thank You to copeg For This Useful Post:

    GoodbyeWorld (November 20th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Object Oriented Programming

    Quote Originally Posted by copeg View Post
    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 [console] Your output [/console] for your output.

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


    Your code doesn't make sense here:
    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.

     
    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.

Similar Threads

  1. Coding on Object oriented Programming
    By JavaAddict in forum Object Oriented Programming
    Replies: 3
    Last Post: November 14th, 2013, 08:53 AM
  2. What Exactly Object Oriented Programming is ??
    By diyaots in forum Object Oriented Programming
    Replies: 3
    Last Post: September 10th, 2013, 08:08 AM
  3. Object Oriented Programming
    By calebite207 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2012, 12:27 PM
  4. Object oriented programming
    By merr78 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 9th, 2012, 01:32 AM
  5. Object oriented programming
    By jonnitwo in forum Object Oriented Programming
    Replies: 8
    Last Post: September 2nd, 2011, 12:18 PM

Tags for this Thread