Results 1 to 19 of 19
  1. #1
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Unhappy [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
    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);
    			}
    	}
    to run...please help.


    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

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    What exactly is your problem? Post error messages or expected/actual output.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Supamagier View Post
    What exactly is your problem? Post error messages or expected/actual output.
    my problem is i have no idea how to go about doing what i just described... as you can see in my code. Would appreciate the help!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  5. #5
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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.
    i did that here
    Java Code:
    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
    		
    		{

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    So what you have to do is compare them with the bound. 00 to 23 for hours and 00 to 59 for minutes.

  7. #7
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    So what you have to do is compare them with the bound. 00 to 23 for hours and 00 to 59 for minutes.
    can you read the code pls. Yes, i did that too....my problem is...calling the return value....can a method have more than one output??

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Java Code:
    boolean status = false;
    
    if(hours >= 0 || hours <= 23) {
         status = true;
    }
    
    if(minutes >= 0 || minutes <= 59) {
         status = true;
    }
    
    return status;

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Did you get the point on the above code?
    yeah, thanks...lemme check that out.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  12. #12
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Fine, just look at the logic I've workout. I've not test it, better to workout on it.
    I did the testing of hours and minutes in one line... like this:
    Java Code:
    if (hours >= 00 || hours <= 23 && minutes >= 00 || minutes <= 59)
    					{
    						return true;
    					}
    				else 
    				return false;

  13. #13
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    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;
    		}
    	
    	}

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  15. #15
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Ok what happen when you run this code?

    This is all about logical operators.
    sorry, the code should be:

    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;
    		}
    	
    	}
    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...

    I figured out the problem....thanks all for the input.

  16. #16
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Fine. But my next suggestion is use of SimpleDateFormat. Much stronger and much faster than this way you have workout. And much safer too.

  17. #17
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Fine. But my next suggestion is use of SimpleDateFormat. Much stronger and much faster than this way you have workout. And much safer too.
    What is SimpleDateFormat??? , can you write it?

  18. #18
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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.

  19. #19
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    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.
    thanks for that...got it on:

    Java Code:
     http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/

Similar Threads

  1. Need help calling from a different method
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 03-08-2009, 09:27 PM
  2. im not familiar with boolean in method...
    By PureAwesomeness in forum New To Java
    Replies: 19
    Last Post: 02-22-2009, 02:36 AM
  3. [SOLVED] boolean method problem
    By shadowblade19 in forum New To Java
    Replies: 6
    Last Post: 11-30-2008, 02:01 AM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. method calling?
    By frejon26 in forum New To Java
    Replies: 4
    Last Post: 01-25-2008, 03:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •