question archive Assume the project shown in the last question is built following the MVC design pattern

Assume the project shown in the last question is built following the MVC design pattern

Subject:Computer SciencePrice:2.84 Bought3

Assume the project shown in the last question is built following the MVC design pattern.  Write the listener class for the Fahrenheit to Celsius conversion (when F==>C button is clicked) as an inner class of the controller class. The class diagram (from Eclipse) of the view class and the complete model class are given below. 

public class ConverterModel {

 public double fToC(double f) {

   return (f - 32) * 5 / 9;

 }

public double cToF(double c) {

   return c * 9 / 5 + 32;

 }

} // end class ConverterModel

 

Here is the partial controller class:

// ConverterController class

public class ConverterController {

 private ConverterView theView;

 private ConverterModel theModel;

 ... // listener class for converting F to C

 class FToCConvertListener implements ActionListener {

   public void actionPerformed(ActionEvent e) {

     // ADD CODE

   }

 }

} // end class ConverterController

Be sure to handle invalid user input. Use the displayErrorMessage() of the view class to display a message like "Please enter a number.".

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Java code

 

import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.*;

public class FToCConvertListener  extends JFrame implements ActionListener {



private JTextField cTempTextField;



private JTextField fTempTextField;



private JButton fToCButton; // F -> C conversion. Top button



private JButton cToFButton; // C -> F conversion. Bottom button



public FToCConvertListener() {

// ADD CODE: set up the GUI interface

  setTitle("Temp Converter");

// set close operation whe we click the x at the corner

  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// initialize components

  cTempTextField=new JTextField(20);


  fTempTextField= new JTextField(20);


  fToCButton=new JButton("F -> C");


  cToFButton=new JButton("C -> F");



// a 2 x 3 grid.


//create the grid

  GridLayout grid = new GridLayout(2,3);

//add the grid

  setLayout(grid);


// END ADD CODE

//add components

  add(fTempTextField);


  add(fToCButton);


  add(cTempTextField);


  add(cToFButton);

//ActionListener for converting F to C and C to F

 fToCButton.addActionListener(this);
 cToFButton.addActionListener(this);

  pack(); // automatically adjust frame window size


//show the gui

  setVisible(true);

 }

   public double fToC(double f) {

    return (f - 32) * 5 / 9;

  }

  public double cToF(double c) {

    return c * 9 / 5 + 32;

 }

void displayErrorMessage() {

        JOptionPane.showMessageDialog(null, "Please enter a number.");

     }


  // listener class for converting F to C and C to F
public void actionPerformed(ActionEvent e) {

      // ADD CODE
try {



if( (e.getSource() == fToCButton)   ){
    Integer.parseInt(fTempTextField.getText()) ;
    JOptionPane.showMessageDialog(null,fToC(Integer.parseInt( fTempTextField.getText()) ));

}else if( (e.getSource() == cToFButton)     ){
    Integer.parseInt(cTempTextField.getText());
          JOptionPane.showMessageDialog(null,cToF(Integer.parseInt( cTempTextField.getText()) ));
}
}catch(NumberFormatException ex){

    displayErrorMessage();
    }

   }


// main for test purpose

public static void main (String argv[]){

 FToCConvertListener frame =new FToCConvertListener();

 }



} // end class ConverterView

Step-by-step explanation

steps of solution

 

added methods that convert Fahrenheit to Celsius and reverse

   public double fToC(double f) {

    return (f - 32) * 5 / 9;

  }

  public double cToF(double c) {

    return c * 9 / 5 + 32;

 }

 

add action to buttons when click on each of them

//ActionListener for converting F to C and C to F

 fToCButton.addActionListener(this);
 cToFButton.addActionListener(this);

 

implement the action for buttons and check if the input is digits or not then display error message

public void actionPerformed(ActionEvent e) {
try {

if( (e.getSource() == fToCButton)   ){
    Integer.parseInt(fTempTextField.getText()) ;
    JOptionPane.showMessageDialog(null,fToC(Integer.parseInt( fTempTextField.getText()) ));

}else if( (e.getSource() == cToFButton)     ){
    Integer.parseInt(cTempTextField.getText());
          JOptionPane.showMessageDialog(null,cToF(Integer.parseInt( cTempTextField.getText()) ));
}
}catch(NumberFormatException ex){

    displayErrorMessage();
    }

   }

 

method display error message if the input is not digits

void displayErrorMessage() {

        JOptionPane.showMessageDialog(null, "Please enter a number.");

     }

Related Questions