i have to write an application to process weather data and calculate and display the average daily temperature.
3 instance variables which i already did.
Name (type string), temperature (an integer), and average (a double).
Now I need help with the Constructors
I have to create the "no argument constructor that accepts no parameters, and the other a constructor that accepts a parameter value used to initialize the NAME. In both constructors, you can initialize temperature and average to 0."
PLease help me, im so lost.
16 replies to this topic
#1
Posted 03 March 2011 - 01:08 PM
|
|
|
#2
Posted 03 March 2011 - 03:09 PM
Hi my assignment says : write an application to process weather data and calculate and display the average daily temperature. Create a class named WeatherStation. 3 instance variables name(string), temperature (integer) and average (a double variable).
I need to create Two constructors the no argument constructor that accepts no parameteres, and the other a constructor that accepts a parameter value used to initialize a name. In both constructors you can initialize temperature and average to 0. anddd do set/get methods for all instance variables
so far i have
im not sure if i am even on the right path? Please help!
I need to create Two constructors the no argument constructor that accepts no parameteres, and the other a constructor that accepts a parameter value used to initialize a name. In both constructors you can initialize temperature and average to 0. anddd do set/get methods for all instance variables
so far i have
public class WeatherStation {
//Instance Variables
private String stationName;
private int temperature;
private double average;
//Constructor no arg
public WeatherStation () {
setStationName (null);
setTemperature (null);
setAverage (0.0);
}
// Constructor with 3 args.
public WeatherStation( String stationName, int temperature, double average){
setStationName (stationName);
setTemperature (temperature);
setAverage (average);
}
// SET GET METHODS
public void setStationName (String newStationName) {
stationName = newStationName;
}
public void setTemperature (int newTemperature) {
temperature = newTemperature;
}
public void setAverage (double newAverage){
average = newAverage;
}
public String getStationName (){
return stationName;
}
public int getTemperature (){
return temperature;
}
public double getAverage (){
return average;
}
}
im not sure if i am even on the right path? Please help!
Edited by ZekeDragon, 03 March 2011 - 04:28 PM.
Please use [CODE] tags (the # button) when posting code.
#3
Posted 03 March 2011 - 05:18 PM
Try this
public WeatherStation()
{
StationName = "";
temperature = 0;
average = 0.0
}
public WeatherStation(String name, int temp, double avg)
{
StationName = name;
temperature = temp;
average = avg;
}
#4
Posted 03 March 2011 - 05:32 PM
okay perfect!
so far I have this:
My assignment says : write an application to process weather data and calculate and display the average daily temperature. Create a class named WeatherStation. 3 instance variables name(string), temperature (integer) and average (a double variable).
I need to create Two constructors the no argument constructor that accepts no parameteres, and the other a constructor that accepts a parameter value used to initialize a name. In both constructors you can initialize temperature and average to 0. anddd do set/get methods for all instance variables.
aaaand the next step of this assignment is to "processTemperatureData () -a method that reads temperature values and calculates the average for the day. The program will ask the user how many temperature values they want to input and then use that value in a statement of repetition to control the number of values read. and thennnnn after reading values and calculating the average, the method will store the average value in the instance variable????
I started this last part... but i am not sure if I am doing it right?
so far I have this:
public class WeatherStation {
//Instance Variables
private String stationName;
private int temperature;
private double average;
public WeatherStation()
{
stationName = "";
temperature = 0;
average = 0.0;
}
public WeatherStation(String name, int temp, double avg)
{
stationName = name;
temperature = temp;
average = avg;
}
// SET GET METHODS
public void setStationName (String newStationName) {
stationName = newStationName;
}
public void setTemperature (int newTemperature) {
temperature = newTemperature;
}
public void setAverage (double newAverage){
average = newAverage;
}
public String getStationName (){
return stationName;
}
public int getTemperature (){
return temperature;
}
public double getAverage (){
return average;
}
public void determineAverageTemperature()
{
Scanner input = new Scanner( System.in );
int total; //sum of temperatures
int temperatureCounter; //temperature to be entered next
int temperature; //temperature value by user
int average; // average temperature
total = 0; //Initialization total
temperatureCounter = 80; //initialize loop counter
while ( temperatureCounter <=10 ) //loop 10 times
{
System.out.print( "Enter Temperature ");
temperature = input.nextInt();
total = total + temperature;
temperatureCounter = temperatureCounter + 3;
}
average = total / 10;
System.out.printf( "\nTotal of all 10 temperatures is %d\n", total );
System.out.printf("Average Temperature is %d\n", average );
}
}
My assignment says : write an application to process weather data and calculate and display the average daily temperature. Create a class named WeatherStation. 3 instance variables name(string), temperature (integer) and average (a double variable).
I need to create Two constructors the no argument constructor that accepts no parameteres, and the other a constructor that accepts a parameter value used to initialize a name. In both constructors you can initialize temperature and average to 0. anddd do set/get methods for all instance variables.
aaaand the next step of this assignment is to "processTemperatureData () -a method that reads temperature values and calculates the average for the day. The program will ask the user how many temperature values they want to input and then use that value in a statement of repetition to control the number of values read. and thennnnn after reading values and calculating the average, the method will store the average value in the instance variable????
I started this last part... but i am not sure if I am doing it right?
Edited by Roger, 07 March 2011 - 02:45 PM.
Please use [CODE] tags (the # button) when posting code.
#5
Posted 03 March 2011 - 07:32 PM
You will need two variables, one to hold the number of temperatures entered and one for the sum of the temperatures. I'll give you psuedo-code but it's quite easy to do in java.
Ask for user input numInput++; // increments the number of temperatures inputed sum += input; // adds the input to the current value of sum return sum / (double) numInput;
#6
Posted 03 March 2011 - 08:37 PM
antface said:
I have to create the "no argument constructor that accepts no parameters, and the other a constructor that accepts a parameter value used to initialize the NAME. In both constructors, you can initialize temperature and average to 0."
Your second constructor should only be accepting one parameter. You may get counted off for this if it is an assignment.
public WeatherStation()
{
StationName = "";
temperature = 0;
average = 0.0
}
public WeatherStation(String name)
{
StationName = name;
temperature = 0;
average = 0.0
}
#7
Posted 04 March 2011 - 02:27 AM
You can refine the 2nd constructor by calling the first.
public WeatherStation(String name)
{
StationName = name;
thsi();
}
It avoids duplicate code, and prevents you from having to change something at multiple places if for example you now want to set the default temperature to 5.
#8
Posted 04 March 2011 - 07:13 AM
wim DC said:
You can refine the 2nd constructor by calling the first.
public WeatherStation(String name)
{
StationName = name;
thsi();
}
It avoids duplicate code, and prevents you from having to change something at multiple places if for example you now want to set the default temperature to 5.The this() keyword must be the first line in the constructor. This will not compile.
#9
Posted 07 March 2011 - 02:32 PM
take no notice of Lethalwire. A call to Super must be in the first line. If the parameter to the constructor is the same name as the instance variable then this is used to remove the ambiguity.
e.g.
e.g.
private String name;
private int ect;
MyConstructor(String name, int ect){
this.name = name;
this.ect = ect;
}
#10
Posted 07 March 2011 - 03:44 PM
the this keyword shouldn't be used in the constructor, as it refers to the object which is being constructed. It's like relying on a building that's under construction.
Also, "no argument constructor that accepts no parameters," quoted from the OP. I just found it funny that it says, no arguments and no parameters. Why not ask for a no pepperoni pizza with no pepperoni :P
Also, "no argument constructor that accepts no parameters," quoted from the OP. I just found it funny that it says, no arguments and no parameters. Why not ask for a no pepperoni pizza with no pepperoni :P
#11
Posted 07 March 2011 - 04:20 PM
I'm not sure what you're referring to Smilex.
Using this.name = name in your constructor is just a programming style, one that I like. I prefer it to using different variable names. Also, you can use the keyword "this" to call another constructor, it's very useful cause it makes it possible to reuse code.
Using this.name = name in your constructor is just a programming style, one that I like. I prefer it to using different variable names. Also, you can use the keyword "this" to call another constructor, it's very useful cause it makes it possible to reuse code.
#12
Posted 07 March 2011 - 04:32 PM
hArrayList said:
take no notice of Lethalwire. A call to Super must be in the first line. If the parameter to the constructor is the same name as the instance variable then this is used to remove the ambiguity.
e.g.
e.g.
private String name;
private int ect;
MyConstructor(String name, int ect){
this.name = name;
this.ect = ect;
}
I was addressing the problem of StationName = name before using this() in a constructor.
You have to call this(); before you call StationName = name in the constructor.
Else, compile error.
Never was I referring to the usage of the this keyword to refer to field variables.
Don't be so rude.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top










