aspose file tools*
The moose likes Beginning Java and the fly likes Returning only minimum value... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning only minimum value..." Watch "Returning only minimum value..." New topic
Author

Returning only minimum value...

Jason Graves
Greenhorn

Joined: Jun 08, 2006
Posts: 2
So here goes my first post. I am a Java noob, and will admit that I am 32, married w/2 kids, work 40 hrs a week, and am trying to figure out school work. I do NOT want a solution here, just a nudge in the right direction. From what I have read in the last week, HW assignments are not welcome here, so I won't ask. Any ways, here goes;

Platform: don't laugh (Win XP SP2 (90% of the time) but dual boot with Suse 10.0)
JDK: 1.5.0
Mission: to find out where I'm messing up my output.
IDE: Borland JBuilder and NetBeans, going back and forth right now.

I am trying to load an array of 10 ints and search the array for the smallest value, then return that value and print it out. The problem I am running into is that I CAN load my array, but when I try to System.out.println the smallest value, I get the whole array dumped. Like I said, this IS a school assignment, I am interested in learning Java, (I have well travelled copies the JAVA All-in-one-desk-reference for Dummies and SAMS Teach Yourself Programming with Java in 24 hrs sitting right next to me). My question is: I think I have a looping problem. I can only get this to output the variables I input (through the random generator). I don't want a solution, only maybe a push in the right direction. Thanks in advance for any help.


[ June 08, 2006: Message edited by: Jason Graves ]

Help me if you have the time.<br />Yell at me if I am being stupid.
Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
nice way of writing code specially the comments & annotation

but here are some problem at code level

1. if you put a println() in a loop it will obviously print multiple value instead of single smallest or largest
2. getMinimum() as the name suggest should return minimum value out of a bunch of value passed as parameter but you are only passing one value at a time in a loop(balance[x] return xth element of balance array)
3. getMinimum() logic is completely wrong and needs a complete rewrite
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch Jason.

I disagree with Subhajit Mitra about your minimum method being completely wrong. He is however right that you are passing a single number rather than the array. It is actually quite simple to pass an array instead and it doesn't need a lot of alteration in your code.

You can tell you are using the SAMS and Dummies books; they tend to put everything in the main method, which is OK for learning the syntax, but no good for learning object orientation. You can also tell you are using NetBeans, having a class called Main. The Main class and its main method are intended just for starting off your application, and should simply create an object of another class and set it off. Try this sort of thing:-I have put a * at the beginning of every line I have added; apart from deleting "static" before the getMinimum method, I have made no other changes.

Now what you have is a proper application

Main------------------->MinimumCalculator
[no fields]............................balances: int[]
+$main(String[]):void...+getMinimum(int):void
.......................................+go():void

This means you have your Main class which has a main method and no fields which calls your MinimumCalculator go method; the go method then calls its own get minimum method and prints it out.

And as Subhajit Mitra implies, you need to change your get minimum method to analyse the array.

CR

Have had to put .......... in the diagram above to keep the spacing.
Added: The + means public access, the $ means "static", and the word after the : means return type. I have msised out the consructor methods.
[ June 09, 2006: Message edited by: Campbell Ritchie ]
Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
Campbell is going to hate this

public class Main {
public static void main(String[] args) {
int min = 12, balances[]=new int[10];
for(int i = 0; i < 10; i++) {
balances[i] = (int)(Math.random() * (10) ) + 1;
System.out.print(balances[i]+", ");
}
for(int i = 0; i < 10; i++)min=(balances[i]<min)?balances[i]:min;
System.out.println("minimum is " + min);
}
}
fred Joly
Ranch Hand

Joined: Jan 19, 2006
Posts: 55
why do you say
Just say int min = balance[o] and then start your loop
at the second cell of the array.
This you you don't care about what migth be the biggest int in the array
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Campbell is going to hate this [pissed]

Oh, is that what the red face is called?
Jason Graves
Greenhorn

Joined: Jun 08, 2006
Posts: 2
Originally posted by Subhajit Mitra:
Campbell is going to hate this

public class Main {
public static void main(String[] args) {
int min = 12, balances[]=new int[10];
for(int i = 0; i < 10; i++) {
balances[i] = (int)(Math.random() * (10) ) + 1;
System.out.print(balances[i]+", ");
}
for(int i = 0; i < 10; i++)min=(balances[i]<min)?balances[i]:min;
System.out.println("minimum is " + min);
}
}



I even agree this looks messy. I have changed my program a bit, and still can generate randoms and load the array, but my System.outprintln always sends out 10. I think I am getting close, but no cigar yet.

This is what I have now.



I now realize this is a scope issue with my smallest or min variables. How do I fix this? I am this close.
Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
[Campbell] h, is that what the red face is called?


why don't you join my Smiley Thread at JavaRanch forum
Subhajit Mitra
Greenhorn

Joined: Jun 06, 2006
Posts: 21
you have got my first point right -- println should be outside loop. what about others

1. getMinimum still accept 1 single integer as (int n). how about accepting complete array as (int n[])
2. check whether you are sending all the value of balances to getMinimum. mind that when n=0 then balance[n] will always return 1st element of array. how about sending array as a whole.
3. finally getMinimum is still as bad as earlier. you have accepted n but did you ever used it inside getMinimum?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning only minimum value...