Java Question

Hello experts, I'm trying to work through this Java book and I'm trying to do some of the programming examples. The book provided the Test.java and I'm supposed to create the Student, FullTimeStudent and PartTimeStudent classes. I'm using Eclipse for my environment.

I'm not quite done yet because I'm stuck on a particular part. This line "if(student.setName(name))" keeps giving me the error to set setName() to boolean. Why is that? I was under the impression that the method returns true if the method was successful or false if it was not.
//Begin 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)) // 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.println("Please enter credit hours for "+name+": ");
				hours = dataIn.readLine();
				student.setCreditHrs(hrs);
				if(!student.setCreditHrs(hrs))
				{
					System.out.println("Hours invalid for "+ student.getStatus()
										+ " student.\n");
					success = false;
				}
			}
			else 
			{
				System.out.println("Name enter 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(student)) +
							" in financial aid.\n\n");
	}
}

//End Test.java

//Begin Student.java

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 = 0.0;
	
	//returns student name
	public String getName() { return name; }
	
	//sets student name
	public void setName(String name) { 
		this.name = name; 
	}
	
	//returns Finacial Aid amount
	public double getFinAid(Student student) {
		//Need to finish this method
		return creditHrs;
	}
	
	//returns student credit hours
	public Double getCreditHrs() { return creditHrs; }
	
	//sets student credit hours
	public void setCreditHrs(double credits) { 
		this.creditHrs = credits;
		}
	
	//returns Status
	public String getStatus() {
		return name;
	}
}
//End Student.java

//Begin FullTimeStudent.java

public class FullTimeStudent extends Student {
	final static String name = "full";
	final static double FINAID_RATE = 0.8;
	
	public static double getRate() {
		return FINAID_RATE;
	}
}

//End FullTimeStudent.java

//Begin PartTimeStudent.java

public class PartTimeStudent extends Student {
	final static String name = "part";
	final static double FINAID_RATE = 0.4;
	
	public static double getRate() {
		return FINAID_RATE;
	}
}

//End PartTimeStudent.java

Open in new window

spogglesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
no methods only return true/false if they are defined to return boolean
setName() has a return type of void which indicates it does not return anything
0
spogglesAuthor Commented:
So my question is, why does the book have it set up as "if(student.setName(name))"?
0
Mick BarryJava DeveloperCommented:
no idea. its wrong
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

spogglesAuthor Commented:
Its just wrong? Thats just weird.

Should I put it in a try/catch? Any ideas on other options on how to change the code to make it compliant?
0
Mick BarryJava DeveloperCommented:
the method doesn't declare that it throws and exception so no need for a try/catch either

should just be:

                                student.setName(name);
                                System.out.println("Please enter credit hours for "+name+": ");

calls setCreditHrs() twice as well
and doesn't parse the string entered

codes a mess basically :(
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spogglesAuthor Commented:
Well, that explains why I was having such a difficult time understanding it.
0
spogglesAuthor Commented:
Thank you for commenting very fast!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.