-1

Here's my code. I'm in a programming class and it seems I get a load of errors I have no idea how to fix. They all say this :

C:\Users\patrickwilliams\Desktop\Java\tuition.java:30: cannot find symbol symbol : variable hours location: class tuition hours = 0;

and so on for each variable

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


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



    displayWelcome();
    hours = getHours();
    rate = getRate(hours);
    tuition = calcTuition(hours, rate);
    fees = calcFees(tuition);
    displayTotal(tuition + fees);

  }

  public static void displayWelcome() {

    System.out.println("Welcome to the tuition calculator!");

  }

  public static int getHours() {
    String strHours;
    hours = 0;
    boolean done = false;



    while (!done) {
      try {
        int hours;

        System.out.println("Enter the total number of hours.");
        hours = Input.nextInt();

        done = true;
      } catch (Exception e) {
        System.out.println("Please enter an integer value.");
      }
    }
    return hours;
  }

  public static double getRate(int hours) {

    if (hours > 15) {
      rate = 44.5;
    } else {
      rate = 50;
    }
    return rate;
  }

  public static double calcTuition(int hours, double rate) {
    tuition = hours * rate;
    return tuition;
  }

  public static double calcFees(double tuition) {

    fees = tuition * .08;
    return fees;
  }

  public static void displayTotal(double total) {

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

    System.out.println("Your total is " + twoDigits.format(tuition + fees));

  }
}

1 Answers1

0
public static int getHours() {
    String strHours;
    hours = 0;
    ...

This is your problem hours. This variable isn't declared before you try and put a value on it.

To expand a bit:

In java you need to declare a variable inside of a function before you can use it. Later in this same function you have

while (!done) {
  try {
    int hours;
...

That int hours would declare the variable for use within this function, but in your case you tried to use it before the declaration. Removing this int hours; line and adding an int before your hours = 0; should clean up this one error.

Shaded
  • 17,276
  • 8
  • 37
  • 62