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

Pages: 1-4041-

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-18 0:15

No singletons or factories.

Your teacher is going to fail you.

Name: Anonymous 2009-02-18 0:25

>No singletons or factories.

yes singletons or factories.

fix'd

Name: Anonymous 2009-02-18 0:32

#!/usr/bin/perl
input:
print "Enter temperature in C or F: ";
$t = <>;
goto input unless ($t =~ /(\d+)\w?(C|F)/);
$t = $1; $s = $2;
if($s eq "C") {
   $t = ($t * 1.8) + 32;
   print "Converted is: $t F\n";
} else {
   $t = ($t - 32) / 1.8;
   print "Converted is: $t C\n";
}

Name: Anonymous 2009-02-18 0:36

>>4

fantastic, helped tremendously

Name: Anonymous 2009-02-18 1:36

HOLY FUCK WHAT IS THIS SHIT?  This is what makes JAVA PROGRAMMERS look bad.

Name: Anonymous 2009-02-18 1:51

This thread is now about how java sucks

Name: Anonymous 2009-02-18 2:12

>>7
This thread is about how OP can't even read any of the dozens of books with exactly this example in to do his homework.

Name: Anonymous 2009-02-18 2:14

>>1
http://www.google.com/search?q=celsius+fahrenheit

You can be an EXPERT PROGRAMMER if you use this link to learn a basic conversion.

I'm not trying to be mean or anything, but this is a really simple problem to solve. If you suck at this, chances are very high that you will also suck at solving much more complicated problems.

>>7
Java is the void of programming. Dohoho.

Name: Anonymous 2009-02-18 2:15

Someone write an ENTERPRISE QUALITY solution for him. Bonus points if he turns it in for marking.

Name: Anonymous 2009-02-18 8:34

>>1
Oh god...

I stopped reading when I saw that you weren't using stdin properly. Don't pass args into another ridiculous function. Just use args[0] to get the argument and do it all in main.

>>10
This.

Name: Anonymous 2009-02-18 8:39

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: " + importimimport);

      
    }
}

Name: Anonymous 2009-02-18 8:44

Use the state monad for referential pointer transparency (but don't overflow it, or it gets segfaulted).

I'm not a programmer, I hang out here just for fun, and I thought I'll check what I have learned from you guys. So, does that make sense? 

Name: Anonymous 2009-02-18 8:57

>>13
I, for one, applaud your effort.

Name: Anonymous 2009-02-18 13:34

shit

Name: Anonymous 2009-02-18 18:55

import java.util.*;
class Main{
 public static void main(String[] args){
  System.out.println("Enter temperature in C or F(e.g. 22 F): ");
  Scanner sc=new Scanner(System.in);
  double t=(int)sc.nextInt();
  if(sc.next().charAt(0)=='C')
   t=t*1.8+32;
  else
   t=(t-32)/1.8;
  System.out.println("Converted is "+t);
 }
}

Name: Anonymous 2009-02-18 19:50

dude,

main = getLine >>= \t -> return ((read t) * 9/5 + 32)


Dude.

Name: Anonymous 2009-02-18 20:05

WTF. Don't use fucking string concatenation. Instant fail. Use StringBuilders.

Name: Anonymous 2009-02-18 20:31

>>17
0 ºF = 32 ºC?

Name: Anonymous 2009-02-18 20:36

>>19
Duh retard.

Name: Anonymous 2009-02-18 21:01

>>19
Did you mean: 0 °F =  °C

>>18
Instant fail.  String concatenation is StringBuilders.

Name: Anonymous 2009-02-18 21:04

celsiusToFahrenheit = (32+).(%5).(9*)
fahrenheitToCelsius = (%9).(5*).subtract 32

Name: Anonymous 2009-02-18 21:20

>>4
God Bless Perl

Name: Anonymous 2009-02-18 21:27

>>23
one minor screwup though, \w should be \s
like a dumbass, I was thinking \whitespace

Name: Anonymous 2009-02-18 22:08

>>21
Incorrect.

15.18.1.2 Optimization of String Concatenation
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

For primitive types, an implementation may also optimize away the creation of a wrapper object by converting directly from a primitive type to a string.

Name: Anonymous 2009-02-18 23:24

>>25
PROTIP: His program isn't worth OPTIMIZING.

Name: Anonymous 2009-02-18 23:48

>>25
s/Incorrect/Correct/

Name: Anonymous 2009-02-18 23:54

>>27
s/s\/Incorrect\/Correct\//s\/Inc\/C\//

Name: Anonymous 2009-02-19 0:06

Everyone in this thread is just butthurt that the java solution >>16
is way easier to read(even w/o syntax highlighting), and around the same length as the perl solution. Oh yea and it doesn't have faggoty dollar signs in front of the variables names.
Java 1 Perl 0.

Name: Anonymous 2009-02-19 0:14

>>28
>>27
NASAQUALITY

Name: Anonymous 2009-02-19 0:15

>>29
Your trolling needs work, try again.

Name: Anonymous 2009-02-19 0:17

>>29
Perl is ENTERPRISE quality.

Name: Anonymous 2009-02-19 5:35

>>17
main = print . (+32) . (*1.8) =<< readLn

Name: Anonymous 2009-02-19 5:50

#!/usr/bin/perl

my ($t,$conv,%conv) = ();
$conv{'C'}=sub($){32+1.8*shift};
$conv{'F'}=sub($){(-32+shift)/1.8};

print "Input temperature in C or F: " and $t=<> until $t=~/^(\d+)([CF])$/;
print "Converted is: ",$conv{$2}($1),"\n";

Name: Anonymous 2009-02-19 6:23

>>34
valid perl code

Name: Anonymous 2009-02-19 6:25

>>35
looks fairly straight forward to me

Name: Anonymous 2009-02-19 14:58

>>34
1. LPARAMS
2. FUNCTION POINTERS
3. REGEX EXPRESSIONS
4. FUNCTION POINTERS
5. ANONYMOUS FUNCTIONS
All to solve OP's trivial question.
Looks like ENTERPRISE QUALITY to me.

Name: Anonymous 2009-02-19 15:22

3. REGEX EXPRESSIONS
The full expansion is REGEX REGULAR EXPREGEX REGEXPREGULAR EXPRESSIONS

Name: Anonymous 2009-02-19 17:14

>>37
all of those things are the complete opposite of ENTERPRISE QUALITY.

Name: Anonymous 2009-02-19 18:23

module Main where
import Ratio

celsiusToFahrenheit = (32+).(%5).(9*)
fahrenheitToCelsius = (%9).(5*).subtract 32

main = putStr "Input temperature in C or F: " >> getLine >>= putStrLn . ("Converted is "++) . show . fromRational . (\(x:xs) -> case x of {'C' -> celsiusToFahrenheit . read $ reverse xs; 'F' -> fahrenheitToCelsius . read $ reverse xs}) . reverse

Name: Anonymous 2009-02-19 20:56

(defn c-to-f [temp]
  (+ 32 (* 1.8 temp)))

(defn f-to-c [temp]
  (/ (- temp 32) 1.8))

(defn main []
  (println "Input temperature in C or F: ")
  (let [temp
    (Double/parseDouble (read-line))]
    (printf "%.2f F\n" (c-to-f temp))
    (printf "%.2f C\n" (f-to-c temp))))

Name: Anonymous 2009-02-19 21:33

>>41
That doesn't convert based on the input scale.

Name: Anonymous 2009-02-19 21:37

>>42
>>1 actually only wanted Celsius to Fahrenheit.

Name: Anonymous 2009-02-19 21:56

>>43
His (attempted) code says otherwise.

Name: THE ANONYMOUS SAGE 2009-02-20 4:26

int main(int argc, char *argv[]) { if(argc != 2) { printf("Bad args, faggot\n"); exit(1); } printf("%f Celcius = %f Fahrenheit\n", atof(argv[1]), atof(argv[1]) * 1.8f + 32.0f); return 0; }

Name: Anonymous 2009-02-20 4:36

>>45
HOLY SHIT, LEARN TO FORMAT FAGGOT.

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();
            }
        }
    }
}

Name: Anonymous 2009-02-20 13:53

>>47
EXPERT JAVA ENTERPRISE PROGRAMMER.

Name: Anonymous 2009-02-20 15:37

>>47
you have way too much spare time, get a job.

Name: Anonymous 2009-02-20 16:37

>>49
Says the /prog/ user on 4chan

Name: Anonymous 2009-02-20 16:58


#!/usr/bin/env ruby
c={'C'=>lambda{|x| 32+1.8*x },'F'=>lambda{|x| (x-32)/1.8 }}
(print "Input temperature in C or F: ";t=gets) until t=~/^(\d+)([CF])$/;
print "Converted is: ",c[$2].call($1.to_i),"\n";

Name: Anonymous 2009-02-20 17:11

Too many outputs without units, hope none of you ever work on actual physics projects.

Name: Anonymous 2009-02-20 18:53

I also need a job.

I interviewed for a c++ role but didnt get it. It made realize that im not c++ fan boi enough for enterprise places.

I also interviewed for a systems role in c. that went much better. but they havent called me.

i think i come across as a little bit insane.

Name: Anonymous 2009-02-20 19:45

>>53

I think the problem is that you're incompetent.

Name: Anonymous 2009-02-20 20:00

>>54
oh dear

Name: Anonymous 2009-02-20 23:20

>>53
Did you show them your EXPERTBBCODE portfolio?

Name: Anonymous 2009-02-21 2:11

>>49
Second year computer science students don't get jobs.

Name: Penis Head 2009-02-21 3:20

I'm a noob programmer and this was easy.

public class degCtoF
{
public static void main(String[] args)
{
    in new = BufferedReader(new InputStreamReader System(in.String));
    while(true)
    }
        try
        {
            System.out.printf("/nDegrees Celcius:/n");
            int C = integer.parseInt(in.readLine());
            break;
        }
        catch (Exception e) System.out.printf("/nnot a valid number/n");
    }
    System.out.printf("Farenheit = %.2f",C*1.8+32);
}
}

Name: Anonymous 2009-02-21 3:25

hey OP, submit this. It works better than any of the programs above:

void main(){while(1);}

Name: Anonymous 2009-03-06 13:25

This day THE EXPERTS took revenge on   The Abelson carving   parentheses into his   mouth and proceeded   to give me   the best blow   job I have   a pretty bad   worse than Python   could possibly exist   but somehow php.

Name: Anonymous 2009-12-02 15:35

Okay.

Name: Anonymous 2009-12-02 17:13

Post truncated.
Stopped reading right there.

Name: Anonymous 2009-12-02 18:08

2009-02-18 00:06
Stopped reading right then.

Name: Anonymous 2009-12-02 21:25

>>47
Needs more factories and singletons.

Name: Anonymous 2009-12-03 10:49

>>64
Factorials and Simpletons

Name: Anonymous 2009-12-03 11:27

>>65
Switches and Bitches!

Name: Anonymous 2009-12-03 17:29

print "Converted is ",
    (print "Enter temperature in C or F(e.g. 22 F): " and <>=~/(\d+) \s* ([CF])/x or die, $2 eq 'C')?
        $1*1.8+32:
        $1/1.8-17.8;

Name: Anonymous 2014-01-21 20:50

>>63
>le pedophile sage

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