Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem with Thermostat class public class Thermostat{ public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85)

Similar presentations


Presentation on theme: "Problem with Thermostat class public class Thermostat{ public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85)"— Presentation transcript:

1 Problem with Thermostat class public class Thermostat{ public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; } private double lowerBound; public byte checkTemperature(){ if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; return action; } private double upperBound; private double curTemprtr; // act has : 0 – current mode; 1 – heater, -1 – A/C public void changeTemperature(double amount, double act){ if (act!=0) action=act; curTemprtr+=action*amount; } public void changeTemperature(double act){ if (act!=0) action=act; curTemprtr+=action;} public Thermostat(double lBound,double uBound){ lowerBound=lBound;upperBound=uBound; curTemprtr=(lBound+uBound)/2; action=0; } private byte action; //0–OFF;1-heater;-1– A/C public Thermostat(){ lowerBound=65; upperBound=85; action=0;curTemprtr=75; } public String toString(){ return (curTemprtr+”: ”+action); }} Instance Variables CONSTRUCTORSCONSTRUCTORS Instance Methods

2 Creating Objects 1.Declaration of variable include type of variable, name of variable and semicolon. 2.Once a class has been declared, the instance of the class can be created by using keyword new. 3. Constructors are used to initialize instance variables of new object. public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; } public Thermostat(){ lowerBound=65; upperBound=85; action=0;curTemprtr=75; } public Thermostat(double lBound,double uBound){ lowerBound=lBound;upperBound=uBound; curTemprtr=(lBound+uBound)/2; action=0; } Thermostat thrmVar; thrmVar = newThermostat(45); Declare Thermostat variable thrmVar, initialize its object with lowerBound =65, upperBound=85, curTemprtr = 45: Thermostat thrmVar = new Thermostat(45);

3 Creating Objects(cont) 1.Declaration of variable include type of variable, name of variable and semicolon. 2.Once a class has been declared, the instance of the class can be created by using keyword new. 3. Constructors are used to initialize instance variables of new object. public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; } public Thermostat(){ lowerBound=65; upperBound=85; action=0;curTemprtr=75; } public Thermostat(double lBound,double uBound){ lowerBound=lBound;upperBound=uBound; curTemprtr=(lBound+uBound)/2; action=0; } Thermostat thrmVar1; thrmVar1 = newThermostat(65,80); Declare Thermostat variable thrmVar1, initialize its object with lowerBound=65, upperBound=80: Thermostat thrmVar1 = new Thermostat(65,80);

4 What we have in thrmVar? public class Thermostat{ … private double lowerBound, upperBound; private double curTemprtr, action; public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; } } Thermostat thrmVar = new Thermostat(45);

5 Working with Object public class Thermostat{ … private double lowerBound, upperBound; private double curTemprtr, action; public void changeTemperature(double amount, double act){ if (act!=0) action=act; curTemprtr+=action*amount; } public void changeTemperature(double act) { if (act!=0) action=act; curTemprtr+=action; } public byte checkTemperature(){ if(curTemprtr>=65&&curTemprtr<=85) action=0; if (curTemprtr<65) action=1; if (curTemprtr>85) action=-1; return action; } } Write a statements that increase temperature in the object of thrmVar variable by 20 units, check the temperature in the object and print out the result of checking to the user. thrmVar.changeTemperature(20,1); or thrmVar.changeTemperature(20); byte res=thrmVar.checkTemperature(); System.out.println(res); System.out.print(thrmVar.checkTemperature());

6 What we have in thrmVar and thrmVar1 after creation and initialization? Thermostat thrmVar = new Thermostat(45); Thermostat thrmVar1 = new Thermostat(65,80); public Thermostat(double lBound,double uBound){ lowerBound=lBound;upperBound=uBound; curTemprtr=(lBound+uBound)/2; action=0; }

7 Working with Objects thrmVar1=thrmVar; thrmVar1.changeTemperature(1); public void changeTemperature(double act){ if (act!=0) action=act; curTemprtr+=action;} 46.0; thrmVar.changeTemperature(20,0); public void changeTemperature(double amount, double act){ if (act!=0) action=act; curTemprtr+=action*amount; } 66.0; System.out.println(thrmVar1); public String toString(){ return (curTemprtr+”: ”+action);} System.out.println(“66.0:1.0”);


Download ppt "Problem with Thermostat class public class Thermostat{ public Thermostat(double tempOutside){ lowerBound=65; upperBound=85; curTemprtr=tempOutside; if(curTemprtr>=65&&curTemprtr<=85)"

Similar presentations


Ads by Google