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

Thread: New to Java: Need help!

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New to Java: Need help!

    I'm taking a Java programming course at my local community college and currently, I'm working on a homework assignment. I'll upload the assignment in word format for the post. My big problem is I can't seem to get the program to display the initial balance instead of the actual balance if a withdrawal amount is greater than the initial balance. You'll understand what I mean after reading the attachment. Any help would be appreciated. Thank you!
    Attached Files Attached Files
    Last edited by vannatrieu; September 30th, 2010 at 05:11 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,609
    Thanks
    25
    Thanked 316 Times in 295 Posts

    Default Re: New to Java: Need help!

    You need to provide us with any of your current code so we can help you. Without having the code, we can't really see how you are doing it now and we can't really help you out.

  3. #3
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java: Need help!

    I'm sorry! I will provide some code.

  4. #4
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java: Need help!

    public class Account
    {
    private double balance;

    public Account( double initialBalance )
    {

    if (initialBalance > 0.0 )
    balance = initialBalance;

    }

    public void credit( double amount )
    {
    balance = balance + amount;
    }

    public void debit( double amount )
    {
    if ( amount > balance )
    System.out.println( "Debit amount exceeds account balance. " );

    balance = balance - amount;

    }


    public double getBalance()
    {
    if ( amount > balance)
    return balance;
    }
    }

    That was the first part of the program. Here's the second part:


    import java.util.Scanner;

    public class AccountTest
    {
    public static void main( String args[] )
    {
    Account account1 = new Account(50.00);
    Account account2 = new Account(-7.53);

    System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );

    Scanner input = new Scanner( System.in );
    double withdrawalAmount;

    System.out.print( "Enter withdrawal amount for account1: " );
    withdrawalAmount = input.nextDouble();
    System.out.printf( "\nsubtracting %.2f from account1 balance\n\n", withdrawalAmount );
    account1.debit( withdrawalAmount );

    System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );

    System.out.print( "Enter withdrawal amount for account2: " );
    withdrawalAmount = input.nextDouble();
    System.out.printf( "\nsubtracting %.2f from account2 balance\n\n", withdrawalAmount );
    account2.debit( withdrawalAmount );

    System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() );
    }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: New to Java: Need help!

    Please put your code in code tags. Info here: Java Forums - BB Code List

    Can you execute your program, copy all of the contents of the window showing your input and the programs output and describe what is wrong with what the program outputs and what you'd like it to be.

    I just looked at the debit() method. What is that method supposed to do?
    The same about the getBalance method.

    Does your program compile?
    If you are getting errors, please copy and paste here the full text of the error messages.

  6. #6
    Junior Member
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New to Java: Need help!

     
    public class Account
    {
    	private double balance;
     
    	public Account( double initialBalance )
    	{
     
    		if (initialBalance > 0.0 )
    		balance = initialBalance;
     
    	}
     
    	public void credit( double amount )
    	{
    		balance = balance + amount;
    	}
     
    	public void debit( double amount )
    	{
    		if ( amount > balance )
    		System.out.println( "Debit amount exceeds account balance. " );
     
    		balance = balance - amount;
     
    	}
     
     
    	public double getBalance()
    	{
    		return balance;
    	}
    }
     
     
    //second java file 
     
     
    import java.util.Scanner; 
     
    public class AccountTest
    {
    	public static void main( String args[] )
    	{
    		Account account1 = new Account(50.00);
    		Account account2 = new Account(-7.53);
     
    		System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    		System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );
     
    		Scanner input = new Scanner( System.in );
    		double withdrawalAmount;
     
    		System.out.print( "Enter withdrawal amount for account1: " );
    		withdrawalAmount = input.nextDouble();
    		System.out.printf( "\nsubtracting %.2f from account1 balance\n\n", withdrawalAmount );
    		account1.debit( withdrawalAmount );
     
    		System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    		System.out.printf( "account2 balance: $%.2f\n\n", account2.getBalance() );
     
    		System.out.print( "Enter withdrawal amount for account2: " );
    		withdrawalAmount = input.nextDouble();
    		System.out.printf( "\nsubtracting %.2f from account2 balance\n\n", withdrawalAmount );
    		account2.debit( withdrawalAmount );
     
    		System.out.printf( "account1 balance: $%.2f\n", account1.getBalance() );
    		System.out.printf( "account2 balance: $%.2f\n", account2.getBalance() ); 
    	}
    }

    The problem with the output is that when I input a debit value higher than the initial balance, $50.00, instead of outputting a value of -$50.00, I want the program to output the initial balance of $50.00 instead. The same for account 2.

    The debit method subtracts a value from the initialBalance in account1 and account2.

    My program compiles without errors, but I was just wondering how to properly code it so that the program displays the initial balance instead of a negative value if I input a debit value that is higher than the initial balance in the account.

    In my first post, the attachment better describes what I'm supposed to do. Thank you so much for your help. I've worked on this assignment for about 5 hours already. Also, I don't know how to do the bb code properly, but I'll try again.
    Attached Images Attached Images
    Last edited by vannatrieu; September 30th, 2010 at 06:00 PM.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: New to Java: Need help!

    The debit method subtracts a value from the initialBalance in account1 and account2.
    Is it supposed to always subtract? Even if the balance goes negative?
    Look at the code for the debit method. What does it do if the amount is greater than the balance?


    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.