Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java

Name: Anonymous 2009-02-18 0:06

I need to convert a user inputted temperature from Celsius to Fahrenheit. this is what I have so far:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
 
    public static void main(String[] args) {
       privateMain(args);
    }

    private static void privateMain(String[] privateArgs) {
        System.out.println("Enter C or F <return>");
        BufferedReader in =
            new BufferedReader(new InputStreamReader(System.in));
        String type = null;
        try {
            type = in.readLine();
        } catch (IOException e) {
           
            e.printStackTrace();
        }
        if (type=);
        {
          //statements 
        }
        else
            //statements
        System.out.println("Converted is: " + temp);

       
    }
}

How do I take the input, read it as  Celsius or Fahrenheit, and convert it with the if statement? And i know I suck at programming, so you don't have to tell me

Name: Anonymous 2009-02-20 5:35

>>39
Don't worry, I've got it covered.

import java.util.Scanner;
public class LinearTemperatureConverter {
    private static final int KELVIN = 0;
    private static final int CELSIUS = 1;
    private static final int FAHRENHEIT = 2;
    private static final int RANKINE = 3;
    private static final int DELISLE = 4;
    private static final int NEWTON = 5;
    private static final int REAUMUR = 6;
    private static final int ROMER = 7;
    private static final String[] NAMES = {"kelvin","celsius","fahrenheit","rankine","delisle","newton","reaumur","romer"};
    private static final boolean[] DEGREES = {false, true, true, true, true, true, true, true};
    private static final double[][] VALUESINKELVIN = {{0.0,1.0},
                                                {-273.15,-272.15},
                                                {-459.67,-457.87},
                                                {0,1.8},
                                                {559.72,558.22},
                                                {-827.73,-824.7},
                                                {-218.52,-217.72},
                                                {-135.90,-135.38}};
    private static LinearTemperatureConverter.LinearTemperatureRelation converter;
    protected LinearTemperatureConverter(int type1, int type2) {
        LinearTemperatureConverter.LinearTemperatureBase kelvin = new LinearTemperatureConverter.LinearTemperatureScale(getPresetPoint1(KELVIN),getPresetPoint2(KELVIN), true);
        LinearTemperatureConverter.LinearTemperatureScale.setReferenceScale(kelvin);
        LinearTemperatureConverter.LinearTemperatureBase celsius = new LinearTemperatureConverter.LinearTemperatureScale(getPresetPoint1(LinearTemperatureConverter.CELSIUS),getPresetPoint2(LinearTemperatureConverter.CELSIUS), false);
        LinearTemperatureConverter.LinearTemperatureScale fahrenheit = new LinearTemperatureConverter.LinearTemperatureScale(getPresetPoint1(LinearTemperatureConverter.FAHRENHEIT),getPresetPoint2(LinearTemperatureConverter.FAHRENHEIT), false);
        converter = new LinearTemperatureConverter.LinearTemperatureRelation(celsius, fahrenheit);
    }
    public double convertValue(double value) {
        return converter.getConvertedValue(value);
    }
    private static DoublePoint getPresetPoint1(int type) {
        return DoublePoint.getInstance(VALUESINKELVIN[0][0],VALUESINKELVIN[type][0]);
    }
    private static DoublePoint getPresetPoint2(int type) {
        return DoublePoint.getInstance(VALUESINKELVIN[0][1],VALUESINKELVIN[type][1]);
    }
    public static LinearTemperatureConverter getInstance(int type1, int type2) {
        return new LinearTemperatureConverter(type1, type2);
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int type1, type2;
        type1 = type2 = -1;
        String types = "";
        for(int i=0; i<LinearTemperatureConverter.NAMES.length; i++) {
            types+=", "+LinearTemperatureConverter.NAMES[i];
        }
        while(true) {
            boolean phase = type1>=0?true:false;
            String tMessage = phase?"to":"from";
            System.out.print("Enter a unit measure to convert "+tMessage+": ");
            String from = sc.nextLine();
            for(int i=0; i<LinearTemperatureConverter.NAMES.length; i++) {
                if(LinearTemperatureConverter.NAMES[i].toLowerCase().indexOf(from.toLowerCase())>=0 || (LinearTemperatureConverter.NAMES[i]+"s").toLowerCase().indexOf(from.toLowerCase())>=0) {
                    if(!phase) {
                        type1 = i;
                    }
                    else {
                        type2 = i;
                    }
                    break;
                }
            }
            if(phase && type2>=0) {
                break;
            }
            else if(!phase && type1>=0) {
                continue;
            }
            System.out.println("Invalid unit entered, please select one of"+types+".");
        }
        LinearTemperatureConverter mainConverter = LinearTemperatureConverter.getInstance(type1, type2);
        java.text.DecimalFormat dF = new java.text.DecimalFormat(".00");
        while(true) {
            System.out.println("Enter a number to convert or type \"quit\" to quit: ");
            String in = sc.nextLine();
            if(in.toLowerCase().equals("quit".toLowerCase())) {
                break;
            }
            try {
                double input = Double.parseDouble(in);
                double output = mainConverter.convertValue(input);
                String degrees = LinearTemperatureConverter.DEGREES[type1]?"degrees ":"";
                String degree2 = LinearTemperatureConverter.DEGREES[type2]?"degrees ":"";
                System.out.println(dF.format(input)+" "+degrees+LinearTemperatureConverter.NAMES[type1]+" is equivalent to "+dF.format(output)+" "+degree2+LinearTemperatureConverter.NAMES[type2]);
            }
            catch(NumberFormatException e) {
                System.out.println("Please enter a valid number.");
            }
        }
    }
    public static class LinearTemperatureRelation extends LinearTemperatureBase {
        public LinearTemperatureRelation(LinearTemperatureBase fromScale, LinearTemperatureBase toScale) {
            super();
            this.multiplicativeCoeffecient = toScale.getMultiplicativeCoeffecient()/fromScale.getMultiplicativeCoeffecient();
            this.additiveCoeffecient = -fromScale.getAdditiveCoeffecient()*multiplicativeCoeffecient+toScale.getAdditiveCoeffecient();
        }
        public double getConvertedValue(double fromValue) {
            return fromValue*this.multiplicativeCoeffecient + this.additiveCoeffecient;
        }
        public double getUnconvertedValue(double fromValue) {
            return fromValue/this.multiplicativeCoeffecient - this.additiveCoeffecient;
        }
    }
    public static abstract class LinearTemperatureBase {
        protected double multiplicativeCoeffecient;
        protected double additiveCoeffecient;
        public double getMultiplicativeCoeffecient() {
            return this.multiplicativeCoeffecient;
        }
        public double getAdditiveCoeffecient() {
            return this.additiveCoeffecient;
        }
    }
    public static class DoublePoint {
        double x;
        double y;
        public static DoublePoint getInstance(double x, double y) {
            return new DoublePoint(x,y);
        }
        private DoublePoint(double x, double y) {
            this.x = x;
            this.y = y;
        }
        public double getX() {
            return this.x;
        }
        public double getY() {
            return this.y;
        }
    }

