Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default [SOLVED] if-else and boolean issues

    Hey im trying to get this code to accept time in 24 hours and charge calls according to peak time and non-peak time but the output for non-peak hours is the same as peak which shouldnt be, please help.
    Java Code:
    import java.util.Scanner;
    
    public class PhoneCalls
    {
    	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
    		
    		double callCost = 0.0;
    		//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();
    			
    			
    			//limits 
    			if (timeStart >= 7 || timeStart <= 19) // peak hours
    			{
    				if (distance > 60) // Trunk (STD) calls
    				{
    					callCost = 0.50;
    				}
    				else // local calls
    				{
    					callCost = 0.20;
    				}
    			}
    			else if (timeStart < 7 && timeStart > 19) // non-peak hours
    			{
    				if (distance > 60) // Trunk (STD) calls
    				{
    					callCost = 0.25; 
    				}
    				else // local calls
    				{
    					callCost = 0.10;
    				}
    			}
    			
    			double costPerCall = total(length, callCost); // calculates the cost per call
    			
    			totalCost+=costPerCall;
    			numOfCall++;
    			
    			System.out.println("The call will cost RM "+ costPerCall);
    			
    			System.out.println();
    			System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): ");
    			timeStart = key.nextInt();
    		}
    		
    		System.out.println("The total cost for all calls is RM" + totalCost);
    			
    			
    	}	
    	
    	public static double total (int value1, double value2)
    	{
    		double total = (value1*value2);
    		return total;	
    	}
    }

  2. #2
    Eric Golf is offline Member
    Join Date
    Dec 2008
    Location
    Edinburgh, Scotland
    Posts
    4
    Rep Power
    0

    Default Think of a number

    Hi,

    Give a number that IS NOT >=7 OR <=19

    Give a number that IS < 7 AND > 19

    Hope this helps,

    Eric

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

    Default

    Quote Originally Posted by Eric Golf View Post
    Hi,

    Give a number that IS NOT >=7 OR <=19

    Give a number that IS < 7 AND > 19

    Hope this helps,

    Eric
    Thanks, i always end up messing up with the operators...mhh...gotta find an easy way to remember. :P

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

    Default

    Actually those are very basis in Java fundamentals. About operators. Not much difficult to remember. Best thing you can do is write all those such things on a paper and keep going for few times.

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

    Default

    Quote Originally Posted by Eranga View Post
    Actually those are very basis in Java fundamentals. About operators. Not much difficult to remember. Best thing you can do is write all those such things on a paper and keep going for few times.
    thanks, will do...
    Have to test it and see if the code works, me such a noob :P

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

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

    Default still not working

    it still doesnt give correct output, here's what i want to get:
    Java Code:
    Enter time in 24-hour mode (NEGATIVE integer to quit): 0800
    Enter distance in kilometres: 50
    Enter length of call in minutes: 10
    That call will cost RM2.00
    Enter time in 24-hour mode (NEGATIVE integer to quit): 0
    Enter distance in kilometres: 50
    Enter length of call in minutes: 10
    That call will cost RM1.00
    Enter time in 24-hour mode (NEGATIVE integer to quit): 2222
    Enter distance in kilometres: 70
    Enter length of call in minutes: 10
    That call will cost RM2.50
    Enter time in 24-hour mode (NEGATIVE integer to quit): 1030
    Enter distance in kilometres: 80
    Enter length of call in minutes: 10
    That call will cost RM5.00
    Enter time in 24-hour mode (NEGATIVE integer to quit): 2020
    Enter distance in kilometres: 60
    Enter length of call in minutes: 11
    That call will cost RM1.10
    Enter time in 24-hour mode (NEGATIVE integer to quit): 900
    Enter distance in kilometres: 61
    Enter length of call in minutes: 11
    That call will cost RM5.50
    Enter time in 24-hour mode (NEGATIVE integer to quit): -999
    The total cost of these calls is RM17.10
    Average cost is RM2.85
    Enter time in 24hr mode (NEGATIVE integer to quit): -999

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

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

    Default

    yes thats the main idea but thats not the problem, try and run it and see...the value of non-peak time doesnt work.

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

    Default

    Yes, it's wrong. You have made a big mistake there.

    Java Code:
    else if (timeStart < 7 && timeStart > 19)
    Can you tell me the meaning of this line of code. Do you know the meaning of &&?

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

    Default

    Quote Originally Posted by Eranga View Post
    Yes, it's wrong. You have made a big mistake there.

    Java Code:
    else if (timeStart < 7 && timeStart > 19)
    Can you tell me the meaning of this line of code. Do you know the meaning of &&?
    && is AND right?? im a newbie...well, basically what i want it to do is to take values out of the range between 7 and 19. How do i do that?? :P

  12. #12
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    if you have a condition where x < 7 and x > 19, then you will never get true. no number is less than 7 but greater than 19. you wanna use or.

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

    Default

    Quote Originally Posted by emceenugget View Post
    if you have a condition where x < 7 and x > 19, then you will never get true. no number is less than 7 but greater than 19. you wanna use or.
    :P..oh, ok. As i said, i really SUCK when it comes to operators, boolean must be laughing his ass off. :(

  14. #14
    Eric Golf is offline Member
    Join Date
    Dec 2008
    Location
    Edinburgh, Scotland
    Posts
    4
    Rep Power
    0

    Default And or Or

    Hi,

    Obviously my first post was too subtle :D

    Have you figured it out, did you get it fixed?

    Eric.

    Edit - p.s. You don't actually need the second If, you can just use Else since if a number is not between 7 & 19 then it must be less than 7 or greater than 19.
    Last edited by Eric Golf; 03-07-2009 at 10:36 PM. Reason: Add p.s.

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

    Default

    Quote Originally Posted by Eric Golf View Post
    Hi,

    Obviously my first post was too subtle :D

    Have you figured it out, did you get it fixed?

    Eric.

    Edit - p.s. You don't actually need the second If, you can just use Else since if a number is not between 7 & 19 then it must be less than 7 or greater than 19.
    Well, i could just have one if-else but the general idea is to limit entries to 24-hour system FIRST, meaning valid entries should be from 0000-2359, then after that criteria has been met then it checks if it's peak time or not.

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

    Default It works!!!, yaay!!!!

    i think this time i got it right, the timeStart limit shouldn't have been 7, and 19. I changed them to 700 and 1959 cause the user enters 24-hours...anyways...test it out for me again...i think it works, Here's the code: PS, thanks all for the help

    Java Code:
    import java.util.Scanner;
    
    public class PhoneCalls
    {
    	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
    		
    		double callCost = 0.0;
    		//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();
    			
    			
    			//limits 
    			if (timeStart >= 700 && timeStart <= 1959) // peak hours
    			{
    				if (distance > 60) // Trunk (STD) calls
    				{
    					callCost = 0.50;
    				}
    				else // local calls
    				{
    					callCost = 0.20;
    				}
    			}
    			else if (timeStart < 700 || timeStart > 1959) // non-peak hours
    			{
    				if (distance > 60) // Trunk (STD) calls
    				{
    					callCost = 0.25; 
    				}
    				else // local calls
    				{
    					callCost = 0.10;
    				}
    			}
    			
    			double costPerCall = total(length, callCost); // calculates the cost per call
    			
    			totalCost+=costPerCall;
    			numOfCall++;
    			
    			System.out.println("The call will cost RM "+ costPerCall);
    			
    			System.out.println();
    			System.out.print("Enter time in 24-hour mode(NEGATIVE integer to quit): ");
    			timeStart = key.nextInt();
    		}
    		
    		System.out.println("The total cost for all calls is RM" + totalCost);
    			
    			
    	}	
    	
    	public static double total (int value1, double value2)
    	{
    		double total = (value1*value2);
    		return total;	
    	}
    }

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

    Default

    Suggestion here. Take a peace of paper and write down your logic you want to evaluate on your application. Then try to put that logic in to a Java code. It make sense a lot. Just think about a step at a time. Mean time you can study bit about logical operators in Java.

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

    Default

    Quote Originally Posted by Eranga View Post
    Suggestion here. Take a peace of paper and write down your logic you want to evaluate on your application. Then try to put that logic in to a Java code. It make sense a lot. Just think about a step at a time. Mean time you can study bit about logical operators in Java.

    Thanks, i did that for the last part and got the code to work very easily..normally i just throw every idea in as they come...and end up with scrambled code.

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

    Default

    Quote Originally Posted by AngrYkIdzrUlE View Post
    normally i just throw every idea in as they come...and end up with scrambled code.
    That's not good lol. If you are going to change your code time to time, the way you work on is not good. Try to look at some concepts on OO.

    By the way, did you solve your questions completely here?

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

    Default

    Quote Originally Posted by Eranga View Post
    That's not good lol. If you are going to change your code time to time, the way you work on is not good. Try to look at some concepts on OO.

    By the way, did you solve your questions completely here?
    Thanks, i should. Yes i solved the questions. Thanks for all the help. I still have to do some small modification of the code...so maybe will need some help later. :D

Page 1 of 2 12 LastLast

Similar Threads

  1. How can I pass a boolean to notifyObservers()
    By dumb_ass in forum New To Java
    Replies: 6
    Last Post: 03-05-2009, 09:15 PM
  2. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 06:01 AM
  3. boolean to string
    By otoro_java in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 06:31 AM
  4. boolean variables
    By ravian in forum New To Java
    Replies: 3
    Last Post: 12-31-2007, 05:58 AM
  5. Boolean Expression
    By ritwik07 in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 05:11 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
  •