कंप्यूटर प्रोग्रामिंग/ऑब्जेक्ट्स/जावा

विकिविश्वविद्यालय से

files.java[सम्पादन | स्रोत सम्पादित करें]

// This program creates instances of the Temperature class to convert Cesius
// and Fahrenheit temperatures.

import java.util.*;

class objects
{
    public static void main(String[] args) 
    {
        Temperature temp1 = new Temperature();
        temp1.setCelsius(100.0);
        System.out.println("temp1.celsius = " + temp1.getCelsius().toString());
        System.out.println("temp1.fahrenheit = " + temp1.getFahrenheit().toString());
        System.out.println("");
        
        Temperature temp2 = new Temperature();
        temp2.setFahrenheit(100.0);
        System.out.println("temp2.fahrenheit = " + temp2.getFahrenheit().toString());
        System.out.println("temp2.celsius = " + temp2.getCelsius().toString());
    }
}

// This class converts temperature between Celsius and Fahrenheit.
// It may be used by assigning a value to either Celsius or Fahrenheit
// and then retrieving the other value, or by calling the ToCelsius or
// ToFahrenheit methods directly.

class Temperature
{
    Double celsius;
    Double fahrenheit;

    public Double getCelsius()
    {
        return celsius;
    }
    
    public void setCelsius(Double value)
    {
        celsius = value;
        fahrenheit = toFahrenheit(celsius);
    }

    public Double getFahrenheit()
    {
        return fahrenheit;
    }
    
    public void setFahrenheit(Double value)
    {
        fahrenheit = value;
        celsius = toCelsius(fahrenheit);
    }

    public Double toCelsius(Double fahrenheit)
    {
        return (fahrenheit - 32) * 5 / 9;
    }
    
    public Double toFahrenheit(Double celsius)
    {
        return celsius * 9 / 5 + 32;
    }
}

कोशिश करो[सम्पादन | स्रोत सम्पादित करें]

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के जावा कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें[सम्पादन | स्रोत सम्पादित करें]