HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding the missing value in a proportion

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theproportionfindingvaluemissing

Problem

Basically, I'd like to be able to solve a proportion after entering in all but one term. Specifically, I'm working on creating a calculator for the Combined Gas Law.

$$\frac{P_1\cdot V_1}{T_1} = \frac{P_2\cdot V_2}{T_2}$$

Right now, what I have is a program that takes in two double arrays for the original and final values, and a String that represents which term to solve for. However, this seems really repetitive. Is there a way to solve for the "missing term" or even create a missing term directly without having to involve many "if" or "switch" statements? At least if I could do a "for-each loop" to find the specific term that I had to solve for, and then it would "fill-in the proportion"

public static double CombinedGasLaw ( double [] original, double [] end, String query)
{
    double p1 = original[0];
    double v1 = original[1];
    double t1 = original[2];
    double p2 = end[0];
    double v2 = end[1];
    double t2 = end[2];
    switch (query.toLowerCase())
    {
        case "v":
            return p1*v1*t2/(t1*p2);
        case "p":
            return p1*v1*t2/(t1*v2);
        case "t":
            return p2*v2*t1/(p1*v1);
    }
}
   public static void main(String[] args) 
{
    double p1 = 1;
    double v1 = 1;
    double t1 = 1;
    double [] original = {p1, v1, t1};
    double p2 = 1;
    double v2 = 1;
    double t2 = 1;
    double [] end = {p2, v2, t2};
    String search = "v";
    double fin = CombinedGasLaw(original, end, search);
    System.out.println("All of the original input except for the missing value");
    System.out.println("the missing value");
}


All in all, is there a way to create a similar program or improve this such that I ONLY need to enter necessary values, and the remaining value could be returned

Solution

public static double CombinedGasLaw ( double [] original, double [] end, String query)


As a general rule, methods have verb names. Noun names represent objects or classes.

To get what you want, you need a way to represent a missing value. One possibility in Java is a null. Consider

public class CombinedGasLaw {

    private double pressure;
    private double volume;
    private double temperature;
    private double constant;

    public CombinedGasLaw(double pressure, double volume, double temperature) {
        this.pressure = pressure;
        this.volume = volume;
        this.temperature = temperature;
        constant = pressure * volume / temperature;
    }

    public CombinedGasLaw(double[] values) {
        this(values[0], values[1], values[2]);
    }

    public double getConstant() {
        return constant;
    }

    public static int findIndexOfNull(Object [] values) {
        for (int i = 0; i < values.length; i++) {
            if (values[i] == null) {
                return i;
            }
        }

        return -1;
    }

    public Double solve(Double [] end) {
        switch (findIndexOfNull(end)) {
        case 0:
            return constant * end[2] / end[1];
        case 1:
            return constant * end[2] / end[0];
        case 2:
            return end[0] * end[1] / constant;
        }

        return null;
    }

    public static void main(String[] args) {
        CombinedGasLaw original = new CombinedGasLaw(1, 1, 1);
        Double [] end = {1.0, null, 1.0};
        Double fin = original.solve(end);
        System.out.println("The missing value:  " + fin);
    }

}


Now we have a class named CombinedGasLaw and we can name the method solve, as in solve for the missing value.

We pass the array of values with one missing.

We search for the missing value with a for loop. Note that this will break if there is more than one missing value.

This version also allows us to find multiple triples and only calculates the constant value once.

There are two constructors because I don't know how you get your input. I wouldn't use an array for original but perhaps you had some reason to do so. So I made it so that you can call the constructor with an array. If so, it just calls the other constructor. I don't use that constructor though, so you could just delete it.

If there is a name used both by an object field and a method parameter, Java defaults to using the parameter. You can override this by saying this.pressure, which tells it to use the field instead. So one easy way to assign values to fields in a constructor is to use the same name for both sides.

The this. is not needed any other time, although you can use it if you want. It's basically saying that it is the pressure associated with the current object. Absent a method parameter with the same name, that's the default, so you don't normally need it. Documentation.

Code Snippets

public static double CombinedGasLaw ( double [] original, double [] end, String query)
public class CombinedGasLaw {

    private double pressure;
    private double volume;
    private double temperature;
    private double constant;

    public CombinedGasLaw(double pressure, double volume, double temperature) {
        this.pressure = pressure;
        this.volume = volume;
        this.temperature = temperature;
        constant = pressure * volume / temperature;
    }

    public CombinedGasLaw(double[] values) {
        this(values[0], values[1], values[2]);
    }

    public double getConstant() {
        return constant;
    }

    public static int findIndexOfNull(Object [] values) {
        for (int i = 0; i < values.length; i++) {
            if (values[i] == null) {
                return i;
            }
        }

        return -1;
    }

    public Double solve(Double [] end) {
        switch (findIndexOfNull(end)) {
        case 0:
            return constant * end[2] / end[1];
        case 1:
            return constant * end[2] / end[0];
        case 2:
            return end[0] * end[1] / constant;
        }

        return null;
    }

    public static void main(String[] args) {
        CombinedGasLaw original = new CombinedGasLaw(1, 1, 1);
        Double [] end = {1.0, null, 1.0};
        Double fin = original.solve(end);
        System.out.println("The missing value:  " + fin);
    }

}

Context

StackExchange Code Review Q#156329, answer score: 2

Revisions (0)

No revisions yet.