i a beginner at java and I'm trying to compile 2 java codes Account.java and AccounTest.java and the command prompt keeps giving this message
"Main method not found in class Account, please define main method or a javaFX application class must extend javafx.application.Application"
researched on javafx apication and none was specific on what and where i should import. here is Account.java
"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 double getBalance()
{
return balance;
}
}
thanks in advance
Related
I need to make a menu so the user can choose what he wants to do with the bank app I am building.
user has the following options:
>1. Select a bank account.
>2. Check the account balance.
>3. Depositing money into the account
>4. Withdraw money from the account
>5. Transfer money from one account to another account
To enable this functionality we will add the following methods to the BankAccountService as you can see in the UML scheme below.
>1. getAccount (String accountNumber)
>2. getAccountBalance (Account account)
>3. deposit (Account account, double amount)
>4. withdraw (Account account, double amount)
>5. transfer (Account source, Account target, double amount)
UML
package BankProject;
import java.util.*;
public class BankAccountService {
private BankAccount[] accounts;
public void addBankAccount(final BankAccount account)
{
if (accounts == null)
{
accounts = new BankAccount[10];
}
int index = 0;
while (accounts[index] != null)
{
index++;
if (index >= accounts.length)
{
accounts = Arrays.copyOf(accounts, accounts.length + 10);
}
}
accounts[index] = account;
}
public void getAccountBalance(final BankAccount account) {
double getAccountBalance = new BankAccount(getAccountBalance());
}
}
As you can I get an error when I try to add the method.
Error
Please keep in mind that I have never coded before and I have no idea why this error is showing.
Can anyone tell me the right way to add this method?
I did figure out a way to get all the account balances in the main folder by adding this code:
double balance = bankAccountService.getAccountBalance();
System.out.println(balance);
double balance2 = account1.balance;
System.out.println(balance2);
double balance3 = account2.balance;
System.out.println(balance3);
But in the UML it is asking me to add the code in BankAccountService.
I have no clue why this is needed if can do it from main.
If the balance property in BankAccount is private you should create a getter for it and then :
public void getAccountBalance(BankAccount account) {
double accountBalance = account.getBalance();
}
So in BankAccount.java you should add:
public double getBalance() {
return this.balance;
}
So, I followed the online youtube tutorial for this, and it worked for the tutorial instructor, but not me... However, only because it wants a "getBalance" class in the source classes. If someone could help me with this, it would be greatly appreciated. I'm new to Java, and I do have some reading to catch up on in the book... So here's the program and its class setup:
BankAccountDemo.java
package bankaccountdemo;
import java.text.DecimalFormat;
public class BankAccountDemo {
public static void main(String[] args) {
BankAccount account1 = new BankAccount(12.00);
BankAccount account2 = new BankAccount(account1);
DecimalFormat dollar = new DecimalFormat("#.##0.00");
System.out.print("The balance in account #1 is $" + dollar.format(account1.getBalance()));
System.out.print("The balance in account #2 is $" + dollar.format(account2.getBalance()));
}
}
BankAccount.java
package bankaccountdemo;
public class BankAccount {
private double balance;
public BankAccount() {
balance = 0.0;
}
public BankAccount(BankAccount obj) {
balance = obj.balance;
}
public BankAccount(double startBalance) {
balance = startBalance;
}
}
I am aware that this is a pretty simple fix, but as I said.. I have some reading to catch up on. I understood the lottery problem better than this very simple bug.
it wants a "getBalance" class in the source classes.
It doesn't want a class it wants a method that is called on your bankaccount object.
account1.getBalance()
So you need to create a method in your BankAccount class.
public double getBalance(){
return balance;
}
This function is called a getter function. In OOP languages an object's properties are usually created as private and can be modified/set and read/get using this setter and getter functions.
so you can create another function like
public void setBalance(double balance){
this.balance = balance;
}
and then you can use
account1.setBalance(10.0);
to set the amount to 10.0
As you said, you need a getBalance() method in BankAccount:
public double getBalance() {
return balance;
}
This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 6 years ago.
Can someone tell me why I'm getting the error Cannot find symbol when I'm creating an object? in bank_account Christine = new bank_account();
public class BankAccountTester
{
public static void main (String []args)
{
bank_account Christine = new bank_account();
Christine.deposit(10000);
Christine.withdraw(2000);
System.out.println(Christine.getBalance());
}
}
this is my class
public class bank_account{
private double balance;
public bank_account()
{
balance = 0;
}
public bank_account (double initialBalance)
{
balance = initialBalance;
}
public void deposit(double amount)
{
balance = balance + amount;
}
public void withdraw (double amount)
{
balance = balance - amount;
}
public double getBalance()
{
return balance;
}
}
The program is fully functional, I've tried your code and it works fine.
This is your compact code: https://gist.github.com/anonymous/8f055d981b27de4ce75c8c64d9a58498
Just go on www.compilejava.net and 'compile and run' it there.
It works on compilejava.net and on my netbeans too.
I think there's a problem with your compiler. You can try to reset it or even clean the cache: Deleting this directory should clear the cache for you- C:\Users\username.netbeans\7.0\var\cache //adapt with your version
An other error could be in creating project. In netbeans you have to create a project for each app you're developing. Have you did it and connected the 2 file of the program?
Need help with my school work, I'm very much new to programming. So the requirements are:
Class Account, change the visibility of all data to protected. Change the withdraw method so that it becomes an abstract method. This action will necessitate you declaring the class as abstract. Deposits should work the same way in OnLineAccount and SavingsAccount so make sure they cannot override their parent’s version.
The OnLineAccount class has one additional attribute to that of class Account, minimumBalance. All instances of OnLineAccount are created with the minimumBalance amount set to $500. If transactions of any OnLineAccount cause the balance to go below minimumBalance, a $25 fee is subtracted from the account balance. Override the toString method to display everything the Account toString displays plus a message dependent upon the balance. If the balance is below the minimumBalance, a message stating that a $25 fee has been already been subtracted needs to alert the customer. Use the parent class toString to do most of the work.
The SavingsAccount class has one additional attribute to that of class Account, overdraft. All instances of SavingsAccount are created with the overdraft amount set to -$1000. An overdraft amount is the amount an object of SavingsAccount class may allow the balance to go to. Implement the withdraw method so that overdrafts are allowed up to the amount stored in overdraft. Any withdrawals that allow the balance to drop below zero and up to the overdraft amount are allowed but the overdraft fee of $30 is incurred each time a transaction causes the balance to be below zero.
Override the toString method to display everything the Account toString
displays plus a message dependent upon the balance. If the balance is below zero, a message stating that the person is in overdraft and a $30 fee has been already been incurred. Use the parent class toString to do most of the work.
Create a driver class with an array of 5 objects of Account, being some instances of the child classes OnLineAccount or SavingsAccount. Systematically test the full functionality of both child classes.
================================
So I created 4 class file, it shows all account info but also showing negative balance which should be an error message instead cause balance should not be less than $500.
Here are my class files:
Account class:
import java.text.NumberFormat;
import java.util.Locale;
public abstract class Account {
private static int defaultAccountNo = 12345;
protected String owner;
protected int accountNo;
protected double balance;
protected Account(String owner, double intialDeposit){
this.owner = owner;
this.balance = intialDeposit;
defaultAccountNo++;
this.accountNo = defaultAccountNo;
}
public final boolean deposit(double amount){
if(amount<=0){
System.err.println("Negative amount can't be deposited");
return false;
}
balance = balance+amount;
return true;
}
protected abstract boolean withdrawl(double amount);
public String getBalance(){
return formatter.format(balance);
}
public int getAccountNo(){
return accountNo;
}
public void setOwner(String owner){
this.owner = owner;
}
protected void addInterest(){
double interest = 0.05*balance;
balance = balance+interest;
}
public boolean equals(Account account){
if(this.owner.equalsIgnoreCase(account.owner))
return true;
else
return false;
}
NumberFormat formatter = NumberFormat.getCurrencyInstance();
public String toString(){
return "AccountNo: "+accountNo+"\nBalance: "+formatter.format(balance);
}
}
OnlineAccount Class:
public class OnLineAccount extends Account{
public OnLineAccount(String owner, double intialDeposit) {
super(owner, intialDeposit);
}
private static final double MINIMUM_BALANCE = 500;
public boolean withdrawl(double amount){
if(amount<0){
System.err.println("Negative amount cannot be withdrawn");
return false;
}
if((balance-amount)<0){
System.err.println("Not enought balance");
return false;
}
balance = balance-amount;
if(balance<MINIMUM_BALANCE)
balance = balance - 25;
return true;
}
public String toString(){
String returnString;
if(balance<500){
returnString = super.toString()+"\n$25 fee has been already been subtracted as account balance reached below minimum";
return returnString;
}
return super.toString();
}
}
SavingsAccount Class:
public class SavingsAccount extends Account{
private static final double DEFAULT_OVERDRAFT = -1000;
public SavingsAccount(String owner, double intialDeposit) {
super(owner, intialDeposit);
}
public boolean withdrawl(double amount) {
if(amount<0){
System.err.println("Negative amount cannot be withdrawn");
return false;
}
if((balance-amount-30)<DEFAULT_OVERDRAFT){
System.err.println("Not enough balance, overdraft reached");
}
if((balance-amount)<0){
balance = balance-amount-30;
return true;
}
return false;
}
public String toString(){
String returnString;
if(balance<500){
returnString = super.toString()+"\nYour are in overdraft and $30 fee has been already been subtracted.";
return returnString;
}
return super.toString();
}
}
TestAccount Class:
import java.util.Random;
public class TestAccount {
public static void main(String[] args) {
Random ran = new Random();
Account[] accountArray = new Account[5];
Account acc1 = new OnLineAccount("Bill", 1000);
Account acc2 = new OnLineAccount("Susan", 1500);
Account acc3 = new SavingsAccount("William", 2500);
Account acc4 = new SavingsAccount("Bill", 9000);
Account acc5 = new SavingsAccount("Bruce", 1355);
accountArray[0] = acc1;
accountArray[1] = acc2;
accountArray[2] = acc3;
accountArray[3] = acc4;
accountArray[4] = acc5;
for(int i=0; i<accountArray.length; i++){
System.out.println("Initial details of Account....");
System.out.println(accountArray[i]);
System.out.println("After some transactions..");
accountArray[i].deposit(ran.nextInt(300));
accountArray[i].withdrawl(ran.nextInt(3000
));
System.out.println("Balance: "+accountArray[i].getBalance());
System.out.println("After adding the interest for the year....");
accountArray[i].addInterest();
System.out.println("Balance: "+accountArray[i].getBalance());
System.out.println(); // for blank line
}
System.out.println("Checking for Duplicates now....");
for(int i=0; i<accountArray.length; i++){
for(int j=0; j<i; j++){
if(accountArray[i].equals(accountArray[j])){
System.out.println("Account "+accountArray[i].getAccountNo()+
" and "+accountArray[j].getAccountNo()+" are duplicates");
}
}
}
}
}
I spent hours with no luck. Any help would be much appreciated. Thanks.
This is my first post here.
The output I'm getting is (Account no 12350 is getting negative balance...):
Negative amounts are possibile in your code.
Imagine this scenario with an online account.
Robert has 3000 dollars. He withdraws 2990 dollars. There are only 10 dollars remaing.
Seeing that 10 dollars is less than the minimum amount, you subract 25 dollars.
So Robert will have -15 dollars on his account.
Thanks guys for your help. It's now working perfectly with all error messages showing up. I added toString() after getBalance() as J Richard Snape suggested and all OK now.
As Richard said, getBalance() was bypassing all warning.
Output now:
Negative amounts are possible because you have a bound that is higher than some of the person's initial amount deposited.
With the deposit and withdrawal in your block of code, you can withdraw into an amount that causes the person to have a balance in the negative.
accountArray[i].withdrawl(ran.nextInt(3000
));
Again, I am a java n00b and I am trying to learn from scratch and running into some embarrassing problems.
I got an Account class as follows:Account.java
public class Account
{
protected double balance;
// Constructor to initialize balance
public Account( double amount )
{
balance = amount;
}
// Overloaded constructor for empty balance
public Account()
{
balance = 0.0;
}
public void deposit( double amount )
{
balance += amount;
}
public double withdraw( double amount )
{
// See if amount can be withdrawn
if (balance >= amount)
{
balance -= amount;
return amount;
}
else
// Withdrawal not allowed
return 0.0;
}
public double getbalance()
{
return balance;
}
}
I am trying to use extends to inherit the methods and variables in this class. So, I used InterestBearingAccount.java
import Account;
class InterestBearingAccount extends Account
{
// Default interest rate of 7.95 percent (const)
private static double default_interest = 7.95;
// Current interest rate
private double interest_rate;
// Overloaded constructor accepting balance and an interest rate
public InterestBearingAccount( double amount, double interest)
{
balance = amount;
interest_rate = interest;
}
// Overloaded constructor accepting balance with a default interest rate
public InterestBearingAccount( double amount )
{
balance = amount;
interest_rate = default_interest;
}
// Overloaded constructor with empty balance and a default interest rate
public InterestBearingAccount()
{
balance = 0.0;
interest_rate = default_interest;
}
public void add_monthly_interest()
{
// Add interest to our account
balance = balance +
(balance * interest_rate / 100) / 12;
}
}
I get an error saying import error '.' expected when I try to compile. All the files are in the same folder.
I did javac -cp . InterestBearingAccount
If all the files are in the same folder / package, you don't need to do an import.
When you define your class you can optionally include a package statement at the top of the file. This mandates the package that the class belongs to and should correlate to its position on the file system. For example, a public class Account in package com.foo should be defined in the following file hierarchy:
com
|
|--foo
|
|--Account.java
As you have omitted the package statement both your classes belong to the anonymous package. For classes belonging to the same package there is no need to import classes in order to reference them; this is only a requirement for classes in a different package.
if you're classes are in the same package, it's not necessary to import. Otherwise you should import the package + the Class name.
make InterestBearingAccount class public, like
public class InterestBearingAccount {}