I'm trying to correctly create a class that correctly implements the energy usage of an appliance. This includes the amount of water, gas or electricity it uses along with if the device is switched on or off and finally the amount of time that the device is being used for. When all these factors are correctly implementation, this information will then get passed onto various meters that will calculate the usage of all appliances associated with it.
For now, I have not programmed anything to be outputted, I am still focusing on structuring each appliance so that it can correctly register this information so that it can later be passed onto their respective meters.
The types of information which should be registered are these:
useTime() - Registers the amount of time that the appliance is being used for.
currentState() - If the appliance is on or off.
Constructor that stores the values of electricity, water and gas use along with the amount of time that the device has been on for.
Individual appliance actions (such as cook() or shower()).
This is what I have come up with so far for one of my appliances:
GasCooker appliance
public class GasCooker extends Cooker
{
public int isOn = -1;
public int isOff = 0;
public int totalTime;
public int incrementTime;
#Override
public int currentState(int x)
{
x = -1;
if (x == 0)
return isOff;
else
return isOn;
//returns isOn;
}
#Override
public void useTime(int defaultTime)
{
defaultTime = 15;
incrementTime = 4;
}
public void cook()
{
//add code for coker to cook;
}
GasCooker(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
totalTime = 15 * incrementTime;
electricityUse = 0 * incrementTime;
gasUse = 4 * incrementTime;
waterUse = 0 * incrementTime;
timeOn = 60 * incrementTime;
}
}
This appliance is a sub class of a Cooker class which looks like this:
Cooker
public abstract class Cooker extends Appliance
{
public int isOn = -1;
public int isOff = 0;
#Override
public int currentState(int x)
{
x = -1;
if (x == 0)
return isOff;
else
return isOn;
//returns isOn;
}
public abstract void cook();
Cooker(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
}
}
Which is extended from Appliance:
Appliance
abstract public class Appliance
{
public abstract void useTime(int defaultTime);
public int currentState(int x)
{
int timeOn = -1;
int timeOff = 0;
x = 0;
if (x == 0)
return timeOff;
else
return timeOn;
}
Appliance(int electricityUse,int gasUse,int waterUse,int timeOn)
{
electricityUse = 0;
gasUse = 0;
waterUse = 0;
timeOn = 0;
}
}
The main queries that I have are these:
Do these classes look like sensible classes for registering this information?
If not, in which part have I misconceived?
This is my first assignment in which I have to define my own methods so any feedback on the sensibility of my program so far or how it could be improved would be much appreciated, thanks.
Related
this is the qa:
Define a class called MoreSpeed which extends the following class, and which provides a new method called incSpeed() which adds 1 to the inherited variable length.
this is my answer:
public class Speed {
private int length = 0;
public int getSpeed () { return length; }
public void setSpeed (int i) {
if (i > 0) {
length = i;
}
}
}
public class MoreSpeed extends Speed {
private int length;
public int incSpeed() {
return length+1;
}}
its says that the syntax is good but the class operation is wrong.
please help me,thanks.
No. You are shadowing the length from Speed. Instead, implement incSpeed with getSpeed() like
public int incSpeed() {
return getSpeed() + 1;
}
If you are supposed to modify it as well then use setSpeed(int) to do so
public int incSpeed() {
int s = getSpeed() + 1;
setSpeed(s);
return s;
}
I am trying to create a method for " winning percentage " in a player class. I know I need to incorporate total wins divided by total games played, but the code is meant to be simple so I cannot use complex code. (beginner project in computer science) Any useful feedback would be great as I have spent multiple days attempting this and getting no where. By the way, ties count as half a win.
Update: Implemented the getters into the getWinningPercentage method. Also calculated everything inside the getWinningPercentage and removed the setWinningPercentage considering it was useless code. Results were as follows:
Bob
5 wins, 1 losses, 2 ties
Winning percentage = 0.75
public class Player
{
private int numWins = 0;
private int numTies = 0;
private int numLosses = 0;
private String name;
public void setWins(int w)
{
numWins = w;
}
public int getWins()
{
return numWins;
}
public void setTies(int t)
{
numTies = t;
}
public int getTies()
{
return numTies;
}
public void setLosses(int L)
{
numLosses = L;
}
public int getLosses()
{
return numLosses;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void incrementWins()
}
numWins++;
}
public void incrementTies()
{
numTies++;
}
public void incrementLosses()
{
numLosses++;
}
public double getWinningPercentage()
{
double totalGames = getWins() + getTies() + getLosses();
double totalWins = getWins() + (getTies() / 2.0);
double winningPercentage = (totalWins / totalGames);
return winningPercentage;
}
}
The winning percentage should be a calculated property, not a field, and not have a setter method. Instead there should only be a "getter" (public double getWinningPercentage()) method and you should calculate and return this value from within the method itself from the other fields that your class already has.
We should leave it up to you to create this method and formula yourself.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I'm working on an assignment that is trying to output the energy consumption of appliances within a house. I have created an Appliance called ElectricCooker and an Appliance called ElectricShower. They both have exactly the same code apart from different variable names yet some how produce different outputs.
Here is the relevant code: (Sorry about the amount of code, this reproduces the program)
ElectricCooker
public class ElectricCooker extends Appliance
{
public int isOn = -1;
public int isOff = 0;
public int incrementTime;
public int varPass = -1;
#Override
public int currentState()
{
if (varPass == 0)
return isOff;
else
{
return isOn;
}
//returns isOn;
}
#Override
public void useTime(int defaultTime)
{
defaultTime = 15;
incrementTime = 4;
}
public void cook()
{
//add code
}
#Override
public void timePasses()
{
if(varPass == isOff)
varPass = 0;
else
{
ElectricMeter.getInstance().incrementConsumed(electricityUse);
ElectricMeter.getInstance().incrementConsumed(5);
int getCookerConsumed = ElectricMeter.getInstance().getElectricityUsed();
System.out.println("Electric cooker electricity consumption = " + getCookerConsumed);
}
}
ElectricCooker(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
this.electricityUse = 5 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 0 * incrementTime;
this.timeOn = 15 * incrementTime;
}
}
ElectricShower
public class ElectricShower extends Appliance
{
public int isOn = -1;
public int isOff = 0;
public int incrementTime;
public int varPass = -1;
#Override
public int currentState()
{
if (varPass == 0)
return isOff;
else
{
return isOn;
}
//returns isOn;
}
#Override
public void useTime(int defaultTime)
{
defaultTime = 15;
incrementTime = 4;
}
#Override
public void timePasses()
{
if(varPass == isOff)
varPass = 0;
else
{
ElectricMeter.getInstance().incrementConsumed(electricityUse);
ElectricMeter.getInstance().incrementConsumed(5);
int getShowerConsumed = ElectricMeter.getInstance().getElectricityUsed();
System.out.println("Electric shower electricity consumption = " + getShowerConsumed);
}
}
ElectricShower(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
this.electricityUse = 5 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 0 * incrementTime;
this.timeOn = 15 * incrementTime;
}
}
Appliance
abstract public class Appliance
{
public int varPass;
public int isOn;
public int isOff;
public int electricityUse, gasUse, waterUse, timeOn;
public abstract void useTime(int defaultTime);
public int currentState()
{
if (varPass == 0)
return isOff;
else
{
return isOn;
}
//returns isOn;
}
public abstract void timePasses();
Appliance(int electricityUse,int gasUse,int waterUse,int timeOn)
{
electricityUse = 0;
gasUse = 0;
waterUse = 0;
timeOn = 0;
}
}
ElectricMeter
public class ElectricMeter
{
ElectricMeter() {}
private static ElectricMeter instance = new ElectricMeter();
public static ElectricMeter getInstance() { return instance; }
private int electricityUsed = 0;
public void incrementConsumed(int value)
{
electricityUsed += value;
}
public int getElectricityUsed()
{
return electricityUsed;
}
}
House
import java.util.ArrayList;
public class House
{
ArrayList<Appliance> applianceList = new ArrayList<>();
ElectricShower calleShower = new ElectricShower(1, 1, 1, 1);
ElectricCooker calleCooker = new ElectricCooker(1, 1, 1, 1);
public void addAppliance()
{
applianceList.add(calleShower);
applianceList.add(calleCooker);
}
public void timePasses()
{
calleShower.timePasses();
calleCooker.timePasses();
//this method is called as part of the simulation to trigger a new fifteen minute period
//in the house. When it is called, it will in turn call timePasses() on all the Appliances in the House.
}
}
public class CourseworkTest {
public static void main(String[] args)
{
House callHouse = new House();
callHouse.timePasses();
}
}
Output
Electric shower electricity consumption = 5
Electric cooker electricity consumption = 10
I've been working on this for hours and I just don't understand how the exact same code can somehow produce different results? I don't see how one can be 10 and other 5 when they do the exact same thing and go through the same process. Any help is very much appreciated, thanks.
The ElectricMeter class is a singleton, meaning that one (same) instance will exist throughout the execution of your application.
First, from ElectricShower.timePasses(), you make the following call:
ElectricMeter.getInstance().incrementConsumed(5);
Then, you again make this call from ElectricCooker.timePasses(). Hence, when you output the consumption the second time, it is being reported as 10.
Using a singleton to represent the common shared electric meter for a house seems like a reasonable design decision.
I'm writing a program that is based around registering the amount of energy consumption that is being used by appliances within a house. So far, I have created various meter classes such as WaterMeter, GasMeter etc. with empty methods that need to be filed with values, I have also created classes for appliances that have methods that will be used to register the consumption of energy within each appliance. What I am working on now is applying the energy values that are stored within a constructor, putting those values into a timePasses() method that will then return those values to their specific meter's methods so that they can be registered. This is what I have so far:
Appliance class example:
public class ElectricShower extends Shower
{
public int isOn = -1;
public int isOff = 0;
public int incrementTime;
public int x = -1;
private static ElectricMeter instance = new ElectricMeter();
public static ElectricMeter getInstance() { return instance; }
#Override
public int currentState()
{
if (x == 0)
return isOff;
else
{
return isOn;
}
//returns isOn;
}
#Override
public void useTime(int defaultTime)
{
defaultTime = 15;
incrementTime = 1;
}
public void shower()
{
//call timePasses() method
}
#Override
public int timePasses()
{
if(x == isOff)
return 0;
else
{
ElectricMeter.getInstance().incrementConsumed(electricityUse);
}
}
ElectricShower(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
this.electricityUse = 12 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 4 * incrementTime;
this.timeOn = 15 * incrementTime;
}
}
Meter example:
public class ElectricMeter
{
public int incrementConsumed(int value)
{
}
public int incrementGenerated()
{
}
public boolean canGenerate()
{
}
public String getConsumed()
{
}
public String getGenerated()
{
}
}
What I need to do next is:
take the values of electricityUse and waterUse and store them within the timePasses() else staement
Within the timePasses() else statement, place the value of electrcityUse in the incrementGenerated() method within the ElectricMeter class and do the same for the waterUse variable.
UPDATE
Classes have been updated, still struggling to find out how to make it work.
First of all, I assume you have an Appliance class that all the appliances extends from. You should create variables in the Appliance class that stores electricity, gas and water usage:
public class Appliance
{
public int electricityUse, gasUse, waterUse, timeOn;
// ...
}
Note that you should always use getters and setters instead of public fields. I'm just lazy :D
Change your constructor so that the variables above get set:
ElectricShower(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
// I don't know why you multiply the constant by incrementTime here. Seems weird. I think you can remove them.
this.electricityUse = 12 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 4 * incrementTime;
this.timeOn = 15 * incrementTime;
}
One way to write the else clause is to use the "Singleton Pattern".
In every meter class, write something like this:
private ElectricMeter() {}
private static ElectricMeter instance = new ElectricMeter();
public static ElectricMeter getInstance() { return instance; }
In the incrementConsumed method, you should accept a parameter that indicates how much to increment:
public int incrementConsumed(int value)
{
// logic here...
}
In the else clause, you can just do:
ElectricMeter.getInstance().incrementConsumed(electricityUse);
GasMeter.getInstance().incrementConsumed(gasUse);
WaterMeter.getInstance().incrementConsumed(waterUse);
You should review your design.
If you need to access to a class parameter you could just define it public or better create a so called getter method that returns the value.
Example:
public class MyData {
public int counter;
}
....
// Some other class
MyData data = new MyData();
data.counter = 5;
System.out.println(data.counter);
Or
public class MyData {
private int counter;
public void setCounter(int counter) {
this.counter = counter;
}
public int getCounter() {
return this.counter;
}
}
....
// Some other class
MyData data = new MyData();
data.setCounter(5);
System.out.println(data.getCounter());
In your code I see:
public int incrementConsumed()
{
//Store value of electricityUse.
}
But this method should just return an integer and have not parameter to get an input to store.
It should be:
public void incrementConsumed(int amount) {
this.amount += amount;
}
I'm concerned about this line:
gasUse = 0 * incrementTime;
If you multiply something to 0 it will be always 0...
I have a class named LotteryTicket that have 3 subclasses: Pick4, Pick5, and Pick6. I want to be able to call a method public void pickNumbers()where once called, will be able to recognize which LotteryTicket subclass is being used and ask for the appropriate amount of arguments (i.e. calling pickNumbers() in an instance of Pick5 will ask for 5 ints).
I've attempted to get around this by providing public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick) for 4, 5, and 6 in the LotteryTicket class, and having the pickNumbers() method call the appropriate method (which will get overridden) based on a field pickAmount. Unfortunately, this would entail having to provide arguments.
Here is the LotteryTicket class:
public class LotteryTicket
{
protected int pickAmount;
protected boolean isRandom;
protected ArrayList<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
public LotteryTicket(int pickAmount, boolean isRandom)
{
// INITIALIZATION OF VARIABLES
this.pickAmount = pickAmount;
this.isRandom = isRandom;
// CONSTRUCTION OF ARRAYLIST
numbersPicked = new ArrayList(pickAmount);
}
/**
* The number pick method for ALL subclasses. Running this method will run the appropriate pickxNumbers
* method, where x is the pickAmount.
*
*/
public void pickNumbers()
{
if(pickAmount == 4){
pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
}
if(pickAmount == 5){
pick5Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick)
}
if(pickAmount == 6){
pick6Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick, int fifthPick, int sixthPick)
}
}
/**
* The number pick method for the Pick4 subclass.
*
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
}
Pick4 class:
public class Pick4 extends LotteryTicket
{
/**
* Constructor for objects of class Pick4
*/
public Pick4(boolean isRandom)
{
super(4, isRandom);
}
/**
* Overloaded pick4Numbers() method. Depending on the ticket type, the amount of picks will vary.
* For example, Pick4 tickets will only ask for 4 int values, Pick5 tickets will ask for 5, etc.
*
*#param int firstPick
*#param int secondPick
*#param int thirdPick
*#param int fourthPick
*/
public void pick4Numbers(int firstPick, int secondPick, int thirdPick, int fourthPick)
{
numbersPicked.add(new Integer(firstPick));
numbersPicked.add(new Integer(secondPick));
numbersPicked.add(new Integer(thirdPick));
numbersPicked.add(new Integer(fourthPick));
}
In my opinion it would be better to do like this:
public class LotteryTicket {
protected int pickAmount;
protected boolean isRandom;
protected List<Integer> numbersPicked;
protected Date datePurchased;
protected SimpleDateFormat sdf;
protected int[] numbersToPick;
//To create random valued ticket
public LotteryTicket(int pickAmount) {
this.pickAmount = pickAmount;
isRandom = true;
}
//To create specified valued ticket
public LotteryTicket(int... numbersToPick) {
pickAmount = numbersToPick.length;
isRandom = false;
this.numbersToPick = numbersToPick;
}
public void pickNumbers() {
numbersPicked = new ArrayList<>(pickAmount);
if (isRandom) {
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(random.nextInt());
}
} else {
for (int i = 0; i < pickAmount; i++) {
numbersPicked.add(numbersToPick[i]);
}
}
}
}
And Pick4, Pick5 ... etc will be like this:
public class Pick4 extends LotteryTicket {
//For random valued ticket
public Pick4() {
super(4);
}
//For specified valued ticket
public Pick4(int pick1, int pick2, int pick3, int pick4) {
super(pick1, pick2, pick3, pick4);
}
}
If you want to extend from LotteryTicket, make pickNumbers() method abstract and accepting List or varargs:
public abstract class LotteryTicket {
//...
abstract public void pickNumbers(int... numbers);
//...
}
then in implementation classes, e. g. Pick4:
public class Pick4 extends LotteryTicket {
//...
#Override
public void pickNumbers(int... numbers) {
if (numbers.length != 4)
throw IllegalArgumentException("For Pick4, there must be exactly 4 numbers!");
for (int n : numbers) {
numbersPicked.add(n); // no need in explicit boxing, Java will do it for you
}
}
}