# can't figure out..

• December 10th, 2010, 12:27 PM
darego
can't figure out..
hey guys i'm pulling my hair out trying to solve this! i have been here for like 2 hours and it's probably something really easy. it's an array question:

Every year at Christmas, the Institute of Technology Termonfeckin sends out thank you cards to its wealthiest donors. This year the college is trying to save money on card preparation and postage costs, so has decided to only send cards to donors who donated more than 100 euro. They will also send a hamper to the company who gave the most. They have given you a list of 5 companies (below) and the amount they donate. You must write a program which initialises and stores this exact data in an array. You must then loop through the array and print out the following information:
1. Which companies will be sent cards
2. Which company will be sent a hamper
3. The total number of cards to be sent
Finally, the program must sort the arrays in order of most money donated and print the results out.
- Company list: “Termonfeckin Ferrys”, “Louth Industries”, “Ninja Enforcement Ltd”, “Termonfeckin Enterprises”, “Pirate Limb Replacement Corp.”
- Amount donated by each company: 120.50, 75.10, 500.00, 50.25, 350.50

i'm stuck on the first part "which companies will be sent cards". i have to print out all the companies who donated over 100 (which is 3 companies). what i'm trying to do is create an array to store the companies who donated over 100 then i will print this array out (thus completing the first part) what i'm trying to do is loop around the amount array and when the array location is > 100 (let's say it's amount[0]) i want to get it's equal equivalent in the names array (names[0]) and put that String in the cards array.

there is probably a way better and easier way of doing it, if there is please tell me or atleast let me know what i'm doing wrong please :D i'm only in my first semester so if i'v explained something wrong please let me know!

this is the code i have so far:

import java.util.Scanner;
class ArrayTest
{
public static void main(String args [])
{
Scanner input = new Scanner(System.in);
// arrys which will store names and donations
String names[] = { "Termonfeckin Ferrys", "Louth Industries", "Ninja Enforcement Ltd", "Termonfeckin Enterprises", "Pirate Limb Replacement Corp." };
double amount[] = { 120.50, 75.10, 500.00, 50.25, 350.50 };
String cards[] = { }; // array which will hold the donations above 100 who will receive cards
for(int i = 0; i < amount.length; i++)
{
if(amount.length > 100)
{
cards[i] = names[i]; // the part i'm having trouble with
}
}
System.out.println(cards);
}
}
• December 10th, 2010, 12:47 PM
javapenguin
Re: can't figure out..
Well, have two arrays, if you can.

One stores Strings, the company names.
The other stores values, the amount donated.

String[] companies = new String[5];
double[] amountDonated = new double[5];

companies[0] = "“Termonfeckin Ferrys”;
amountDonated[0] = 120.50;
companies[1] = “Louth Industries”;
amountDonated[1] = 75.10;
companies[2] = “Ninja Enforcement Ltd”;
amountDonated[2] = 500.00;
companies[3] = “Termonfeckin Enterprises”;
amountDonated[3] = 50.25;
companies[4] = “Pirate Limb Replacement Corp.”;
amountDonated[4] = 350.50;

public boolean donatedEnough(double amount)
{
if (amount > 100)
return true;
else
return false;
}

now for a for loop
int cardsToBeSent = 0;
for (int i =0; i < amountDonated.length; i++)
{
if (donatedEnough(amountDonated[i]))
{
System.out.println(companies[i] + " can get a card.");
cardsToBeSent++;
}
else
System.out.println(companies[i] + "can't get a card.");
}
• December 10th, 2010, 12:52 PM
darego
Re: can't figure out..
thanks a lot man, appreciate it!
• December 10th, 2010, 12:59 PM
darego
Re: can't figure out..
woops i forgot to add my program should has to look like this:

Your program should match this output as closely as possible:
Cards will be sent to:
Termonfeckin Ferrys
Ninja Enforcement Ltd
Pirate Limb Replacement Corp.

The hamper will be sent to: Ninja Enforcement Ltd
The number of cards to be sent: 3
The most generous donors were:
Ninja Enforcement Ltd
Pirate Limb Replacement Corp.
Termonfeckin Ferrys
Louth Industries
Termonfeckin Enterprises

so i think i need to add the names of the companies into an array? i just need help with this little part and i will try do the rest myself. thanks again mate!
• December 10th, 2010, 01:00 PM
javapenguin
Re: can't figure out..
I can think of a way, to find the largest donation.

double largestAmountDonated = 0;
for (int j = 0; j < amountDonated; j++)
{
if (amountDonated[j] > largestAmountDonated)
{
largestAmountDonated = amountDonated[j];
}

}

for (int k =0; k < amountDonated.length; k++)
{
if (amountDonated[k] == largestAmountDonated())
System.out.println(companies[k] + " gets the hamper.");
}
• December 10th, 2010, 01:09 PM
javapenguin
Re: can't figure out..
Code java:

```public static void selection_sort(double []a, String[] str) { int i,j; double max; int max_index;   for (i=a.length-1;i>0;i--) { max = a[0]; max_index = 0; for (j=1;j<=i;j++) { if (a[j]>max) { max = a[j]; max_index = j; } } double temp = a[max_index]; a[max_index] = a[i]; a[i] = temp; } }   public static void bubble_sort(double []a, String[] str) { int i,j,temp; boolean swap;   for (i=a.length-1;i>0;i--) { //for array a[0-i] swap = false; for (j=0;j<i;j++) { if (a[j]>a[j+1]) //elements are out of order { //swap a[j] and a[j+1] swap = true; temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } } if (!swap) { return; } } }   public static void insertion_sort(int []a) { int i,j; int key;   for (i=1;i<a.length;i++) { key = a[i]; //try to insert key into array a[0..i-1]   j = i-1; while ((j>=0) && (a[j]>key)) { a[j+1] = a[j]; //write a[j] at next position j--; } a[j+1] = key; //j+1 is the correct place for key   //at the end, array [0...i] is sorted }```

Here's some methods of sorting I found.

Needs to be fixed thought to work with double and also sort the companies along with amounts they donated.
• December 10th, 2010, 01:26 PM
darego
Re: can't figure out..
you're a legend mate thanks a lot :)