Help With Beginner Level Problem

java problem

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 974 Views - Last Post: 11 September 2009 - 08:43 PM Rate Topic: -----

#1 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Help With Beginner Level Problem

Posted 11 September 2009 - 06:19 PM

Tipping Waiters

Heres problem statement:

* The sum I pay must be round, i.e., divisible by 5.
* The tip must be between 5% and 10% of the final sum I pay, inclusive.

Clearly, sometimes there may be multiple "correct" ways of settling the bill.

Inputs:

* an int bill -- the amount I have to pay for the dinner
* an int cash -- the amount of money I have in my pocket

Write a function that computes how many different final sums satisfy the conditions above.

Heres the code I have so far:


public class TippingWaiters {		
	public int possiblePayments(int bill, int cash) { 
	
	int start=(int)(bill+.05*bill);
	int end=(int)(bill+.1*bill);
		
		
		if(start>cash)				
			start=cash;				
		if(end>cash)				
			end=cash;
		
		int count=0;
		
		
		for(int i=start;i<=end;++i)					
			if(i%5==0)								
				++count;							
		return count;								
	}
}




But I'm getting errors when compiling. For instance, with the inputs : 240 (bill) 279 (cash), the answer is 3 but the code gives me 2.

Can someone help

This post has been edited by messianic: 11 September 2009 - 06:46 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help With Beginner Level Problem

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:45 PM

code tags please
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:46 PM

View Postmessianic, on 11 Sep, 2009 - 07:19 PM, said:

But I'm getting errors when compiling.


Which are...?

And, as mostyfriedman said, :code: .

This post has been edited by Locke: 11 September 2009 - 06:47 PM

Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:54 PM

btw there's no need for type casting here


edit: dunno what's wrong with me tonight :P

This post has been edited by mostyfriedman: 11 September 2009 - 07:32 PM

Was This Post Helpful? 0
  • +
  • -

#5 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 06:59 PM

what does type casting mean?

Can you help me out on my code
Was This Post Helpful? 0
  • +
  • -

#6 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:06 PM

I honestly do not know what is wrong with my code. When I do it manually, i find that the number i get working with my code is the right answer, but for some reason when I run it it gives back a wrong answer
Was This Post Helpful? 0
  • +
  • -

#7 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:12 PM

int start=(int)(bill+.05*bill);


the (int) is called type casting..

Quote

But I'm getting errors when compiling. For instance, with the inputs : 240 (bill) 279 (cash), the answer is 3 but the code gives me 2.

i dont really see any compilation errors.. what exactly are you trying to do?

int end=(int)(bill+.1*bill);



it should be 0.01 instead of 0.1
Was This Post Helpful? 0
  • +
  • -

#8 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:14 PM

well 10% should be .1 correct?

And are you saying I should delete int? Should I replace it with something

And I run my code in a program that feeds in input and I am getting wrong answers for some of them.
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:15 PM

View Postmostyfriedman, on 11 Sep, 2009 - 08:12 PM, said:

it should be 0.01 instead of 0.1


Not true...10% is .1.
Was This Post Helpful? 0
  • +
  • -

#10 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:17 PM

Locke, could you please help me figure this out?

Basically, when i input the numbers manually and figure it out, the code should give the right answers, but somehow when I run it through the program the code is giving wrong answers. Im kinda in a dead end
Was This Post Helpful? 0
  • +
  • -

#11 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:20 PM

View PostLocke, on 11 Sep, 2009 - 06:15 PM, said:

View Postmostyfriedman, on 11 Sep, 2009 - 08:12 PM, said:

it should be 0.01 instead of 0.1


Not true...10% is .1.

ah yea right..crap, brain overflow again
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 725
  • View blog
  • Posts: 4,471
  • Joined: 24-October 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:36 PM

i also ran it by hand and start will be 252, end will be 264..only 255 and 260 are divisible by 5 so the output is 2
Was This Post Helpful? 0
  • +
  • -

#13 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:37 PM

is this supposed to be a help forum
Was This Post Helpful? 0
  • +
  • -

#14 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:46 PM

Yes...but sadly, you haven't given much information...

But from what I can tell, I don't see why it's going wrong.
Was This Post Helpful? 0
  • +
  • -

#15 messianic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 03-September 09

Re: Help With Beginner Level Problem

Posted 11 September 2009 - 07:56 PM

here are other constraints that might help:

Assume that both bill and cash are in dollars
All the money I have is in one-dollar banknotes.

Here are some examples:

1234567 (bill), 12345678 (cash); answer = 14440

Anyone please
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2