-
April 13th, 2009, 06:39 PM
#1
Look Over Code Please!
This is a project for my college java class, Ive already shown the teacher and he even doesnt know what the problem is!
Code:
import java.util.*;
import java.text.*;
public class Project2
{
public static void main (String[] args)
{
//
//Variables
//
double dblTotalPrice = 0;
double dblTotalUsage = 0;
int intUserBulbs;
int intUserFans;
int intUserTVs;
int intEnergyEff;
int intFanUse;
int intTVUse;
double intBulbUse;
String strWhen = "o";
int rndWhen;
//
//Scanner/User Input
//
Scanner scan = new Scanner (System.in);
System.out.println("Kevin Saul\nCIS-129-01 Project #2");
System.out.println("\n");
System.out.println("Number of Light Bulbs:");
intUserBulbs = scan.nextInt();
System.out.println("Number of ceiling fans:");
intUserFans = scan.nextInt();
System.out.println("Number of TVs:");
intUserTVs = scan.nextInt();
System.out.println("Do you use Energy Efficent light Bulbs (or CFLs)?\n 1 for Yes or 2 for No");
intEnergyEff = scan.nextInt();
//
//Conversion/Mathwork
//
switch (intEnergyEff) {
case 1:
intBulbUse = ((intUserBulbs * 16) / 1000);
intFanUse = (intUserFans * 85); //85kwh average monthly per each fan
intTVUse = (intUserTVs * 40); //19"-32" = 30, 35"+ = 39.4++ kwh average monthly
dblTotalUsage = ((intBulbUse) + (intFanUse) + (intTVUse));
break;
case 2:
intBulbUse = ((intUserBulbs * 60) / 1000);
intFanUse = (intUserFans * 85); //85kwh average monthly per each fan
intTVUse = (intUserTVs * 40); //19"-32" = 30, 35"+ = 39.4++ kwh average monthly
dblTotalUsage = ((intBulbUse) + (intFanUse) + (intTVUse));
break;
default:
System.out.println("You either do or dont use CFLs.\n");
break;
}
//
//Random Generator, Randomly Desides when your using power
//
Random generator = new Random();
rndWhen = generator.nextInt(3) + 1; //random number between 1 & 3 exclusive
if (rndWhen == 1) {
dblTotalPrice = (dblTotalUsage * 0.10); //Intermediate Peak is 9.933 cents/kwh
strWhen = "Intermediate-Peak";
} if (rndWhen == 2) {
dblTotalPrice = (dblTotalUsage * 0.15); //On Peak is 15.298 cents/kwh
strWhen = "On-Peak";
} if (rndWhen == 3) {
dblTotalPrice = (dblTotalUsage * 0.09); //Off-Peak is 8.917cents/kwh
strWhen = "Off-Peak";
}
//
//Show/End
//
System.out.println ("\nThis is just an idea of how much power you use each month");
System.out.println ("If you used " + dblTotalUsage + " kilowatts each month at " + strWhen);
System.out.println ("Your Esimated Bill would be: $" + dblTotalPrice);
}
}
I don't get any compiler errors, just when i run the program, it doesnt run
intBulbUse = ((intUserBulbs * 16) / 1000);
it keeps it at 0, which it is originally declared at the beginning of the code. If I remove the 0, it says the variable hasn't been initiated yet..
Thanks in advance for the help! My teacher was even stumped with it.
-
April 13th, 2009, 06:55 PM
#2
Re: Look Over Code Please!
If you want to get a floating-point result from a calculation involving integral types (e.g. the calculation that gives zero), you need to cast one of the elements involved to a floating-point type, otherwise the result will be an integer (zero) that is assigned to your floating-point variable (an expression is evaluated according to the types in the expression, not the type it gets assigned to). A simple way to do this is either to give one of the numeric literals a decimal point, making it a floating-point literal, or to put an 'f' or 'F' or 'D' or 'd' after it, making it a float or a double respectively, e.g:
intBulbUse = ((intUserBulbs * 16.0) / 1000);
or
intBulbUse = ((intUserBulbs * 16f) / 1000);
A teacher who doesn't know this isn't really a Java teacher... but there are many things to know and learn, and even a teacher can't know eveything.
The generation of random numbers is too important to be left to chance...
R. Coveyou
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 13th, 2009, 07:13 PM
#3
Re: Look Over Code Please!
Originally Posted by dlorde
If you want to get a floating-point result from a calculation involving integral types (e.g. the calculation that gives zero), you need to cast one of the elements involved to a floating-point type, otherwise the result will be an integer (zero) that is assigned to your floating-point variable (an expression is evaluated according to the types in the expression, not the type it gets assigned to). A simple way to do this is either to give one of the numeric literals a decimal point, making it a floating-point literal, or to put an 'f' or 'F' or 'D' or 'd' after it, making it a float or a double respectively, e.g:
intBulbUse = ((intUserBulbs * 16.0) / 1000);
or
intBulbUse = ((intUserBulbs * 16f) / 1000);
A teacher who doesn't know this isn't really a Java teacher... but there are many things to know and learn, and even a teacher can't know eveything.
The generation of random numbers is too important to be left to chance...
R. Coveyou
Awesome! I really appreciate the quick response!
And your right, my teacher isn't a java teacher. My college used to teach VB up until this semester and my teacher was required to learn the new material, or find another job.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|