    public static class LinearTemperatureScale extends LinearTemperatureBase {
        private DoublePoint p1;
        private DoublePoint p2;
        private static LinearTemperatureBase referenceScale;
        public LinearTemperatureScale(DoublePoint p1, DoublePoint p2, boolean isReference) {
            super();
            this.p1 = p1;
            this.p2 = p2;
            calculateCoeffecients(isReference);
        }
        public LinearTemperatureScale(int x1, int y1, int x2, int y2,boolean isReference) {
            this(new DoublePoint(x1,y1),new DoublePoint(x2,y2), isReference);
        }
        public static void setReferenceScale(LinearTemperatureBase referenceScale) {
            LinearTemperatureScale.referenceScale = referenceScale;
        }
        public static LinearTemperatureBase getReferenceScale() {
            return LinearTemperatureScale.referenceScale;
        }
        public DoublePoint getPoint1() {
            return this.p1;
        }
        public DoublePoint getPoint2() {
            return this.p2;
        }
        private void calculateCoeffecients(boolean isReference) throws NumberFormatException {
            if(this.getPoint1().getX()>this.getPoint2().getX()) {
                multiplicativeCoeffecient = (this.getPoint1().getY()-this.getPoint2().getY())/(this.getPoint1().getX()-this.getPoint2().getX());
            }
            else if(this.getPoint1().getX()<this.getPoint2().getX()) {
                multiplicativeCoeffecient = (this.getPoint2().getY()-this.getPoint1().getY())/(this.getPoint2().getX()-this.getPoint1().getX());
            }
            else {
                throw new NumberFormatException("Cannot operate on a nil-gradient scale.");
            }
            if(!isReference) {
                multiplicativeCoeffecient = multiplicativeCoeffecient/this.getReferenceScale().getMultiplicativeCoeffecient();
                additiveCoeffecient = this.getReferenceScale().getAdditiveCoeffecient()-multiplicativeCoeffecient*this.getPoint1().getX()+this.getPoint1().getY();
            }
        }
    }
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List