Polymorphic Classes

How to call methods?

Page 1 of 1

8 Replies - 1421 Views - Last Post: 15 December 2009 - 04:16 PM Rate Topic: -----

#1 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Polymorphic Classes

Post icon  Posted 06 November 2009 - 06:16 PM

I am working on a Accounting system that is to use Polymorphism.

The problem I run into is that I can't figure out how to call a method.
The book I am learning out of gives a really poor example of how this is done, and I can't seem to get it to work for me.

Here is my Test Class.
import java.util.Scanner;

public class Test
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );

		Account ACT = new Account();
		Account ACTS[] = new Account[32];

		int inInt;
		double inDouble;
		int spool = 0;

		System.out.printf("Enter one of the following\n");
		System.out.printf("1) Create a new Checking Account\n");
		System.out.printf("2) Create a new Savings Account\n");
		System.out.printf("3) Make a Deposit\n");
		System.out.printf("4) Make a Withdraw\n");
		System.out.printf("5) Display all accounts\n");
		System.out.printf("6) Calculate Daily interest\n");
		System.out.printf("7) Exit\n");
		System.out.printf("\n");
		int in = input.nextInt();

		switch (in)
		{
			case 1:
				System.out.printf("A new Checking account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Checking(inInt, inDouble);
				spool++;
				break;
			case 2:
				System.out.printf("A new Savings account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Savings(inInt, inDouble);
				spool++;
				break;
			case 3:
				System.out.printf("Which account to deposit?: \n");
				inInt = input.nextInt();
				System.out.printf(" Enter the amount of deposit: ");
				inDouble = input.nextFloat();
				ACT.deposit(inInt, inDouble);
				break;
			case 4:
				System.out.printf("Which account to withdraw?: \n");
				inInt = input.nextInt();
				System.out.printf(" Enter the amount of withdraw: ");
				inDouble = input.nextFloat();
				ACT.withdraw(inInt, inDouble);
				break;
			case 5:
				System.out.printf("*********************************");
				for(int run = 0; run <= spool-1; run++)
				{
					inInt = ACTS[run].getClass().getAcctNum(); //ERROR how do I call the getAcctNum() method from class Savings or Checking?
					inDouble = ACTS[run].// call getAcctBalance()???
					System.out.printf("Account " + inInt + " has balance " + inDouble + "\n");
				}
				break;
			case 6:
				break;
			case 7:
				break;
			default:
				break;

		}

	}

}


note: in case5 is where I run into the problem, how do I call the getAcctNum method from class Savings or Checking?

thanks for reading my post.

This post has been edited by dnamrax: 06 November 2009 - 06:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Polymorphic Classes

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Polymorphic Classes

Posted 06 November 2009 - 06:37 PM

hi
I dont see any objects of Savings or Checking classes.
basically, calling a method is very simple.
you should have an object of the class, and then just use it to call certain method.
here is an example:
public class A {
public A(){}//constructor of A
public void doSomething(){}//method
}

public class TestClass {
//note, you dont have to call a method from the main method. you can also call methods from inside methods or constructors.
//this is just an example.

public static void main(String[] args){
A aObject = new A();//new instance of A
aObject.doSomething();//call the method.
}
}



anyway, from the code you supplied it is hard to understand the problem.
if ill go from the little you supplied i'd say it should look like:
//global variables:
Savings s = new Savings();//in case this is how the constructor looks like
Checking c = new Checking();//in case this is how the constructor looks like

//then inside case 5:
s.getAcctNum();
c.getAcctBalance();


Was This Post Helpful? 0
  • +
  • -

#3 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: Polymorphic Classes

Posted 06 November 2009 - 06:51 PM

thanks for the reply japanir,

I do understand for to call a method from a class defined simply as A aObject = new A();
What I am trying to do is call a method from the poly type class defined as Account ACTS[] = new Account[32];

The class is defined here as either type "Checking" or type "Savings"...
			case 1:
				System.out.printf("A new Checking account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Checking(inInt, inDouble);
				spool++;
				break;
			case 2:
				System.out.printf("A new Savings account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Savings(inInt, inDouble);
				spool++;
				break;



and here is where I try to call the method...
			case 5:
				System.out.printf("*********************************");
				for(int run = 0; run <= spool-1; run++)
				{
					inInt = ACTS[run].getClass().getAcctNum(); //ERROR how do I call the getAcctNum() method from class Savings or Checking?
					inDouble = ACTS[run].// call getAcctBalance()???
					System.out.printf("Account " + inInt + " has balance " + inDouble + "\n");
				}



The classes Savings and Checking and Account are really simple and not finished yet that's why I didn't post them, however if it helps, here they are....

Savings
public class Savings extends Account
{
	private int savingsNum;
	private double savingsBalance;

	public Savings(int newNum, double newBalance )
	{
		savingsNum = newNum;
		savingsBalance = newBalance;
	}

	public int getAcctNum()
	{
		return savingsNum;
	}

	public double getAcctBalance()
	{
		return savingsBalance;
	}

	public void setAcctBalance(double editBalance)
	{
		savingsBalance = editBalance;
	}

}


Checking
public class Checking extends Account
{
	private int checkingNum;
	private double checkingBalance;

	public Checking( int newNum, double newBalance )
	{
		checkingNum = newNum;
		checkingBalance = newBalance;
	}

	public int getAcctNum()
	{
		return checkingNum;
	}

	public double getAcctBalance()
	{
		return checkingBalance;
	}

	public void setAcctBalance(double editBalance)
	{
		checkingBalance = editBalance;
	}

}


Account
public class Account
{

}


I need to be able to assign the Class ACTS[number] to either a Checking or Savings, using a polymorphic class? To my understanding this is what a basic polymorphic class is?

thanks for reading my post.

This post has been edited by dnamrax: 06 November 2009 - 06:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Polymorphic Classes

Posted 06 November 2009 - 07:02 PM

i see now.
there is no reason extend Account.
Savings and Checking has the same methods and method's implementations.
so Savings and Checking should be instances of Account.
as:
public class Account {
private int accountNum;
private double accountBalance;
public Account(int newNum, double newBalance )
	{
		accountNum = newNum;
		accountBalance = newBalance;
	}

	public int getAcctNum()
	{
		return accountNum;
	}

	public double getAcctBalance()
	{
		return accountBalance;
	}

	public void setAcctBalance(double editBalance)
	{
		accountBalance = editBalance;
	}

}



then, all you have to do is:
//if you need instances for checking and savings.
Account savings = new Account(/*pass parameters*/);
Account checking = new Account(/*pass parameters*/);

//or else, as your code goes:
inInt = ACTS[run].getAcctNum();
inDouble = ACTS[run].getBalance();



that is all.
Was This Post Helpful? 0
  • +
  • -

#5 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: Polymorphic Classes

Posted 06 November 2009 - 07:12 PM

Thanks for the reply.

I know it would make the whole issue easier to make Savings and Checking as instances of Account.
However, the assignment I am working on is to help me understand the use of Hierarchy and Polymorphism.
Being that when I start to calculate interest rates and such, I will have two separate classes of accounts to work with.

Maybe I need to rethink my question...
How do I use Polymorphism to create instances of two subclasses in an array of the superclass?
Also, how do I then call methods from the declared subclasses?
Am I even going about these concepts correctly?

Thanks for reading my post.
Was This Post Helpful? 0
  • +
  • -

#6 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Polymorphic Classes

Posted 06 November 2009 - 07:29 PM

in that case, keep Accounts as:
public class Account {
private int accountNum;
private double accountBalance;
public Account(int newNum, double newBalance )
	{
		accountNum = newNum;
		accountBalance = newBalance;
	}

	public int getAcctNum()
	{
		return accountNum;
	}

	public double getAcctBalance()
	{
		return accountBalance;
	}

	public void setAcctBalance(double editBalance)
	{
		accountBalance = editBalance;
	}

}




then, extend it to Checking and Saving as:
public class Savings extends Account{
public Savings(int newNum, double newBalance){
super(newNum, newBalance);
}
//new methods unique to Savings.
}

public class Checking extends Account{
public Checking(int newNum, double newBalance){
super(newNum, newBalance);
}
//new methods unique to Checking class.
}



then you can populate an Array of Account objects also with Saving and Checking objects.
notice that you will be able to use only methods inherited from Account. when doing as:
Account[] ACT = new Account[5];
int index;//index is any number between 0 -5 (your array indexes)
ACT[0] = new Savings(/*parameters*/);
//only as ACT[index].getAcctNum() or ACT[index].getBalance() or ACT[index]setAcctBalance(balance)



if you dont create instances of Acount, you can think of declaring it as an abstract class.
Was This Post Helpful? 1
  • +
  • -

#7 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: Polymorphic Classes

Posted 06 November 2009 - 07:38 PM

Thank you very much japanir!

I was just reading threw your tutorial on abstract methods, it looks like this is something that I should do with this program.

So the subclasses inherit the methods that the superclass has + they can have their own, if the superclass is abstract can you still call the methods threw the subclass?
Was This Post Helpful? 0
  • +
  • -

#8 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Polymorphic Classes

Posted 06 November 2009 - 07:48 PM

yeah,
you can call methods from of an abstract class.
however, remember that you cant create an instance of an abstract class.
for example:
public abstract class A {
public A(){}
public void doSomething(){}
}

public class B extends A {
public B(){
super();
}
}

public class TestAbstract {
public static void main(String[] args){

//error: A aObject = new A();
//error: aObject.doSomething();

//however this code is ok:
B bObject = new B();
bObject.doSomething();

//this is also ok:
A[] aArray = new A[10];

//as long as you only add B objects as:
aArray[0] = new B();
aArray[1] = new B();

//then ,this is also ok
aArray[0].doSomething();

//this is wrong:
aArray = new A();
}



hope the tutorial helps you :^:
Was This Post Helpful? 1
  • +
  • -

#9 dnamrax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 18-September 09

Re: Polymorphic Classes

Posted 15 December 2009 - 04:16 PM

Here is a working version of what I was trying to do...
Account.java
public abstract class Account
{
	int accountNum;
	double accountBalance;
	double interest;

	public Account(int newNum, double newBalance )
	{
		accountNum = newNum;
		accountBalance = newBalance;
	}

	public void deposit(double addBalance)
	{
		setAcctBalance(accountBalance + addBalance);
	}

	public void withdraw(double removeBalance)
	{
		setAcctBalance(accountBalance - removeBalance);
	}

	public int getAcctNum()
	{
		return accountNum;
	}

	public double getAcctBalance()
	{
		return accountBalance;
	}

	public void setAcctBalance(double editBalance)
	{
		accountBalance = editBalance;
	}

	public abstract void calculateDailyInterest();

}

Checking.java
public class Checking extends Account
{
	public Checking(int newNum, double newBalance )
	{
		super(newNum, newBalance);
	}
	public void calculateDailyInterest()
	{
		if (accountBalance > 500)
		{
			interest = (accountBalance - 500) * 0.00005618;
			accountBalance = accountBalance + interest;
		}
		else
			interest = 0;

		System.out.printf("Account %d %s $%.2f\n", accountNum, "gets interest", interest);
	}

}

Savings.java
public class Savings extends Account
{
	public Savings(int newNum, double newBalance )
	{
		super(newNum, newBalance);
	}

	public void calculateDailyInterest()
	{
		interest = accountBalance * 0.00008427;
		accountBalance = accountBalance + interest;
		System.out.printf("Account %d %s $%.2f\n", accountNum, "gets interest", interest);
	}

}

Test.java
import java.util.Scanner;

public class Test
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );

		Account ACTS[] = new Account[32];

		int in = 0;
		int inInt;
		double inDouble;
		int spool = 0;

		
		System.out.printf("Enter one of the following\n");
		System.out.printf("1) Create a new Checking Account\n");
		System.out.printf("2) Create a new Savings Account\n");
		System.out.printf("3) Make a Deposit\n");
		System.out.printf("4) Make a Withdraw\n");
		System.out.printf("5) Display all accounts\n");
		System.out.printf("6) Calculate Daily interest\n");
		System.out.printf("7) Exit\n");
		System.out.printf("\n");

		while (in != 7)
		{
		in = input.nextInt();

		switch (in)
		{
			case 1:
				System.out.printf("A new Checking account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Checking(inInt, inDouble);
				spool++;
				break;
			case 2:
				System.out.printf("A new Savings account with account number: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the initial balance: ");
				inDouble = input.nextFloat();
				ACTS[spool] = new Savings(inInt, inDouble);
				spool++;
				break;
			case 3:
				System.out.printf("Which account to deposit?: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the amount of deposit: ");
				inDouble = input.nextFloat();
				for(int run = 0; run <= spool-1; run++)
				{
					if (ACTS[run].getAcctNum() == inInt)
						ACTS[run].deposit(inDouble);
				}
				break;
			case 4:
				System.out.printf("Which account to withdraw?: ");
				inInt = input.nextInt();
				System.out.printf(" Enter the amount of withdraw: ");
				inDouble = input.nextFloat();
				for(int run = 0; run <= spool-1; run++)
				{
					if (ACTS[run].getAcctNum() == inInt)
						ACTS[run].withdraw(inDouble);
				}
				break;
			case 5:
				System.out.printf("*********************************\n");
				for(int run = 0; run <= spool-1; run++)
				{
					inInt = ACTS[run].getAcctNum();
					inDouble = ACTS[run].getAcctBalance();
					System.out.printf("Account %d %s $%.2f\n", inInt, "has balance", inDouble);
				}
				System.out.printf("*********************************\n");
				break;
			case 6:
				for(int run = 0; run <= spool-1; run++)
				{
					ACTS[run].calculateDailyInterest();
				}
				break;
			case 7:
				break;
			default:
				System.out.printf("Invalid entry! please use (1-7)\n");
				break;

		}
		}

	}

}



The Next Step is to Convert this code to C++ :)
I am going to make a new post in the C++ section, if you think you can help hop on over.

Here is the link to my C++ topic

This post has been edited by dnamrax: 15 December 2009 - 04:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1