/*
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!