1,105,556 Community Members

Debugging Help - Interfaces and Classes

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
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);
Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

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);
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

try:

new transferIn(transfer);

new transferIn(..)?

it's not a constructor :)

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

new transferIn(..)?

it's not a constructor :)

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

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Oh! Let me try it out...

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
1
 

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

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

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 :)

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!)

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

yup, no problem there

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Like this:

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

?

Member Avatar
DavidKroukamp
Master Poster
737 posts since Dec 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 180 [?]
Skill Endorsements: 4 [?]
Team Colleague
Featured
 
0
 

Like this:

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

?

thats it :)

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

can you post your entire code?

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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);
		
	} 
}
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
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

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
ThaiAmL
Light Poster
38 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

(It works now! :) )

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: