jeudi 26 novembre 2015

Please tell me what im doing wrong with program made to convert fahrenheit with celsius

I made this program to convert Fahrenheit with Celsius. I'm getting a compile error because its bypassing the if statements and the conversions aren't doing anything.

     package temp;

        import java.util.Scanner;

this is the class TheTemp

        public class TheTemp {
            private double tempValue;
            private char tempType;
            private static int obj;

this constructor sets the default 0 degrees and Celsius

        public TheTemp(){
            double tempType = 'c';
            tempValue = 0;
            obj = 0;
        }

this next 3 constructors are for importing the values... this one

        public void setTemp(double tempValue){
            this.tempValue=tempValue;
        }

this one

        public void setTemp(char t){
            if(t == 'C'||t == 'c'){
                obj = 0;
                tempType = t;
            }
            if(t == 'F'||t == 'f'){
                obj = 1;
                tempType = t;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");
            }

        }

and this one

        public void setTemp(char t, double tempValue){

            this.tempValue=tempValue;

            if(t == 'C'||t == 'c'){
                obj = 0;
                tempType = t;
            }
            if(t == 'F'||t == 'f'){
                obj = 1;
                tempType = t;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");
            }

        }

this are the conversions the first if statement is Celsius to Fahrenheit and the second if statement is Fahrenheit to Celsius

        public void convertValue(){
            if(obj == 0){
                tempValue = 9*(tempValue/5) + 32;
            }
            if(obj == 1){
                tempValue = 5*(tempValue - 32) / 9;
            }
            else{
                throw new IllegalArgumentException("Choose ether C or F");

            }
        }

        public double getTempValue(){
            return tempValue;
        }

this is the program to test the class

            public static void main(String[] args) {
                TheTemp A = new TheTemp();
                Scanner kb = new Scanner(System.in);
                double a;

                A.setTemp(55);
                A.convertValue();
                a=A.getTempValue();
                System.out.println(a);
            }

        }

Aucun commentaire:

Enregistrer un commentaire