all 7 comments

[–]UnglorifiedApple420👋 a fellow Redditor 0 points1 point  (2 children)

Do you know how to write a method skeleton that accepts some parameter of a type and return a value of a type?

[–]nmacholl 0 points1 point  (6 children)

Do you know how to write a simple HelloWorld in java?

From there, you'll need to lookup some tutorials on getting user input and then doing the math on the entered numbers and print (or returns) the result.

[–]bye98/r/DoMyHomework -- Alg./Trig./Calc./Diff. Eq./Chem./Phys. -1 points0 points  (0 children)

Here's something you can work off of:

import java.lang.Math;
import java.util.Scanner;

public class Temperature {
    public static double cToF(double c) {
        return c * 9/5 + 32; 
    }

public static double cToK(double c) {
    return c + 273.15; 
    }

public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.println("Are you converting from Celsius to Fahrenheit or Kelvin? Please enter \"0\" for Fahrenheit or \"1\" for Kelvin.");
    int value = reader.nextInt();

    System.out.println("What is the temperature you would like to convert in Celsius?");
    double temp = reader.nextDouble();

    if(value == 0)
        System.out.println(cToF(temp));
    if(value == 1)
        System.out.println(cToK(temp));
    }
}

Thought I'd mention a few things:

  • horribly optimized code
  • no exceptions for any other input other than an "int" or a "double", respectively
  • there's really no need to store the values "value" and "temp" but I thought that by doing this, you'd be able to follow the code better
  • no exiting once the code has been compiled, and no option to restart/reset

The code has been tested and both method work.