1 Replies - 1598 Views - Last Post: 23 October 2009 - 04:07 PM Rate Topic: -----

#1 Magshabib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-October 09

Need help with abstract classes and arrays

Posted 23 October 2009 - 01:45 PM

Okay, so I'm trying writing a program that calculates Tax liablities for a users according to their tax status which can either be Single, Joint, Married, Household. I have created an abstract class for the user called Payer. I've also created the class for each type of user. My driver class then creates an array which should allow the user to store multiple users information and at the end of the loop or when user quits the program then prints out one report of all the inputs. Here is what i have. I know there is something wrong with the way I'm trying to store the different status in the array, I just can't figure out what and it's driving me crazy! Any help would be great. Thank
import java.lang.Math.*;
import java.util.Scanner;
abstract class Payer
{
	public double inc;
	public String name, ssn, status;
	public Payer(String stat, String nam, String ss, double i)
	{
		name = nam;	
		ssn = ss;
		inc = i;
		status = stat;

		Scanner input = new Scanner(System.in);
		System.out.print(" Enter Status: ");
		status = input.next();
		System.out.print("Please enter name: ");
		name = input.next();
		System.out.print("Please enter SSN: ");
		ssn = input.next();
		System.out.print("Please enter Income: ");
		inc = input.nextDouble();
	}	
	public abstract double computeTax();
	public abstract String getStatus();
	
		
		
	

}

class Single extends Payer
{
	
	
	
	public Single(String stat, String nam, String ss, double i)
	 
	{	
		super(stat, nam, ss, i);
	
	}
	
	public String getStatus()
	{
		
		return status;
	}	
		
		  //Computes the tax Liability
	public double computeTax()
	{
			if (inc > 0 && inc <= 8025) // 10%
		   		inc = inc * .10;
			else if (inc <= 32550) //15%
					inc =802.50 + (inc - 8025) * .15;
		else if (inc <= 78850) //25%
		   		inc = 4481.25 + (inc - 32550) * .25;
			else if (inc <= 164550) //28%
		   		inc =16056.25 + (inc - 78850) * .28;
	   		else if (inc <= 357700) //33%
		   		inc =40052.25 + (inc - 164550) * .33;
	   		else inc = 103791.75 + (inc - 357700) * .35;
   		return  inc; 
	}

	//Method to take input from user (tax payers name)
	public String taxPayer()
	{
			return name;		
	}

	//Method to take input from User (TaxPayers SSN)	
	public String taxPayerss()
	{
		return ssn;	
	}
	//Method to run the final report that shows all the taxpayer profiles that were stored in the array. 
	public void taxReport()
	{
		System.out.println("******************************************************************************");
		System.out.println( "					   Name \t" + " SSN \t" + "	Liability Amount");
		System.out.println("Tax Payer Profile\t"+ getStatus()+ "\t" + taxPayer()  + "\t" + taxPayerss() + "\t" + computeTax());						  
   	}
}

class Joint extends Payer
{
	
	public Joint(String stat, String nam, String ss, double i)
	 
	{	
		super(stat, nam, ss, i);
	
	}
	
	public String getStatus()
	{
		return status;
	}	
		
		
		  //Computes the tax Liability
	public double computeTax()
	{
			if (inc > 0 && inc <= 16050) // 10%
		   		inc = inc * .10;
			else if (inc <= 65100) //15%
					inc = 1605 + (inc -16050 ) * .15;
		else if (inc <= 131450) //25%
		   		inc =8962.50 + (inc - 65100) * .25;
			else if (inc <= 200300) //28%
		   		inc =25550 + (inc - 131450) * .28;
	   		else if (inc <= 357700) //33%
		   		inc =44828 + (inc - 200300) * .33;
	   		else inc = 96770 + (inc - 357700) * .35;
   		return  inc; 
	}

	//Method to take input from user (tax payers name)
	public String taxPayer()
	{
			return name;		
	}

	//Method to take input from User (TaxPayers SSN)	
	public String taxPayerss()
	{
		return ssn;	
	}
	//Method to run the final report that shows all the taxpayer profiles that were stored in the array. 
	public void taxReport()
	{
		System.out.println("******************************************************************************");
		System.out.println( "					   Name \t" + " SSN \t" + "	Liability Amount");
		System.out.println("Tax Payer Profile\t" + getStatus()+ "\t" + taxPayer()  + "\t" + taxPayerss() + "\t" + computeTax());						  
   	}
}
class Married extends Payer
{
	public Married(String stat, String nam, String ss, double i)
	 
	{	
		super(stat, nam, ss, i);
	
	}
	
	public String getStatus()
	{
		return status;
	}	
		
		
		  //Computes the tax Liability
	public double computeTax()
	{
			if (inc > 0 && inc <= 8025) // 10%
		   		inc = inc * .10;
			else if (inc <= 32550) //15%
					inc = 802.50 + (inc -8025 ) * .15;
		else if (inc <= 65725) //25%
		   		inc =4481.25 + (inc - 32550) * .25;
			else if (inc <= 100150) //28%
		   		inc =12775 + (inc - 65725) * .28;
	   		else if (inc <= 178850) //33%
		   		inc =22414 + (inc - 100150) * .33;
	   		else inc = 48385 + (inc - 178850) * .35;
   		return  inc; 
	}

