Good Day,
I have an Abstract Class, Student, which has two child classes, FulltimeStudent and PartTimeStudent. I use the class, Test, to test how many credithours each student has and how much financial aid each student is getting.
My output requests that firstly, i enter wheter a student is Full-time, part-time or q to quit. This part, i get right and is working.
Then the output requests the amount of credit hours for the specific student and this is where it gets interesting, as soon as i enter the amount of hours, i get the message, invalid credit hours entered.
My guess is this is because of a line that I have taken out, because my code just does not compile once i run it with this line entered and i do not know where to search for the solution. I will attach all my code and highlight the line (in red) where i know there is a problem. The error message i am getting is: "setCreditHrs in Student cannot be applied to (double) if(!student.setCreditHrs(hrs))".
In my Student abstract class, i have given my creditHrs the data type int to see if that will work, but it does not ... not even if i change it to double.
I am attaching my code: first the Student Abstract class, then the FullTimeStudent and PartTimeStudent classes and lastly the Test Class.
public abstract class Student
{
// Initialise variables
String name;
int creditHrs;
private double MIN_FULLTIME_HRS = 12.0;
private double MAX_FULLTIME_HRS = 18.0;
private double MIN_PARTTIME_HRS = 0.5;
private double MAX_PARTTIME_HRS = 11.5;
private double TUITION_RATE = 220.5;
public Student()
{
this.name = name;
this.creditHrs = creditHrs;
}
public String getName()
{
return name;
}
public boolean setName(String name)
{
if (name.length() > 0)
{
name = name;
}
return true;
}
public abstract int getCreditHrs();
public abstract int setCreditHrs();
public abstract double getFinAid();
public abstract boolean getStatus();
}
public class FullTimeStudent extends Student
{
// Initialise constants
double FINAID_RATE = 0.8;
boolean fullTime;
public FullTimeStudent()
{
this.fullTime = fullTime;
}
public boolean getStatus()
{
return fullTime;
}
public int setCreditHrs()
{
return creditHrs;
}
public int getCreditHrs()
{
return creditHrs;
}
public double getFinAid()
{
return 0.8;
}
}
public class PartTimeStudent extends Student
{
// Initialise constant
double FINAID_RATE = 0.4;
boolean partTime;
public boolean getStatus()
{
return partTime;
}
public int setCreditHrs()
{
return creditHrs;
}
public int getCreditHrs()
{
return creditHrs;
}
public double getFinAid()
{
return FINAID_RATE;
}
}
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.println("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");
}
}