I am new to java and I am facing a problem like this.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
void init() {
this.acctID = nextID++;
}
}
public class SavingsAccount extends Account {
private double interest;
public SavingsAccount(String n, double bal, double interest) {
super(n, bal);
this.interest = interest;
}
void init() {
}
}
The class SavingsAccount will use its init() and this is wrong. I know I can do this to solve the problem.
public class Account {
private String name;
private double balance;
private static int nextID;
int acctID;
Account(String n, double bal) {
init();
this.name = n;
this.balance = bal;
print();
}
{
this.acctID = nextID++;
}
}
I would like to ask if there any other way to solve this problem, for example force other class to use the Account.init() or don't allow others to make their only init()?
If you make the init() method final, then nobody can override it.
final void init() {
this.acctID = nextID++;
}
http://docs.oracle.com/javase/tutorial/java/IandI/final.html
Related
I have the Accounts Java class as follows:
public class Accounts {
These are the fields
private int accountNumber;
private String accountName;
private double accountBalance;
public Accounts(int accountNumber, String accountName, double accountBalance) {
this.accountNumber = accountNumber;
this.accountName = accountName;
this.accountBalance = accountBalance;
}
public int getAccountNumber() {
return accountNumber;
}
public String getAccountName() {
return accountName;
}
public double getAccountBalance() {
return accountBalance;
}
public void setAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
}
public void setAccountName(String accountName) {
this.accountName = accountName;
}
public void setAccountBalance(double accountBalance) {
this.accountBalance = accountBalance;
}
}
public class AccountController {
ArrayList<Accounts> accounts = new ArrayList<>();
public boolean addAccount(int accountNumber, String accountName, double accountBalance) {
Accounts acc = new Accounts(accountNumber, accountName, accountBalance);
if (findAccount(accountNumber) == null) {
accounts.add(acc);
return true;
}
return false;
}
These are the classes I have:
1: Accounts
2: Account Controller
3: MyMainView
I want to print the list of the Accounts I add to the ArrayList along which contains accountNumber , accountBalance and accountName. I want to add the create the method inside the MyMainView and call it inside the main method of the MyMainView.
You could try overriding the toString() method in your Accounts class.
Next, create a method in Account Controller that iterates over all instances of Accounts, calling their respective toString() method and collecting the outputs.
Now you can call that method in the main in MyMainView.
I am supposed to Add the following method to allow BankAccounts to transfer money to another BankAccount. Where would I add this method to make sure it works for all BankAccounts, including SavingsAccount and CheckingAccount?
The method im supposed to use is transferTo(BankAccount destinationAccount, int transferAmount)
public class BankAccount {
private String accountHolderName;
private String accountNumber;
private int balance;
public BankAccount(String accountHolder, String accountNumber) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = 0;
}
public BankAccount(String accountHolder, String accountNumber, int balance) {
this.accountHolderName = accountHolder;
this.accountNumber = accountNumber;
this.balance = balance;
}
public String getAccountHolderName() {
return accountHolderName;
}
public String getAccountNumber() {
return accountNumber;
}
public int getBalance() {
return balance;
}
// Update the balance by using the DollarAmount.Plus method
public int deposit(int amountToDeposit) {
balance = balance + amountToDeposit;
return balance;
}
// Update the balance by using the DollarAmount.Minus method
public int withdraw(int amountToWithdraw) {
balance = balance - amountToWithdraw;
return balance;
}
public int transferTo(BankAccount destinationAccount, int transferAmount) {
return balance;
}
}
Assumptions:
This is only an exercise and not a real application for a bank.
SavingsAccount and CheckingAccount are subclasses of BankAccount
The method transferTo can be implemented like the following:
public int transferTo(BankAccount destinationAccount, int transferAmount) {
this.balance -= transferAmount;
destinationAccount.deposit(transferAmount);
return balance;
}
In a real world application you need to ensure that this operation will be always atomic and thread-safe. Additionally, using int for the balance is strongly not recommended.
I'm trying to use a setter/getter class in multiple classes to modify a single variable. I understand that I have to use the same object in order for this to work, but I'm unsure on how to make a single object accessible to multiple classes.
public class accountbalance {
private double balance;
//getter
public double getBalance() {
return balance;
}
//Setter
public void setBalance(double newBalance) {
this.balance = newBalance;
}
}
This is where I'm trying to use it first:
public class CreateAccount {
String name;
double social;
int pin;
public CreateAccount()
{
name = "null";
social = 0;
pin = 0;
}
public CreateAccount(String enteredName, double enteredSocial,int enteredPin, double value)
{
name = enteredName;
social = enteredSocial;
pin = enteredPin;
double accountnum = 87495;
accountbalance a1 = new accountbalance();
a1.setBalance(value);
System.out.println(name);
System.out.println(social);
System.out.println("Your new PIN is " + pin);
System.out.println("Your new account balance is " + (a1.getBalance()));
}
}
And then I'm trying to use it again here:
public class deposit {
double enteredAmt;
double amt;
public void deposit() {
System.out.println("Enter an amount to desposit: ");
Scanner in = new Scanner(System.in);
enteredAmt = in.nextDouble();
accountbalance ab1 = new accountbalance();
System.out.println("current balence: " + ab1.getBalance());
amt = ab1.getBalance() + enteredAmt;
ab1.setBalance(amt);
System.out.println("Your new balance is " + (ab1.getBalance()));
}
}
I believe what you're trying to do is use the Command Design Pattern.
If you changed your classes to look more like this, things might work a tad bit better.
public class Account {
// Please do not use SS for an identifier.
private String identifier;
private String name;
// Money needs to be stored in it's lowest common denominator.
// Which in the united states, is pennies.
private long balance;
public Account(String identifier, String name) {
this.identifier = identifier;
this.name = name;
this.balance = 0L;
}
public String getIdentifier() {
return this.identifier;
}
public String getName() {
return this.name;
}
public long getBalance() {
return balance;
}
public void credit(long amount) {
balance =+ amount;
}
public void debit(long amount) {
balance =- amount;
}
}
Then here would be your CreateAccountCommand. It is a bit anemic, but that is ok.
public class CreateAccountCommand {
private String identifier;
private String name;
public CreateAccount(String identifier, String name) {
// Identifier sould actually be generated by something else.
this.identifier = identifier;
name = name;
}
public Account execute() {
return new Account(identifier, name);
}
}
Here is your DepositCommand:
public class DepositCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.credit(amount);
}
}
WithdrawCommand:
public class WithdrawCommand {
private Account account;
private long amount;
public DepositCommand(Account account, long amount) {
this.account = account;
this.amount = amount;
}
public void execute() {
this.account.debit(amount);
}
}
Then run everything:
public class Run {
public static void main(String... args) {
CreateAccountCommand createAccountCommand = new CreateAccountCommand("12345678", "First_Name Last_Name");
Account account = createAccountCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Deposit $100.00
DepositCommand depositCommand = new DepositCommand(account, 10000);
depositCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
// Withdraw $75.00
WithdrawCommand withdrawCommand = new WithdrawCommand(account, 7500);
withdrawCommand.execute();
System.out.println(account.getIdentifier());
System.out.println(account.getName());
System.out.println("Your new account balance in pennies: " + account.getBalance());
}
}
In Java, my basketball players keep sending back "infinity" in my getStats method and I do not know why. Can someone help please? I needed getters and setters so I have that. I am suppose to test the getStats() methods but it errors out every time. At first it was NaNa now its infinity
public class BasketBallPlayer
{
// instance variables - replace the example below with your own
private String name;
private int height;
private int weight;
private double freeThrowsAttempted;
private double freeThrowsMade;
private double twoPointFieldGoalsAttempted;
private double twoPointFieldGoalsMade;
private double threePointersAttempted;
private double threePointersMade;
private int turnovers;
private int assist;
private String stats;
public BasketBallPlayer(String name, int height, int weight, double freeThrowsMade, double twoPointFieldGoalsAttempted,double twoPointFieldGoalsMade, double threePointersAttempted, double threePointersMade, int assist, int turnovers)
{
//identifies the age, name, height-in., weight-lbs
this.name=name;
this.height=height;
this.weight=weight;
this.freeThrowsAttempted=freeThrowsAttempted;
this.freeThrowsMade=freeThrowsMade;
this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
this.threePointersMade=threePointersMade;
this.turnovers=turnovers;
this.assist=assist;
}
public BasketBallPlayer(int weight, int height, String name)
//identifies the weight(lbs.), height(inches) and String name
{
//identifies the name, height-in., weight-lbs
this.name=name;
this.height=height;
this.weight=weight;
}
//Sets the Name
public void setName(String name)
{
this.name=name;
}
//Sets the Height
public void setHeight (int height)
{
this.height=height;
}
//Sets the Weight
public void setWeight (int weight)
{
this.weight=weight;
}
//Sets the Free Throws Attempted
public void setFreeThrowsAttempted( double freeThrowsAttempted)
{
this.freeThrowsAttempted=freeThrowsAttempted;
}
//Sets the Free Throws Made
public void setFreeThrowsMade(double freeThrowsMade)
{
this.freeThrowsMade=freeThrowsMade;
}
// Sets two Point Field Goals Attempted
public void setTwoPointFieldGoalsAttempted (double twoPointFieldGoalsAttempted)
{
this.twoPointFieldGoalsAttempted=twoPointFieldGoalsAttempted;
}
public void setTwoPointFieldGoalsMade (double twoPointFieldGoalsMade)
{
this.twoPointFieldGoalsMade=twoPointFieldGoalsMade;
}
public void setThreePointerAttempted(double threePointersAttempted)
{
this.threePointersAttempted=threePointersAttempted;
}
public void setThreePointersMade(double threePointersMade)
{
this.threePointersMade=threePointersMade;
}
public void setTurnovers(int turnovers)
{
this.turnovers=turnovers;
}
public void setAssist(int assist)
{
this.assist=assist;
}
//Returns a Name
public String getName()
{
return name;
}
public int getHeight ()
{
return height;
}
public int getWeight ()
{
return weight;
}
public double getFreeThrowsAttempted()
{
return freeThrowsAttempted;
}
public double getfreeThrowsMade()
{
return freeThrowsMade;
}
public double getTwoPointFieldGoalsAttempted ()
{
return twoPointFieldGoalsAttempted;
}
public double getTwoPointFieldGoalsMade ()
{
return twoPointFieldGoalsMade;
}
public double getThreePointerAttempted()
{
return threePointersAttempted;
}
public double getthreePointersMade()
{
return threePointersMade;
}
public int getTurnovers()
{
return turnovers;
}
public int gettAssist()
{
return assist;
}
/** The geStats Method allows you to get all information on the player in print. All Percentages
*
*/
public void getStats()
{
double Percentage1;
double Percentage2;
double Percentage3;
Percentage1=(double)((twoPointFieldGoalsMade*100)/twoPointFieldGoalsAttempted);
Percentage2=((double)(threePointersMade*100)/threePointersAttempted);
Percentage3=((double)((freeThrowsMade*100)/freeThrowsAttempted));
System.out.println("*************************");
System.out.println("BasketBall Player Name:" + name);
System.out.println("Field Goal Percentage:" + Percentage1 +"%");
System.out.println("3 Pointer Percentage:" + Percentage2 +"%");
System.out.println("Free Throw Percentage:" + Percentage3 +"%");
System.out.println("Assist to Turnover Ration:" + assist/turnovers);
System.out.println("*************************");
}
}
First, you should not be using doubles for these number. It just accounts to inefficiency and makes no sense. However, when using ints, you have to note that you cannot directly get a percentage when dividing two ints. This piece of code should work, you may want to include some code that checks that the divisors are not 0.
private int freeThrowsAttempted;
private int freeThrowsMade;
private int twoPointFieldGoalsAttempted;
private int twoPointFieldGoalsMade;
private int threePointersAttempted;
private int threePointersMade;
...
double percentage1 = (twoPointFieldGoalsMade * 100F) / twoPointFieldGoalsAttempted;
double percentage2 = (threePointersMade * 100F) / threePointersAttempted;
double percentage3 = (freeThrowsMade * 100F) / freeThrowsAttempted;
I have an abstract class that has 4 methods for crediting, debiting, depositing and withdrawing. I am stuck on how I should go about setting up the methods for crediting and debiting the account, since I would assume it would be the same as deposit and withdrawal, but I don't believe it is. There is already a test-harness that she gave us, so I just need to setup the classes. This is the abstract class
abstract public class Account implements PostInterest
{
private String fName;
private String lName;
private String nbrSSN;
private char acctStatus = 'A';
private double balance = 0.0;
//five-argument constructor
public Account(String first, String last, String ssn, char status, double begBalance)
{
setName(first);
setLast(last);
setSSN(ssn);
setStatus(status);
setBalance(begBalance);
}
public Account(String first, String last, String ssn)
{
setName(first);
setLast(last);
setSSN(ssn);
}
public void setName(String first)
{
fName = first;
}
public String getName()
{
return fName;
}
public void setLast(String last)
{
lName = last;
}
public String getLast()
{
return lName;
}
public void setSSN(String ssn)
{
nbrSSN = ssn;
}
public String getSSN()
{
return nbrSSN;
}
public void setStatus(char status)
{
acctStatus = status;
}
public char getStatus()
{
return acctStatus;
}
public void setBalance(double begBalance)
{
balance = begBalance;
}
public double getBalance()
{
return balance;
}
public abstract void debitAccount(double amount);
public abstract void creditAccount(double amount);
public abstract void deposit(double amount);
public abstract void withdraw(double amount);
}
And this is one of the classes that I have for savings. I need a few other classes, but I was stuck on the difference in setting up the credit and debit compared to the deposit and withdrawal if that makes sense.
public class Savings extends Account
{
private double intRate;
public Savings(String first, String last, String ssn, double begBalance, double rate)
{
super(first, last, ssn, begBalance);
setRate(rate);
}
Public Savings(String first, String last, String ssn, double rate)
{
super(first, last, ssn);
setRate(rate);
}
public void setRate(double rate)
{
intRate = rate;
}
public double getRate()
{
return intRate;
}
#Override
public void debitAccount(double amount)
{
}
#Override
public void creditAccount(double amount)
{
}
#Override
public void deposit(double amount)
{
balance += amount;
}
#Override
public void withdraw(double amount)
{
if(amount > balance)
{
balance = -1;
}
else
{
balance -= amount;
}
}
#Override
public void postMonthly()
{
((intRate/12)* balance);
}
}
I haven't setup the postMonthly method yet, but she wants us to calculate the interest for the month based on the current balance and intRate and it invokes creditAcct(), passing result of interest calculation, which confused me a bit too. Any help would be greatly appreciated!