1.Write a GUI program called SwingTemperatureConverter to convert temperature va
ID: 3815352 • Letter: 1
Question
1.Write a GUI program called SwingTemperatureConverter to convert temperature values between Celsius and Fahrenheit. User can enter either the Celsius or the Fahrenheit value, in floating-point number. Hints: To display a floating-point number in a specific format (e.g., 1 decimal place), use the static method String.format(), which has the same form as printf(). For example, String.format("%.1f", 1.234) returns String "1.2".
2.Write a number guessing game in Swing (as shown in the Figure). The program shall generate a random number between 1 to 100. It shall mask out the random number generated and output "Yot Got it", "Try Higher" or "Try Lower" depending on the user’s input.
Hints: • You can use Math.random() or rnd.nextInt(100) to generate a random number in double in the range of [0.0, 1.0).
3.A Java Swing application has a login page as shown in the Figure. Users are required to enter the correct passcode to start the application. The system uses a scramble keypad with a randomly allocated set of numbers from 0 to 9. The display shall show "Enter passcode" initially, and show an asterisk (*) for each number entered. Upon pushing the "Enter" button, the system verifies the passcode. If the passcode is correct, the system invokes a method called startApp() to start the application. Otherwise, it displays "Wrong passcode". The "Clear" button shall clear the display. Assume that the following methods are available:
// return the passcode
public String getPasscode(){
//Random rnd = new Random();
//String passcode4digit = rnd.nextInt(10) + “” + rnd.nextInt(10) + “” + // rnd.nextInt(10) + “” + rnd.nextInt(10);
String passcode4digit =”7312”;
return passcode4digit;
}
// Start the application public void startApp(){
System.out.println(“Application started”); }
// Shuffle (Randomize) the given int array, e.g.,
// int[] numbers = {1, 2, 3, 4, 5};
// shuffleArray(numbers);
// randomize the elements
public void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
Explanation / Answer
public class TempConverter {
public static enum TEMP_TYPE {
Celsius, Fahrenheit;
}
private final static TEMP_TYPE DEFAULT_TEMP_TYPE = TEMP_TYPE.Celsius;
private final TEMP_TYPE convertingFrom, convertingTo;
public TempConverter(TEMP_TYPE convertFrom) {
convertingFrom = convertFrom;
try {
fromTemp = new Scanner(tempTextField.getText()).nextDouble();
validInput = true;
}
catch (java.util.InputMismatchException e) {
System.out.println("**ERROR**: Entered temperature isn't valid.");
}
catch (java.util.NoSuchElementException e) {
System.out.println("**ERROR**: No temperature entered.");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.