Question: 4 . 9 . 1 : Complete the user interface. - Complete the following program to implement the user interface of the preceding exercise. For

4.9.1: Complete the user interface.
-Complete the following program to implement the user interface of the preceding exercise. For simplicity, only the units cm, m, and in are supported.
Hint: The value of factor1 or factor2 should be the conversion factor from the selected unit to cm. Ex: If the selected unit is in, factor1 is 2.54 because 1 in =2.54 cm.
import java.util.Scanner;
public class UnitConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
boolean done = false;
double factor1=0;
double factor2=0;
String unit1="";
String unit2="";
while (!done)
{
boolean getSecond = true;
String command = in.next();
System.out.println("From unit (in, cm, m, again, quit): "+ command);
if (command.equals("in"))
{
factor1=2.54; // Convert to cm
unit1= command;
}
else if (command.equals("cm"))
{
/* Your code goes here */
}
else if (command.equals("m"))
{
/* Your code goes here */
}
else if (command.equals("again"))
{
getSecond = false;
}
else if (command.equals("quit"))
{
done = true;
getSecond = false;
}
else
{
System.out.println("Sorry, unknown unit.
");
getSecond = false;
}
if (getSecond)// If 'from' unit is valid, get 'to' unit
{
unit2= in.next();
System.out.println("To unit: "+ unit2);
if (unit2.equals("in"))
{
factor2=2.54; // Convert from cm
}
else if /* Your code goes here */
else if /* Your code goes here */
else /* Your code goes here */
}
if (/* Your code goes here */)
{
double value = in.nextDouble();
double result = value * factor1/ factor2;
System.out.println(value +""+ unit1+"="+ result +""+ unit2+"
");
}
}
}
}
4.3.3: Reversing digits of an integer.
- Write a program that prints all digits of any integer in reverse order.
import java.util.Scanner;
public class ReverseDigits
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// TODO: Print the digits of n in reverse
/* Your code goes here */
if (n <0)
{
/* Your code goes here */
}
else if (n ==0)
{
/* Your code goes here */
}
while (/* Your code goes here */)
{
/* Your code goes here */
}
System.out.print(/* Your code goes here */);
System.out.println();
}
4.1.3: Simple while Loop Exercises.
- Let's modify the program from this section so that the user can supply the interest rate. For very small interest rates, it may take a very long time for the balance to double. Assume the user can't wait for more than twenty years. Stop adding interest when the balance has doubled or twenty years have elapsed.
import java.util.Scanner;
/**
This program computes the time required to double an investment.
*/
public class DoubleInvestment
{
public static void main(String[] args)
{
final double INITIAL_BALANCE =10000;
final double TARGET =2* INITIAL_BALANCE;
double balance = INITIAL_BALANCE;
int year =0;
Scanner in = new Scanner(System.in);
System.out.print("Interest rate in percent: ");
double rate = in.nextDouble();
// TODO: Count the years required for the investment to double
// but no more than 20 years
/* Your code goes here */
System.out.println("Year: "+ year);
System.out.printf("Balance: %.2f%n", balance);
}
}
Please follow the format provided , thank you soo muchh!!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!