PDA

View Full Version : one main method


sniperx
12-22-03, 01:36 PM
I only want to use one method " the main method" because i am not aware of creating other methods as yet. I am new to java and i only want to focus on creating this program using the main method alone. I have written the following code so far and i need to continue it in order to finish it. I would appreciate it if you help me continue it and if you want you can make adjustments where necessary, but the program must contain one method, ie "the main method".

I am working on writing a java code to run a program. The program must do the
following:


INPUT
* Read in from the keyboard a student number (an integer between 10000 and
99999).
* Read in from the keyboard a student's first and last name.
* Read in a degree course title.
* Read in the number of courses a student wants to enter his/her final
coursework and exam marks for: this will always be a whole number (an integer)
between 1 and 8.
* For each course, read in the final coursework mark, and the final exam mark
(these may be decimal - real numbers).



OUTPUT
The program should print out the student's details, as entered, and then:
* Calculate and display the student's average (mean) coursework mark.
* Calculate and display the student's average (mean) exam mark.
* Calculate and display the student's average overall mark: this is based on
taking 20% of the average coursework mark and adding it to 80% of the average
exam mark.
* Calculate a letter grade for the student, on the basis that 70 or above is
an A, 60 up to 70 is a B, 50 up to 60 is a C, 40 up to 50 is a D, and 35 up to
40 is an E.


I have written the following code so far but seem to be getting some problems.
Could you continue writing out the code for me without changing anything that I
have written so far? Reason being that I am trying to only use one "method" to
write the code, which is the main method. I wrote the following so far:


import java.io.*;
class assessment
{
public static void main (String [] args) throws IOException
{
BufferedReader in=
new BufferedReader(new InputStreamReader(System.in));

String a, stuname, degtitle, b, c, ctitle, d;
int stunum, numcourse, cmark, fmark;

System.out.print("Enter Student Number: ");
a=in.readLine();
stunum=Integer.parseInt(a);
if ((stunum>=10000) && (stunum<=99999))
{
System.out.print("Enter Student's First and Last Name: ");
stuname=in.readLine();
System.out.print("Enter Degree Title: ");
degtitle=in.readLine();
System.out.print("Enter Number of Courses Applied For: ");
b=in.readLine();
numcourse=Integer.parseInt(b);
if ((numcourse>=1) && (numcourse<=8))
for (int i=0; i<numcourse; i++)
{
System.out.print("Enter Course Title: ");
ctitle=in.readLine();
System.out.print("Enter Course Mark for "+ctitle+" : ");
c=in.readLine();
cmark=Integer.parseInt(c);
if ((numcourse>=1) && (numcourse<=8))
System.out.print("Enter Final Exam Mark for "+ctitle+" : ");
d=in.readLine();
fmark=Integer.parseInt(d);
}
else
{
System.out.println("Maximum Number of Courses Applied for is 8");
}

}
else
{
System.out.println("Student Number Must be between 10000 and 99999");
}
stunum=0;
numcourse=0;

System.out.println("-------------------------------------------------------------------------------");
System.out.println(" F I N A L A S S E S S M E N T R E P O R T");
System.out.println("-------------------------------------------------------------------------------");
System.out.println();
System.out.println(" Student Number : "+stunum);
System.ou t.println(" Student Name : "+stuname);
System.out.println(" Degree Title : "+degtitle);
System.out.println(" Number of Courses Applied : "+numcourse);
System.out.println();

}
}

hyjacked
12-22-03, 02:09 PM
post should be moved to java section...

Since you are saving more than one course title, mark, exam mark, you should be storing them in an array, otherwise you are just overwriting the previous marks.

Next you have to write code to calculate the mean and average mark. This should be pretty straight forward. Sum the values stored in the array, then divide by the number of courses.

In order to print out the letter grade, you could set up an if--else if--else statement, each if tests for the mark to be within a range,
somehting like: if(mark>=70){System.out.println("A");}

Hopefully that will get you going again in the right direction.

Best of luck.

esh
12-23-03, 12:24 AM
this should get you started...
just cleaned it up some, you need to add a method to compute the averages (you had them as integers, they want doubles)



import java.io.*;

class assessment
{
public static void main (String [] args) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String input = "", firstName = "", lastName = "", degreeTitle = "";
int studentNumber = 0, courseNumber = 0;
double courseMark, examMark;

System.out.print("Enter Student Number: ");
input = in.readLine();
studentNumber = Integer.parseInt(input);
if ((studentNumber>=10000) && (studentNumber<=99999))
{
System.out.print("Enter Student's First Name: ");
firstName = in.readLine();
System.out.print("Enter Student's Last Name: ");
lastName = in.readLine();
System.out.print("Enter Degree Course Title: ");
degreeTitle = in.readLine();
System.out.print("Enter Number of Courses Applied For: ");
input = in.readLine();
courseNumber = Integer.parseInt(input);

if ((courseNumber>=1) && (courseNumber<=8))
for (int i=0; i<courseNumber; i++)
{
System.out.print("Enter Course Mark for course #" + (i + 1) + " : ");
input = in.readLine();
courseMark = Double.parseDouble(input);
System.out.print("Enter Final Exam Mark for " + (i + 1) + " : ");
input = in.readLine();
examMark = Double.parseDouble(input);
}
else
{
System.out.println("Maximum Number of Courses Applied for is 8");
}

}
else
{
System.out.println("Student Number Must be between 10000 and 99999");
}



System.out.println(" F I N A L A S S E S S M E N T R E P O R T");
System.out.println();
System.out.println(" Student Number : " + studentNumber);
System.out.println(" Student: " + firstName + ", " + lastName);
System.out.println(" Degree Title : " + degreeTitle);
System.out.println(" Number of Courses Applied : " + courseNumber);
System.out.println();

}
}

esh
12-23-03, 12:26 AM
FYI: you were using a lot of random String objects (a,b,c,d) for your input. But you can just use one String for that, you will parse it right away... unless you want to make life hard for yourself.

Bobo
01-22-04, 10:19 AM
If I want to develop a class to store student information for this program, how to do?

esh
01-22-04, 11:54 AM
Create the class with the constructor and any methods you need... here is a little sample just to show you

class Student
{
public Student(String aFirst, String aLast, String aDegree, int aStudentID, int aCourseNumber)
{
firstName = aFirst;
lastName = aLast;
degreeTitle = aDegree;
etc.....
}

}


then in the main method you must initialize an object of the class student. you could do it like this...

Student joe = new Student("Joe", "Doe", "Computer Science", 12345, 5);

then you just initialized object joe of class student.
now you can use methods from the student class.
joe.method();

like that...

Bobo
01-23-04, 07:02 AM
Can the student class and original program store in one file? If yes, how to arrange.

If I need to store information for more than one student, should coursemark and exam mark include in the class?

Could you show me more about the development of student class?