9 Replies - 571 Views - Last Post: 07 October 2009 - 03:24 PM Rate Topic: -----

#1 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Issues with inheritance and polymorph

Post icon  Posted 05 October 2009 - 02:29 PM

So I have a program where I need to read values in from a file and then use inheritacne and ploymorphiosm to calculate some values. I can get the program to run with manual keboard inputs but everytime i read from the file and try to inherit those values I get errors that that variable can not be found or that it cannot be applied between two classes.

for example I have a string for a balance I read in say 500 and parse that value into an int then I need to take that value and pass it to one of the classes below. Here is what I have but can not get the balance variable to inherit or pass

import java.io.*;//used for I/O functions
import java.util.*;//used for the scanner funtion
import java.lang.Object;

public class Statement //Superclass
{   
  public static void main(String[] args) //main argument
  { 
	Getinput File = new Getinput();
	Getinput h,p,ss,n,b,an = new Getinput();
	Checking obj1 = new Checking();
	Business obj2 = new Business();				 //constructors and objects
	Savings obj3 = new Savings();
	System.out.println(obj1.bal(f1));
	System.out.println(obj2.f1(500));
	System.out.println(obj3.f1(10000));	 
  }
}

class Getinput  //class to read data file
{  
  String header, phone, soc_security,type;
  protected String name; 
  protected String balance;
  protected String account_number;
	
   {
	 try
	   {
		Scanner sc=new Scanner (new File("account_info.txt"));//opens file to read in information from a file
		header=sc.nextLine();
		System.out.println(header);
		while(sc.hasNext())
		{
		name=sc.next();
		account_number=sc.next();
		phone=sc.next();
		soc_security=sc.next();
		balance=sc.next();
		type=sc.next();
		}
		sc.close();
	   }
		catch(IOException e)
		{System.out.println("There was a problem:" + e);}//displays error if file can not be read
	}
   
   double bal = Integer.parseInt(balance);
   
 } 

class Checking extends Statement //inherited class
{
  protected double interest_rate=2.5;
  
  public double f1(double balance)//polymorph funtion
  {return ((balance*(interest_rate/100))+balance);}
}

class Business extends Statement //inhereited class 2
{
  protected double interest_rate=0;
  
  public double f1(double balance)//polymorphfunction
  {return ((balance*interest_rate)+balance);}
}

class Savings extends Statement // inherited class 3
{
  protected double interest_rate_a=4; 
  protected double interest_rate_b=5;
  
  public double f1(double balance)//polymorph function
  {
   if (balance<5000)
	return(balance*(interest_rate_a/100)+balance); 
  else
	return (balance*(interest_rate_a/100)+balance);
  }
}

//End program




Any help is obliged. On a side note the balance will be inherited int one of the class accounts based on the variable type. thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with inheritance and polymorph

#2 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 02:46 PM

Are there any errors or exceptions?

Technically polymorphism would be if you declared all your objects like this...
Statement obj1 = new Checking();



Your statement class would then need to have an f1 method (could be abstract) to be able to call
obj1.f1();



Edit: Just noticed a couple errors,

System.out.println(obj1.bal(f1));



Your Checking class doesn't have a bal method, you probably meant obj1.f1(...).

The additional class for input is a bit overkill and seems to be making things too complicated, try making a method in your Statement class or just do it all in your main method.

This post has been edited by syfran: 05 October 2009 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 03:14 PM

If I take away the Getinput class I can not use those variable because I will get this error:

Error: non-static variable "x" cannot be referenced from a static context

I have to read values from a file. My issue is how can I move those values I read from the file in to the other classes of the program. I tried super method and could not get that to work. there must be an easy way to do this or I am just making this way to complicated.
Was This Post Helpful? 0
  • +
  • -

#4 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 03:31 PM

You are :P

If your going to use the GetInput class you only need one not 7. Then when you call the object's f1 method you use the variables from your GetInput class as arguments ( or in the constructor).
Was This Post Helpful? 0
  • +
  • -

#5 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 03:34 PM

So I can just increase the size of my Getinput class to include the other classes and have that do all my return functions and outputs for me? eliminate the need to pass the values between classes.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 05:14 PM

View Postkoesterkep, on 5 Oct, 2009 - 02:34 PM, said:

So I can just increase the size of my Getinput class to include the other classes and have that do all my return functions and outputs for me? eliminate the need to pass the values between classes.

You starting to have too many classes
Make getInput() a method and put it inside Statement
why should it be a separate class ?
Was This Post Helpful? 0
  • +
  • -

#7 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 06:14 PM

For some reason C++ came alot easier to me than java:

When I create the getinput as a method inside the statement class I still have issues then using super to pass my variables to other classes. I typically get the

Error: cannot reference "variable/object before supertype constructor has been called

here is the modified code

import java.io.*;//used for I/O functions
import java.util.*;//used for the scanner funtion
import java.lang.Object;

public class Statement //Superclass
{   
  public static void main(String[] args) //main argument
  { 
   
	Business obj2 = new Business();				 //constructors and objects
	Savings obj3 = new Savings();
	System.out.println(obj2.f1(500));
	System.out.println(obj3.f1(10000));	 
  }
  
