Results 1 to 20 of 22
- 11-02-2008, 02:20 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
-
is this line called:
before the sales data has been entered into the sales array? If so, please understand that sales[0] and in fact sales[i] for any valid i will be zero. You will want to likely initialize your min and max variables just before your for loop and certainly after your sales array has been filled with the appropriate data.Java Code:min = sales[0];
- 11-02-2008, 02:35 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Can you explain a little further?
Do I set min to 0 when I declare it a double?
Would I have to change min = sales[0]?
Thanks for your help.
-
Here, look at and run this example. It should be self-explanatory:
Java Code:import java.util.Arrays; class Fubar { private static final int ARRAY_SIZE = 5; public static void main(String[] args) { int[] intArray = new int[ARRAY_SIZE]; int min1 = intArray[0]; // initialize min1 *before* filling the array int min2; // fill the array for (int i = 0; i < intArray.length; i++) { intArray[i] = 5 + i; } min2 = intArray[0]; // initialize min2 *after* filling the array for (int i = 0; i < intArray.length; i++) { // check for mins here if (intArray[i] < min1) { min1 = intArray[i]; } if (intArray[i] < min2) { min2 = intArray[i]; } } System.out.println("min1 = " + min1); System.out.println("min2 = " + min2); System.out.println("intArray = " + Arrays.toString(intArray)); } }
- 11-02-2008, 05:35 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Thanks for ur help.
Last edited by Sophiie; 11-16-2008 at 11:16 PM.
- 11-02-2008, 06:52 PM #6
To debug your code, add: System.out.println("min=" +min); inside your loop to show the value of min during every loop. Then you will see why min is always 0!
Hint:
When searching for min/max values, set the initial values of those variables to the end most value in the other direction. max = smallest possible value, min = largest possible valueLast edited by Norm; 11-02-2008 at 09:43 PM.
- 11-02-2008, 09:35 PM #7
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
am sorry sophiie i need the whole program ,I just started learning about arrays too and realy want to know whats wrong with ur program..
- 11-03-2008, 12:36 AM #8
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Thank you Norm!!! The hint was exactly what I needed to figure this thing out!! Thanks again.
Thanks for everyone that tried to help! :)
Still, the second part of my help is not addressed. The min and max values are in reference to an array also. How do I make the min and max values in reference to that certain #?
1 - 23 min --- In the print out I want to say that 1 has the lowest value
but cant get it to work, also with max.
2 - 32
3 - 55 max
4 - 31
The array was initialized very early on and Im not sure how to make reference to it now with the min and max values.
Thanks for the help!! :)
- 11-03-2008, 02:29 AM #9
Not sure what that means? Are you asking how to have min and max be indexes into the array with the values vs containing the values?make the min and max values in reference to that certain #?
One way: put the initial min(big number) and max(small number) values into the array before putting in the other values to be searched and set the min and max indexes accordingly. Then start any searchs at index = 2 vs 0.
- 11-03-2008, 02:43 AM #10
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
My program is about having a sales amount entered for several employees. When running the program, it prompts for the user to enter the sales amount for each employee. Then I needed to find the max and min sales and print that with the employee that it is in reference to. If employee 1 made the highest sales, I want it to print that that employee made the highest. If employee 4 made the lowest sales, I want it to print that employee made the lowest sales also while printing the sales amount they made. I've tried but I still dont understand what to do.
Thanks for the help!
- 11-03-2008, 03:04 AM #11
You need a way of keeping each employee's data together. Create a small class to hold the data for each employee. As you read in the data, create an instance of that class with the data and store the instance in the array.
- 11-03-2008, 04:26 AM #12
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Would this class be the one that goes without the public static void main(String[] args) statement?
I am recently starting to learn about programs with more than 1 class.
Can you explain to me how programs work with more than 1 class?
I hope I am not asking to much. I really appreciate all the help given!! It's a life safer!
Thanks again, I will get to my project.
- 11-03-2008, 12:47 PM #13
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
no ur program runs either way with or without [public static void main(String[] args) statement]Would this class be the one that goes without the public static void main(String[] args) statement?
u can put ur program inside a method in one class and call it by the main class 4 exampleCan you explain to me how programs work with more than 1 class?
this is the main class which will use the fields & methods in class AccountJava Code:class Account { String name; double balance; void display() { System.out.print(name); System.out.print(" has $"); System.out.print(balance); } double getInterest(double percentageRate) { return balance * percentageRate / 100.00; } }
i hope it helped u:)Java Code:class UseAccount { public static void main(String args[]) { Account myAccount = new Account(); Account yourAccount = new Account(); myAccount.name = "gamilah"; //here we used the name field in Account class myAccount.balance = 24.02; yourAccount.name = "sophiie"; yourAccount.balance = 55.63; myAccount.display(); //we used display method in Account class System.out.print(" plus $"); System.out.print(myAccount.getInterest(5.00)); System.out.println(" interest "); yourAccount.display(); double yourInterestRate = 7.00; System.out.print(" plus $"); double yourInterestAmount = yourAccount.getInterest(yourInterestRate); System.out.print(yourInterestAmount); System.out.println(" interest "); } }
- 11-03-2008, 10:37 PM #14
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Im still figuring it all out but I really do appreciate you helping me out!! Thanks for your time and effort put into helping me!
- 11-04-2008, 12:01 AM #15
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Aaah, I cant get it to work. I have been following different examples in the effort to do it myself but I dont know what Im doing wrong. Where would I start? Which names and methods would I have to use for the second class to hold the data?
Thanks for the help!
- 11-04-2008, 12:29 AM #16
Member
- Join Date
- Oct 2008
- Posts
- 19
- Rep Power
- 0
don't mention it ,just tell me if u need a simpler example
for now i can tell u that any class or a methos is like a car if u don't call it it still standing once u call it (drive it) it moves
so always always put ur program in a separate class and in the main class the object that calls that class
[/CODE]Java Code:public class Print{//this class alone is like a stopping car public void toPrint(){ System.out.println("I used Print class"); } } [CODE] public class UsePrint{ public static void main(String[]args){ Print x= new Print();//here the car starts moving x.toPrint(); } }
i hope its clear now:)
- 11-04-2008, 07:55 AM #17
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
I know the min max thing was an exercise for school, but in "real life" there is an easier way.
Java Code:int[] bogus = { 4, 6, 1, 5, 3 }; Arrays.sort(bogus); System.out.println("min: " + bogus[0]); System.out.println("max: " + bogus[bogus.length - 1]);
- 11-05-2008, 02:03 AM #18
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Thank you very much for your help.
Back to making references...um, I dont think Im suppose to create another class to hold the data...Is there another way to do it?
Im refering to the part where I have to print the sales amount and the employee it is in reference to.
THANKS!!
- 11-05-2008, 02:26 AM #19
If you have more than one value to save for each employee, you could have parallel arrays to hold the data. The data for each employee would be found at the same index in each array.
- 11-05-2008, 02:35 AM #20
Member
- Join Date
- Nov 2008
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Help in Printing
By kirly in forum Advanced JavaReplies: 3Last Post: 10-03-2011, 03:40 PM -
Printing Example
By Java Tip in forum SWTReplies: 0Last Post: 07-11-2008, 04:41 PM -
The highest number
By I-Shine in forum Java AppletsReplies: 3Last Post: 02-13-2008, 05:05 AM -
[Java Printing]
By remnahush in forum Advanced JavaReplies: 1Last Post: 11-08-2007, 03:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks