Results 1 to 3 of 3
Thread: Inheritance Heirarchy Error
- 04-08-2011, 05:12 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 3
- Rep Power
- 0
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
- 04-08-2011, 05:33 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 04-08-2011, 09:10 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Similar Threads
-
Inheritance
By new_2_j in forum New To JavaReplies: 5Last Post: 02-20-2011, 05:56 PM -
JPA Inheritance
By videanuadrian in forum New To JavaReplies: 1Last Post: 01-10-2011, 06:44 AM -
Error with inheritance when adding packages
By Zorrent12 in forum New To JavaReplies: 10Last Post: 12-14-2010, 12:35 AM -
Inheritance
By Nerijus in forum New To JavaReplies: 5Last Post: 04-20-2010, 03:47 AM -
Inheritance
By mew in forum New To JavaReplies: 1Last Post: 12-07-2007, 06:08 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks