Question

This is the Console. When entering values after the user decides what to calculate. The values that return from the class are zero. I have private variables, with accessors and mutators. I don't know what the reason for this is. Any ideas?? Please!!!

public class Console {

    public static final int USER_CHOIDE_VOLTAGE = 1;
    public static final int USER_CHOIDE_AMPERAGE = 2;
    public static final int USER_CHOIDE_RESISTANCE = 3;
    public static void main(final String[] args) {
        // Creates a Circuit Object
        Circuit myCircuit = new Circuit();
        // Creates a Scanner Object to get input from user
        Scanner keyboard = new Scanner(System.in);
        // Holds input from user
        int userChoice;
System.out.println("\n");
        System.out.println("This system will calculate the ");
        System.out.println("\tVoltage, Amperage, or Resistance ");
        System.out.println("\tgiven the other two values using Ohms Law.");
        System.out.println("\n");

        // Ask user what to calculate, if it is not one
        // of the options, ask again(while-do loop)
        do {
            System.out.println("Which value would you like to calculate?");
            System.out.println("\t1. Voltage");
            System.out.println("\t2. Resistane");
            System.out.println("\t3. Amperage");
            System.out.println("\n");
            System.out.println("Please select 1, 2, or 3");
            userChoice = keyboard.nextInt();

            //Switch follows cases for what the user would
            // like to calculate
        switch (userChoice) {
            case USER_CHOIDE_VOLTAGE:
                // Gets Amperage from User
                System.out.println("Please enter the Amperage:");
                // Sets Amperage value
                myCircuit.setAmperage(keyboard.nextDouble());
                // Gets Resistance from User
                System.out.println("Please enter the Resistance:");
                // Sets Resistance value
                myCircuit.setResistance(keyboard.nextDouble());
                // Returns Value for voltage from method
                System.out.println("The value of Voltage is: "
                        + myCircuit.getVoltage());
                break;

            case USER_CHOIDE_AMPERAGE:
                // Gets Voltage from User
                System.out.println("Please enter the Voltage:");
                myCircuit.setVoltage(keyboard.nextDouble());
                // Gets Resistance from User
                System.out.println("Please enter the Resistance:");
                // Sets Resistance value
                myCircuit.setResistance(keyboard.nextDouble());
                // Returns Value for Amperage from method
                System.out.println("The value of Amperage is: "
                        + myCircuit.getAmperage());
                break;

            case USER_CHOIDE_RESISTANCE:
                // Gets Amperage from User
                System.out.println("Please enter the Amperage:");
                // Sets Amperage value
                myCircuit.setAmperage(keyboard.nextDouble());
                // Gets Voltage from User
                System.out.println("Please enter the Voltage:");
                myCircuit.setVoltage(keyboard.nextDouble());
                // Returns Value for Resistance from method
                System.out.println("The value of Resistance is: "
                        + myCircuit.getResistance());
                break;
            // Do Nothing Since do while loop takes care of this option
            default:
            }
        } while (userChoice != USER_CHOIDE_VOLTAGE
            && userChoice != USER_CHOIDE_AMPERAGE
            && userChoice != USER_CHOIDE_RESISTANCE);

        System.exit(0);
        keyboard.close();
    }
}

This is the Class

public class Circuit {

    private double voltage, resistance, amperage;

    public double getVoltage() {
        return voltage;
    }

    public double getResistance() {
        return resistance;
    }

    public double getAmperage() {
        return amperage;
    }

    public void setVoltage(double pVoltage) {
        voltage = pVoltage;
    }

    public void setResistance(double pResistance) {
        resistance = pResistance;
    }

    public void setAmperage(double pAmperage) {
        amperage = pAmperage;
    }

    public void calcVoltage() {
        voltage = amperage * resistance;
    }

    public  void calcResistance() {
        resistance = voltage / amperage;
    }
    public void calcAmperage() {
        amperage = voltage / resistance;
    }
}
Was it helpful?

Solution 3

You are not calling your calcSomething() methods, therefore, you are not computing anything and just returning (by getSomething()) the values without modifications. Before continuing, please check your print statements, I think they should look like

System.out.println("\t1. Voltage");
System.out.println("\t2. Amperate");
System.out.println("\t3. Resistance");

since you declared your constants:

public static final int USER_CHOIDE_VOLTAGE = 1;
public static final int USER_CHOIDE_AMPERAGE = 2;
public static final int USER_CHOIDE_RESISTANCE = 3;

Continuing with the explanation, let's say the input is 2, so the switch would enter the case USER_CHOIDE_AMPERAGE, here is the problem, you must compute the "amperage" by calling myCircuit.calcAmperage():

case USER_CHOIDE_AMPERAGE:
    System.out.println("Please enter the Voltage:");
    myCircuit.setVoltage(keyboard.nextDouble());
    System.out.println("Please enter the Resistance:");
    myCircuit.setResistance(keyboard.nextDouble());
    myCircuit.calcAmperage(); // ADD THIS LINE
    System.out.println("The value of Amperage is: " + myCircuit.getAmperage());
    break;

You must do similar changes to the other swith-cases.

OTHER TIPS

You never actually call any of the Circuit classes calc...() methods.

You need to call the appropriate calc method before your final print statements.

For example, when userChoice is USER_CHOIDE_VOLTAGE:

        case USER_CHOIDE_VOLTAGE:
            // Gets Amperage from User
            System.out.println("Please enter the Amperage:");
            // Sets Amperage value
            myCircuit.setAmperage(keyboard.nextDouble());
            // Gets Resistance from User
            System.out.println("Please enter the Resistance:");
            // Sets Resistance value
            myCircuit.setResistance(keyboard.nextDouble());
            // *** Add the following line to your program. ***
            myCircuit.calcVoltage();
            // Returns Value for voltage from method
            System.out.println("The value of Voltage is: "
                    + myCircuit.getVoltage());
            break;

You can call your cal.. Method in every Getter method like..

  public double getAmperage() {
    calcAmperage();
    return amperage;
}



 public double getVoltage() {
    calcVoltage();
    return voltage;
}

Here is the problem

For example

you are calling myCircuit.getVoltage()

and in the function you are not calculating anything

public double getVoltage() {
    return voltage;
}

instead call

myCircuit.calcVoltage()

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top