Compiling errors

Java - Tuition calc.

Page 1 of 1

4 Replies - 2692 Views - Last Post: 19 June 2008 - 04:57 AM Rate Topic: -----

#1 JWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 13-June 08

Compiling errors

Post icon  Posted 19 June 2008 - 02:20 AM

Hello,

Please help... I keep getting these 5 errors no matter what i do.

Thank you!!


import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Tuition
{
	public static void main(String[] args)
	{
		//declare variables
		int hours;
		double fees, rate, tuition;

		//call methods
		displayWelcome();
		hours = getHours();
		rate = getRate(hours);
		tuition = calcTuition(hours, rate);
		fees = calcFees(tuition);
		displayTotal(tuition + fees);
	}
	// the welcome() method
	public static void displayWelcome()
	{
		System.out.println("Welcome to the Tuition Calculator");
		System.out.println();
	}
	//the getHours() receives input from the user
	public static void getHours()
	{
		//declare variables
		String strHours;
		int hours;

			String answer = JOptionPane.showInputDialog(null,"Please enter your total credit hours.");

			try
			{
				hours = Integer.parseInt(strHours);
				if(hours <= 0) throw new NumberFormatException();
			}
			catch(NumberFormatException e)
			{
				JOptionPane.showMessageDialog(null,"You did not enter the correct format.","Error",JOptionPane.INFORMATION_MESSAGE);
			}
			return hours;
		}
		//the getRate()
		public static double getRate(int hours)
		{
			//declare variables
			double rate;

			if(hours > 15)
				rate = hours * 44.50;
			else
				rate = hours * 50.00;

			return rate;
		}
		//the calcTuition() method
		public static double calcTuition(int hours, double rate)
		{
			//declare variables
			double tuition;

			tuition = hours * rate;

			return tuition;
		}
		//the calcFees() method
		public static calcFees(double tuition)
		{
			//declare variables
			double fees;

			fees = tuition * .08;

			return fees;
		}
		//the displayTotal() method
		public static void displayTotal(double total)
		{
			//declare
			double tuition, fees;

			total = tuition + fees;

			DecimalFormat twoDigits = new DecimalFormat("$#000.00");

			System.out.println("Your Tuition is " + total);
		}
	}



Compiling errors:

C:\Users\Documents\Java Applications\Chapter04\Tuition.java:79: invalid method declaration; return type required
public static calcFees(double tuition)
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:23: incompatible types
found : void
required: int
hours = getHours();
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:26: cannot resolve symbol
symbol : method calcFees (double)
location: class Tuition
fees = calcFees(tuition);
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:53: cannot return a value from method whose result type is void
return hours;
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:86: cannot return a value from method whose result type is void
return fees;
^
5 errors

Tool completed with exit code 1

Is This A Good Question/Topic? 0
  • +

Replies To: Compiling errors

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: Compiling errors

Posted 19 June 2008 - 02:29 AM

public static calcFees(double tuition)
//should be
public static double calcFees(double tuition)

public static void getHours()
//should be
public static int getHours()



I think this will solve the problem.
Was This Post Helpful? 0
  • +
  • -

#3 JWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 13-June 08

Re: Compiling errors

Posted 19 June 2008 - 03:31 AM

Hi thank you.... that took care of that problem, but now it's leaving me with these errors:


C:\UsersDocuments\Java Applications\Chapter04\Tuition.java:46: variable strHours might not have been initialized
hours = Integer.parseInt(strHours);
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:53: variable hours might not have been initialized
return hours;
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:94: variable tuition might not have been initialized
total = tuition + fees;
^
C:\Users\Documents\Java Applications\Chapter04\Tuition.java:94: variable fees might not have been initialized
total = tuition + fees;
^
4 errors

Tool completed with exit code
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,476
  • Joined: 07-April 08

Re: Compiling errors

Posted 19 June 2008 - 04:29 AM

You are using variables that hold no data. Remember java does not automatically set a variable to any value.


My question to you though is why are you declaring a String variable strHours and then never using it?

same with hours. If your going to use a variable in a try catch it has to be predefined. This will help you with your errors:

public static int getHours()
    {
        //declare variables
        String strHours;
        int hours = 0;

            strHours = JOptionPane.showInputDialog(null,"Please enter your total credit hours.");

            try
            {
                hours = Integer.parseInt(strHours);
                if(hours <= 0) throw new NumberFormatException();
            }
            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null,"You did not enter the correct format.","Error",JOptionPane.INFORMATION_MESSAGE);
            }
            return hours;
        }



now, on to your next to errors. Your displayTotal method takes in a double called total...... but then you go ahead and override it. what you need is two inputs, tuition and fees

//the displayTotal() method
        public static void displayTotal(double tuition, double fees)
        {
            //declare
            double total;

            total = tuition + fees;

            DecimalFormat twoDigits = new DecimalFormat("$#000.00");

            System.out.println("Your Tuition is " + total);
        }



then change the call for that function like so

displayTotal(tuition, fees);



now your code had a lot of redundancy. In the display total you were taking in the total, yet you tried to add up to get the total. remember variables used inside one function can not be readily used within another function.

working code:

import java.io.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Tuition
{
    public static void main(String[] args)
    {
        //declare variables
        int hours;
        double fees, rate, tuition;

        //call methods
        displayWelcome();
        hours = getHours();
        rate = getRate(hours);
        tuition = calcTuition(hours, rate);
        fees = calcFees(tuition);
        displayTotal(tuition, fees);
    }
    // the welcome() method
    public static void displayWelcome()
    {
        System.out.println("Welcome to the Tuition Calculator");
        System.out.println();
    }
    //the getHours() receives input from the user
    public static int getHours()
    {
        //declare variables
        String strHours;
        int hours = 0;

            strHours = JOptionPane.showInputDialog(null,"Please enter your total credit hours.");

            try
            {
                hours = Integer.parseInt(strHours);
                if(hours <= 0) throw new NumberFormatException();
            }
            catch(NumberFormatException e)
            {
                JOptionPane.showMessageDialog(null,"You did not enter the correct format.","Error",JOptionPane.INFORMATION_MESSAGE);
            }
            return hours;
        }
        //the getRate()
        public static double getRate(int hours)
        {
            //declare variables
            double rate;

            if(hours > 15)
                rate = hours * 44.50;
            else
                rate = hours * 50.00;

            return rate;
        }
        //the calcTuition() method
        public static double calcTuition(int hours, double rate)
        {
            //declare variables
            double tuition;

            tuition = hours * rate;

            return tuition;
        }
        //the calcFees() method
        public static double calcFees(double tuition)
        {
            //declare variables
            double fees;

            fees = tuition * .08;

            return fees;
        }
        //the displayTotal() method
        public static void displayTotal(double tuition, double fees)
        {
            //declare
            double total;

            total = tuition + fees;

            DecimalFormat twoDigits = new DecimalFormat("$#000.00");

            System.out.println("Your Tuition is " + total);
        }
    }




now for a test i input 18 credit hours and it said my tuition was 15571.44.

This post has been edited by rgfirefly24: 19 June 2008 - 04:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 JWD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 13-June 08

Re: Compiling errors

Posted 19 June 2008 - 04:57 AM

Hi there... yes, obviously something wrong there with the claculations... will check and try and fix that.


Thank your for taking the extra time to explain!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1