Results 1 to 19 of 19
- 03-12-2009, 08:18 AM #1
[SOLVED] calling a boolean method, confusion!!
Im trying to make user enter hours in 24-hours mode then the method "isValidTime" tests if it is in the range of hours between 00 to 23 and the minutes from 00 to 59 and supposed to return the value to allow the code from
to run...please help.Java Code:while (timeStart > 0) { System.out.print("Enter distance on Kilometres: "); int distance = key.nextInt(); System.out.print("Enter length of call in minutes: "); length = key.nextInt(); double costPerCall = total(timeStart, distance, length); // calculates the cost per call totalCost+=costPerCall; numOfCall++; System.out.printf("The call will cost RM %.2f\n", costPerCall); System.out.println(); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); timeStart = key.nextInt(); } double AvgCost = Average(totalCost, numOfCall); if (totalCost == 0) { System.out.println("No call is made"); } else { System.out.printf("The total cost for all calls is RM %.2f\n", totalCost); System.out.println(); System.out.printf("The average cost is RM %.2f\n", AvgCost); } }
THE WHOLE PROGRAM HERE!!!!
Java Code:import java.util.Scanner; public class Tester { public static void main(String[] args) { Scanner key = new Scanner(System.in); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); int timeStart = key.nextInt(); int numOfCall = 0; // initialise the number of calls made double totalCost = 0.0; //int hours = (timeStart/100); // separates the time //int minutes = (timeStart%100); int length = 0;// initialise the length of call boolean timeTest = isValidTime(timeStart); //loop begins here while (timeStart > 0) { System.out.print("Enter distance on Kilometres: "); int distance = key.nextInt(); System.out.print("Enter length of call in minutes: "); length = key.nextInt(); double costPerCall = total(timeStart, distance, length); // calculates the cost per call totalCost+=costPerCall; numOfCall++; System.out.printf("The call will cost RM %.2f\n", costPerCall); System.out.println(); System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): "); timeStart = key.nextInt(); } double AvgCost = Average(totalCost, numOfCall); if (totalCost == 0) { System.out.println("No call is made"); } else { System.out.printf("The total cost for all calls is RM %.2f\n", totalCost); System.out.println(); System.out.printf("The average cost is RM %.2f\n", AvgCost); } } public static double total (int time, int distance, int length) { double callCost = 0.0; //limits if (time >= 700 && time <= 1959) // peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.50; } else // local calls { callCost = 0.20; } } else if (time < 700 || time > 1959) // non-peak hours { if (distance > 60) // Trunk (STD) calls { callCost = 0.25; } else // local calls { callCost = 0.10; } } double costPerCall = (length*callCost); // calculates the cost for single call. return costPerCall; } public static double Average (double total, int numOfItems) { double Average = (total/numOfItems); return Average; } public static boolean isValidTime(int timeIn) { int hours = (timeIn/100); // extract hours from 24-hours time int minutes = (timeIn%100); // extract minutes from 24-hours time { if (timeIn >= 0000 || timeIn <= 2359) { if (hours >= 00 || hours <= 23 && minutes >= 00 || minutes <= 59) { return true; } else return false; } else return false; } } }Last edited by AngrYkIdzrUlE; 03-12-2009 at 08:26 AM. Reason: typo
- 03-12-2009, 08:31 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
What exactly is your problem? Post error messages or expected/actual output.
I die a little on the inside...
Every time I get shot.
- 03-12-2009, 08:34 AM #3
- 03-12-2009, 10:11 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually we cannot say any solution here. Because you had to have a design for that. As an example, say your time format is 12:23, in standard format.
First of all you have to separately find number of hours and number of minutes. Then comparing with the limitation you spoke about.
- 03-12-2009, 10:36 AM #5
- 03-12-2009, 10:38 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So what you have to do is compare them with the bound. 00 to 23 for hours and 00 to 59 for minutes.
- 03-12-2009, 10:46 AM #7
- 03-12-2009, 11:11 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Java Code:boolean status = false; if(hours >= 0 || hours <= 23) { status = true; } if(minutes >= 0 || minutes <= 59) { status = true; } return status;
- 03-12-2009, 11:12 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you get the point on the above code?
- 03-12-2009, 11:39 AM #10
- 03-12-2009, 04:25 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine, just look at the logic I've workout. I've not test it, better to workout on it.
- 03-12-2009, 06:45 PM #12
- 03-12-2009, 09:01 PM #13
does this method make sense???
Java Code:public static boolean isValidTime(int timeIn) { boolean vTime= true; boolean invTime= true; boolean invalTime= true; int hours = (timeIn/100); // extract hours from 24-hours time int minutes = (timeIn%100); // extract minutes from 24-hours time { if (timeIn >= 0000 || timeIn <= 2359) { if (hours >= 00 || hours <= 23 && minutes >= 00 || minutes <= 59) { return true; } else return false; } else return false; } }
- 03-13-2009, 05:29 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ok what happen when you run this code?
This is all about logical operators.
- 03-13-2009, 08:18 PM #15
sorry, the code should be:
checks for the value entered i.e timeIn if it's in 24-hour mode and returns true and if it's not in 24-hour mode, returns false...Java Code:public static boolean isValidTime(int timeIn) // checks if the time format is in 24-hour mode! { int hours = (timeIn/100); int minutes = (timeIn%100); { if (timeIn >= 0000 && timeIn <= 2359) { if ((hours >= 00 && hours <= 23) && (minutes >= 00 && minutes <= 59)) return true; else return false; } else return false; } }
I figured out the problem....thanks all for the input.
- 03-14-2009, 02:18 AM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Fine. But my next suggestion is use of SimpleDateFormat. Much stronger and much faster than this way you have workout. And much safer too.
- 03-14-2009, 03:04 AM #17
- 03-15-2009, 09:50 AM #18
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you read anything about this on the web? Or else on Java doc?
It's the best way to working on with Date/Time formatting.
- 03-15-2009, 10:23 AM #19
Similar Threads
-
Need help calling from a different method
By Mayur in forum New To JavaReplies: 6Last Post: 03-08-2009, 09:27 PM -
im not familiar with boolean in method...
By PureAwesomeness in forum New To JavaReplies: 19Last Post: 02-22-2009, 02:36 AM -
[SOLVED] boolean method problem
By shadowblade19 in forum New To JavaReplies: 6Last Post: 11-30-2008, 02:01 AM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM


LinkBack URL
About LinkBacks


Bookmarks