Results 1 to 18 of 18
  1. #1
    antface is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default 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!

  2. #2
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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);
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by berkeleybross View Post
    You shouldnt set strings to be null, they should usually be set to be the empty string by default.
    Says who?

    By the way your default constructor can look like this:
    Java Code:
    //Constructor no arg
    public WeatherStation () { }
    Instance variables are assigned their default values, ie null, 0 and 0.0.

  5. #5
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    Quote Originally Posted by Junky View Post
    Says who?
    My personal opinion. It prevents so many null pointer exceptions, or strings starting with the text "null".
    Its not a rule as such, but i think its just good programming practice.

  6. #6
    antface is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    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;
    }
    }

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    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.

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  9. #9
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    @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!

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by berkeleybross View Post
    @junky - what kind of errors would it hide?
    Trivial example:
    Java Code:
    String s;
    if (someMethodReturnsBoolean) {
        s = someMethodReturnsString();
    }
    for(int index = 0; index < s.length(); index++) {
        System.out.println("hello");
    }
    Imagine if someMethodReturnsBoolean was false and the someMethodReturnsString method was never called and s never got assigned a value.

    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.

  11. #11
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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.
    Java Code:
    string s;
    s += "hello";
    System.out.println (s);
    would print out "nullhello" - which i think you agree would not be the desired result.

    Perhaps this isn't the "correct" way of programming but i find it easier and quicker

  12. #12
    antface is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    :/ 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?

  13. #13
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    5

    Default

    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????
    Well going by what you put in the spec, heres some pseudo code

    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
    I think that covers everything in the spec.
    Your turn now - make some java code!

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  15. #15
    antface is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by berkeleybross View Post
    Well going by what you put in the spec, heres some pseudo code

    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
    I think that covers everything in the spec.
    Your turn now - make some java code!
    :] 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 );

    }
    }

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  17. #17
    antface is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you imported java.util.*?

    The scanner is part of the util package so you must import it in order to use it.

    For loops look as follows
    Java Code:
    for(int i = 0; i < 10; i++){
        //do stuff
    }

Similar Threads

  1. Intro
    By hayden06f4i in forum Introductions
    Replies: 1
    Last Post: 10-20-2010, 09:47 PM
  2. intro to java problem for school
    By thr33 in forum New To Java
    Replies: 1
    Last Post: 10-16-2009, 01:19 AM
  3. Intro!
    By lakhmir.singh in forum Introductions
    Replies: 3
    Last Post: 10-30-2007, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •