resolving bracketing error when creating constructors in Java - java

I am getting a bracketing error in Eclipse (lines 15 & 18) "public Account myCustomAccount ... balance = initial balance; }" when I try to open my second constructor in the following program. The program is for Dietel "Introduction to Programming" chapter 9 exercise 7.
I suspect that I am creating the constructor incorrectly. What advice do you offer? (Thank you kindly in advance!!)
import java.util.Date;
public class Account {
//declare required variables
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0; //assume all accounts have the same interest rate
private Date dateCreated = new Date(); //no-argument instance stores the present date
//define default & custom constructors
public Account mydefaultaccount = new Account(); //no-argument instance of Account
public Account myCustomAccount = new Account(int identNum, double initialBalance) {
id = identNum;
balance = initialBalance;
}
//define getters
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double annualInterestRate() {
return annualInterestRate;
}
public Date getDate() {
return dateCreated;
}
//define setters
public void setId(int idSetter) {
id = idSetter;
}
public void setBalance(double balanceSetter) {
balance = balanceSetter;
}
public void setAnnualInterestRate(double annualSetter) {
annualInterestRate = annualSetter;
}
//define required monthly interest rate getter
public double getMonthlyInterestRate() {
double moInt = annualInterestRate / 12;
return moInt;
}
//define modifiers
public double withdraw(int withdraw) {
balance = balance - withdraw;
}
public double deposit(int deposit) {
balance = balance + deposit;
}
}

That isn't how you define constructors. Constructors should follow the form:
public className(parameters) {}
Then, to instantiate the class, call this:
ClassName variable = new ClassName(Parameters);
In your case,
public Account() {
/* Body */
}
public Account(int identNum, double initialBalance) {
/* Body */
}
And to instantiate,
Account ac = new Account(Parameters);

Related

The method is no applicable to the arguments in Java

I am fairly new to objects and classes in Java, so sorry for the newbie question. In the main method's print statement I get the error:
The method getId(int) in the type Account is not applicable for the arguments ()
I am not quite sure what exactly is happening here, but I am just trying to print user2's id. Thank you in advance.
I also do apologize for trying to get all the code into a single block, but I had some issues, clearly.
package problems;
import java.time.LocalDate;
public class AccountTest {
public static void main(String[] args) {
//User 1 with no input values
Account user1 = new Account();
//User 2 with the specified id and balance values
Account user2 = new Account(1122, 20000);
//Test to see if the getters and setters work
System.out.println("The id is + " + user2.getId());
}
}
//Create the class
class Account {
private int id;
private double balance;
private static double annualInterestRate;
private LocalDate dateCreated;
//Object for when no values are passed
Account() {
id = 0;
balance = 0;
annualInterestRate = 4.5;
System.out.println("There are no parameters set for the object");
}
//Object for when values are passed
Account(int id, int balance) {
this.id = id;
this.balance = balance;
annualInterestRate = 4.5;
dateCreated = LocalDate.now();
}
//Getters
public int getId(int id) {
return this.id;
}
public double getBalance(double balance) {
return this.balance;
}
public double getMonthlyInterest(double annualInterestRate) {
return (annualInterestRate / 100 /12 * balance);
}
//Setters
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//Deposit and withdraw methods
public double withdraw(double balance) {
this.balance = balance - 2500;
return this.balance;
}
public double deposit(double balance) {
this.balance = balance + 3000;
return this.balance;
}
//Get the date for when the account was made
public long getDate(long dateCreated) {
return dateCreated;
}
}
As the name implies, when you do a getter, you get a value, so there is no point in giving it an argument when all it does is return the value of the private variable.
Example:
private String myVar;
public String getMyVar()
{
return this.myVar;
}
public void setMyVar(String theValueIWantMyVar)
{
this.myVar = theValueIWantMyVar;
}
I recommend you read a little to see how object oriented programming works
https://dev.to/codegym_cc/18-best-java-books-for-beginners-in-2019-fme

Reading a file and writing to cmd window using mutators/accessors

