Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: can't figure out..

  1. #1
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Angry 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 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);
    }
    }
    Last edited by darego; December 10th, 2010 at 01:45 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.");
    }

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    darego (December 10th, 2010)

  4. #3
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: can't figure out..

    thanks a lot man, appreciate it!

  5. #4
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default 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!
    Last edited by darego; December 10th, 2010 at 02:01 PM.

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.");
    }

  7. The Following User Says Thank You to javapenguin For This Useful Post:

    darego (December 10th, 2010)

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: can't figure out..

    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.

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    darego (December 10th, 2010)

  10. #7
    Junior Member darego's Avatar
    Join Date
    Dec 2010
    Location
    Ireland
    Posts
    17
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: can't figure out..

    you're a legend mate thanks a lot

Similar Threads

  1. Easy help but can't figure it out.
    By weezer562 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 19th, 2010, 09:50 PM
  2. Simple error can't figure out.
    By n00bprogrammer in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 30th, 2010, 12:19 PM
  3. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  4. can't figure out how to sort an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 6th, 2010, 03:07 PM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM