Instructions: Add an attribute for the boiling point (in Kelvin). Modify the con
ID: 3756302 • Letter: I
Question
Instructions:
Add an attribute for the boiling point (in Kelvin).
Modify the constructor, so that it takes a forth argument, and sets its value to be the value of the boiling point attribute.
Create a static FromKelvinToFahrenheit method, taking inspiration from the FromKelvinToCelsius method.
Create a MeltingInFahrenheit method, that returns the melting point in Fahrenheit of the calling object. This method should use your FromKelvinToFahrenheit method.
Create a BoilingInFahrenheit method, that return the boiling point in Fahrenheit of the calling object. This method should use your FromKelvinToFahrenheit method.
Modify the ToString method, so that the string returned includes
The name of the chemical element and its atomic number,
The melting point in Kelvin and in Fahrenheit,
The boiling point in Kelvin and in Fahrenheit.
Code:
class ChemElem
{
// Attributes:
private int atomicNumber; // Atomic number of the chemical element
private string name; // Name of the chemical element
private decimal melt; // Melting point (in Kelvin) of the chemical element
// Constructor:
public ChemElem(int atomicNumberParam, string nameParam, decimal meltParam)
{
atomicNumber = atomicNumberParam;
name = nameParam;
melt = meltParam;
}
/* To convert from Kelvin to Celsius
* Cf. conversion rules at https://en.wikipedia.org/wiki/Kelvin
* from kelvins to kelvins
* Celsius [°C] = [K] 273.15 [K] = [°C] + 273.15
* Fahrenheit [°F] = [K] × 95 459.67 [K] = ([°F] + 459.67) × 59
*/
public static decimal FromKelvinToCelsius(decimal kelvinParam)
{
return (kelvinParam - 273.15M);
}
public decimal MeltingInCelsius()
{
return FromKelvinToCelsius(melt);
}
public override string ToString()
{
return name + "'s atomic number is " + atomicNumber + " "
+ "It melts at " + melt + "K (" + MeltingInCelsius() +
"°C). ";
}
}
(I think I have 95% of it done, but I want to compare)-
Explanation / Answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
class ChemElem
{
// Attributes:
private int atomicNumber; // Atomic number of the chemical element
private string name; // Name of the chemical element
private double melt; // Melting point (in Kelvin) of the chemical element
private double boil; // Boiling point (in Kelvin) of the chemical element
// Constructor:
public ChemElem(int atomicNumberParam, string nameParam, double meltParam, double boilParam)
{
atomicNumber = atomicNumberParam;
name = nameParam;
melt = meltParam;
boil = boilParam;
}
public static double FromKelvinToCelsius(double kelvinParam)
{
return (kelvinParam - 273.15);
}
public double MeltingInCelsius()
{
return FromKelvinToCelsius(melt);
}
public double FromKelvinToFahrenheit(double K)
{
return K * 9.0 / 5.0 - 459.67;
}
public double MeltingInFahrenheit()
{
return FromKelvinToFahrenheit(melt);
}
public double BoilingInFahrenheit()
{
return FromKelvinToFahrenheit(boil);
}
public override string ToString()
{
return name + "'s atomic number is " + atomicNumber + " " + "It melts at " +melt + "K (" + MeltingInFahrenheit() + "°F). " + "It boils at " + boil + "K (" + BoilingInFahrenheit() + "°F). ";;
}
}
public class Program
{
public static void Main(string[] args)
{
ChemElem ob = new ChemElem(1 ,"Hydrogen", 745646.68 , -2563.56);
Console.WriteLine(ob);
}
}
}
Sample Output