I'm working on a project for school and seem to have dug myself into a rabbit hole. I need to read a file consisting of just positive and negative numbers from a file and display them to a command window along with the date. I was able to get the end result but cannot list the marginal results. Thanks in advance!
class Account{
private int id = 0; //private int data field named id for the account (default 0).
private double balance = 0.0; //private double data field named balance for the account (default 0)
private static double annualInterestRate = 0.0; //private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.
private java.util.Date dateCreated; //private Date data field named dateCreated that stores the date when the account was created.
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account() { //no-arg constructor that creates a default account.
dateCreated = new java.util.Date();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public Account(int id, double balance) { //constructor that creates an account with the specified id and initial balance.
this();
this.id = id;
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public int getId() {//accessor and mutator methods for id, balance, and annualInterestRate.
return this.id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getBalance() {//accessor and mutator methods for id, balance, and annualInterestRate.
return this.balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getAnnualInterestRate() {//accessor and mutator methods for id, balance, and annualInterestRate.
return annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public String getDateCreated() {//accessor method for dateCreated
return this.dateCreated.toString();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setId(int id) { //mutator for id
this.id = id;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setBalance(double balance) { //mutator for balance
this.balance = balance;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void setAnnualInterestRate(double annualInterestRate) { //mutator annual interest rate
this.annualInterestRate = annualInterestRate;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterestRate() { //method named getMonthlyInterestRate() that returns the monthly interest rate.
return (annualInterestRate / 100) / 12 ;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public double getMonthlyInterest() { //method named getMonthlyInterest() that returns the monthly interest.
return balance * getMonthlyInterestRate();
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void withdraw(double amount) { //method named withdraw that withdraws a specified amount from the account.
this.balance -= amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
public void deposit(double amount) { //method named deposit that deposits a specified amount to the account.
this.balance += amount;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
}//end class account
I'm pretty sure that class is OK. It's the one with the main method that I need the most help or guidance with.
import java.util.Scanner;
import java.io.*;
public class AccountHomework{
public static void main(String[] args)throws IOException{
File fn = new File("transactions.txt");
Scanner dataIn = new Scanner(fn);
Account account = new Account(1122, 20000);
double[] transactions = new double[10];
account.setAnnualInterestRate(4.5);
account.withdraw(2500.0);
account.deposit(3000.0);
printMethod(account);
}
public static void printMethod(Account acct){
System.out.printf(" %8s %17s %13s","Balance","Monthly Interest","Date Created\n");
System.out.printf(" $%6.2f $%6.2f %s\n",acct.getBalance(),acct.getMonthlyInterest(),acct.getDateCreated());
}
public static void fillTransactions(Scanner dataIn, Stock[] stocks){
double transactions;
for(int indx = 0; indx<stocks.length;indx++){
transactions = dataIn.nextDouble();
dataIn.nextLine(); // read the extra carriage return
if (transactions < 0)
dataIn.withdraw();
else
dataIn.deposit();
account[indx] = new Account();
account[indx].populateStockData(transactions);
}//end for
}//end fillStockArray
}
I noticed 2 things:
1. the scanner dataIn is not used to read the data, I see you have a method fillTransaction, but its not called from main
2. Within fillTransactions you are using the methods withdraw() and deposit() on the scanner, when the methods are actually defined on the class Account

Creating Instances/Testing code

Arlight so i have all of this code that i have done, its quite a bit.. Basically its just making a banking account. I made code for Checking, and Savings account. All of this code is correct. i just need help with making an instance of Savings and Checking accounts in my Testaccount class.
Main Account code:
public class Accountdrv {
public static void main (String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id =id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Savings:
class Savings extends Account{
public Savings(int id, double balance, double annualInterestRate) {
super(id, balance, annualInterestRate);
}
public void withdraw(double amount) {
if(super.getBalance() < amount)
{
System.out.println("Error");
}
else
{
super.withdraw( amount );
System.out.println("Withdraw Completed");
}
}
}
Checking:
public class Checking extends Account{
private double overdraft_limit = 100;
public Checking(int id, double balance, double annualInterestrate){
//super();
super(id, balance, annualInterestrate);
}
public void withdraw(double amount) {
if(super.getBalance() >= (amount + overdraft_limit))
{
System.out.println(" account Overdrawn");
}
else
{
super.withdraw( amount );
System.out.println("Withdraw Completed");
}
}
}
Okay this is the part where i need help, its probably really simple but i can't wrap my head around how to write it out,i need to create an instance of Savings and Checking and withdraw some money, here is what i did so far.
Testaccount:
public class Testaccount {
public static void main(String[] args) {
Account account = new Account(0, 100, 0.6);
System.out.println(account);
account.withdraw(10.50);
System.out.println(account);
account.deposit(5.0);
System.out.println(account);
// Need to add test cases for Savings and Checking
}
}
Did you want to use the information already existing in the Account instance you created? If not (i.e. you want to create a new checking and savings account with initial amount in them) you could do something like
Checking checking = new Checking(0,100,0.6);
checking.withdraw(50.00);
Otherwise, if you want to use the existing Account instance and make it a Checking account and withdraw, do the following:
Checking checking = (Checking) account;
checking.withdraw(50.00);

Creating subclasses

I had to create a class using Account that was defined to model a bank account. (the account has the properties account number, balance, annual interest rate, date created, and methods to deposit/withdraw funds.)
How would i create two subclasses for checking and saving accounts? A checking account has to have an overdraft limit, but the savings cant be over drawn.
Any help or advice would be awesome, thank you(:
public class Accountdrv {
public static void main (String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated());
}
}
class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int id) {
this.id =id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
Consider defining one or more interfaces.
For example:
public interface Account
{
public double getAnnualInterestRate();
public Date getDateCreated();
public int getId();
public double getMonthlyInterest();
public void deposit(double amount);
public void withdraw(double amount);
}
public interface CheckingAccount
extends Account
{
public long getOverdraftLimit();
}
It might be reasonable to add setters, but I prefer to either set the vlues in the implementing class constructor or put the setters in the implementing class, but not in the interface. A reasonable exception might be setOverDraftLimit() in the CheckingAccount interface.
You are making some common money mistakes:
money is not a floating point value. it is a fixed point value. double is never correct for money. Use long instead and store as fractions of a unit (for example 10000 = 1 dollar).
Learn how do perform interest calculations. Annual interest rate is never 12 * monthly interest rate.

Initializing array of objects to $100

This program creates 10 accounts and sets the balance to $100 each.
I wrote a for loop to initialize the balance but doesn't work.
The setBalance() method is not recognize within the AccountArray Class
How do I assign $100 balance to each account?
public class AccountArray{
public static AccountArray[] createAccountArray(){
AccountArray[] atm = new AccountArray [10];
for(int i = 0; i < atm.length; i++){
atm[i] = new AccountArray(setBalance(100));
}
return atm;
}
}
import java.util.Date;
public class Account{
public int id = 0;
public double balance = 0;
public double annualInterestRate = 0;
public double withdraw;
public double deposit;
java.util.Date date = new java.util.Date();
public Account(){
}
public Account(int id, double balance, double annualInterestRate){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public Account(int id, double balance, double annualInterestRate, Date date){
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
this.date = date;
}
public void setID(int id){
this.id = id;
}
public int getID(){
return id;
}
public double getBalance(){
return balance - withdraw + deposit;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public double getAnnualInterestRate(){
return (annualInterestRate/100);
}
public Date getDateCreated(){
return date;
}
public double getMonthlyInterestRate(){
return getBalance() * ((annualInterestRate/100) / 12);
}
public void withdraw(double withdraw){
this.withdraw = withdraw;
}
public double deposit(){
return balance + deposit;
}
public void deposit (double deposit){
this.deposit = deposit;
}
}
.
public class TestAccount extends AccountArray{
public static void main(String[] args) {
Account account1122 = new Account(1122, 20000, 4.5);
account1122.withdraw(2500);
account1122.deposit(3000);
System.out.println("Your account balance is: \t" + "$"+ account1122.getBalance() +
"\nYour monthly interest rate is: \t" + "$" + account1122.getMonthlyInterestRate() +
"\nYour account was created on: \t" + account1122.getDateCreated());
//Declare account array */
AccountArray[] atm;
//Create Account array */
atm = createAccountArray();
System.out.println(atm[i].getBalance);
}
}
Your program is attempting to create 10 AccountArray instances, not 10 Accounts. Even if that were to work, you're calling a method outside of the context of either a static reference or the object instance - that is never going to work.
What you probably want to do is this:
Account[] atm = new Account[10];
for(int i = 0; i < atm.length; i++){
atm[i] = new Account();
atm[i].setBalance(100);
}
...although ignoring the other constructors in this case seems a bit...wrong to me. I leave this part as an exercise for the reader.

Categories

Resources