Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pongster08

macrumors newbie
Original poster
May 26, 2009
8
0
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)

}
 

iBadger

macrumors member
Apr 20, 2009
56
0
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

Moderator emeritus
Aug 16, 2005
7,852
9
USA
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

macrumors newbie
Original poster
May 26, 2009
8
0
I already have problem 3


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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

iBadger

macrumors member
Apr 20, 2009
56
0
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

macrumors newbie
Original poster
May 26, 2009
8
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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 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

macrumors 6502a
Oct 14, 2008
857
28
Brazil
public int over100(int[] list)
{
int count = 0;
for (int i = 0; i < list.length; i++)
if (list > 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

macrumors member
Jul 11, 2007
85
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.