Results 1 to 18 of 18
Thread: intro to java please help me
- 03-03-2011, 11:05 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
intro to java please help me
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
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!
- 03-03-2011, 11:16 PM #2
A few things.
You shouldnt set strings to be null, they should usually be set to be the empty string by default.
Likewise ints shouldnt be null (i dont think they CAN be) - they should be set to some default value, which in this case is specified to be zero.
"the other a constructor that accepts a parameter value used to initialize a name"
- this means you need a constructor which accepts only a string.
Java Code:public WeatherStation( String stationName){ setStationName (stationName); setTemperature (0); setAverage (0.0); }
- 03-03-2011, 11:17 PM #3
You tell us. Have you tried compiling your code? Do you get any errors? If so fix them before moving on.
In future posts like "what should I do", "it doesn't work" or "does this look right" are vague and difficult to answer. When asking a question make it as specific as you can and we can provide a specific answer.
- 03-03-2011, 11:21 PM #4
- 03-03-2011, 11:23 PM #5
- 03-03-2011, 11:24 PM #6
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
okkay great! :) so I fixed my constructors.... are my set/get methods on the right path?
and 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????
does anyone know how to start this part?
6 minutes ago
oh and my new and improved code is :
public class WeatherStation {
//Instance Variables
private String stationName;
private int temperature;
private double average;
//Constructor no arg
public WeatherStation () {
this (null);
}
//Constructor with 1 args.
public WeatherStation( String stationName){
setStationName (stationName);
setTemperature (0);
setAverage (0);
}
// 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;
}
}
- 03-03-2011, 11:26 PM #7
But if you get a NPE then it highlights a problem in your code which should be fixed. Assigning an empty String to a variable can hide that problem.
- 03-03-2011, 11:34 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You shouldt even need this(null). Also, you don't need to use getter and setters inside the class, in the constructor you can manipulate instance variables directly.
For the next method, what were you thinking of doing? It's not so challenging, try using a loop and see where it gets you.
- 03-03-2011, 11:37 PM #9
@junky - what kind of errors would it hide? I like to think of it as presuming default values for primitives.
@antface - I suggest you think about it for a while. Work through some pseudo code, and come back when you have a basic idea which you don't know how to implement, or need suggestions on improving it. You don't learn by being told the answer!
- 03-03-2011, 11:49 PM #10
Trivial example:
Imagine if someMethodReturnsBoolean was false and the someMethodReturnsString method was never called and s never got assigned a value.Java Code:String s; if (someMethodReturnsBoolean) { s = someMethodReturnsString(); } for(int index = 0; index < s.length(); index++) { System.out.println("hello"); }
Using your preferred option of initialising s to empty String then the for loop fails to run and the program happily continues on its way. Then you sit around scratching your head wondering why.
Using the option of not initialising the String then a NPE will be thrown and it will be quicker and easier to find where the problem lies.
- 03-04-2011, 12:08 AM #11
Sorry for hijacking the thread, antface - this will be my last post re string initialisation
I agree that in this case, a null pointer would be easier to solve. Obviously, you're example is much simplified, but in my methodology of programming, I would put the for loop inside the if statement.
In my (admittedly limited - 5 years or so) experience there are more problems solved by setting strings to be empty than leaving them null. A simple, if contrived example would be string appending.
would print out "nullhello" - which i think you agree would not be the desired result.Java Code:string s; s += "hello"; System.out.println (s);
Perhaps this isn't the "correct" way of programming but i find it easier and quicker
- 03-04-2011, 12:38 AM #12
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
:/ i really dont know where to start...
I did the instance variables, constructor, set/get methods, and now i'm stuck at processTemperatureData ()
i know i do some type of repetition
do i also have to do something that calculates the average?
- 03-04-2011, 12:44 AM #13
Well going by what you put in the spec, heres some pseudo codeThe 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 think that covers everything in the spec.Java Code:ask users how many values there are int sum = 0 for i < numValues { read in value sum = sum + value } average = sum / numValues store average
Your turn now - make some java code!
- 03-04-2011, 12:55 AM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
When you go to program something, try and explain to me the process you take. You should generally try your best to split the problem up into small steps. Even pseudo code on a lot of this gives a lot away, try your best to sit back and think, write it out with pen and paper if you need, re read the question as well if necessary. Finally, learning yourself will definitely make future problems much easier.
- 03-04-2011, 01:17 AM #15
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
:] so when i worked on my code I just added whats in blue text. do I need the scanner input = new scanner( system.in ); ? it is giving me an error
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 );
}
}
- 03-04-2011, 01:53 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
That looks mostly right, did you compile it and run it? There are some errors, the loop goes while tempcounter is <= 10, however; you set tempcounter to 80, so your loop will never pass the conditional.
Also, do you know how to use a for loop? It would be great in this situation.
You seem to have the general idea in your mind but you should test it yourself. The compiler will check for syntax problems and running it will allow you to monitor results. Also, wasn't this method supposed to ask the user how many temperatures to use?
- 03-04-2011, 02:00 AM #17
Member
- Join Date
- Mar 2011
- Posts
- 5
- Rep Power
- 0
hmmm, i have no idea what i am doing. and i'm using eclipse and it has a little red x next to where i have written Scanner... blah blah blah so that part doesnt work. :'(
and i dont know how to test it.
This is my first java project ever.
- 03-04-2011, 02:07 AM #18
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Intro
By hayden06f4i in forum IntroductionsReplies: 1Last Post: 10-20-2010, 09:47 PM -
intro to java problem for school
By thr33 in forum New To JavaReplies: 1Last Post: 10-16-2009, 01:19 AM -
Intro!
By lakhmir.singh in forum IntroductionsReplies: 3Last Post: 10-30-2007, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks