We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Debugging Help - Interfaces and Classes

I'm writing an assignment for my introduction to programming class and we're supposed to implement the following interface:

public Interface Account{ 
//Calculates interest (1%) and adds to the account balance
public void interest(); 
//Calculates the balance after a deposit
public void payIn(double money); 
//Calculates the balance after a charge on the account
public boolean payOut(double money); 
//Prints the balance
public void print(); 
}

into a class called GiroAccount. We're also supposed to include a constructor that can set the initial account balance. We're supposed to write our own interface Interaction including methods that would need to happen if an online money transfer were going on between this account and another one. I wrote the following interface:

public Interface Interaction {


	//Calculates the account balance if "deposit" sent the money
	public void transferOut(double transfer);
	//Calculates the account balance if "deposit" received the money
	public void transferIn(double tranfser);
}

And here is the class that I have written:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		
		//transfer.transferIn();
	} 
}

In the third-to-last-line you can see my poor attempt to try and get the transferIn method to be used after the transaction of the transfer with the user. I'm just not quite sure how to get that to work.

3
Contributors
21
Replies
40 Minutes
Discussion Span
1 Year Ago
Last Updated
22
Views
Question
Answered
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

the class double doesn't have a method transerIn
you'll need an instance of the class you're working in, and
use a method of this:

// create an instance
instance.transferIn(transfer);
stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

I'm writing an assignment for my introduction to programming class and we're supposed to implement the following interface:

public Interface Account{ 
//Calculates interest (1%) and adds to the account balance
public void interest(); 
//Calculates the balance after a deposit
public void payIn(double money); 
//Calculates the balance after a charge on the account
public boolean payOut(double money); 
//Prints the balance
public void print(); 
}

into a class called GiroAccount. We're also supposed to include a constructor that can set the initial account balance. We're supposed to write our own interface Interaction including methods that would need to happen if an online money transfer were going on between this account and another one. I wrote the following interface:

public Interface Interaction {


	//Calculates the account balance if "deposit" sent the money
	public void transferOut(double transfer);
	//Calculates the account balance if "deposit" received the money
	public void transferIn(double tranfser);
}

And here is the class that I have written:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		
		//transfer.transferIn();
	} 
}

In the third-to-last-line you can see my poor attempt to try and get the transferIn method to be used after the transaction of the transfer with the user. I'm just not quite sure how to get that to work.

try:

new GiroAccount().transferIn(transfer);
DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

try:

new transferIn(transfer);

new transferIn(..)?

it's not a constructor :)

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

new transferIn(..)?

it's not a constructor :)

yup i changed already,see my editedt post:P. lol. but fast eyes stultuske

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

Oh! Let me try it out...

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hm. I get the error "cannot find symbol" with the arrow pointing to the word 'new'.

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

yup i changed already,see my editedt post:P. lol. but fast eyes stultuske

ever heard of Lucky Luke, the (comic) cowboy supposedly shoots faster than his shadow?
I'm from the same country dude ;) :D

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Hm. I get the error "cannot find symbol" with the arrow pointing to the word 'new'.

well i guess it would depend where you are calling the method from. try what stultuske said create a new instance of the class then access the method from there.
[EDIT]You win lmao :)

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

Haha. :D (sorry - just had to laugh at that - I read Lucky Luke as a kid!)

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

But it should still work if I do that within the main method? (If my questions are silly, I apologize - I've only been working with java for about two months!)

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

yup, no problem there

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Like this:

GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);

?

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Like this:

GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);

?

thats it :)

DavidKroukamp
Master Poster
Team Colleague
735 posts since Dec 2011
Reputation Points: 279
Solved Threads: 181
Skill Endorsements: 4

That makes sense to me - but I still get the error with 'new'. :/ Why might that be?

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

can you post your entire code?

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Sure:

import java.util.Scanner;


public class GiroAccount implements Account, Interaction {
	
	double deposit;
	
	public GiroAccount(double deposit) {
		this.deposit = deposit;
	}
	
	public void interest() {
		deposit = (deposit*.01) + deposit;
	}
	
	public void payIn(double money) {
		deposit = deposit + money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
	}
	
	public boolean payOut(double money) {
		if (money < -3000.00){
		throw new RuntimeException("Your account is more than 3000 Euros overdrawn!");
		}
		else {
		deposit = deposit - money;
		System.out.println("Your new balance is " +deposit+ " Euros.");
		}
		return true;
	}
	
	public void transferOut(double transfer) {
		deposit = deposit - transfer;
		System.out.println("You have transfered " + transfer + " Euros and your new account balance is " + deposit + " Euros.");
	}
	
	public void transferIn(double transfer) {
		deposit = deposit + transfer;
		System.out.println("You have received "+transfer+" Euros from the transfer and your new balance is "+deposit+" Euros.");
	}
	
	public void print() {
		System.out.println("Your balance is " + deposit + " Euros.");
	}
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);		
		
				
		System.out.println("Please enter the account number of the person to whom you wish to transfer money.");
		String accountNumber = input.nextLine();
		System.out.println("Thank you. Now please input the amount (in the form: ##.##) you wish to transfer.");
		double transfer = input.nextDouble();
		System.out.println("Thank you once more. Now can you please enter the reason for this transfer?");
		String reason = input.nextLine();
		System.out.println("Very well. Your transaction has been completed.");
		GiroAccount myAccount = new GiroAccount();
		myAccount.transferIn(transfer);
		
	} 
}
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

you don't have a default constructor there, only one that takes a double as parameter.
so, try

double initialAmount = // set your initial Amount;
GiroAccount myAccount = new GiroAccount(initialAmount);

or add a default constructor (one that doesn't take a parameter) to your class

stultuske
Industrious Poster
4,382 posts since Jan 2007
Reputation Points: 1,318
Solved Threads: 610
Skill Endorsements: 24

Oh! Of course. :P Thank you!
If I can bug you with one last question: if I were to extend a class from this one, would that new class automatically use Account and Interaction, or do I need to write 'implements' again?

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

(It works now! :) )

ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1446 seconds using 2.78MB