Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "ConvertTemperature.java" 118L, 3871C written
- aylward@sunfire [22:13:09] ~ $ javac ConvertTemperature.java
- ConvertTemperature.java:114: error: variable sourceString might not have been initialized
- System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
- ^
- ConvertTemperature.java:114: error: variable newTemp might not have been initialized
- System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
- ^
- 2 errors
- aylward@sunfire [22:13:14] ~ $ vim ConvertTemperature.java
- 1 // CS1020 (AY2014/5 Semester 2)
- 2 // Take-home Lab1 Ex1
- 3 // Name: aylward
- 4 // Matric. No.: A0124174L
- 5 // Lab group: 4
- 6 // Write the program description below.
- 7 // It is mandatory to write program description at the top of every
- 8 // program.
- 9 // Marks will be awarded for this in sit-in labs.
- 10
- 11 //this program will read a temperature and convert the value from one
- 12 //scale to another in two decimal places.
- 13
- 14 import java.util.Scanner;
- 15 import java.text.DecimalFormat;
- 16
- 17 public class ConvertTemperature {
- 18
- 19 public static void main(String[] args){
- 20
- 21 Scanner sc = new Scanner(System.in);
- 22
- 23 System.out.print("Enter temperature: ");
- 24 double temperature = sc.nextDouble();
- 25 printMenuSource();
- 26 int fromScale = sc.nextInt();
- 27 printMenuDest(fromScale);
- 28 int toScale = sc.nextInt();
- 29
- 30 convert(temperature, fromScale, toScale);
- 31 }
- 32
- 33 // Print menu to select source scale
- 34 private static void printMenuSource() {
- 35 System.out.println();
- 36 System.out.println("Choose source scale:");
- 37 System.out.println(" 1. Celsius");
- 38 System.out.println(" 2. Fahrenheit");
- 39 System.out.println(" 3. Kelvin");
- 40 System.out.print("Enter your choice: ");
- 41 }
- 42
- 43 //Print menu to select destination scale
- 44 private static void printMenuDest(int scale) {
- 45 System.out.println();
- 46 if (scale == 1) {
- 47 System.out.println();
- 48 System.out.println("Choose destination scale:");
- 49 System.out.println(" 1. Fahrenheit");
- 50 System.out.println(" 2. Kelvin");
- 51 System.out.print("Enter your choice: ");
- 52 } else if (scale == 2) {
- 53 System.out.println();
- 54 System.out.println("Choose destination scale:");
- 55 System.out.println(" 1. Celsius");
- 56 System.out.println(" 2. Kelvin");
- 57 System.out.print("Enter your choice: ");
- 58 } else if (scale == 3) {
- 59 System.out.println();
- 60 System.out.println("Choose destination scale:");
- 61 System.out.println(" 1. Celsius");
- 62 System.out.println(" 2. Fahrenheit");
- 63 System.out.print("Enter your choice: ");
- 64 } else {}
- 65 }
- 66
- 67 // Convert temperature from fromScale to toScale
- 68 private static void convert(double temperature, int fromScale,
- 69 int toScale) {
- 70
- 71 double newTemp;
- 72 String sourceString, destString;
- 73
- 74
- 75 if (fromScale == 1 && toScale == 1) {
- 76 newTemp = 1.8 * temperature + 32.0;
- 77 } else if (fromScale == 1 && toScale == 2) {
- 78 newTemp = temperature + 273.15;
- 79 } else if (fromScale == 2 && toScale == 1) {
- 80 newTemp = (double)5/9 * (temperature - 32.0);
- 81 } else if (fromScale == 2 && toScale == 2) {
- 82 newTemp = (double)5/9 * (temperature - 459.67);
- 83 } else if (fromScale == 3 && toScale == 1) {
- 84 newTemp = temperature - 273.15;
- 85 } else if (fromScale == 3 && toScale == 2) {
- 86 newTemp = 1.8 * (temperature - 273.15) + 32.0;
- 87 } else {}
- 88
- 89 //determine unit of measurement to print at the end.
- 90
- 91 if (fromScale == 1) {
- 92 sourceString = "Celsius";
- 93 if (toScale == 1) {
- 94 destString = "Fahrenheit";
- 95 } else {
- 96 destString = "Kelvin";
- 97 }
- 98 }
- 99
- 100 else if (fromScale == 2) {
- 101 sourceString = "Fahrenheit";
- 102 if (toScale == 1) {
- 103 destString = "Celsius";
- 104 } else {
- 105 destString = "Fahrenheit";
- 106 }
- 107 } else {
- 108 destString = "Fahrenheit";
- 109 }
- 110
- 111 DecimalFormat df = new DecimalFormat("0.00");
- 112
- 113 System.out.println();
- 114 System.out.println(temperature + " degrees " + sourceString + " = " + df.format(newTemp) + " degrees " + destString);
- 115 }
- 116 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement