Results 1 to 12 of 12
Thread: need help with java project
- 02-07-2011, 02:20 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
need help with java project
hi. this is an assignment for java.
what im required to do is write a program to tabulate marks for trainees
what is required is more or less already done.
im only stuck with this part where it requires more than 1 trainee to enter the marks, then say if 2 trainees entered their marks, it should print out after they told the program to print.
heres what i've done so far:
what is troubling me is when i try to print out the results, it shows null for string fields like names, and 0 for integer or double fields.Java Code:// section 1 - code below imports packages from both utilities package and math package import java.util.*; import java.math.*; class MarksTabulation { public static void main ( String [] args) { Scanner in = new Scanner(System.in); //section 2 - variable declarations char enterAnother = 'Y'; int [] traineeNo = new int [20]; // note for self - allow 20 trainees int keyedNumberOfQuiz = 0; double [] noOfQuiz; double [] modTestMark = new double [20]; double [] averageMark = new double [20]; double [] overallMark = new double [20]; String [] familyName = new String [20]; String [] firstName = new String [20]; String [] moduleName = new String [20]; repeat: for (int noOfTrainees = 0; noOfTrainees < 20; noOfTrainees ++) { //section 3 - prints prompts for user to key in input System.out.print("Trainee Number: "); traineeNo [noOfTrainees] = in.nextInt(); //traineeNo limiter while (traineeNo [noOfTrainees] < 1000 || traineeNo [noOfTrainees] > 9999) { System.out.print("Invalid Trainee Number. Please key in a valid Trainee number: "); traineeNo [noOfTrainees] = in.nextInt(); } //continuation of section 3 System.out.print("Family Name: "); familyName [noOfTrainees] = in.next(); System.out.print("First Name: "); firstName [noOfTrainees] = in.next(); System.out.print("Instruction Module: \n 1. Forward Scanning Radar Display Repair\n 2. IR Beacon Maintenance\n 3. SAM Detection System Replacement\nEnter Module No.: "); int module = in.nextInt(); while (module > 3 || module < 1) { System.out.print("Invalid module number. Please key in a valid module number: "); module = in.nextInt(); } switch (module) { case 1: moduleName [noOfTrainees] = "Forward Scanning Radar Display Repair"; break; case 2: moduleName [noOfTrainees] = "IR Beacon Maintenance"; break; case 3: moduleName [noOfTrainees] = "SAM Detection System Replacement"; break; } System.out.print("How many quizes shall I calculate?: "); keyedNumberOfQuiz = in.nextInt(); //"number of quizzes" limiter while (keyedNumberOfQuiz < 1 || keyedNumberOfQuiz > 8) { System.out.print("Invalid number of quizes. Please key in a valid number: "); keyedNumberOfQuiz = in.nextInt(); } //section 4 - create an array noOfQuiz = new double [keyedNumberOfQuiz]; averageMark [noOfTrainees] = tabulateModuleMarks(noOfQuiz); System.out.print("Final Module Test mark: "); modTestMark [noOfTrainees] = in.nextDouble(); while (modTestMark [noOfTrainees] < 0 || modTestMark [noOfTrainees] > 100) { System.out.print("Invalid Final Module score. Please key in a valid score: "); modTestMark [noOfTrainees] = in.nextDouble(); } overallMark [noOfTrainees] = getOverallMark(averageMark [noOfTrainees],modTestMark [noOfTrainees]); System.out.println("\n"); averageMark [noOfTrainees] = roundingOff1Decimal(averageMark [noOfTrainees]); overallMark [noOfTrainees] = roundingOff1Decimal(overallMark [noOfTrainees]); System.out.print("Another [Y/N]: "); enterAnother = in.next().charAt(0); if (enterAnother == 'N') { break repeat; } } //section 5 - print results for (int noOfTrainees = 0; noOfTrainees < 20; noOfTrainees ++) { printResults(traineeNo [noOfTrainees] ,firstName [noOfTrainees],familyName [noOfTrainees],moduleName [noOfTrainees],averageMark [noOfTrainees],modTestMark [noOfTrainees],overallMark [noOfTrainees]); } } //section 6 - my own methods and their purposes static void printResults (int traineeNo, String familyName, String firstName, String moduleName, double averageMark, double modTestMark, double overallMark) { System.out.println("Results for " + familyName + " " + firstName); System.out.println("Trainee Number" + " " + traineeNo); System.out.println("Module: " + moduleName); System.out.print("Average Module Quiz: " + averageMark + " "); System.out.print("Final Module Test: " + modTestMark + " "); System.out.println("Overall mark: " + overallMark + " "); if ( overallMark >= 70 ) { System.out.println("Letter Grade: A"); } else if (overallMark >= 60 && overallMark < 70) { System.out.println("Letter Grade: B"); } else if (overallMark >= 50 && overallMark < 60) { System.out.println("Letter Grade: C"); } else if (overallMark >= 40 && overallMark < 50) { System.out.println("Letter Grade: D"); } else if (overallMark >= 35 && overallMark < 40) { System.out.println("Letter Grade: E"); } } static double tabulateModuleMarks (double [] noOfQuiz) { Scanner in = new Scanner(System.in); double sum = 0; for (int x = 0; x < noOfQuiz.length; x++) { System.out.print("Module "+(x+1)+" Quiz Mark: "); noOfQuiz [x] = in.nextDouble(); while (noOfQuiz [x] < 0 || noOfQuiz [x] > 100) { System.out.print("Invalid module " + (x+1) + " score. Please key in a valid score(1-100): "); noOfQuiz [x] = in.nextDouble(); } sum = sum + noOfQuiz [x]; } return sum/(noOfQuiz.length); } static double getOverallMark (double averageMark, double modTestMark) { double modQuizWeighting; double modTestWeighting; double overallMark; modQuizWeighting = averageMark/100 *20; modTestWeighting = modTestMark/100 *80; return (modQuizWeighting + modTestWeighting); } static double roundingOff1Decimal (double decimal) { decimal = decimal * 10; return Math.rint(decimal) / 10; } }
hope im posting it right. if not pls let me know how. and thanks for any kind help provided.Last edited by doxycycline; 02-07-2011 at 03:03 PM. Reason: added code tag
- 02-07-2011, 02:53 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, please use code tags next time when you are posting code segments in the forum. Unformatted codes are really hard to read.
- 02-07-2011, 02:55 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Regarding your question, as you said you can workout with a one user. Then think about that how you keep those data.
Then if you want to do the same process again, with different user details, work around with loops.
- 02-07-2011, 03:01 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
yea. i have a loop to collect the inputs in arrays. then i have a for loop to print them out. but when i printed them out, the string inputs become nulls and the integer or double inputs become 0s.
i tried printing 1 or 2 sets of inputs without the for loop and it works. but if the for loop is there it doesnt work already.
n sorry about the code tag.
- 02-07-2011, 03:24 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Before that here,
what happen if a user enter 'n', rather 'N' ?Java Code:if (enterAnother == 'N') { break repeat; }
- 02-07-2011, 03:28 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Carefully look at the following code segment,
Look at the loop validation. You always expecting 20 entries, and if you enter only one record you can see last 19 records with default values, such as null and all as you said.Java Code://section 5 - print results for (int noOfTrainees = 0; noOfTrainees < 20; noOfTrainees++) { printResults(traineeNo[noOfTrainees], firstName[noOfTrainees], familyName[noOfTrainees], moduleName[noOfTrainees], averageMark[noOfTrainees], modTestMark[noOfTrainees], overallMark[noOfTrainees]); }
- 02-07-2011, 03:28 PM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Are you clear with what I'm said?
- 02-07-2011, 03:33 PM #8
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
oh yes thanks. because of that i cannot see the top 10 entries in BlueJ. in fact my code has no error. its because BlueJ didnt display the top 10 entries. I hav set a counter in the loop which gets inputs, and let the for loop refer to the counter. so it will only print out the number of entries.
as for your previous question, if u enter N or n, it should break the main for loop which gets inputs. but i only know how to set it to break the loop with N or n, and not both. erm. do u hav any hints to it?
thanks a lot for your help.
- 02-07-2011, 03:43 PM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For the terminating part you can use logical OR with the both combination. But it's not the best way.
- 02-07-2011, 03:47 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
For the for loop thing, never user constant values unless you can configure values. You should validate with updated values. Actually with arrays you cannot do that, with ArrayList you can handle dynamic updates, users in your case.
- 02-07-2011, 03:53 PM #11
Member
- Join Date
- Feb 2011
- Posts
- 4
- Rep Power
- 0
thanks a lot =) i have managed to settle that problem using else if. thanks again for your help.
- 02-07-2011, 04:05 PM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You are welcome.
If you've solved the problem please mark the thread solved.
Similar Threads
-
Help with Java Project
By j@v@ in forum New To JavaReplies: 1Last Post: 12-03-2010, 04:37 AM -
Need a little help with a java project.
By JustJava in forum New To JavaReplies: 5Last Post: 10-21-2009, 05:39 AM -
Use Java 2 Project in different IDE
By ramzansadiq in forum EclipseReplies: 8Last Post: 02-15-2009, 04:33 PM -
java project
By gvdn in forum Advanced JavaReplies: 4Last Post: 07-13-2008, 06:45 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks