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");
}
}

Recommended Answers

All 7 Replies

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");
}
}

well when you have a mutator method, instead of making the method an int, it would be a void method unless you meant to make it a get(accessor) method
also if the method is a set method, you would have to pass a parameter, check the parameter to see if it is valid, and set it equal to the variable

i believe this is right, but im not sure

When i change the set method to data type void, i still get an error at the line (// if(!student.setCreditHrs(hrs))), now stating that the void data type is not valid here.

These are the two important pieces of code:

public int setCreditHrs()
{
       return creditHrs;
}

if(!student.setCreditHrs(hrs))

Mistakes:

1. As you can see, in the first piece of code, you have declared your method as "public int setCreditHrs()". What this means is that the method returns an int (where it says "int" before the setCreditHrs) and when you call this method, you cannot call it using an argument (the () have nothing in between them). So to call this method correctly you'd use student.setCreditHrs(). You said "student.setCreditHrs(hrs)", which is incorrect, because you are passing one argument to a method which was declared as taking no arguments. As an example, if you wanted to pass a float argument to your setCreditHrs method, the method would be defined as

public int setCreditHrs(float nameYouGiveItHere)
{
       return creditHrs;
}

And it would be called by student.setCreditHrs(someFloatHere).

2. Methods which are declared as "setWhatever" usually "set" the whatever to something. Currently, the method declaration you posted "gets" something (it returns something). So your method is named one thing, but it is doing another thing. An example of a typical set method:

public class Example{
float something; 

   public void setSomething(float otherThing){
      something = otherThing;
   }

}

3. if(!student.setCreditHrs(hrs)): This will produce an error since you can only use an if statement to check boolean values for true or false. So (the way you have your code as of your first post) something like if (student.setCreditHrs() == 0) will work, because "==" evaluates to true (if the two things are equal) or false (if they are not equal).

Apart from all the issues around accesors, the if statement fails because the thing in the brackets ater the "if" must evaluate to a boolean true/false. Your code if(!student.setCreditHrs(hrs)) will only work if setCreditHours returns a boolean value.

if(!student.setCreditHrs(hrs)): This will produce an error since you can only use an if statement to check boolean values for true or false. . .

Yeah that's what I was trying to say James

Its all good though :)

Yeah that's what I was trying to say James

Sorry man, didn't mean to tread on your toes. I hadn't read you point 3 properly - my fault. ;-)

Not a problem, your explanation was more clear anyway, and I realize you just didn't see that part of my post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.