2014-10-05 3 views
-3

Demo.Java:Начало «inchesAndFeetToCentimeteres метод (двойной, двойной) не определено для типа Demo»

import javax.swing.*; 

/** 
* This class is used to demonstrate the functionality of the MetricConverter 
* class. 
*/  
class Demo { 

    public static void main (String[] args) { 

     MetricConverter converter = new MetricConverter(); 

     double inputInches; 
     double inputFeet; 
     double centimeters, inches; 
     String inputInchesAsString; 
     String inputFeetAsString; 
     //Get input 
     inputInchesAsString = JOptionPane.showInputDialog(null, "Enter inches: "); 
     inputInches = Double.parseDouble(inputInchesAsString); 

     inputFeetAsString = JOptionPane.showInputDialog(null, "Enter feet: "); 
     inputFeet = Double.parseDouble(inputFeetAsString); 
     //Perform various conversion routines 

     centimeters = converter.inchesToCentimeters(inputInches); 
     inches = converter.centimetersToInches(centimeters); 

В строке ниже это один, что я имею проблемы с этим я не могу понять. Мой метод создан правильно и возвращает правильные типы, но он все еще не работает.

 centimeters = inchesAndFeetToCentimeteres(inputFeet, inputInches); 

     //Display the result 
     JOptionPane.showMessageDialog(null, "Input: " + inputInches + 
             " inches is equivalent to " + 
             centimeters + " centimeters"); 
     JOptionPane.showMessageDialog(null, "Converting back to inches: " + inches); 

     JOptionPane.showMessageDialog(null, "Input: " + inputFeet +" Feet and " + inputInches + 
             " inches is equivalent to " + 
             centimeters + " centimeters"); 
     JOptionPane.showMessageDialog(null, "Converting back to inches: " + inches); 
    } 


} 

MetricConverter.java:

/** 
* This class provides various routines to 
* convert metric measurements to U.S. units and 
* vice versa. 
*/ 
class MetricConverter { 

    //---------------------------------- 
    // Data Members 
    //---------------------------------- 

    /** 
    * A factor to convert inches to centimeters 
    */ 
    public static final double INCHES_TO_CENTIMETERS = 2.54; 

    /** 
    * A factor to convert centimeters to inches 
    */ 
    public static final double CENTIMETERS_TO_INCHES = 1/2.54; 

    /** 
    * A factor to convert feet to inches 
    */ 
    public static final double FEET_TO_INCHES = 12.0; 

    //---------------------------------- 
    // Constructors 
    //---------------------------------- 

    /** 
    * Default constructor 
    */ 
    public MetricConverter() { 

    } 

    //------------------------------------------------- 
    // Public Methods: 
    // 
    //  double inchesToCentimeters  (double  ) 
    //  double centimetersToInches  (double  ) 
    //  double feetAndInchesToCentimeters (double, double) 
    //------------------------------------------------ 

    /** 
    * Converts a given length in inches to 
    * equivalent centimeters. 
    * 
    * @param inches the length expressed in inches 
    * @return length expressed in centimeters 
    */ 
    public double inchesToCentimeters(double inches) { 
     return inches * INCHES_TO_CENTIMETERS; 
    } 

    /** 
    * Converts a given length in centimeters to 
    * equivalent inches. 
    * 
    * @param centimeters the length expressed in centimeters 
    * @return length expressed in inches 
    */ 
    public double centimetersToInches(double inches) { 
     return inches * CENTIMETERS_TO_INCHES; 
    } 

    /** 
    * Converts a given length in feet and inches to 
    * equivalent centimeters. 
    * 
    * @param feet the feet portion of the length 
    * @param inches the inch portion of the length 
    * @return length expressed in centimeters 
    */ 
    public double feetAndInchesToCentimeters(double inputFeet, double inputInches) { 
     inputInches = (inputFeet * FEET_TO_INCHES) + inputInches; 
     double centimeters = inchesToCentimeters(inputInches); 
     return centimeters; 
    }     
} 

ответ

2

inchesAndFeetToCentimeteres должно быть feetAndInchesToCentimeters и должны быть вызваны с помощью экземпляра MetricConverter, так как метод определен здесь, а не в Demo

centimeters = converter.feetAndInchesToCentimeters(inputFeet, inputInches); 
Смежные вопросы