Results 1 to 7 of 7
- 03-06-2011, 11:55 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Help with java
This illustrates one of the important functions of methods. They should guarantee that the instance data of the object is always valid. In this case the methods (and constructor) prevent the balance from ever being negative. They also prevent reject invalid parameters.
You should test all the modified methods and constructors but you do not need to submit a Tester class
So this is what i have so far, can anyone help me with it?
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
public static void main(String[] args)
{
double balance;
int transaction;
if (balance < 0)
System.out.println("ERROR NEGATIVE AMOUNT");
else
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
balance = 0;
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
}
/**
Deposits money into the bank account./**
Im not exactly sure if i have to make a tester class or not, because i do not know how to combine the classes.
/**
A class to test the BankAccount class.
*/
public class BankAccountTester
{
/**
Tests the methods of the BankAccount class.
@param args not used
*/
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
harrysChecking.deposit(2000);
harrysChecking.withdraw(500);
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1500");
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1498");
harrysChecking.deposit(1000);
harrysChecking.withdraw(500);
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
System.out.println("Expected: 1996");
}
}Last edited by armedrabbit; 03-07-2011 at 05:54 PM. Reason: accidently double paste
- 03-07-2011, 12:05 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
in the methods where you want to only accept a positive number just put an if clause to test that they are positive. If the argument is positive, continue as normal, if not print that the number needs to be positive and don't make any changes.
- 03-07-2011, 01:30 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
- 03-07-2011, 01:47 AM #4
To be blunt, your code is a mess.
You have the BankAccount constructors and methods inside a main method. You also have 2 BankAccountTester classes, one inside BankAccount and one separate. Clean your code up and things might look better and be easier to see what needs to be done.
- 03-07-2011, 01:55 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
sorry Junky, i accidently double pasted. but i'll try to fix it right now...
- 03-07-2011, 02:34 AM #6
Member
- Join Date
- Mar 2011
- Posts
- 10
- Rep Power
- 0
Here's my updated program can you guys tell me if i did it right? :confused:
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
*/
public class BankAccount
{
double balance;
int transaction;
/**
Constructs a bank account with a zero balance.
*/
public BankAccount()
{
if (balance < 0)
System.out.println("ERROR NEGATIVE AMOUNT");
}
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
public BankAccount(double initialBalance)
{
balance = initialBalance;
initialBalance = 0;
}
/**
Deposits money into the bank account.
@param amount the amount to deposit
*/
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
transaction = transaction +1;
if (balance < 0)
System.out.println("ERROR NEGATIVE AMOUNT");
}
/**
Withdraws money from the bank account.
@param amount the amount to withdraw
*/
public void withdraw(double amount)
{
double newBalance = balance - amount;
balance = newBalance;
transaction = transaction +1;
if (balance < 0)
System.out.println("ERROR NEGATIVE AMOUNT");
}
/**
Gets the current balance of the bank account.
@return the current balance
*/
public double getBalance()
{
if (balance < 0)
System.out.println("ERROR NEGATIVE AMOUNT");
return balance;
}
/**
Charges monthly fee
*/
public void endOfMonth()
{
double newbalance = balance - transaction;
balance = newbalance;
transaction = 0;
}
}
-----------------------------------------------------------------------
/**
A class to test the BankAccount class.
*/
public class BankAccountTester
{
/**
Tests the methods of the BankAccount class.
@param args not used
*/
public static void main(String[] args)
{
BankAccount harrysChecking = new BankAccount();
harrysChecking.deposit(2000);
harrysChecking.withdraw(2100);
System.out.println(harrysChecking.getBalance());
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
harrysChecking.deposit(1000);
harrysChecking.withdraw(500);
harrysChecking.endOfMonth();
System.out.println(harrysChecking.getBalance());
}
}
- 03-07-2011, 03:12 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 30
- Rep Power
- 0
import java.util.Scanner;
class BankAccount
{
public Scanner S=new Scanner(System.in);
public double balance,amnt,initialBalance;
/**
Constructs a bank account with a given balance.
@param initialBalance the initial balance
*/
void newBankAccount()
{
System.out.println("Enter The initail Balance");
double initialBalance=S.nextDouble();
if(initialBalance<0)
balance = 0;
else
balance=initialBalance;
}
/**
Deposits money into the bank account.*/
void deposit()
{
System.out.println("Enter The amount to be deposited");
double amnt=S.nextDouble();
if(amnt<0)
{
balance=balance;
System.out.println("This value can not be deposited");
}
else
balance=balance+amnt;
}
/** Withrawl money from bank account*/
void withdrawls()
{
System.out.println("Enter The amount to be withdrwaled");
double amnt=S.nextDouble();
if(amnt<0)
{
balance=balance;
System.out.println("This value can not be withdrawdled");
}
else
balance=balance-amnt;
}
void getBalance()
{
System.out.println("The Balance is:"+balance);
}
}
/**
A class to test the BankAccount class.
*/
public class BankAccountTester
{
/**
Tests the methods of the BankAccount class.
@param args not used
*/
public static void main(String[] args)
{
BankAccount h = new BankAccount();
Scanner S=new Scanner(System.in);
int ch;
do
{
System.out.println("1.To create a new account");
System.out.println("2.To deposit the amount in account");
System.out.println("3.To Withdrawal tha amount from account");
System.out.println("4.to get the current balance");
System.out.println("5.Exit");
System.out.println("Enter the choice");
ch=S.nextInt();
switch(ch)
{
case 1: h.newBankAccount();
break;
case 2:
h.deposit();
break;
case 3:
h.withdrawls();
break;
case 4: h.getBalance();
break;
case 5: System.exit(0);
break;
default:
System.out.println("Invalid");
}
}while(ch>1||ch<5);
}
}
Similar Threads
-
Mod of Negative Numbers
By Venny in forum New To JavaReplies: 7Last Post: 01-28-2011, 05:32 AM -
Byte value comes out negative
By Bluefox815 in forum New To JavaReplies: 1Last Post: 07-03-2010, 03:52 AM -
I don't want negative money [BUG]
By anthonym2121 in forum New To JavaReplies: 1Last Post: 04-07-2009, 08:06 AM -
what's the fastest method of storing and retrieving large amounts of data in Java?
By 2potatocakes in forum New To JavaReplies: 1Last Post: 12-28-2008, 10:25 AM -
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12-15-2008, 04:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks