0% found this document useful (0 votes)
11 views8 pages

Java Distance and Temperature Converters

This Java program allows users to convert between kilometers and miles. It prompts the user to choose between converting kilometers to miles or miles to kilometers, then prompts the user to enter a distance. Based on the options selected, it performs the conversion calculation and displays the result.

Uploaded by

fikrue
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views8 pages

Java Distance and Temperature Converters

This Java program allows users to convert between kilometers and miles. It prompts the user to choose between converting kilometers to miles or miles to kilometers, then prompts the user to enter a distance. Based on the options selected, it performs the conversion calculation and displays the result.

Uploaded by

fikrue
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class DistanceConverter {


public static void main(String args[]) {
Scanner input = null;
double km,mi,distance;
int option;
try {
input = new Scanner([Link]);
[Link]("Choose an
Option:");
[Link]("Convert Kilometers
to Miles(1)");
[Link]("Convert Miles to
Kilometers(2)");
option =
[Link]([Link]());
if (option != 1 && option != 2)
{
[Link]("Plz choose
option 1 or 2");
[Link](0);
}
if(option == 1) {
[Link]("Enter Distance
in Kilometers");
distance =
[Link]([Link]());
mi = 0.62 * distance;
[Link]("Distance in
Miles is:" + mi );

}
else if(option == 2) {
[Link]("Enter Distance
in Miles");
distance =
[Link]([Link]());
km = 1.60 * distance;
[Link]("Distance in
Kilometers is:" + km );
}
}
catch(Exception e)
{
[Link]("[Link]
Terminated");
}
finally
{
if (input != null) {
[Link]();
}
}
}
}

In this tutorial, we will develop a java program to convert


Fahrenheit to Celsius temperature. The mathematical formula to
converting fahrenheit temperature to celsius temperature is
below:

Celsius = 5 * (Fahrenheit – 32) /9


IPO chart
Input Process Output
Celsius = 5.0 *
Temperature in Temperature in
(fahrenheit – 32.0) /
Fahrenheit Celsius
9.0;
Java Program
The following program takes the input of Fahrenheit temperature
from the user. It calculates the Celsius equivalent of the Fahrenheit
temperature. Displays the Celsius temperature in the output.

public class FahrenheitToCelsius {

public static void main(String args[]) throws


IOException {
BufferedReader input = null;
double fahrenheit,celsius;
try {
input = new BufferedReader(new
InputStreamReader([Link]));
[Link]("Please enter
temperature in Fahrenheit : ");
fahrenheit =
[Link]([Link]());

celsius = 5.0 * (fahrenheit - 32.0) /


9.0;
[Link]("The temperature in
Fahrenheit provided:"
+ fahrenheit + " F");
[Link]("The temperature in
Celsius is:" + celsius + " C");
}
catch(IOException ioe)
{
[Link]("Unexpected IO
error.");
}
catch(NumberFormatException nfe)
{
[Link]("Please check for
valid input.");
}
catch(Exception e)
{
[Link]("Unexpected error.");
}
finally
{
if (input != null) {
[Link]();
}
} } }
import [Link];

public class MathUnitConversions6 {

public static void main(String[] args) {

double kilometers;

[Link]("Please enter kilometers:");

Scanner in = new Scanner([Link]);


kilometers = [Link]();

double miles = kilometers / 1.6;

[Link](miles + " Miles");

}
}

Java program for conversion of Kilometers


(KMS) to Miles
import [Link];

public class DeveloperHelps {

public static void main(String[] args) {

double kilometers;
[Link]("Please enter the kilometers");

Scanner in = new Scanner([Link]);

kilometers = [Link]();

double miles = kilometers / 1.609;

[Link](miles + " miles");

Java program for conversion of Miles to


Kilometers (KMS)
import [Link];

public class DeveloperHelps {

public static void main(String[] args) {

double miles;

Scanner in = new Scanner([Link]);

[Link]("Please enter the miles:");

miles = [Link]();

double kilometers = miles * 1.609;

[Link](kilometers + " Kilometers");

}
}

Java Program to determine whether a given string


is palindrome
In this program, we need to check whether a given string is palindrome or not.

A string is said to be palindrome if it is the same from both the ends. For e.g. above
string is a palindrome because if we try to read it from backward, it is same as forward.
One of the approach to check this is iterate through the string till middle of string and
compare a character from back and forth.

ALGORITHM
o STEP 1: START

o STEP 2: DEFINE String string = "Kayak"

o STEP 3: SET flag = true

o STEP 4: CONVERT string into lowercase.

o STEP 5: SET i =0. REPEAT STEP 6 to STEP 7 UNTIL i<="" li="">

o STEP 6: IF ([Link](i) != [Link]([Link]()-i-1))


              then
              SET flag = false
              break
o STEP 7: SET i = i + 1

o STEP 8: IF flag


              then PRINT "Yes"
              else
              PRINT "No"
o STEP 9: END

Program:
1. public class PalindromeString    
2. {    
3.     public static void main(String[] args) {    
4.         String string = "Kayak";    
5.         boolean flag = true;    
6.             
7.         //Converts the given string into lowercase    
8.         string = [Link]();    
9.             
10.         //Iterate the string forward and backward, compare one character at a tim
e     
11.         //till middle of the string is reached    
12.         for(int i = 0; i < [Link]()/2; i++){    
13.             if([Link](i) != [Link]([Link]()-i-1)){    
14.                 flag = false;    
15.                 break;    
16.             }    
17.         }    
18.         if(flag)    
19.             [Link]("Given string is palindrome");    
20.         else    
21.             [Link]("Given string is not a palindrome");    
22.     }    
23. } 

ava Program to find Reverse of the string.


In this program, we need to find the reverse of the string. This can be done by iterating
the string backward and storing each character from the original string into a new
string.

1. Original string: Dream big  
2. Reverse of the string: big maerD  

ALGORITHM
o STEP 1: START
o STEP 2: DEFINE String string = "Dream big"

o STEP 3: DEFINE reversedStr = " "

o STEP 4: SET i =[Link]()-1. REPEAT STEP 5 to STEP 6 UNTIL i>=0

o STEP 5: reversedStr = reversedStr + [Link](i)

o STEP 6: i = i - 1

o STEP 7: PRINT string.

o STEP 8: PRINT reversedStr.

o STEP 9: END

Program:
1.   public class Reverse   
2. {    
3.     public static void main(String[] args) {    
4.         String string = "Dream big";    
5.         //Stores the reverse of given string    
6.         String reversedStr = "";    
7.             
8.         //Iterate through the string from last and add each character to variable re
versedStr    
9.         for(int i = [Link]()-1; i >= 0; i--){    
10.             reversedStr = reversedStr + [Link](i);    
11.         }    
12.             
13.         [Link]("Original string: " + string);    
14.         //Displays the reverse of given string    
15.         [Link]("Reverse of given string: " + reversedStr);    
16.     }    
17. }    

Output:

Common questions

Powered by AI

In the temperature conversion program, error handling involves catching IO and number format exceptions, allowing the program to notify users of incorrect input and exit gracefully. In contrast, the distance conversion program primarily handles invalid choices through conditional checks and uses input parsing but lacks detailed exception handling, relying more on input validation for error management. This discrepancy highlights varying error handling sophistication .

The Java program checks if a string is a palindrome by first converting the string to lowercase. It then iterates through the string from the start to the midpoint, comparing each character with its counterpart from the end. If any characters do not match, it sets a flag to false and exits the loop. Finally, it checks the flag to determine if the string is a palindrome, printing 'Yes' for palindromes and 'No' otherwise .

Loops are significant in the palindrome and reverse string programs as they facilitate iterative handling of string data. In the palindrome program, a loop iterates to the string's midpoint, comparing characters to verify symmetry. In the reverse string program, a loop iterates backwards, systematically constructing the reverse string. These loops are pivotal for performance, ensuring each character is efficiently processed without redundant operations .

The program prompts the user to enter the distance in kilometers, then uses the conversion factor of 1.6 (or 1.609 in other versions) to convert kilometers to miles by dividing the kilometers by the conversion factor, and finally prints the distance in miles .

The program takes the input of Fahrenheit temperature, uses the formula Celsius = 5.0 * (Fahrenheit - 32.0) / 9.0 to calculate the Celsius equivalent, and then displays the calculated Celsius temperature .

User input in the distance conversion program is managed using the Scanner class, which reads input during runtime. This approach is advantageous because it allows the program to process user inputs in real-time, supports type conversion (e.g., String to double), and includes methods for input validation, reducing the likelihood of errors due to incorrect input types .

The Java program reverses a string by iterating over it backward, starting from the last character to the first, and appending each character to a new string. This process continues until all characters have been added to the new string, which represents the reverse of the original .

The separation of concerns is demonstrated in the DistanceConverter program, which separates user interface elements (input prompts) from processing logic (distance conversion calculations) and output (displaying results). This modular approach facilitates maintainability by isolating each component, allowing easy updates of display messages without affecting the conversion logic, and vice versa, showcasing a clean software architecture practice .

The Fahrenheit to Celsius conversion program enhances reliability by using try-catch blocks to handle potential exceptions such as IO errors and invalid number format inputs. By catching and responding to these exceptions, the program prevents crashes and provides informative messages to the user, thus increasing robustness and user trust .

Converting the case of a string to lowercase is important in palindrome verification to ensure that comparisons are case-insensitive. Without this step, strings with the same letters in different cases (e.g., 'Kayak' and 'kayaK') would not be recognized as palindromes, even though they are semantically identical, ensuring that the logic applies uniformly regardless of input case .

You might also like