Java
1
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
41
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))))
42
Name:
Anonymous
2009-02-19 21:33
>>41
That doesn't convert based on the input scale.
43
Name:
Anonymous
2009-02-19 21:37
>>42
>>1 actually only wanted Celsius to Fahrenheit.
44
Name:
Anonymous
2009-02-19 21:56
>>43
His (attempted) code says otherwise.
45
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; }
46
Name:
Anonymous
2009-02-20 4:36
>>45
HOLY SHIT, LEARN TO FORMAT FAGGOT.
47
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();
}
}
}
}
48
Name:
Anonymous
2009-02-20 13:53
>>47
EXPERT JAVA ENTERPRISE PROGRAMMER .
49
Name:
Anonymous
2009-02-20 15:37
>>47
you have way too much spare time, get a job.
50
Name:
Anonymous
2009-02-20 16:37
>>49
Says the
/prog/ user on
4chan
51
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";
52
Name:
Anonymous
2009-02-20 17:11
Too many outputs without units, hope none of you ever work on actual physics projects.
53
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.
54
Name:
Anonymous
2009-02-20 19:45
>>53
I think the problem is that you're incompetent.
55
Name:
Anonymous
2009-02-20 20:00
56
Name:
Anonymous
2009-02-20 23:20
>>53
Did you show them your
EXPERT B B C O D E portfolio?
57
Name:
Anonymous
2009-02-21 2:11
>>49
Second year computer science students don't get jobs.
58
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);
}
}
59
Name:
Anonymous
2009-02-21 3:25
hey OP, submit this. It works better than any of the programs above:
void main(){while(1);}
60
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.
61
Name:
Anonymous
2009-12-02 15:35
Okay.
62
Name:
Anonymous
2009-12-02 17:13
Post truncated.
Stopped reading right there.
63
Name:
Anonymous
2009-12-02 18:08
2009-02-18 00:06
Stopped reading right then.
64
Name:
Anonymous
2009-12-02 21:25
>>47
Needs more factories and singletons.
65
Name:
Anonymous
2009-12-03 10:49
>>64
Factorials and Simpletons
66
Name:
Anonymous
2009-12-03 11:27
>>65
Switches and Bitches!
67
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;
70
Name:
Anonymous
2014-01-21 20:50
Newer Posts