Netbeans help(Java coding for HNC)

Associate
Joined
22 May 2004
Posts
1,792
Location
N.Ireland
Hi guys am doing an assigment for my part time HNC and have a few problems. Could someone look over the code for me. Here it is!

/*
* ExamResultsPc2.java
*
* Created on 13 January 2008, 14:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package pkgExamResultsPc2;
import keyb.InOut;
/**
*
* @author G
*/
public class ExamResultsPc2 {


//========================================================== //
// NAME : initialiseArrays
// RETURN TYPE : none
// PARAMETERS : mathsMark: maths student's test score
// engMark : english student's test score
// mathsID : maths student's unique ID number
// engID : english student's unique ID number
// DESCRIPTION : Initialises the maths and english studentID
// and exam mark arrays with the data provided
//
// //==========================================================
static void initialiseArrays(int[] mathsMark, int[] engMark, String[] mathsID ,String[] engID)
{
//Assign data to mathsExamMark array
mathsMark[0]=72;
mathsMark[1]=84;
mathsMark[2]=21;
mathsMark[3]=48;
mathsMark[4]=55;
mathsMark[5]=49;
mathsMark[6]=78;
mathsMark[7]=21;
mathsMark[8]=35;
mathsMark[9]=98;
mathsMark[10]=44;
mathsMark[11]=56;

//Assign data to engExamMark array
engMark[0]=82;
engMark[1]=56;
engMark[2]=34;
engMark[3]=42;
engMark[4]=71;
engMark[5]=55;
engMark[6]=67;
engMark[7]=34;
engMark[8]=28;
engMark[9]=59;
engMark[10]=47;
engMark[11]=65;
engMark[12]=52;
engMark[13]=12;
engMark[14]=74;

//Assign data to mathsStudentID array
mathsID[0]="M1005";
mathsID[1]="M1002";
mathsID[2]="M1000";
mathsID[3]="M1003";
mathsID[4]="M1001";
mathsID[5]="M1004";
mathsID[6]="M1006";
mathsID[7]="M1010";
mathsID[8]="M1009";
mathsID[9]="M1008";
mathsID[10]="M1007";
mathsID[11]="M1011";

//Assign data to engStudentArray
engID[0]="E2003";
engID[1]="E2005";
engID[2]="E2001";
engID[3]="E2002";
engID[4]="E2006";
engID[5]="E2000";
engID[6]="E2004";
engID[7]="E2008";
engID[8]="E2007";
engID[9]="E2010";
engID[10]="E2009";
engID[11]="E2012";
engID[12]="E2011";
engID[13]="E2013";
engID[14]="E2014";

} //End of InitialiseArrays method


//==========================================================
//
// NAME : displayMenu
// RETURN TYPE : none
// PARAMETERS : none
// DESCRIPTION : Displays a menu from which the user can select
// various options.
//
// //==========================================================
static void displayMenu()
{
System.out.println("\n\n\tXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
System.out.println("\tX STUDENT RECORDS MAIN MENU X");
System.out.println("\tXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
System.out.println("\n\n1. - All Mathematics students");
System.out.println("\n2. - All English students sorted by Student ID");
System.out.println("\n3. - Display English Merit students");
System.out.println("\n4. - Display the average examination marks");
System.out.println("\n5. - All students above the Mathematics average");
System.out.println("\n6. - EXIT");

} //End of displayMenu method




//==========================================================
//
// NAME : displayStudents
// RETURN TYPE : none
// PARAMETERS : studentID : student's unique ID number
// studentMark : student's test score
// size : size of the array
// upperLimitMark : upper inclusion mark in display
// lowerLimitMark : lower inclusion mark in display
// category : Displays the category of display
// DESCRIPTION : Displays all students by ID alongside
// their corresponding exam marks, as well as
// identifying the group being displayed.
//
// //==========================================================
static void displayStudents(String[] studentID, int[] studentMark, int size, int upperLimitMark, int lowerLimitMark, String category)
{
//Display headings
System.out.println("\n "+category+"\n");
System.out.println("Student Identity\t Student");
System.out.println(" Number\t\tExam Mark\n");

//Use for loop to display students
for (int count=0; count<=size-1; count++)
{
//Use if statement to determine students
//who meet the criteria
if (studentMark[count]>=lowerLimitMark && studentMark[count]<upperLimitMark)
{
System.out.println(" "+studentID[count]+"\t\t "+studentMark[count]);
} //End of if statement

} //End of for loop

} //End of displayStudents method




//==========================================================
//
// NAME : sortStudents
// RETURN TYPE : none
// PARAMETERS : studentID : maths student's unique ID number
// studentMark : maths student's test score
// size : size of the array
// DESCRIPTION : sorts students in ascending order of student ID
//
// //==========================================================
static void sortStudents(String[] studentID, int[] studentMark, int size)
{
//The outer for loop steps through the sequence
//so the 2nd is copared to all others, then the 3rd and so on.
for(int count=0; count<size-2; count++)
{
//Use inner for loop to compare 1st student ID
//with all the others and obtain the first student ID
//in ascending order.
for (int index=(count+1); index<size-1; index++)
{
//Use if statement to compare, and swap student IDs if required
if (studentID[count].compareTo(studentID[index])>0)
{
String tempID=studentID[count];
studentID[count]=studentID[index];
studentID[index]=tempID;
} //End of if statement
} //End of inner for loop
} //End of outer for loop
} //End of sortStudents method


//========================================================== //
// NAME : averageResult
// RETURN TYPE : int
// PARAMETERS : studentID : maths student's unique ID number
// studentMark: maths student's test score
// size : size of the array
// DESCRIPTION : sorts students in ascending order of student ID
//
// //==========================================================
static double averageResult(int[] studentMark, int size)
{
//Declare variables
double totalMark=0, averageMark=0;

//Use for loop to calculate sum of marks
for (int count=0; count<=size-1; count++)
{
totalMark=studentMark[count]+totalMark;
} //End of for loop
averageMark=totalMark/size;
return averageMark;
} //End of averageResult method


//========================================================== //
// NAME : main
// RETURN TYPE : none
// PARAMETERS : args : [DEFAULT]
// DESCRIPTION : Provides the user with a menu from which they can
// select the processes they wish to perform.
// The selections allow the user to:
// Display all Maths students
// Sort and display all English students by student ID
// Display all English Merit students
// Display the average exam marks by subject
// Display all students above the Maths average
//
// //==========================================================
public static void main(String[] args)
{
//Declare arrays for student IDs and exam marks
int [] mathsExamMark=new int[12];
int [] engExamMark=new int[15];
String[] mathsStudentID=new String[12];
String[] engStudentID=new String[15];

//Initialise all student and mark arrays
initialiseArrays(mathsExamMark, engExamMark, mathsStudentID, engStudentID);

//Declare variables
int menuOption=0;
boolean menuExit=false;
int mathsArraySize=mathsStudentID.length;
int englishArraySize=engStudentID.length;

//Use while loop to display menu until exit is selected
while (menuExit!=true)
{
displayMenu();

//Prompt for menu option selection
System.out.println("\n\nPlease select a menu option number to continue.");
menuOption=InOut.readInt();

//Use while loop to ensure valid option selected
while(menuOption<1 || menuOption>6)
{
System.out.println("\n\n+++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("+ INVALID! - selection should be in the range 1 - 6! +");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++\n");
displayMenu();
System.out.println("\nPlease select a menu option number to continue.");
menuOption=InOut.readInt();
} //End of while loop for invalidity

switch (menuOption)
{
case 1: displayStudents(mathsStudentID, mathsExamMark, mathsArraySize, 100, 1, "All mathematics students\n ************************");
break;

case 2: sortStudents(engStudentID, engExamMark, englishArraySize);
displayStudents(engStudentID, engExamMark, englishArraySize, 100, 1, "English students in Ascending order\n ***********************************");
break;

case 3: displayStudents(engStudentID, engExamMark, englishArraySize, 70, 55, "English Merit students\n **********************");
break;

case 4: averageResult(mathsExamMark, mathsArraySize);
System.out.println("\nThe Mathematics average is "+averageResult(mathsExamMark, mathsArraySize));
averageResult(engExamMark, englishArraySize);
System.out.println("\nThe English average is "+averageResult(engExamMark, englishArraySize));
break;

case 5: averageResult(mathsExamMark, mathsArraySize);
displayStudents(mathsStudentID, mathsExamMark, mathsArraySize, 100, averageResult(mathsExamMark,mathsArraySize)+1, "Mathematics students above the average\n **************************************");
break;

case 6: menuExit=true;
break;

} //End of switch statement

} //End of while loop for main menu


} //End of main() method

}

I cant get the avervage mark to work. It runs when using INT. But not with double which is what you need for average values.

Also is it possible to get my exit menu to exit netbeans altogether. Dont have to have this but would like the exit option to ask if I want to just stop running the program or do I want to close netbeans alltogether? Is this possible to get a program to tell netbeans to exit.

Also if someone has the time could they add anyideas for how to clean up the code? Shorter ways to do things etc?

It will be greatly appricated
 
Also is it possible to get my exit menu to exit netbeans altogether. Dont have to have this but would like the exit option to ask if I want to just stop running the program or do I want to close netbeans alltogether? Is this possible to get a program to tell netbeans to exit.

Do you even know what Netbeans is? I have a feeling you wouldn't be asking this question if you did.
 
Do you even know what Netbeans is? I have a feeling you wouldn't be asking this question if you did.

Ok so I take it isnt possible then. And to answer that question. All i know its a place I type code and it compiles them and runs them.. But thanks for your brilliant insight and help.

So anyone here want to help or just take the mick?
 
How well do you know Java? Are you being taught it at Uni/College at the moment or are you self-learning, you coding style seems quite erratic and prodecural which makes it hard to read. We can help but I don't want to get to complex if you only have a basic understanding of Java.
 
Ok so I take it isnt possible then. And to answer that question. All i know its a place I type code and it compiles them and runs them.. But thanks for your brilliant insight and help.

So anyone here want to help or just take the mick?

I was being serious about using code tags – without formatting, that code you posted is incredibly difficult to read, especially given its volume.

For anyone else who happens to look at this, here's the formatted code (courtesy of Visual Studio):
Code:
/*
* ExamResultsPc2.java
*
* Created on 13 January 2008, 14:14
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package pkgExamResultsPc2;
import keyb.InOut;
/**
*
* @author G
*/
public class ExamResultsPc2
{
    //================================================== ======== //
    // NAME : initialiseArrays
    // RETURN TYPE : none
    // PARAMETERS : mathsMark: maths student's test score
    // engMark : english student's test score
    // mathsID : maths student's unique ID number
    // engID : english student's unique ID number
    // DESCRIPTION : Initialises the maths and english studentID
    // and exam mark arrays with the data provided
    //
    // //================================================== ========
    static void initialiseArrays(int[] mathsMark, int[] engMark, String[] mathsID, String[] engID)
    {
        //Assign data to mathsExamMark array
        mathsMark[0] = 72;
        mathsMark[1] = 84;
        mathsMark[2] = 21;
        mathsMark[3] = 48;
        mathsMark[4] = 55;
        mathsMark[5] = 49;
        mathsMark[6] = 78;
        mathsMark[7] = 21;
        mathsMark[8] = 35;
        mathsMark[9] = 98;
        mathsMark[10] = 44;
        mathsMark[11] = 56;

        //Assign data to engExamMark array
        engMark[0] = 82;
        engMark[1] = 56;
        engMark[2] = 34;
        engMark[3] = 42;
        engMark[4] = 71;
        engMark[5] = 55;
        engMark[6] = 67;
        engMark[7] = 34;
        engMark[8] = 28;
        engMark[9] = 59;
        engMark[10] = 47;
        engMark[11] = 65;
        engMark[12] = 52;
        engMark[13] = 12;
        engMark[14] = 74;

        //Assign data to mathsStudentID array
        mathsID[0] = "M1005";
        mathsID[1] = "M1002";
        mathsID[2] = "M1000";
        mathsID[3] = "M1003";
        mathsID[4] = "M1001";
        mathsID[5] = "M1004";
        mathsID[6] = "M1006";
        mathsID[7] = "M1010";
        mathsID[8] = "M1009";
        mathsID[9] = "M1008";
        mathsID[10] = "M1007";
        mathsID[11] = "M1011";

        //Assign data to engStudentArray
        engID[0] = "E2003";
        engID[1] = "E2005";
        engID[2] = "E2001";
        engID[3] = "E2002";
        engID[4] = "E2006";
        engID[5] = "E2000";
        engID[6] = "E2004";
        engID[7] = "E2008";
        engID[8] = "E2007";
        engID[9] = "E2010";
        engID[10] = "E2009";
        engID[11] = "E2012";
        engID[12] = "E2011";
        engID[13] = "E2013";
        engID[14] = "E2014";
    } //End of InitialiseArrays method

    //================================================== ========
    //
    // NAME : displayMenu
    // RETURN TYPE : none
    // PARAMETERS : none
    // DESCRIPTION : Displays a menu from which the user can select
    // various options.
    //
    // //================================================== ========
    static void displayMenu()
    {
        System.out.println("\n\n\tXXXXXXXXXXXXXXXXXXXXXXXX XXXXX");
        System.out.println("\tX STUDENT RECORDS MAIN MENU X");
        System.out.println("\tXXXXXXXXXXXXXXXXXXXXXXXXXXXX X");
        System.out.println("\n\n1. - All Mathematics students");
        System.out.println("\n2. - All English students sorted by Student ID");
        System.out.println("\n3. - Display English Merit students");
        System.out.println("\n4. - Display the average examination marks");
        System.out.println("\n5. - All students above the Mathematics average");
        System.out.println("\n6. - EXIT");
    } //End of displayMenu method

    //================================================== ========
    //
    // NAME : displayStudents
    // RETURN TYPE : none
    // PARAMETERS : studentID : student's unique ID number
    // studentMark : student's test score
    // size : size of the array
    // upperLimitMark : upper inclusion mark in display
    // lowerLimitMark : lower inclusion mark in display
    // category : Displays the category of display
    // DESCRIPTION : Displays all students by ID alongside
    // their corresponding exam marks, as well as
    // identifying the group being displayed.
    //
    // //================================================== ========
    static void displayStudents(String[] studentID, int[] studentMark, int size, int upperLimitMark, int lowerLimitMark, String category)
    {
        //Display headings
        System.out.println("\n " + category + "\n");
        System.out.println("Student Identity\t Student");
        System.out.println(" Number\t\tExam Mark\n");

        //Use for loop to display students
        for (int count = 0; count <= size - 1; count++)
        {
            //Use if statement to determine students
            //who meet the criteria
            if (studentMark[count] >= lowerLimitMark && studentMark[count] < upperLimitMark)
            {
                System.out.println(" " + studentID[count] + "\t\t " + studentMark[count]);
            } //End of if statement

        } //End of for loop

    } //End of displayStudents method

    //================================================== ========
    //
    // NAME : sortStudents
    // RETURN TYPE : none
    // PARAMETERS : studentID : maths student's unique ID number
    // studentMark : maths student's test score
    // size : size of the array
    // DESCRIPTION : sorts students in ascending order of student ID
    //
    // //================================================== ========
    static void sortStudents(String[] studentID, int[] studentMark, int size)
    {
        //The outer for loop steps through the sequence
        //so the 2nd is copared to all others, then the 3rd and so on.
        for (int count = 0; count < size - 2; count++)
        {
            //Use inner for loop to compare 1st student ID
            //with all the others and obtain the first student ID
            //in ascending order.
            for (int index = (count + 1); index < size - 1; index++)
            {
                //Use if statement to compare, and swap student IDs if required
                if (studentID[count].compareTo(studentID[index]) > 0)
                {
                    String tempID = studentID[count];
                    studentID[count] = studentID[index];
                    studentID[index] = tempID;
                } //End of if statement
            } //End of inner for loop
        } //End of outer for loop
    } //End of sortStudents method

    //================================================== ======== //
    // NAME : averageResult
    // RETURN TYPE : int
    // PARAMETERS : studentID : maths student's unique ID number
    // studentMark: maths student's test score
    // size : size of the array
    // DESCRIPTION : sorts students in ascending order of student ID
    //
    // //================================================== ========
    static double averageResult(int[] studentMark, int size)
    {
        //Declare variables
        double totalMark = 0, averageMark = 0;

        //Use for loop to calculate sum of marks
        for (int count = 0; count <= size - 1; count++)
        {
            totalMark = studentMark[count] + totalMark;
        } //End of for loop
        averageMark = totalMark / size;
        return averageMark;
    } //End of averageResult method

    //================================================== ======== //
    // NAME : main
    // RETURN TYPE : none
    // PARAMETERS : args : [DEFAULT]
    // DESCRIPTION : Provides the user with a menu from which they can
    // select the processes they wish to perform.
    // The selections allow the user to:
    // Display all Maths students
    // Sort and display all English students by student ID
    // Display all English Merit students
    // Display the average exam marks by subject
    // Display all students above the Maths average
    //
    // //================================================== ========
    public static void main(String[] args)
    {
        //Declare arrays for student IDs and exam marks
        int[] mathsExamMark = new int[12];
        int[] engExamMark = new int[15];
        String[] mathsStudentID = new String[12];
        String[] engStudentID = new String[15];

        //Initialise all student and mark arrays
        initialiseArrays(mathsExamMark, engExamMark, mathsStudentID, engStudentID);

        //Declare variables
        int menuOption = 0;
        boolean menuExit = false;
        int mathsArraySize = mathsStudentID.length;
        int englishArraySize = engStudentID.length;

        //Use while loop to display menu until exit is selected
        while (menuExit != true)
        {
            displayMenu();

            //Prompt for menu option selection
            System.out.println("\n\nPlease select a menu option number to continue.");
            menuOption = InOut.readInt();

            //Use while loop to ensure valid option selected
            while (menuOption < 1 || menuOption > 6)
            {
                System.out.println("\n\n++++++++++++++++++++++++++ +++++++++++++++++++++++++++++");
                System.out.println("+ INVALID! - selection should be in the range 1 - 6! +");
                System.out.println("++++++++++++++++++++++++++++++ +++++++++++++++++++++++++\n");
                displayMenu();
                System.out.println("\nPlease select a menu option number to continue.");
                menuOption = InOut.readInt();
            } //End of while loop for invalidity

            switch (menuOption)
            {
                case 1:
                    displayStudents(mathsStudentID, mathsExamMark, mathsArraySize, 100, 1, "All mathematics students\n ************************");
                    break;
                case 2:
                    sortStudents(engStudentID, engExamMark, englishArraySize);
                    displayStudents(engStudentID, engExamMark, englishArraySize, 100, 1, "English students in Ascending order\n ***********************************");
                    break;
                case 3:
                    displayStudents(engStudentID, engExamMark, englishArraySize, 70, 55, "English Merit students\n **********************");
                    break;
                case 4:
                    averageResult(mathsExamMark, mathsArraySize);
                    System.out.println("\nThe Mathematics average is " + averageResult(mathsExamMark, mathsArraySize));
                    averageResult(engExamMark, englishArraySize);
                    System.out.println("\nThe English average is " + averageResult(engExamMark, englishArraySize));
                    break;
                case 5:
                    averageResult(mathsExamMark, mathsArraySize);
                    displayStudents(mathsStudentID, mathsExamMark, mathsArraySize, 100, averageResult(mathsExamMark, mathsArraySize) + 1, "Mathematics students above the average\n **************************************");
                    break;
                case 6:
                    menuExit = true;
                    break;
            } //End of switch statement
        } //End of while loop for main menu
    } //End of main() method
}
 
Last edited:
Try modifying this line:

Code:
displayStudents(mathsStudentID, mathsExamMark, mathsArraySize, 100, [B][COLOR="Red"](int)[/COLOR][/B]averageResult(mathsExamMark,mathsArraySize)+1, "Mathematics students above the average\n **************************************");

Also replace 'break' with the following:

Code:
case 6:
menuExit = true;
[B][COLOR="Red"]System.exit(0);[/COLOR][/B]

The first change simply casts a double value to an integer as that's what the moethod is expecting. The second change will cause the application to exit when run from a command line.

To run the program from a command line (rather than in Netbeans) you have to first compile the java file using the following command:

javac <file>.java

You can then run the program by using the following command:

java <file>

Easy.
 
Thanks guys

I am sorry if i came across like an ass earlier. Just am abit stressed at the moment. Am In my first year of a part time HNC.

We get 2hours a week in class and we are meant to figure out the rest ourselves.

So I am finding it pretty hard and so are the rest of us.

So yeah a very basic understanding of what is happening.

Any ways of cleaning it up a bit to make it look better? Or better way to place the code? Better order?

Thanks for your help guys I really appricate it.:)
 
Back
Top Bottom