	//Method to take input from user (tax payers name)
	public String taxPayer()
	{
			return name;		
	}

	//Method to take input from User (TaxPayers SSN)	
	public String taxPayerss()
	{
		return ssn;	
	}
	//Method to run the final report that shows all the taxpayer profiles that were stored in the array. 
	public void taxReport()
	{
		System.out.println("******************************************************************************");
		System.out.println( "					   Name \t" + " SSN \t" + "	Liability Amount");
		System.out.println("Tax Payer Profile\t"+ getStatus() + "\t" + taxPayer()  + "\t" + taxPayerss() + "\t" + computeTax());						  
   	}
}
class Household extends Payer
{
	public Household(String stat, String nam, String ss, double i)
	 
	{	
		super(stat, nam, ss, i);
	
	}
	
	public String getStatus()
	{
		return status;
	}	
		
		  //Computes the tax Liability
	public double computeTax()
	{
			if (inc > 0 && inc <= 11450) // 10%
		   		inc = inc * .10;
			else if (inc <= 43650) //15%
					inc = 1145 + (inc -11450 ) * .15;
		else if (inc <= 112650) //25%
		   		inc =5975 + (inc - 43650) * .25;
			else if (inc <= 182400) //28%
		   		inc =23255 + (inc -112650 ) * .28;
	   		else if (inc <= 357700) //33%
		   		inc =44828 + (inc - 182400) * .33;
	   		else inc = 100604 + (inc - 357700) * .35;
   		return  inc; 
	}

	//Method to take input from user (tax payers name)
	public String taxPayer()
	{
			return name;		
	}

	//Method to take input from User (TaxPayers SSN)	
	public String taxPayerss()
	{
		return ssn;	
	}
	//Method to run the final report that shows all the taxpayer profiles that were stored in the array. 
	public void taxReport()
	{
		System.out.println("******************************************************************************");
		System.out.println( "					   Name \t" + " SSN \t" + "	Liability Amount");
		System.out.println("Tax Payer Profile\t"+ getStatus()+ "\t" + taxPayer()  + "\t" + taxPayerss() + "\t" + computeTax());						  
   	}
}
public class SingleTax
{	
	public static void main (String[] args)
	{
		 double [] itax = new double[10];
		String [] status = new String[10];
		 String [] payername = new String [10];
			 String	[] payerssn = new String [10];
				 Payer [] taxprofile = new Payer[10]; 
		
String sta;
		 Payer sta = Payer.getStatus();		
		 
		for( int i = 0; i < 10; i++)
			
			{
				switch (sta)
				{
				case "Single":
					taxprofile [i] = new Single(status[i], payername[i], payerssn[i], itax[i]);
					break;
				case "Joint":	
					taxprofile [i] = new Joint(status[i], payername[i], payerssn[i], itax[i]);
					break;
				case "Married":
					taxprofile [i] = new Married(status[i], payername[i], payerssn[i], itax[i]);
					break;	
				case "HouseHold":
					taxprofile [i] = new Household(status[i], payername[i], payerssn[i], itax[i]);
				break;
				}

			}
				for( int i = 0; i < 10; i++)
				{
					taxprofile[i].taxReport();	
				}
			
			
			
			  
								 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with abstract classes and arrays

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

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

Re: Need help with abstract classes and arrays

Posted 23 October 2009 - 04:07 PM

hi
couple of mistakes at first glance.
why all that code inside the constructor of Player?
all that code should be done inside methods, or done before you create a new instance of a subclass of Player.
try run it inside the main method of Player for example.

public void static main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print(" Enter Status: ");
		status = input.next();
		System.out.print("Please enter name: ");
		name = input.next();
		System.out.print("Please enter SSN: ");
		ssn = input.next();
		System.out.print("Please enter Income: ");
		inc = input.nextDouble();
Player player = new Player(status, name, ssn, inc);
}


Most important,Player shouldnt be abstract!

married, single etc are Instances of Player rather than its subclasses!
you implement the methods getStatus and computeTax the same way.
I suggest that you read some tutorials about abstract classes, and inheritance.
Player should be look like:
public class Payer
{
	public double inc;
	public String name, ssn, status;
	public Payer(String stat, String nam, String ss, double i)
	{
		name = nam;	
		ssn = ss;
		inc = i;
		status = stat;
}
//the methods AND the implemantation. NO abstract methods!

	}


thats it.
then, when you create new Players there is sense in scanning the status as well, and you dont need to check the status. Here is an example to help you understand:
Player p = new Player("single", "leroy", "ss", 34);
Player p1 = new Player("married", "jo", "sdf", 2222);
Player p2 = new Player("single", "alex", "asd", 355);
//then you dont have to have different arrays to store different data types.
Player[] players = new Player[3];
//add the p,p1,p2
//you dont need that switch case! they are all players and in the same array



hope it helps :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1