Results 1 to 10 of 10
Thread: Code for Array and Sorting
- 10-28-2011, 08:35 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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 08:41 AM.
- 10-28-2011, 02:24 PM #2
Re: Code for Array and Sorting
Well, first of all you'll want to wrap that method in a class, otherwise you can't even try to compile it.This is what I have so far and im stuck
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-28-2011, 03:43 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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.
- 10-28-2011, 04:52 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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
- 10-28-2011, 05:34 PM #5
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
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
- 10-29-2011, 10:42 AM #6
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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;
}
}
}
}
}
- 10-29-2011, 08:46 PM #7
Member
- Join Date
- Oct 2011
- Posts
- 83
- Rep Power
- 0
Re: Code for Array and Sorting
Reread point 1 in my first post.
- 10-30-2011, 12:11 AM #8
Member
- Join Date
- Oct 2011
- Posts
- 4
- Rep Power
- 0
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
- 10-30-2011, 02:10 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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:
Here's an example of a method that returns somethingJava Code:voidMethod(); public static void voidMethod(){ System.out.println("Everything in here is done when voidMethod is called"); }
^^ The above will print out 7Java 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; }
Hope this helps
- 10-30-2011, 02:22 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
offtopic
Who said that? Barack Obama - Computer Science Question - YouTube
Similar Threads
-
Sorting Array High-Low
By Brandon Seale in forum New To JavaReplies: 2Last Post: 02-18-2011, 01:56 AM -
Sorting Array UI
By Brandon Seale in forum New To JavaReplies: 6Last Post: 02-18-2011, 01:50 AM -
Help with sorting an object array.
By TommyLR in forum New To JavaReplies: 1Last Post: 02-03-2011, 11:43 AM -
Array List Sorting
By makpandian in forum New To JavaReplies: 5Last Post: 11-14-2010, 02:33 AM -
Sorting Array
By saqib15 in forum New To JavaReplies: 1Last Post: 02-12-2010, 03:42 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks