please help me write this class /** * This class represents metal plate as a 2-d
ID: 3880694 • Letter: P
Question
please help me write this class
/**
* This class represents metal plate as a 2-dimensional grid of
* temperature values.
*
*/
public class HeatPlate
{
/**
* A flag to indicated when we have reached convergence within the tolerance.
*/
private boolean converged;
/**
* A tolerance value used to determine whether two temperature values should be considered 'equal'.
*/
private double epsilon;
/**
* The maximum temperature allowed in our plate, 10000 Kelvin.
*/
private static double MAX_TEMP = 10000;
/**
* The minimum temperature allowed in our plate, 0 Kelvin.
*/
private static double MIN_TEMP = 0;
/**
* The number of steps computed so far.
*/
private int numSteps;
/**
* The array of double values representing discrete temperatures in our metal plate.
*/
private double[][] theValues;
/**
* Constructs a HeatPlate whose plate contains the temperatures as originally specified by initTemps. A default tolerance for comparisons of 1.0x10^-2 will be used.
* @param initTemps An array of double values to use as initial temperatures.
*/
public HeatPlate(double[][] initTemps)
{
}
/**
* Constructs a HeatPlate whose plate contains the temperatures as originally specified by initTemps. The custom tolerance value will be used for all comparisons.
* @param toleranceIn The tolerance to use for double comparisons
* @param initTemps An array of double values to use as initial temperatures.
*/
public HeatPlate(double toleranceIn, double[][] initTemps)
{
}
/**
* Retrieves an individual temperature from the HeatPlate at a specified coordinate.
* @param row The row of the indicated cell.col
* @param col The column of the indicated cell.
* @return The temperature value in the specified cell when the coordinates are valid. -1 will be returned if the given coordinates are invalid.
*/
public double getTempAt(int row, int col)
{
}
/**
* Returns the number of rows in the HeatPlate.
* @return The number of rows.
*/
public int getNumRows()
{
}
/**
* Returns the number of columns in the HeatPlate.
* @return The number of columns.
*/
public int getNumCols()
{
}
/**
* Returns the total number of simulation steps that have been computed on this HeatPlate object.
* @return The number of steps computed so far. If no steps have been requested yet, this will be 0.
*/
public int numberSteps()
{
}
/**
* Tells us whether the HeatPlate has converged at equilibrium.
* @return True if two consecutive simulation steps have resulted in the same overall temperature gradient. False otherwise.
*/
public boolean hasConverged()
{
}
/**
* This method must simulate a single step in the simulation given the current temperature values. See details in the Canvas assignment description.
*/
public void simulateStep()
{
}
/**
* Getter method to retrieve the tolerance used for comparing values by this HeatPlate.
* @return the configured tolerance
*/
public double getTolerance()
{
}
/**
* Computes a single cell value as the average of the neighbors.
* @param i The row of the indicated cell
* @param j The col of the indicated cell
* @return The new value
*/
private double computeCell(int i, int j)
{
}
}
Explanation / Answer
here is your code :-------------------->>>>>>>>>>>>>>>
public class HeatPlate{
private boolean converged;
private double epsilon;
static double MAX_TEMP = 0;
static double MIN_TEMP = 1000;
private int numSteps;
private double[][] theValues;
public HeatPlate(double[][] initTemps){
initializeTemp(initTemps);
}
public HeatPlate(double tolerence,double[][] initTemps){
epsilon = tolerence;
initializeTemp(initTemps);
}
private void initializeTemp(double[][] temp){
theValues = new Double[temp.length][temp[0].length];
for(int i = 0;i<temp.length;i++){
for(int j = 0;j<temp[0].length;j++){
theValues[i][j] = temp[i][j];
if(MAX_TEMP < temp[i][j]){
MAX_TEMP = temp[i][j];
}
if(MIN_TEMP > temp[i][j]){
MIN_TEMP = temp[i][j];
}
}
}
converged = false;
}
private double compute(int i,int j){
double res = 0;
if(i == 0 && j == 0 ){
res = (theValues[i+1][j] + theValues[i][j+1])/2;
}else if(i == 0 ){
res = (theValues[i+1][j] + theValues[i][j+1] + theValues[i][j-1])/3;
}else if(j == 0 ){
res = (theValues[i+1][j] + theValues[i][j+1]+theValues[i-1][j])/3;
}else if(i == getNumRows()-1 && j == getNumCols()-1){
res = (theValues[i-1][j] + theValues[i][j-1])/2;
}else if(i == getNumRows()-1){
res = (theValues[i-1][j] + theValues[i][j+1]+theValues[i][j-1])/3;
}else if(j == getNumCols()-1){
res = (theValues[i+1][j] + theValues[i-1][j]+theValues[i][j-1])/3;
}else{
res = (theValues[i+1][j] + theValues[i][j+1]+theValues[i-1][j]+theValues[i][j-1])/4;
}
return res;
}
int getNumRows(){
return theValues.length;
}
int getNumCols(){
return theValues[0].length;
}
double getTempAt(int i,int j){
if(i > 0 && j > 0 && i < getNumRows() && j < getNumCols()){
return theValues[i][j];
}
return 0;
}
double getTolerence(){
return epsilon;
}
boolean hasConverged(){
return converged;
}
int getNumSteps(){
return numSteps;
}
void simulateSteps(){
numSteps++;
double temp;
for(int i=0;i<numRows;i++){
for(int j = 0;j<numCols;j++){
temp = compute(i,j);
if(abs(temp - theValues[i][j]) == epsilon){
if(j == 0 && i == 0)
converged = true;
}else{
converged = false;
}
if(temp > MAX_TEMP){
MAX_TEMP = temp;
}
if(temp < MIN_TEMP){
MIN_TEMP = temp;
}
theValues[i][j] = temp;
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.