2016-04-22 1 views
-1

Ich habe die Struktur für mein gesamtes Programm eingerichtet, aber habe Probleme beim Erstellen von zwei Klassen, TooColdException und TooHotException, sie haben einen Auftragnehmer, der einen String-Parameter übernimmt und an den Exception-Klassenkonstruktor übergibt , Ich habe die Klassen halb erstellt, bin mir aber nicht sicher, wie ich das beenden soll, mein Code ist unten und jeder secimport java.awt. ; importieren Sie java.awt.event.; Import javax.swing. *;Erstellen einer Klasse in Verbindung mit Ausnahmen

public class HotCoffeePanel extends JPanel implements ActionListener 
{ 
    private JLabel  label; 
    private JTextField temperature; 
    private JButton check_temp; 

    public HotCoffeePanel() 
    { 
    label  = new JLabel("Water temperature in \u00b0F:"); 
    temperature = new JTextField(4); 
    check_temp = new JButton("Check Temperature"); 

    add(label); 
    add(temperature); 
    add(check_temp); 
    check_temp.addActionListener(this); 

    setPreferredSize(new Dimension(300, 75)); 
    setBackground(Color.yellow); 
    } 

    // ----------------------------------------------------- 
    // Listen for the Check Temperature button and determine 
    // if water is the correct temperature to brew coffee 
    // ----------------------------------------------------- 
    public void actionPerformed(ActionEvent event) 
    { 
    if (Integer.parseInt(temperature.getText()) < 190) 
     try 
     { 
     throwTooColdException(); 
     } 
     catch(TooColdException tce) 
     { 
     JOptionPane.showMessageDialog(null, tce.getMessage()); 
     } 
    else if (Integer.parseInt(temperature.getText()) > 200) 
     try 
     { 
     throwTooHotException(); 
     } 
     catch(TooHotException the) 
     { 
     JOptionPane.showMessageDialog(null, the.getMessage()); 
     } 
    else 
     JOptionPane.showMessageDialog(null, "Water temperature is fine for brewing coffee."); 
    } 

    //------------------------ 
    // TooColdException class 
    //------------------------ 

    public class TooColdException 
    } 
    public TooColdException(String ) 

    } 

    //------------------------ 
    // TooHotException class 
    //------------------------ 

    public class TooHotException 
    } 
    public TooColdException(String ) 

    } 



    // ------------------------------------- 
    // Exception thrown if water is too cold 
    // ------------------------------------- 
    private void throwTooColdException() throws TooColdException 
    { 
    throw new TooColdException("Temperature is too cold to brew coffee."); 
    } 

    // ------------------------------------ 
    // Exception thrown if water is too hot 
    // ------------------------------------ 
    private void throwTooHotException() throws TooHotException 
    { 
    throw new TooHotException("Temperature is too hot to brew coffee."); 
    } 
} // End of HotCoffeePanel class definition 
+0

"habe ich die Struktur für mein gesamtes Programm up" - nein, du nicht. Du öffnest deine Klammer nicht einmal, du schließt sie. Versuchen Sie, sich über grundlegende Java-Syntax zu informieren. – f1sh

Antwort

1

Ihre Ausnahmen sind nicht richtig formuliert. Sie müssen die Klammern richtig schließen. Außerdem müssen sie von der Java-Ausnahmeklasse erben.

Wie so:

public class TooHotException extends Exception { 
    public TooHotException(String message) { 
     super(message); 
    } 
}