• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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

0
spoggles
Asked:
spoggles
  • 4
  • 3
1 Solution
 
objectsCommented:
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
 
objectsCommented:
no idea. its wrong
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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
 
objectsCommented:
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now