PDA

View Full Version : Wanna help me do a few problems?

pongster08
Jun 11, 2009, 02:09 PM
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:

/**
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

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)

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

(insert code here)

}

Jun 11, 2009, 02:15 PM
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?

angelwatt
Jun 11, 2009, 02:19 PM
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.

pongster08
Jun 11, 2009, 02:33 PM

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

lee1210
Jun 11, 2009, 02:37 PM
For problem one this is a very simple for-each loop.:

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

Jun 11, 2009, 02:38 PM
I am just having a hard time with problem 1 basically

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.

pongster08
Jun 11, 2009, 02:47 PM
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?

lee1210
Jun 11, 2009, 03:34 PM
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?

What part of that is checking if list[i] 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

Wowzera
Jun 11, 2009, 03:42 PM
public int over100(int[] list)
{
int count = 0;
for (int i = 0; i < list.length; i++)
if (list[i] > 100)
count++;
return count;
}

public static double divideBill(double mealCost, int numPeople)
{
double calc = mealCost * 0.15;
return (calc + mealCost) / numPeople;

}

public static void main(String[] args)
{
System.out.println(divideBill(20.25, 3));
}

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

dbell
Jun 11, 2009, 03:45 PM
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?

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

lee1210
Jun 11, 2009, 04:01 PM
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;
}

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

dbell
Jun 11, 2009, 06:58 PM
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

;)