Results 1 to 4 of 4
Thread: Boolean value not working?
- 09-29-2009, 05:21 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
Boolean value not working?
Here, the part of this program is supposed to tell whether or not a cellphone call was in "peak hours" or in "off peak hours". Its set false when in offpeak hours, and true when in peak hours. Along with each false/true comes a string statement (because printf does not seem to support booleans?).
However, my result always ends up being this.
The program prints out call logs..
Call To/From Date Minutes Off-Peak
809-324-8278 Wed Sep 12 17:08:11 EDT 2007 65 True
809-324-3435 Wed Sep 12 18:21:11 EDT 2007 20 True
809-324-8278 Wed Sep 13 12:20:10 EDT 2007 75 True
809-324-8278 Wed Sep 14 13:21:11 EDT 2007 13 True
809-324-3435 Wed Sep 12 18:21:11 EDT 2007 203 True
Here is the three related classes of my code.
The CallLog constructor is where the Boolean is set.
However, do to the comparison operator seemingly not working.. it does not differentiate.
-------------------------Java Code:public class CallLog { /** * Instance variables for Class CallLog */ private String numberCalled; private String dateCalled; private int hoursCalled; private int minutesCalled; private int secondsCalled; private String timeZone; private int yearCalled; private int minutesUsed; private boolean offPeakTime; private String peakTimeState; /** * Constructor for Class CallLog * @param numCalled = Number of the phone called/received. * @param dateOfCall = Date the phone call was on. * @param hourCalled = The hour the phone call occurred. * @param minuteCalled = The minute the phone call occurred. * @param secondCalled = The second the phone call occurred. * @param timeZoneCalled = Time Zone the phone call was in. * @param yearOfCall = The year the phone call took place. * @param minutesUse = The total minutes used. */ public CallLog(String numCalled, String dateOfCall, int hourCalled, int minuteCalled, int secondCalled, String timeZoneCalled, int yearOfCall, int minutesUse){ numberCalled = numCalled; dateCalled = dateOfCall; hoursCalled = hourCalled; minutesCalled = minuteCalled; secondsCalled = secondCalled; timeZone = timeZoneCalled; yearCalled = yearOfCall; minutesUsed = minutesUse; /** * PeakTime is a boolean variable in which it tells us whether or not the phone call was during off-peak time or peak-time. * It's automatically set to false at first. There is a string variable to accommodate the printf display statements later on. */ /** * The program checks to see if the hour called in "Peak" time or not. If it is, then the boolean is set to true, as well as the string. */ if(hoursCalled >= 18){ offPeakTime = true; peakTimeState = "True"; } if(hoursCalled <= 18){ OffPeakTime = false; peakTimeState="False"; } } /** * @return the offPeakTime */ public boolean getPeakTime() { return offPeakTime; }
and here the main is executed....Java Code:public class Invoice { /** * instance variables are string CustName (Customer Name) and class PhonePlan phonePlan. */ private String custName; private PhonePlan phonePlan; public Invoice(String customerName, PhonePlan phoneClass){ custName = customerName; phonePlan = phoneClass; } /** * * @param addCallLog - prints/adds the callLog to a invoice. Uses printf with getter methods. */ public void addCallLog(CallLog newCallLog[], int i){ int min = newCallLog[i].getMinutesCalled(); if(min >= 10){ System.out.printf("%s %s %d:%d:%d %s %s %d %s%n", newCallLog[i].getNumberCalled(), newCallLog[i].getDateCalled(), newCallLog[i].getHoursCalled(), newCallLog[i].getMinutesCalled(), newCallLog[i].getSecondsCalled(), newCallLog[i].getTimeZone(),newCallLog[1].getYearCalled(), newCallLog[i].getMinutesUsed(), newCallLog[i].getPeakTimeState()); } if(min <= 10){ System.out.printf("%s %s %d:0%d:%d %s %s %d %s%n", newCallLog[i].getNumberCalled(), newCallLog[i].getDateCalled(), newCallLog[i].getHoursCalled(), newCallLog[i].getMinutesCalled(),newCallLog[i].getSecondsCalled(), newCallLog[i].getTimeZone(),newCallLog[1].getYearCalled(), newCallLog[i].getMinutesUsed(), newCallLog[i].getPeakTimeState()); } } /** * * @param peakTotal - does part of the formula, and combines its answer into one variable. * @param offPeakTotal - does part of the formula, and combines its answer into one variable. * @return - Returns amount due. */ public double getAmountDue(int usedOffpeakMinutes,int usedPeakMinutes){ int peakIncluded = phonePlan.getPeakIncluded(); int offPeakIncluded = phonePlan.getOffPeakIncluded(); int peakTotal = (usedPeakMinutes - peakIncluded); int offPeakTotal = (usedPeakMinutes - offPeakIncluded); /** * These IF statements check to see if the value of peakTotal/offPeakTotal are 0 or negative. * This is to prevent getting a negative amount due. */ if (peakTotal <= 0){ peakTotal = 0; } if (offPeakTotal <= 0){ offPeakTotal = 0; } /** * Formula for AmountDue */ return phonePlan.getPlanPrice() + peakTotal * phonePlan.getRateExtraMin() + offPeakTotal * phonePlan.getRateExtraMin(); } /** * * @param callLog - CallLog array that is used as a parameter. * @param n - a INT that is used to control how many CallLogs to process * @returns Used Off Peak Total. */ public int computeUsedOffPeak(CallLog callLog[], int n){ int offPeakTotal = 0; for(int i=0;i<n;i++){ if (callLog[i].getPeakTime() == false){ int temp = 0; temp = callLog[i].getMinutesUsed(); offPeakTotal = temp + offPeakTotal; } } return offPeakTotal; } /** * computeUsedPeak method * @param callLog - CallLog array that is used as a parameter. * @param n n - a INT that is used to control how many CallLogs to process * @returns Used Peak Total. */ public int computeUsedPeak(CallLog callLog[], int n){ int peakTotal = 0; for(int i=0;i<n;i++){ if (callLog[i].getPeakTime() == true){ int temp = 0; temp = callLog[i].getMinutesUsed(); peakTotal = temp + peakTotal; } } return peakTotal; }
Java Code:/** * * @author - TestInvoice contains the "main" of the program. * */ public class TestInvoice { public static void main(String [ ] args){ /** * Following Statements call our objects from the classes. I have named the PhonePlan object, jonesPlan, the Invoice object, jonesInvoice, * after the sample Customer's last name. * */ PhonePlan jonesPlan = new PhonePlan ("800-555-8594", 45.00, 100, -1, 0.35); Invoice jonesInvoice = new Invoice ("Bill Jones", jonesPlan); /** * The next 6 statements call our CallLog objects in a form of an array, callLog[]. */ CallLog[] callLog = new CallLog[5]; callLog[0] = new CallLog ("809-324-8278", "Wed Sep 12", 17, 8, 11, "EDT", 2007, 65); callLog[1] = new CallLog ("809-324-3435", "Wed Sep 12", 18, 21, 11, "EDT", 2007, 20); callLog[2] = new CallLog ("809-324-8278", "Wed Sep 13", 12, 20, 10, "EDT", 2007, 75); callLog[3] = new CallLog ("809-324-8278", "Wed Sep 14", 13, 21, 11, "EDT", 2007, 13); callLog[4] = new CallLog ("809-324-3435", "Wed Sep 12", 18, 21, 11, "EDT", 2007, 203); for(int i=0;i < 5;i++){ jonesInvoice.addCallLog(callLog, i); } } }Last edited by zerkz; 09-29-2009 at 06:08 AM.
- 09-29-2009, 05:56 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Can you specifically post the relevant code segment lol. It's a huge code, difficult to read it.
- 09-29-2009, 06:08 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
Ok, Well I just cut it down to the best I could , while still making most of it relevant.
- 09-29-2009, 06:42 AM #4
Member
- Join Date
- Sep 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
transfer boolean to 1's and 0's
By Nikohw in forum Java AppletsReplies: 5Last Post: 09-12-2009, 09:05 PM -
1 as an integer, not boolean
By McChill in forum New To JavaReplies: 1Last Post: 05-02-2009, 09:39 PM -
Java mail problem(working in intranet,but not working in iternet)
By sundarjothi in forum Advanced JavaReplies: 8Last Post: 05-28-2008, 07:00 AM -
boolean to string
By otoro_java in forum New To JavaReplies: 2Last Post: 01-30-2008, 05:31 AM -
boolean variables
By ravian in forum New To JavaReplies: 3Last Post: 12-31-2007, 04:58 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks