1.11M Members

temperature conversion

 
0
 

I have to create a temperature conversion project using a method and driver class...... i did it b4 put only as one file , i started the driver class but when i compile i get a couple errors; the errors i get are:
Degree.java:30: illegal start of expression
public double fahrenheitTemp()
^
Degree.java:24: Degree(float) is already defined in Degree
public Degree(float degree)
^
Degree.java:55: unexpected type
required: variable
found : value
'F' = fahrenheitTemp;
^
Degree.java:56: unexpected type
required: variable
found : value
'C' = celsiusTemp;
^
here is what my constructor method looks like

public class Degree 
{

float degree;
double temp;
double fahrenheitTemp;
double celsiusTemp;
char scale;


public Degree(float degree)
{
 temp = degree;
 scale = 'C';

}
 
public Degree(float degree)
{
 temp = degree;
 scale = 'F';


public double fahrenheitTemp()
{
  fahrenheitTemp = celsiusTemp * 5/9-32;
  return fahrenheitTemp;
}

public double celsiusTemp()
{
  celsiusTemp = fahrenheitTemp * 9/5+32;
  return celsiusTemp;
}

public String toString()
{
   return "Fahrenheit Temperature: " + fahrenheitTemp;
	return "Celsius Temperature: " + celsiusTemp;
}

public void setDegrees(float degree)
{
  temp = degree;
}

public char setScale()
{
  'F' = fahrenheitTemp;
  'C' = celsiusTemp;
}
}

now here is what my driver class looks like:

public class TempConverter
{

public static void main(String[] args){

Degree degree1 = new Degree(2);
double fahrenheitTemp;
double celsiusTemp;

fahrenheitTemp = degree1.fahrenheitTemp();
System.out.println("The Fahrenheit Temperature is " + fahrenheitTemp);

celsiusTemp = degree1.celsiusTemp();
System.out.println("The Celsius Temperature is " + celsiusTemp);

}
}

and here is a full discription of the assignment
Write a Temperature class that respresents temperatures in degrees in both Celsius and Fahrenheit. Use a floating-point number for the temperature and a character for the scale: either ‘C’ for Celsius or ‘F’ for Fahrenheit. The class should have
•Four constructors: one for the number of degrees, one for the scale, one for both degrees and the scale, and one default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given.
•Two accessor methods: one to return the temperature in degrees Celsius, the other to return it in degrees Fahrenheit. Use these formulas: C = 5/9(F – 32) and F=9/5 * C + 32
•Three set methods: one to set the number of degrees, one to set the scale, and one to set both
•One comparison method to test whether 2 temperatures are the same
•One toString to print out one line that describes the temperature
Write a driver program that tests all the methods. Be sure to invoke each of the constructors, to include at least one case where the 2 temperatures are the same and one where they are different.

IF ANYONE WULD BE ABLE TO HELP ME WITH MY ERRORS IN MY CONSTRUTOR METHOD OR AS LEAST LET ME KNOW WHAT I DID WRONG THAT WOULD GREATLY BE APPRECIATED......

 
0
 

You have two of the same constructors

Change this

public Degree(float degree)
{
 temp = degree;
 scale = 'C';

}
 
public Degree(float degree)
{
 temp = degree;
 scale = 'F';

}

Read what you are assigned

"Four constructors: one for the number of degrees, one for the scale, one for both degrees and the scale, and one default constructor. For each of these constructors, assume zero degrees if no value is specified and Celsius if no scale is given."

Here are the constructors, read your instructions on what to do with them

public Degree(){
}

public Degree(float degree){
}

public Degree(char scale){
}

public Degree(float cDegree, float fDegree, char scale){
}
 
0
 

When you have a degree as a parameter you don't know if it is in Celsius or in Fahrenheit. So you will use the scale to determine that and calculate the results accordingly.

So don't put any calculations in your get Methods.

When you set the degree (by Constructor or be a set Method), use the scale to see if it is Celsius or Fahrenheit and use the appropriate formula:

public void setDegrees(float degree)
{
  if (scale=='C') {
          celsiusTemp =degree;
          fahrenheitTemp = celsiusTemp * 5/9-32;
  } else {
         fahrenheitTemp =degree;
         celsiusTemp = fahrenheitTemp * 9/5+32;
  }
}

This is wrong: 'F' = scale;
This will work: scale = 'F';

Meaning that your set methods should have arguments and will pass that argument to the class variable.

Use the instructions of the assignment to create the 4 constructors. Based on the input calculate the fahrenheitTemp, celsiusTemp like I did in the setDegree. If no input specified the assignment tells you what values to put.

And you declared the same Constructor twice which gave you an error:

public Degree(float degree)
{
 temp = degree;
 scale = 'C';

}
 
public Degree(float degree)
{
 temp = degree;
 scale = 'F';

And the other mistakes are because of missing and/or misplaced brackets

 
0
 

this is an update on my constructor method file:

public class Degree 
 {

  float degree;
  double fahrenheitTemp;
  double celsiusTemp;
  char scale;


 public Degree()
  {
  }

 public Degree(float degree)
  {
   degree = 0;
  }

 public Degree(char scale)
  {
    scale = 'C';
  }

 public Degree(float cDegree, float fDegree, char scale)
  {
   
  }


 public double getfahrenheitTemp()
  {
  return fahrenheitTemp;
 }

 public double getcelsiusTemp()
  {
  return celsiusTemp;
 }

 public String toString()
  {
   return "Fahrenheit Temperature: " + fahrenheitTemp;
	return "Celsius Temperature: " + celsiusTemp;
 }

 public void setDegrees(float degree)
  {
   if (scale=='C')
   {
    celsiusTemp = degree;
    fahrenheitTemp = celsiusTemp * 5/9-32;
   }
   else
    {
    fahrenheitTemp = degree;
    celsiusTemp = fahrenheitTemp * 9/5+32;
    }
  }

 public char setScale()
  {
    scale = 'F';
    scale = 'C';
	return scale;
  }
}

but i read my instructions which are stated above in the original post but i dont think i applied the instructors correctly in my 4 constructor methods at the beginning can anyone look over it to see if im missing anything,
and in my toString method i get unreachable statement
return "Celsius Temperature: " + celsiusTemp;
did i miss something in my toString method which is causing this error

 
0
 

You can't have the multiple returns in your toString.

public String toString()
  {
if(scale == 'F'){
   return "Fahrenheit Temperature: " + fahrenheitTemp;
}
else {
	return "Celsius Temperature: " + celsiusTemp;
}
 }
 
0
 

ok thank you sooo much for everyones help but i have another problem when i run my tempconverter driver it returns 0.0 for both temperatures
here's what my four constructors are but im not sure if there right:

public Degree()
  {
  }

 public Degree(float degree)
  {
   degree = 0;
  }

 public Degree(char scale)
  {
    scale = 'C';
  }

 public Degree(float cDegree, float fDegree, char scale)
  {
   
  }

and here are my two accessor methods

public double getfahrenheitTemp()
  {
  return fahrenheitTemp;
 }

 public double getcelsiusTemp()
  {
  return celsiusTemp;
 }

again i dont think i have its right, in the assignment it says for the accessor methods i have to use the formulas how would i do that ?

 
0
 

the actual constructor signature is fine, the code inside is not

public Degree()
  {
this.scale = 'C';
this.degree = 0;
setDegrees(this.degree);
  }

 public Degree(float degree)
  {
this.scale = 'C';
   this.degree = degree;
setDegrees(this.degree);
  }

 public Degree(char scale)
  {
    this.scale = scale;
this.degree = 0;
setDegrees(this.degree);
  }
 
0
 

thank yoou so much for ur help a couple ore things and then i think i got it,
ok i got the first 3 constructors for the last one:

public Degree(float cDegree, float fDegree, char scale)

if have this, is this right?

{
   this.scale = 'C'; 
	this.scale = 'F';
	this.scale = scale;
	this.degree = degree;
	setDegrees(this.degree); 
  }

and as far as my getters
are they right?

public double getfahrenheitTemp()
  {
   fahrenheitTemp = celsiusTemp * 5/9-32;
   return fahrenheitTemp;
  }

 public double getcelsiusTemp()
  {
   celsiusTemp = fahrenheitTemp * 9/5+32;
   return celsiusTemp;
  }
 
0
 

sorry i misread the original, it is BOTH "degree" (not degrees) and scale

public Degree(float degree, char scale)
{
   this.scale = scale; 
   this.degree = degree;
setDegrees(this.degree); 
  }

the conversion is taking place in the setter, which is always called on each constructor, the getters only need to return the value

 
0
 

thank u very much for ur time and help

Question Answered as of 5 Years Ago by dickersonka and javaAddict
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article