# Wanna help me do a few problems?

Discussion in 'Mac Programming' started by pongster08, Jun 11, 2009.

1. ### pongster08 macrumors newbie

Joined:
May 26, 2009
#1
Hey guys, the following problems are my graded homework assignment. If any of you could help me that would be great. The code must be in java.

Thank you!

________________________
Problem 1:

Code:
```/**
counts how many values in an array are over 100.
returns the number of entries over 100 in the array.
*/

public int over100(int[] list){

(insert code here)

}```

problem 2:

Recall that the polygon class has a constructor that takes three parameters:
Polygon(int[] x, int[] y, int npoints)

Write a method that creates and returns a random triangle.
The x and y coordinates should satisfy:

5 <=x <=400, 50 <= y<= 500

Code:
```public Polygon randomTriangle()
{

(insert code here)

}```

Problem 3:

Write a method to help you figure out how to split the bill with your friends at a restaurant, A probram like this would be nice to have on a PDA or pocket PC. The program takes as input the total bill and the number of peopel who will split the bill. It adds a 15% tip and then outputs the amount of each person should pay. (tax is already included in mealCost)

Code:
```public static double divideBill(double mealCost, int numPeople)
{

(insert code here)

}```

Joined:
Apr 20, 2009
#2
You should read your book, they are all easy problems once you know the syntax .

What do you think the steps are to solve problem 1?

3. ### angelwatt Moderator emeritus

Joined:
Aug 16, 2005
Location:
USA
#3
Might be willing to help with your HW, but not do your HW. You should at least try doing it yourself before asking for someone to do it for you. You'll never learn anything otherwise.

4. ### pongster08 thread starter macrumors newbie

Joined:
May 26, 2009
#4

public class TestProblems{

public static double divideBill(double mealCost, int numPeople){

return (((mealCost * .15) + mealCost) / numPeople);
}

public static void main(String[] args){

System.out.print(divideBill(200,2));
}
}

I am just having a hard time with problem 1 basically

5. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#5
For problem one this is a very simple for-each loop.:
Code:
```for (int x : list) {
if(<something goes here>) {
<keep track of how many, somehow>
}
}
```
The second is pretty easy as well. I'm assuming they don't care if you make sure that the angles are > 0, etc. so you can basically just generate 3 random numbers between 0 and 395, then add 5 to get your x's, and the same between 0 and 450 then + 50 for your y's. Once you have those, you just shove them in arrays and return the new polygon you create. npoints will always be 3, the array width is always 3, etc.

The last one is a one-liner. I don't even know what help to offer. To get the amount after tip, multiply by 1.15. Then divide. The problem with this, like most monetary calculations people seem to do, is the type... You always want whole cents... for the vast majority of uses money is fixed point, so you can just store cents as an int... the only time you need to throw in a decimal is when you're formatting for output. The teacher gave you the skeleton of the method, so no use bothering with that, but you might want to *100 then take the ceiling of the result, then divide by 100 again. The waitperson might get a tad more than 15% sometimes, but they'll never get shorted.

-Lee

Joined:
Apr 20, 2009
#6
Problem 1 you need to :

Find the size of the array.
loop through each element in the array.
If an element if over 100, increment a counter.

7. ### pongster08 thread starter macrumors newbie

Joined:
May 26, 2009
#7
public static int over100(int list){

int counter = 0;

for (int i = 0; i<list.length; i++){

counter + 1;
}

return counter;
}

Is that close to it?

8. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#8
What part of that is checking if list is greater than 100? If you're using java 5 or above, you can also just use the for-each syntax to make looping over the array simpler.

-Lee

9. ### Wowzera macrumors 6502a

Joined:
Oct 14, 2008
Location:
Brazil
#9

Sorry, I couldn't understand the other problem. These problems are easy to solve, you must study

10. ### dbell macrumors member

Joined:
Jul 11, 2007
#10
You could just do a ternary operation to keep it simple.

public static int over100(int[] list){
return (list.length > 100) ? (list.length - 100) : 0;
}

11. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#11
Not quite the question... and don't write too much code, this is his HW...

It's how many of the elements of the list are greater than 100.

-Lee

Joined:
Jul 11, 2007
#12