Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By DiamondSoul
  • 1 Post By pbrockway2

Thread: Code for Array and Sorting

  1. #1
    Sync is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Code for Array and Sorting

    Hello im trying to create a code to calculate the average luminance of each pixel than the average luminance of all the pixels in five random pictures. After creating that coding I must create a main method to create an array that sorts the five pictures from their average luminance of all pixels from lowest to highest. This is what I have so far and im stuck Thank you for your help! Also im using Dr Java if this helps at all Im very new so im confused.

    public void averageLuminence()
    {
    Pixel pixel = null;
    Pixel[] pixelArray = this.getPixels();
    double averageLuminence = 0;
    double redValue = 0;
    double blueValue= 0;
    double greenValue = 0;
    double sumLuminence = 0;
    double luminence = 0;
    for (int i = 0; i < pixelArray.length; i++)
    {
    pixel = pixelArray[i];
    redValue = pixel.getRed() * 0.299;
    greenValue = pixel.getGreen() * 0.587;
    blueValue = pixel.getBlue() * 0.114;
    luminence = (int) (redValue + greenValue + blueValue);
    sumLuminence += luminence;
    averageLuminence = sumLuminence / pixelArray.length;
    System.out.println("Average Luminence is " + averageLuminence);
    }
    }
    public static void main(String[] args)
    {
    int[] myArray = new int[5];

    String fileName1 = "C:/Users/Scott/Pictures/Pic/Desert.jpg";
    Picture myPic1 = new Picture(fileName1);
    myPic1.averageLuminence();


    String fileName2 = "C:/Users/Scott/Pictures/Pic/Waterfall.jpg";
    Picture myPic2 = new Picture(fileName2);
    myPic2.averageLuminence();
    myPic2.explore();

    String fileName3 = "C:/Users/Scott/Pictures/Pic/Painting.jpg";
    Picture myPic3 = new Picture(fileName3);
    myPic3.averageLuminence();
    myPic3.explore();

    String fileName4 = "C:/Users/Scott/Pictures/Pic/Snow.jpg";
    Picture myPic4 = new Picture(fileName4);
    myPic4.averageLuminence();
    myPic4.explore();

    String fileName5 = "C:/Users/Scott/Pictures/Pic/Walkway.jpg";
    Picture myPic5 = new Picture(fileName5);
    myPic5.averageLuminence();
    myPic5.explore();
    }
    Last edited by Sync; 10-28-2011 at 09:41 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Code for Array and Sorting

    This is what I have so far and im stuck
    Well, first of all you'll want to wrap that method in a class, otherwise you can't even try to compile it.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Code for Array and Sorting

    Please use [code] tags in the future, it makes the code much more readable.



    It looks like you have the code to calculate luminescence of each image, but you're stuck sorting them? A few things which I hope will help you:


    1) The method averageLuminence() doesn't return anything. The calculated value of averageLuminence is lost when the method finishes, since it's a local variable.

    2) Do you really want to output averageLuminence to the console after every single pixel? I would suggest putting the System.out.println outside of the for loop. In fact, you could put the line of code just before that one outside of the loop as well, since you don't need to calculate the average until you've summed the luminescence of ALL the pixels.

    3) For sorting, selection sort is a simple way to sort a collection of items. Merge sort is much more efficient, but more difficult to code. For just sorting 5 things, selection sort would be fine, though you might consider using merge sort anyway in case you every want to expand your program to work with more than 5 images.
    Sync likes this.

  4. #4
    Sync is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Code for Array and Sorting

    I see yes that worked but instead of selection sort someone told me to use a bubble sort instead? Would that be just as efficent and if so how do you use that in my coding? thanks for the help

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Code for Array and Sorting

    Bubble sort and selection sort have approximately equal efficiency. They both work by repeatedly comparing two elements of the collection, and swapping them if they're out of order. They differ in terms of their schemes for determining which pairs of elements to compare.

    Suppose you had an array of 4 elements (indexed 0 to 3). Here is the order in which selection and bubble sort would do "compare, swap if out of order" operations on pairs of elements:

    selection sort:
    0, 1
    0, 2
    0, 3
    1, 2
    1, 3
    2, 3

    Bubble sort:
    0, 1
    1, 2
    2, 3
    0, 1
    1, 2
    0, 1

  6. #6
    Sync is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Code for Array and Sorting

    Ok I understand however, Im cant get my values to sort im very new to this so bare with me but this is my main method for running my array that sorts using a bubble, the bubble is just an example i pasted from the internet. I need to get my pictures to sort from low to high according to their luminence. when I run my code i am able to get their average luminence but they are not sorted so any help with that and im set thanks for all your help so far.

    public class main extends Picture
    {
    public static void main(String[] args)
    {


    String fileName1 = "C:/Users/Scott/Pictures/Pic/Desert.jpg";
    Picture myPic1 = new Picture(fileName1);
    myPic1.averageLuminence();


    String fileName2 = "C:/Users/Scott/Pictures/Pic/Waterfall.jpg";
    Picture myPic2 = new Picture(fileName2);
    myPic2.averageLuminence();



    String fileName3 = "C:/Users/Scott/Pictures/Pic/Painting.jpg";
    Picture myPic3 = new Picture(fileName3);
    myPic3.averageLuminence();


    String fileName4 = "C:/Users/Scott/Pictures/Pic/Snow.jpg";
    Picture myPic4 = new Picture(fileName4);
    myPic4.averageLuminence();


    String fileName5 = "C:/Users/Scott/Pictures/Pic/Walkway.jpg";
    Picture myPic5 = new Picture(fileName5);
    myPic5.averageLuminence();
    }


    public void bubbleSort(int[] arr)
    {
    boolean swapped = true;
    int j = 0;
    int tmp;
    while (swapped) {
    swapped = false;
    j++;
    for (int i = 0; i < arr.length - j; i++) {
    if (arr[i] > arr[i + 1]) {
    tmp = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = tmp;
    swapped = true;
    }
    }
    }
    }

    }

  7. #7
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Code for Array and Sorting

    Reread point 1 in my first post.

  8. #8
    Sync is offline Member
    Join Date
    Oct 2011
    Posts
    4
    Rep Power
    0

    Default Re: Code for Array and Sorting

    Whenever I try to return a value it says Void methods cannot return a value sorry for the trouble haha

  9. #9
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Code for Array and Sorting

    void means it does not return a value at all, it just means it does an operation.
    Here's an example of a void method:
    Java Code:
    voidMethod();
    
    public static void voidMethod(){
    System.out.println("Everything in here is done when voidMethod is called");
    }
    Here's an example of a method that returns something
    Java Code:
    System.out.println(returingMethod());
    
    public static int returningMethod() { //When you replace int with void, it means this method returns an integer, same thing goes for strings.
    int x = 7;
    return x;
    }
    ^^ The above will print out 7

    Hope this helps

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default offtopic

    Quote Originally Posted by Sync View Post
    I see yes that worked but instead of selection sort someone told me to use a bubble sort instead?
    Who said that? Barack Obama - Computer Science Question - YouTube
    DiamondSoul likes this.

Similar Threads

  1. Sorting Array High-Low
    By Brandon Seale in forum New To Java
    Replies: 2
    Last Post: 02-18-2011, 02:56 AM
  2. Sorting Array UI
    By Brandon Seale in forum New To Java
    Replies: 6
    Last Post: 02-18-2011, 02:50 AM
  3. Help with sorting an object array.
    By TommyLR in forum New To Java
    Replies: 1
    Last Post: 02-03-2011, 12:43 PM
  4. Array List Sorting
    By makpandian in forum New To Java
    Replies: 5
    Last Post: 11-14-2010, 03:33 AM
  5. Sorting Array
    By saqib15 in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •