প্রোগ্রামিংয়ের মৌলিক ধারণা/জাভা অবজেক্ট উদাহরণ
অবয়ব
অবজেক্ট
[সম্পাদনা] // এই প্রোগ্রামটি Temperature ক্লাসের ইনস্ট্যান্স তৈরি করে সেলসিয়াস
// এবং ফারেনহাইট তাপমাত্রার রূপান্তর করতে।
//
// রেফারেন্স:
// https://www.mathsisfun.com/temperature-conversion.html
// https://en.wikibooks.org/wiki/Java_Programming
import java.util.*;
class Main {
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());
}
}
// এই ক্লাসটি সেলসিয়াস এবং ফারেনহাইটের মধ্যে তাপমাত্রা রূপান্তর করে।
// এটি একটি মান সেলসিয়াস বা ফারেনহাইটে নির্ধারণ করে এবং তারপর অন্যটি বের করে আনা যেতে পারে,
// অথবা সরাসরি ToCelsius বা ToFahrenheit মেথডগুলি কল করা যেতে পারে।
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;
}
}
আউটপুট
[সম্পাদনা]temp1.celsius = 100.0 temp1.fahrenheit = 212.0 temp2.fahrenheit = 100.0 temp2.celsius = 37.77777777777778