Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    3
    Rep Power
    0

    Default Inheritance Heirarchy Error

    PHP Code:
    public class FullTimeStudent extends Student
    {
    	final double FINAID_RATE = 0.8;
    
    
    	public FullTimeStudent(String name, double hrs)
    	{
    		//assign parameters to parent class constructor
    		super(name, hrs);
    	}
    
    	public double getFinAid()
    	{
    		return TUTION_RATE * FINAID_RATE * super.getCreditHrs();
    	}
    
    	public String getStatus()
    	{
    		return "Full-Time student";
    	}
    
    
    }
    PHP Code:
    public abstract class Student
    {
    	final double MIN_FULLTIME_HRS = 12.0;
    	final double MAX_FULLTIME_HRS = 18.0;
    	final double MIN_PARTTIME_HRS = 0.5;
    	final double MAX_PARTTIME_HRS = 11.5;
    	final double TUTION_RATE = 220.5;
    
    	String name;
    	double creditHrs;
    
    
    	//Constructor Class
    	protected Student(String name, double creditHrs)
    	{
    		this.name = name;
    		this.creditHrs = creditHrs;
    	}
    
    	// Other Abstract Methods
    	public String getName()
    	{
    		return name;
    	}
    
    	public void setName(String name)
    	{
    		this.name = name;
    	}
    
    	public double getCreditHrs()
    	{
    		return creditHrs;
    	}
    
    	public void setCreditHrs(double creditHrs)
    	{
    		this.creditHrs = creditHrs;
    	}
    
    	public abstract double getFinAid();
    
    	public abstract String getStatus();
    
    }
    PHP Code:
    public class PartTimeStudent extends Student
    {
    	final double FINAID_RATE = 0.4;
    
    	public PartTimeStudent(String name, double hrs) // This is your original parameterized constructor
    	{
    	  //assign parameters to parent class constructor
    	  super(name, hrs);
    	}
    
    	public PartTimeStudent() // This is the constructor taking no values(default)
    	{
    	  super("unknown",0); // default values given in case no parameters were supplied
    	}
    }
    PHP Code:
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    import java.io.*;
    
    public class Test
    {
    	public static void main(String[] args)
    	{
            BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    
            boolean terminated = false;
            boolean validChoice = true;
    		Student student = null;
    
    		String fullOrPartTime;
    
    		do {
    			validChoice = true;
    			System.out.print("Please enter full-time (F), part-time (P), "
    			                +"or 'Q' to quit: ");
    			try
    			{
    				fullOrPartTime = dataIn.readLine();
    
    				switch(fullOrPartTime.charAt(0))  // look at first character entered
    				{
    					case 'f':
    					case 'F': // full-time student
    							  student = new FullTimeStudent();
    							  break;
    					case 'p':
    					case 'P': // part-time student
    							  student = new PartTimeStudent();
    							  break;
    					case 'q':
    					case 'Q': // quit program
    							  terminated = true;
    							  break;
    					default : // invalid response
    							  validChoice = false;
    							  System.out.print("Please enter only an F, P, or Q.");
    				}
    
    				if(!terminated && validChoice)
    					if(getData(student))	// data input with no errors
    				   		displayData(student);
    			}
    			catch (IOException e)
    			{
    				System.out.println("Invalid entry.");
    			}
    			catch (StringIndexOutOfBoundsException e)
    			{
    				System.out.println("Invalid entry. Enter 'Q' to quit.");
    			}
    		} while(!terminated);
    
    		System.exit(0);
       }
    
    	private static boolean getData(Student student)
    	{
    		boolean success = true;
    		double hrs = 0.0;
    		String name, hours;
            BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    
    		System.out.print("Please enter student name: ");
    		try
    		{
    			name = dataIn.readLine();
    			if(student.setName(name))
            	{
    				System.out.print("Please enter credit hours for "+name+": ");
    				hours = dataIn.readLine();
    				hrs = Double.parseDouble(hours);
    				if(!student.setCreditHrs(hrs))
    				{
    					System.out.println("Hours invalid for "+student.getStatus()
    					                  +" student.\n");
    					success = false;
    				}
    			}
    			else
    			{
    				System.out.println("Name entered is not valid.\n");
    				success = false;
    			}
    		}
    		catch (IOException e)
    		{
    			System.out.println("Invalid entry.\n");
    			success = false;
    		}
    		catch (NumberFormatException e)
    		{
    			System.out.println("Invalid hours entered.\n");
    			success = false;
    		}
    
    		return success;
    	}
    
    	private static void displayData(Student student)
    	{
    		DecimalFormat twoDigits = new DecimalFormat("$##,##0.00");
    
    		System.out.println("\nStudent : "+student.getName() + " is taking "
    		                  +student.getCreditHrs() + " credit hours,");
            System.out.println("and is receiving "+ twoDigits.format(student.getFinAid()) +
            					   " in financial aid.\n\n");
    	}
    }



    NOTE: I been getting these errors and Im not quit sure what to do???

    PHP Code:
    .\FullTimeStudent.java:1: package FullTimeStudent does not exist
    import FullTimeStudent.*;
    ^
    C:\Users\Owner\Documents\ZZZZZ\Test.java:29: cannot find symbol
    symbol  : constructor FullTimeStudent()
    location: class FullTimeStudent
    							  student = new FullTimeStudent();
                                                                        ^
    C:\Users\Owner\Documents\ZZZZZ\Test.java:72: incompatible types
    found   : void
    required: boolean
    			if(student.setName(name))
                                              ^
    C:\Users\Owner\Documents\ZZZZZ\Test.java:77: 'void' type not allowed here
    				if(!student.setCreditHrs(hrs))
                                                            ^
    .\PartTimeStudent.java:1: PartTimeStudent is not abstract and does not override abstract method getStatus() in Student
    public class PartTimeStudent extends Student
           ^
    5 errors

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you extend an abstract class you must implement every abstract method.

    You also can't use methods that don't return a boolean in if clauses.

    student.setName(name) simply sets the name, and returns nothing, tell me, when will that if clause ever be true or false?

    I can't see where you imported full time student in the code you posted, however; you need to properly package code to import.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Are you showing all your code, because that first error implies something up with your package declarations, which aren't visible here.

Similar Threads

  1. Inheritance
    By new_2_j in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 05:56 PM
  2. JPA Inheritance
    By videanuadrian in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:44 AM
  3. Error with inheritance when adding packages
    By Zorrent12 in forum New To Java
    Replies: 10
    Last Post: 12-14-2010, 12:35 AM
  4. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 03:47 AM
  5. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 06:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •