Results 1 to 7 of 7
Thread: Nearly there but really not :(
- 04-25-2009, 02:35 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Nearly there but really not :(
Hi,
This is Preet. I can't seem to figure out how to get the total number of students who did not pass. I have tried a for loop which has an if statement in it but that does not work. I think i am very close to getting the right output. Please guide me through that part so that i can figure out my mistake. I have pasted the whole question and my code at the bottom.
Thanks :)
Question:
Write a program called Marks that calculates the final result for students studying a subject. The program prompts the user for an input file name containing information about each student. Information on each student consists of six lines as follows:
student name can be more than one word
progress check mark a double, maximum 5.0 (non-exam mark)
assignment 1 mark an integer, maximum 10 (non-exam mark)
assignment 2 mark a double, maximum 10.0 (non-exam mark)
laboratory test mark an integer, maximum 15 (non-exam mark)
written exam mark an integer, maximum 60 (exam mark)
These six lines make up what is known as a record (so if there is a student name in the file, there will be five other lines after that name). The file may contain any number of records. An example input file containing two records follows:
Stu Dent
3.1
8
4.2
14
48
A Nother One
4.2
9
8.5
15
25
Remember your program must be able to read a file containing any number of records. The program repeatedly reads the information for each student from the file (one record of six lines) and displays each student’s result.
• To pass the subject a student must get at least 50% overall (i.e. 50 marks when all the marks are added up) and pass the non-exam and exam hurdles.
• If a student gets at least 50% overall but gets less than 50% in their total non-exam marks, they have not passed the non-exam hurdle. Their result is reported as SAH-A (which means they must do another assignment).
• If a student gets at least 50% overall but gets less than 45% in their written exam mark, they have not passed the exam hurdle. Their result is reported as SAH-E (which means they must do another exam).
• If a student gets less than 50% overall then the student must repeat the subject.
The program should display three things for each student; their name, their mark and whether they passed or received an SAH-A or an SAH-E or must repeat the subject.
After all the records have been read and each student’s result has been displayed to screen, the program should also display the highest and lowest overall mark obtained and the number of students who did not pass. Make your output to screen clear and use meaningful messages.
Code:
import java.util.*;
import java.io.*;
public class Marks
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter file name: ");
String fileName = keyboard.nextLine();
File fileOpen = new File(fileName);
Scanner fileInput = new Scanner(fileOpen);
double max = 0;
double min = 100;
double totNotPas = 0; //initialize num of stu not pass
while(fileInput.hasNextLine())
{
String name = fileInput.nextLine();
double progMark = fileInput.nextDouble();
int asgnMark1 = fileInput.nextInt();
double asgnMark2 = fileInput.nextDouble();
int labMark = fileInput.nextInt();
int wrtMark = fileInput.nextInt();
fileInput.nextLine();
double nonEMark = (progMark + asgnMark1 + asgnMark2 + labMark) / 40 * 100;
double totalMarks = wrtMark + progMark + asgnMark1 + asgnMark2 + labMark;
System.out.println("Name: " + name);
if (totalMarks < 50)
{
System.out.println("Mark: " + totalMarks + "%");
System.out.println("Repeat the subject");
}
else if (totalMarks >= 50 && nonEMark < 50)
{
System.out.println("Mark: " + totalMarks + "%");
System.out.println("SAH-A");
}
else if (totalMarks >= 50 && wrtMark < 45)
{
System.out.println("Mark: " + totalMarks + "%");
System.out.println("SAH-E");
}
else
{
System.out.println("Mark: " + totalMarks + "%");
System.out.println("Passed");
}
System.out.println();
if (max < totalMarks)
{
max = totalMarks;
}
if (min > totalMarks)
{
min = totalMarks;
}
// num of stu who did not pass
for (double counter = 0; counter <= fileInput; counter++) //get num of records then get not pass
{
if (totalMarks < 50 || nonEMark < 50) //this asks for not pass
{
counter = totNotPas; //gives num of not pass
}
}
}
System.out.println("Highest overall mark: " + max + "%");
System.out.println("Lowest overall mark: " + min + "%");
System.out.println("Number of students did not pass: " + totNotPas + "%"); //print out not pass stu num
}
}
Reply soon :D
- 04-25-2009, 05:04 PM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
why using counter <= fileInput?Java Code:for (double counter = 0; counter <= fileInput; counter++)
fileInput is not double, int... cant compare
and i think... counter should in "int" type
...
and i found that the for loop seems not Necessary, as it is in the while loop...
as following and no for loop is needed
Java Code:while(fileInput.hasNextLine()) { // many code if (totalMarks < 50 || nonEMark < 50) //this asks for not pass totNotPas++; }
- 04-26-2009, 04:00 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Thanks alot mate :)
It's solves my problem further.
But i had to change totNotPass to an int and initialize totNotPass = 1 instead of 0 because the output was one less than expected.
It all works now. I just have to figure out how to limit decimal to two decimal points only.
Thanks for the help mate
TC :D
- 04-26-2009, 04:06 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
create a DecimalFormat("#.##") and format the double and make it into a string, then convert it back to a double as needed use Double.parseDouble(String).
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 04-26-2009, 05:05 AM #5
I think he just needs to print it out to two spaces. He can do that using the printf method to print it out to 2 decimal places. That doesn't take care of the rounding part, however.
You need something like
Mr. BeansJava Code:public static double round(double grade, int decimalPlaces) { double toRound = Math.pow(10,decimalPlaces); grade = grade * toRound; double tmp = Math.round(grade); return tmp/toRound; } System.out.printf("%.2f", round( numberToRound ) );
- 04-26-2009, 05:18 AM #6
Member
- Join Date
- Apr 2009
- Posts
- 3
- Rep Power
- 0
Thanks for that :)
Prob solved mate :D
- 04-26-2009, 06:39 AM #7
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks