Hey everybody,

I was wondering if somebody could help me. I'm having trouble with this assignment, tried to email my teacher a few days ago and he has not returned my email, it's for an online class. The whole chapter is talking about abstract classes and Inheritance and it's very confusing to me. In this assignment we have a parent class named Student which has the constant variables, instance variables, concrete and abstract methods. It has two child classes called FullTimeStudent and PartTimeStudent. When I try to run my code I get the error: FullTime Student is not abstract and does not override abstract method setCreditHrs() in Student. This is my code for my parent class:

public abstract class Student
{
	final double MIN_FULLTIME_HRS = 12.0;
	final double MAX_FULLTIME_HRS = 20.0;
	final double MIN_PARTTIME_HRS = 0.5;
	final double MAX_PARTTIME_HRS = 11.5;
	final double TUITION_RATE = 240.5;
	String name;
	double creditHrs;
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public abstract double getFinAid();
	public abstract double getCreditHrs();
	public abstract double setCreditHrs();
	public abstract String getStatus();
}

and those methods at the end HAVE to be abstract according to the book. Here is my code for child class FullTimeStudent:

public class FullTimeStudent extends Student
{
	final double FINAID_RATE = .8;
	String status;
	double finAid;
	
	public String getStatus()
	{
		return status;
	}
	
	public void setStatus(String status)
	{
		this.status = status;
	}
	
	public double getCreditHrs()
	{
		return creditHrs;
	}
	
	public void setCreditHrs(double creditHrs)
	{
		this.creditHrs = creditHrs;
	}
	
	public double getFinAid()
	{
		finAid = (TUITION_RATE * creditHrs) * FINAID_RATE;
		return finAid;
	}
}

now if somebody could help me understand where my error is coming from and maybe understand abstract methods a little bit I'd really appreciate it thanks.

Recommended Answers

All 31 Replies

Take this chage:
The setCreditHrs is declard with double modifier in the super calss but the setCreditHrs is declared as void in the subclassed class
Take this change

public abstract double setCreditHrs();

sorry:
To

public abstract void setCreditHrs();

i get the error that void is the wrong type needs to be double and when I set double I get the same exact error msg

You have to change the declaration in class Student as follow:

public abstract void setCreditHrs();

and let the declaratin in the calss FullTimeStudent as follow:

public void setCreditHrs(double creditHrs)
	{
		this.creditHrs = creditHrs;
	}

hope it helps.

But if the assignement suggest that the class Studen must have a setCreditHrs function with a return value the you should do that:

//in the student class:
public abstract double setCreditHrs();

and

//in the FullTimeStudent
public double setCreditHrs(double creditHrs)
	{
		this.creditHrs = creditHrs;
                return  this.creditHrs;
	}

But in general the sters does not returns.

that worked but why would I have two return credithrs methods?

I just don't understand why the setCreditHrs() was throwing an error when the setStatus() wasn't and it was the same syntax

Can you repost your code to see the your changes please.

//student class
public abstract class Student
{
	final double MIN_FULLTIME_HRS = 12.0;
	final double MAX_FULLTIME_HRS = 20.0;
	final double MIN_PARTTIME_HRS = 0.5;
	final double MAX_PARTTIME_HRS = 11.5;
	final double TUITION_RATE = 240.5;
	String name;
	double creditHrs;
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public abstract double getFinAid();
	public abstract double getCreditHrs();
	public abstract double setCreditHrs();
	public abstract String getStatus();
}
//FullTimeStudent Class
public class FullTimeStudent extends Student
{
	final double FINAID_RATE = .8;
	String status;
	double finAid;
	
	public String getStatus()
	{
		return status;
	}
	
	public void setStatus(String status)
	{
		this.status = status;
	}
	
	public double getCreditHrs()
	{
		return creditHrs;
	}
	
	public double setCreditHrs()
	{
		this.creditHrs = creditHrs;
		return this.creditHrs;
	}
	
	public double getFinAid()
	{
		return FINAID_RATE;
	}
}

this time everything compiled but like I said I don't know why I need two returns for creditHrs

if you choise that in the Student class the setStatus methode returns a double value then you can delete the Methode from the FullTimeStudent classe and its Ok.

public void setStatus(String status)
	{
		this.status = status;
	}

this methode is not necessary in that case.
good luck.

oh ok, thank you very much for your help, i'm still trying to figure out Abstract classes and methods lol. In an online class it's harder to learn stuff like this.

Happy to help
good luck

lol ok I have one more problem, i don't know if it's b/c of what i did or what, but it gave me the code to test out our classes:

//test.java
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))
				{
					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");
	}
}

and line 72
if(student.setName(name))

is throwing the error "incompatible types found: void required: boolean" and idk how or why setName would come back boolean

the other error is line 77: setCreditHrs(0 in Student cannont be applied to (double) and i don't know what that means...also this code here "test.java" is from the book

i'm clueless about this stuff, i mean if you could explain how they work a bit then maybe I could understand it

The if Statement as define in the java language specification follows:

"The if statement allows conditional execution of a statement or a conditional
choice of two statements, executing one or the other but not both."
...
all the forms of the if statement begin with:

if ( Expression ) Statement
.......

The Expression MUST have type boolean or Boolean, or a compile-time error.

As you setter return a double value then you could not use it as Expression.
occurs.

The if Statement as define in the java language specification follows:

"The if statement allows conditional execution of a statement or a conditional
choice of two statements, executing one or the other but not both."
...
all the forms of the if statement begin with:

if ( Expression ) Statement
.......

The Expression MUST have type boolean or Boolean, or a compile-time error.

As you setter return a double value then you could not use it as Expression.
occurs.

so since the test.java source code is straight from the book and it say

if(student.setName(name))
....

then my setName in the Student class needs to return a type boolean?

Yes; it is possible.
but In fact I'm agree to implement the setName as a function that returns a value.
it is more convinient that this declation take the forme:

//In the Student class
public abstract setName(String name);

and the:

/**
* put the decription of the methode here
**/
public void setName(String name){
this.name=name;
}

Sorry i repose the message

Yes; it is possible.
but In fact I'm NOT agree to implement the setName as a function that returns a value.
it is more convinient that this declation take the forme:

//In the Student class
 
      public abstract void setName(String name);

and the:

//in the FullTimeStudent class

      public void setName(String name){
   
          this.name=name;
   
      }

If somebody could still help me I'd really appreciate it. I really need directed somewhere to help learn and teach myself abstract classes. Also in reference to this lab I've been receiving help for. User moutanna stated I should change my method setStatus() to an abstract class, but my book and the lab still has yet to get back to me. I'm getting very upset with my teacher because I've emailed him and messaged him 4-5 time through the course of the past two weeks and have yet to receive a response so for me to figure this out and understand relies on you guys. I've learned a lot with the help of you guys on here :-). Thank you to anybody that can help me.

Just to specify what my problem is currently: I need to "Create the Java source code files for the three classes "Student", "FullTimeStudent", "PartTimeStudent". The base class should be abstract and should contain the indicated abstract methods, as well as the listed concreat methods, instance variables, and constatns. Each of the two child classes should contain the listed constant." The Student Class needs the constants MIN_FULLTIME_HRS = 12.0, MAX_FULLTIME_HRS = 20.0, MIN_PARTTIME_HRS = .5, MAX_PARTTIME_HRS = 11.5, TUITION_RATE = 240.5, the instance variables name & creditHrs. It also needs the concrete methods getName(), setName(), as well as the abstract methods getFinAid(), getCreditHrs(), setCreditHrs(), getStatus(). FullTimeStudent only needs the constant FINAID_RATE = .8, and PartTimeStudent only needs the constant FINAID_RATE = .4. After that we need to copy the code from the book called Test Class so as to test our new classes. So I made the following classes of code:

//Student Class
public abstract class Student
{
	final double MIN_FULLTIME_HRS = 12.0;
	final double MAX_FULLTIME_HRS = 20.0;
	final double MIN_PARTTIME_HRS = 0.5;
	final double MAX_PARTTIME_HRS = 11.5;
	final double TUITION_RATE = 240.5;
	String name;
	double creditHrs;
	
	public String getName()
	{
		return name;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public abstract double getFinAid();
	public abstract double getCreditHrs();
	public abstract double setCreditHrs();
	public abstract String getStatus();
}
//FullTimeStudent Class
public class FullTimeStudent extends Student
{
	final double FINAID_RATE = .8;
	String status;
	double finAid;
	
	public String getStatus()
	{
		return status;
	}
		
	public double getCreditHrs()
	{
		return creditHrs;
	}
	
	public double setCreditHrs()
	{
		this.creditHrs = creditHrs;
		return this.creditHrs;
	}
	
	public double getFinAid()
	{
		return FINAID_RATE;
	}
}
//PartTimeStudent Class
public class PartTimeStudent extends Student
{
	final double FINAID_RATE = .4;
	String status;
	double finAid;
	
	public String getStatus()
	{
		return status;
	}
		
	public double getCreditHrs()
	{
		return creditHrs;
	}
	
	public double setCreditHrs()
	{
		this.creditHrs = creditHrs;
		return this.creditHrs;
	}
	
	public double getFinAid()
	{
		return FINAID_RATE;
	}
}
//Test Class
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))
				{
					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");
	}
}

Now I'm getting two errors Line 69 in Test.java
( if(student.setName(name) )
and
Line 74 in Test.java
( if(!student.setCreditHrs(hrs) )

Now I know that since it's an if statement it needs to be a boolean value but with how the program is set up I don't know how to change those methods to be Boolean and still work properly. I think I have a small grasp on abstract methods (found a couple links to help out) now I just need to get this to work properly. Thank you to anybody that can help me.

Hi;
You'r right about the boolean vlaue.
as the setName is declared void it is not alowed to take a test on it because it does not return a value
So what do want to test here?:
When the student name value is valid? for instance:
if a name in not empty (not egale to "";
or if the name must contain olny some kind of caracteres (a-z or A to Z)
That's up to you
The same thing about
when the Hours is assumed to be valid?

I'm assuming it's just a test for null values. So how would I be able to implement this?

so you can do some think like:

if(name!=null){
//and the here call your setName(name)  function.
{

would there be a way for me to alter it in the student class because my test class came straight from the book and it told me to copy the for my test class

As I mentioned before it is common practice to decalre a setter as a void; but for didactical purpose it is possible to declare a setter function wish reterned some value.
If you chouse to do so the it is possible.
Yo can declare a setName as function in that returns a Strnig value and mak a test in your test calsse.

Or you can declare the setName as function that returns a boolean value and make a test inside it:
If the value passed in parrameter is null it returns false else it returns true;

the setting it as a boolean and testing it inside worked...I feel stupid for not knowing how to do that. It's basic first thing you learn programming lol. Thank you for all your help. This was my final method I used:

public boolean setName(String name)
	{
		boolean nameExists = false;
		
		if(name!= null)
		{
			this.name = name;
			nameExists = true;
			return nameExists;
		}
		else
		{
			return nameExists;
		}
	}
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.