Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Homework Assignment help..

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Homework Assignment help..

    I have a programming assignment where I'm basically supposed to have a Thermostat class and a main. With the Thermostat, you set the desired temperature and heating/cooling and once it gets to that temp it shuts off. It also contains a cooling and heating threshold so that if it gets to either one, the mode switches from heating to cooling or vice versa. It also contains some time-related info in which the degree should show the temperature changing by 1 every minute. The main is just used as a test to visually display the thermostat and to show the professor that it powers off, switches mode, etc. I think I have most of this, but I'm still having a few problems and can't figure it out. I will insert my code below in case anyone has any suggestions.

    import java.util.*;
     
    public class Thermostat {
     
    	public double currentTemperature;
    	public double desiredTemperature;
    	public boolean heating;
    	public double heatingThreshold;
    	public double coolingThreshold;
    	public boolean power;
     
    	Calendar cal = GregorianCalendar.getInstance();
     
    	public double getCurrentTemperature() {
    		return currentTemperature;
    	}
     
    	public void setCurrentTemperature(double currentTemperature){
    		this.currentTemperature = currentTemperature;
     
    		if (currentTemperature >= desiredTemperature && heating == true);
    				power = false;
     
    		if (currentTemperature <= desiredTemperature && heating == false);
    				power = true;
     
    		if (currentTemperature >= heatingThreshold);
    			heating = false;
     
    		if (currentTemperature <= coolingThreshold);
    			heating = true;
     
     
    	}
    	public double getDesiredTemperature() {
    		return desiredTemperature;
    	}
    	public void setDesiredTemperature(double desiredTemperature) {
    		this.desiredTemperature = desiredTemperature;
    	}
    	public boolean isHeating() {
    		return heating;
    	}
    	public void setHeating(boolean heating) {
    		this.heating = heating;
    	}
    	public double getHeatingThreshold() {
    		return heatingThreshold;
    	}
    	public void setHeatingThreshold(double heatingThreshold) {
    		this.heatingThreshold = heatingThreshold;
    	}
    	public double getCoolingThreshold() {
    		return coolingThreshold;
    	}
    	public void setCoolingThreshold(double coolingThreshold) {
    		this.coolingThreshold = coolingThreshold;
    	}
    	public boolean isPower() {
    		return power;
    	}
    	public void setPower(boolean power) {
    		this.power = power;
    	}
     
    	public void advanceOneMinute(){
    		cal.add(Calendar.MINUTE, 1);
     
    		if (heating = true);
    			currentTemperature =+ 1;
    		if (heating = false);
    			currentTemperature =- 1;
    	}
     
    	public void advanceOneHour(){
    		cal.add(Calendar.HOUR, 1);
     
    		if (heating = true);
    		currentTemperature =+ 60;
    	if (heating = false);
    		currentTemperature =- 60;
    	}
     
    	public String currentStatus(){
    		String output = "";
     
    		if (power == true);
    			output += "\tPower: On" + "\n";
    		if (power == false);
    			output += "\tPower: Off" + "\n";
     
    		if (heating == true);
    			output += "\tMode: Heating" + "\n";
    		if (heating == false);
    			output += "\tMode: Cooling" + "\n";
     
    		output += "\tDesired Temperature: " + desiredTemperature + "\n";
    		output += "\n\tCurrent Temperature: " + currentTemperature + "\n";
     
    		return output;
     
    	}
    }

    import java.util.*;
     
    public class Assignment {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Thermostat house = new Thermostat();
    		house.power = true;
    		house.heating = true;
    		house.currentTemperature = 65.0;
    		house.desiredTemperature = 67.0;
    		house.heatingThreshold = 70.0;
    		house.coolingThreshold = 60.0;
    	}	
     
    	System.out.println(house.currentStatus());
     
    	System.out.println("5 minutes later");
     
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
     
    	System.out.println(house.currentStatus());
     
    	System.out.println("Turning Thermostat Power Off...");
    	System.out.println(house.currentStatus());
     
    	System.out.println("2 minutes later...");
    	house.advanceMinute();
    	house.advanceMinute();
    	System.out.println(house.currentStatus());
     
    	System.out.println("One hour later...");
    	System.out.println(house.currentStatus());
     
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    I'm still having a few problems and can't figure it out.
    Please explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    My fault on that, I should have specified. A couple of problems to start with...

    A) I use Eclipse, and this is the error I get when running the main:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at Assignment.main(Assignment.java:5)

    B) He gave us the assignment and had the purpose of each class listed and under the advanceOneMinute, he put "Adjust the current temperature using the setCurrentTemperature method (assume one degree a minute)" Not quite sure how to make it implement.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    For A) eclipse should show what the problem is when the cursor is moved over the line with the error.

    B) Adjust must mean to change its value up or down.
    Have you tried calling the setCurrentTemperature method with different values to see what happens?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    For A) Nope that's the thing. It doesn't show me an error in eclipse. Just in the console at the bottom when I try to run it, and it gives the error I posted above. Eclipse is showing nothing wrong with it even if I hold cursor over the line.

    For B) No I haven't tried calling it, because I can't get main to work. I will post the full assignment and see if that clarifies anything. This is first time we've used the date/time stuff, so having the temperature increment or decrement by the time is throwing me off.

    Instructions:

    For this assignment, you are to create a Thermostat class. The purpose of this class is to model a household thermostat. It will be a "smart" thermostat that has the ability to switch between heating and cooling.
    Your Thermostat class should have the following data fields:
    current temperature (double)
    desired temperature (double)
    heating/cooling (boolean) (NOTE: it doesn't matter what you name this, just let it be an indicator of whether the thermostat is in heating or cooling mode)
    heating threshold (double)
    cooling threshold (double)
    time (Java Date class)
    on/off (boolean) (NOTE: again, name doesn't matter)
    Have setters/getters (except for time & note the special functions for the set current temperature) for all data fields as well as the following methods:
    setCurrentTemperature
    input: temp (double)
    output: void
    Besides just being a setter method, this method should also do the following:
    Check to see if the heating/cooling threshold has been reached, if so change the mode of the thermostat if necessary
    Check to see if the current temp is equal to or above (heating) or equal to or below (cooling) and turn the thermostat off/on if necessary
    advanceOneMinute()
    input: none
    output: void
    This method should advance the time by one minute (see Java Date class reference here: Date (Java Platform SE 6)). Once advanced, this method should do the following:
    Adjust the current temperature using the setCurrentTemperature method (assume one degree a minute)
    advanceOneHour()
    same as the above method, but adjust the time an entire hour
    currentStatus()
    input: none
    output: string
    This method returns a string with the current status of the thermostat. Include the following:
    Whether it is on/off
    Whether it is cooling/heating
    The desired temp
    The current temp
    In your main, you should create a themostat and thoroughly test it. Be sure to showcase time passing and the temperature reaching the desired setting. Also, showcase switching modes from heating to cooling.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    It doesn't show me an error in eclipse.
    That's strange. The error message says: Unresolved compilation problem:
    I'd interpret that as saying there is a syntax error in the code that gives an error when it is compiled.
    Try compiling the code with the javac compiler to get a better error message.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    Yeah it gave several errors starting with line 17. It says "<identifier> expected" after the println. There's a few of those errors for that line pretty much same error just different spot. It's doing that same error for any of the currentStatus printouts. Not sure what I'm doing wrong...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    "<identifier> expected"
    That message is often given when the compiler is lost because of something wrong on the lines immediately before the line with the error. Take a close look at the lines of code just before the error.

    What code is at lines 15 through 18?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Homework Assignment help..

    There are statements outside any method (main()?) in the Assignment class. For some reason, Eclipse doesn't warn you of that in advance with a precise explanation, and the compiler just knows there's an unresolved compilation problem.

  10. #10
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    I went back and moved the } to the end so that everything was in main, but the new error is :

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token ")", delete this token
    Syntax error, insert "}" to complete ClassBody

    at Assignment.main(Assignment.java:40)

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    What is on line 40?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Feb 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    In the main file
    house.advanceMinute(); should be house.advanceOneMinute();

    --- Update ---

    after solving it your code is working properly

  13. #13
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    Yeah it is working, but not properly. For some reason, it keeps giving me the same numbers despite the testing I do. For example, Here is the data that I start with (I changed the values around a bit but that was it):
    house.power = true;
    house.heating = true;
    house.currentTemperature = 60.0;
    house.desiredTemperature = 75.0;
    house.heatingThreshold = 90.0;
    house.coolingThreshold = 40.0;

    Even after advancing minutes and advancing the hours, everything is staying the same when I print it the 2nd time, 3rd time, etc. The values aren't changing even when powering off. Here is my full code once again just to be safe:

    public class Assignment {
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    		Thermostat house = new Thermostat();
    		house.power = true;
    		house.heating = true;
    		house.currentTemperature = 60.0;
    		house.desiredTemperature = 75.0;
    		house.heatingThreshold = 90.0;
    		house.coolingThreshold = 40.0;
     
    	System.out.println(house.currentStatus());
     
    	System.out.println("5 minutes later");
     
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	house.advanceOneMinute();
     
    	System.out.println(house.currentStatus());
     
    	System.out.println("Turning Thermostat Power Off...");
    	house.power = false;
    	System.out.println(house.currentStatus());
     
    	System.out.println("2 minutes later...");
    	house.advanceOneMinute();
    	house.advanceOneMinute();
    	System.out.println(house.currentStatus());
     
    	System.out.println("One hour later...");
    	System.out.println(house.currentStatus());
     
     
    	}
    }
    import java.util.*;
     
    public class Thermostat {
     
    	public double currentTemperature;
    	public double desiredTemperature;
    	public boolean heating;
    	public double heatingThreshold;
    	public double coolingThreshold;
    	public boolean power;
     
    	Calendar cal = GregorianCalendar.getInstance();
     
    	public double getCurrentTemperature() {
    		return currentTemperature;
    	}
     
    	public void setCurrentTemperature(double currentTemperature){
    		this.currentTemperature = currentTemperature;
     
    		if (currentTemperature >= desiredTemperature && heating == true);
    				power = false;
     
    		if (currentTemperature <= desiredTemperature && heating == false);
    				power = true;
     
    		if (currentTemperature >= heatingThreshold);
    			heating = false;
     
    		if (currentTemperature <= coolingThreshold);
    			heating = true;
     
     
    	}
    	public double getDesiredTemperature() {
    		return desiredTemperature;
    	}
    	public void setDesiredTemperature(double desiredTemperature) {
    		this.desiredTemperature = desiredTemperature;
    	}
    	public boolean isHeating() {
    		return heating;
    	}
    	public void setHeating(boolean heating) {
    		this.heating = heating;
    	}
    	public double getHeatingThreshold() {
    		return heatingThreshold;
    	}
    	public void setHeatingThreshold(double heatingThreshold) {
    		this.heatingThreshold = heatingThreshold;
    	}
    	public double getCoolingThreshold() {
    		return coolingThreshold;
    	}
    	public void setCoolingThreshold(double coolingThreshold) {
    		this.coolingThreshold = coolingThreshold;
    	}
    	public boolean isPower() {
    		return power;
    	}
    	public void setPower(boolean power) {
    		this.power = power;
    	}
     
    	public void advanceOneMinute(){
    		cal.add(Calendar.MINUTE, 1);
     
    		if (heating = true);
    			setCurrentTemperature(currentTemperature +1);
    		if (heating = false);
    		setCurrentTemperature(currentTemperature -1);
    	}
     
    	public void advanceOneHour(){
    		cal.add(Calendar.HOUR, 1);
     
    		if (heating = true);
    		setCurrentTemperature(currentTemperature +60);
    	if (heating = false);
    		setCurrentTemperature(currentTemperature -60);
    	}
     
    	public String currentStatus(){
    		String output = "";
     
    		output += "\tPower: " + power;
    		output += "\n\tHeating: " + heating;
    		output += "\n\tDesired Temperature: " + desiredTemperature;
    		output += "\n\tCurrent Temperature: " + currentTemperature + "\n";
     
    		return output;
     
    	}
    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    Your code has problems that the compiler will warn you about if you
    use the javac -Xlint option. For example:
    D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint Thermostat.java

    You need to have Eclipse give you all the warnings and not ignore any so that you can see the problems in your code.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    And that's what I'm saying. It compiles and everything correctly. Zero errors. The problem now is logic, I'm guessing within the program. No matter what, the actual printed values are the same. They never change. I'll run it and paste from Eclipse (which is same from command prompt) what it shows:

    Power: true
    Heating: true
    Desired Temperature: 75.0
    Current Temperature: 60.0

    5 minutes later
    Power: true
    Heating: true
    Desired Temperature: 75.0
    Current Temperature: 60.0

    Turning Thermostat Power Off...
    Power: false
    Heating: true
    Desired Temperature: 75.0
    Current Temperature: 60.0

    2 minutes later...
    Power: true
    Heating: true
    Desired Temperature: 75.0
    Current Temperature: 60.0

    One hour later...
    Power: true
    Heating: true
    Desired Temperature: 75.0
    Current Temperature: 60.0

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Homework Assignment help..

    Turn off all of eclipse's ignore options and compile it again to get the messages.

    Or compile with the javac command with the -Xlint option as shown above.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Feb 2014
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Homework Assignment help..

    Your logic in the public void setCurrentTemperature(double currentTemperature) is disturbing one
    u just check the method by putting the values which are giving u will see none of the codition is getting satisfied

    --- Update ---

    can u plz post Your assignment question with full description

Similar Threads

  1. Help with my code for homework assignment, I don't know how to fix it!
    By maxman190 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 19th, 2013, 03:01 AM
  2. Homework - Calendar Assignment
    By Reegan777 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: April 26th, 2013, 06:27 AM
  3. Need help with understanding a homework assignment, not asking for answers
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 22
    Last Post: September 14th, 2012, 11:12 PM
  4. Homework assignment (beginner)
    By z3ohyr84 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 30th, 2011, 01:32 PM
  5. Array Homework assignment
    By lich0802 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 27th, 2011, 08:17 PM