/*
	Problem 3 Writing User-defined Methods
	Programmer:	
	Date:			March 10,2011
	ProgramName:Tuition.java
*/

import java.io.*;
import java.text.DecimalFormat;

public class Tuition
{
	public static void main(String[] arg) throws IOException
	{
		//declare and construct variable
		int hours;
		double fees, rate, tuition;
		
		//call methods
		displayWelcome();
		hours = getHours();
		rate = getRate(hours);
		tuition = calcTuition(hours, rate);
		fees = calcFees(tuition);
		displayTotal(tuition+fees);
	}
	
	//DisplayWelcome 
	public static void displayWelcome()
	{
		//welcome user output
		System.out.println("\t\tWelcome!");
		System.out.println(" ");
	}
	//the getHours() method
	public static int getHours()throws IOException
	{
		//declare method variables
		BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
		int hours = 0;
		String strHours;
		
		try
		{
			System.out.println("Please enter number of hours: ");
				strHours = dataIn.readLine();
				hours = Integer.parseInt(strHours);
		}
		catch(NumberFormatException e)
		{
			System.out.println("\tInvalid.");
		}
	return hours;
	}
	
	//the getRate() method
	public static double getRate(int hours)
	{
		//declare method variables
		double rate;
		
		//if hours is greater than 15 charge 44.50 an hour if else 50.00 an hour.
		if (hours > 15)
		{
			rate = 44.50;
		}
		else
		{
			rate = 50.00;
		}
	return rate;
	}
	
	//the calcTuition method
	public static double calcTuition(int hours, double rate)
	{
		//declare method variable
		
		double tuition;
		//calculations
		tuition = hours * rate;
	return tuition;
	}
	
	//the calcFees method
	public static double calcFees(double tuition)
	{
		//declare method variables
		double fees;
		//calculations
		fees = tuition * .08;
	return fees;
	}
	
	//the displayTotal method
	public static void displayTotal(double total)
	{
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
		
		double fees;
		double tuition;

		fees += tuition;
		
		System.out.println("The total is: "+ tuition +".");
	}
}

Ok, i am new to java this is my first full project to write. When i compile this program I get a varaible fees might not have been initialized and a variable tuition might not have been initialized. not sure what is going on, I have googled it etc. I am wondering if my methods are not returning a value or what. If i set the fees and tuition default value to 0.0d like my books example did - it runs BUT, does not run correctly - just then everything is calculated as being 0.0 total hours. HELP!

Recommended Answers

All 9 Replies

The compiler just wants to make sure that your variables have been assigned a value before you use them in an expression. If you assign them a default value when you declare them, the compiler will be happy

double fees = 0;

as you noted.

You are supplying the total to the displayTotal(double total) method, so why are you making a separate calculation (with empty variables) for the total that you are printing out

System.out.println("The total is: "+ tuition +".");

Yes the compiler is now happy but - now my expression is not valued correctly and i do not know if that is a problem of the initialization or with the code itself (previous methods).

now when I run it after compiling correctly. I input my hours of 40,42,30,1 and I get back a total of 0.0 no matter the input! What is going on?

Ezzaral -

The public static void displayTotal(double total)

was specified in my homework problem. That must be included but when the problem displayed the function of that method I provided the calculation...and they shouldn't be empty variables because i defined them and returned them from the previous methods. Is that correct or am I way off?

Well, look at tuition. Apparently it's 0.0 when you print it. What happens to it before you print it? How does it get that value? And what happens before that? How does it get the preceding value? Work back through the code this way and you should be able to spot it.

In this case, you should be able to spot it in about one hop backwards.

ok i got it thank you ezzaral..i did not realize that total in itself would produce an answer...brain fart!

I think you may be confused about variable scope. The "fees" you declared in main() is not the same variable that you are working with here

public static void displayTotal(double total)
	{
		DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
		
		[B]double fees;[/B]
		double tuition;

		fees += tuition;
		
		System.out.println("The total is: "+ tuition +".");
	}

In displayTotal, you have delared a new and completely separate varialbe "fees" that is only visible within that method declaration.

Edit: Glad you got it figured.

oh ok thank you so much! I was wondering why i kept having to go back and declare my method variables!

my professor does not allow us to use a textbook or anything like that so I am learning from discussion forums and a basic java book i bought anyway at half price books....his theory is "coding is personal - no book can teach you - you already know" (this was quoted from the top of our syllabus! lol...

my professor does not allow us to use a textbook or anything like that

I hope he's got some redeeming qualities. He sounds like an idiot from over here.

You can learn a lot from reading through the language spec. It's not an easy read, and you'll probably puzzle over a lot of it, but you can get it free on line.
For example, this section tells you what you need to know about scope - but it's going to take a bit of work to understand it, if you're new to the language.
It's work worth doing, but it's still work.

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.