  Statement(String h, String n, String p, String ss, String t, String b, String an) 
   {header=h;name=n;phone=p;soc_security=ss;type=t;balance=b;account_number=an;}
   
   
public String getInput()  
   {
	 try
	   {
		Scanner sc=new Scanner (new File("account_info.txt"));//opens file to read in information from a file
		header=sc.nextLine();
		while(sc.hasNext())
		{
		name=sc.next();
		account_number=sc.next();
		phone=sc.next();
		soc_security=sc.next();
		balance=sc.next();
		type=sc.next();
		}
		sc.close();   
	   }	
		catch(IOException e)
		{System.out.println("There was a problem:" + e);}//displays error if file can not be read
	 
		return name + account_number;   
   }
  String header,phone,soc_security,type;
  protected String balance, name, account_number;	
} 

class Checking extends Statement //inherited class
{
 
  public Checking(String h, String n, String p, String ss, String t, String b, String an, double c) 
   {super(header,name,phone,soc_security,type,balance,account_number);closing=c;}
 
  double bal = Integer.parseInt(balance); 
 
  {return c=bal*(interest_rate/100)+bal;}
  
   protected double interest_rate=2.5, closing;
}



Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Issues with inheritance and polymorph

Posted 05 October 2009 - 06:23 PM

View Postkoesterkep, on 5 Oct, 2009 - 05:14 PM, said:

For some reason C++ came alot easier to me than java:

When I create the getinput as a method inside the statement class I still have issues then using super to pass my variables to other classes. I typically get the

Error: cannot reference "variable/object before supertype constructor has been called

here is the modified code

You would have the same problem in C++
you cannot invoke an object method befor the object is actually created

Actually really bad design to have the constructor of an object to perform I/O to actually create it
Object creation should be independant on the way the data to create it is collected (console I/O, hardcode, databse access)

OK to fix your really bad architecture make GetInput() a static method that you can invoke from the main() method
Was This Post Helpful? 0
  • +
  • -

#9 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Re: Issues with inheritance and polymorph

Posted 06 October 2009 - 02:32 PM

So I have been modifying my code over and over and keep getting a step closer to the final result each time. I really feel that this assignment is a total waste, but so far I have now got all my values to read into the program from a file and no longer am receiving any errors. Now I am having problems out putting the results in a nice format. Here is a sample of the code

import java.io.*;//used for I/O functions
import java.util.*;//used for the scanner funtion
import java.lang.Object;

public class Statement //Superclass
{   
  public static void main(String[] args) //main argument
  { 
	
  }
}

class Getinput  
{
  protected String name,account_number, balance;
  public String header,phone, soc_security, type;
  Getinput(String inH, String inN, String inAN, String inP,  String inSS, String inB, String inT) 
  {header=inH; name=inN; account_number=inAN; phone=inP;soc_security=inSS; balance=inB;type=inT;}
 {
	 try
	   {
		Scanner sc=new Scanner (new File("account_info.txt"));//opens file to read in information from a file
		header=sc.nextLine();
		while(sc.hasNext())
		{
		name=sc.next();
		account_number=sc.next();
		phone=sc.next();
		soc_security=sc.next();
		balance=sc.next();
		type=sc.next();
		}
		sc.close();
	   }
		catch(IOException e)
		{System.out.println("There was a problem:" + e);}//displays error if file can not be read
		System.out.println(name + " " + account_number);
 } 
 
 public String toString()
 {return getHeader()+ " " + getName()+ " " + getAccount_number()+ " " + getPhone()+ " " + getSoc_security()+ " " + getBalance()+ " " + getType();}
   public String getHeader() {return header;}
	public String getName() {return name;}
	 public String getAccount_number() {return account_number;}
	  public String getPhone() {return phone;}
	   public String getSoc_security() {return soc_security;}
		public String getBalance() {return balance;}
		 public String getType() {return type;}
 
}   

class Checking extends Getinput //inherited class
{
  protected double interest_rate=2.5, closing;
  
  Checking(String inH, String inN, String inAC, String inP,  String inSS, String inB, String inT, double inC) 
   {super (inH, inN,inAC,inP, inSS, inB,inT);closing=inC;}
 
  double bal = Integer.parseInt(balance); 
	public String toString()
	{return getHeader()+ " " + getName()+ " " + getAccount_number()+ " " + getPhone()+ " " + getSoc_security()+ " " + getBalance()+ " " + getType()+ " " + getClosing();}
  public double getClosing()
  {return closing=bal*(interest_rate/100)+bal;}
}




I am trying to read in the following information

NAME	   ACCOUNT	  PHONE					 SSN		 BALANCE	   TYPE
Norm	   27873773		555-2932	918-22-0911		1400			   C=1435
Cliff	   38737737		555-9171	811-12-9811		6700			   S=7035



then out put the results with updated balances based on interest rates like this:
NAME	   ACCOUNT				 PHONE					 SSN		Open BALANCE	   Close balance
Norm	   27873773		555-2932	918-22-0911		1400					1435
Cliff	   38737737		555-9171	811-12-9811		6700					7035



Problem is whenever I run my program I will not get any out put or i will get some repeated out put. Any ideas where I can move to from here?
Was This Post Helpful? 0
  • +
  • -

#10 koesterkep  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 01-March 09

Re: Issues with inheritance and polymorph

Posted 07 October 2009 - 03:24 PM

So I know I need to add something into the main statement to get an out put but when I add different objects i seem to get and error that it can not find the cnstructior when the classes are clearly stated example

Checking output = new Checking (getClosing());
Business output2 = new Business (getClosing();


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1