Is there something missing? add array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ALKASER266
    New Member
    • May 2008
    • 7

    Is there something missing? add array?

    Hey guyz

    I have a prac and I am beginner and I did this code>
    Is my code is complete and if is it not complete how i can complete it?
    and how i can arrange it more?
    How I can make my driver to an array that generates an array of 20 students instead of my way of driver to enter it manually? THANK YOU VERY MUCH
    I will put the question then I will put my answer
    It is two codes one is Student which is the main one and the other one is driverStu which is a test driver

    This is my prac question:
    Assume a class called Student has the following attributes and methods:
    Attributes:
    • a unique “idNumber”, which is stored as an integer.
    • a firstname and surname
    • unit code (eg FIT1002)
    • an array called “results” of type float that stores the results of 5 assessment tasks each out of 20 marks.
    Methods:
    • A computeGrade method that computes and returns the final grade. The final grade is based on the final mark which is the sum of the five assessment items and the final grade is computed using the following rules:
    HD if final mark is between 80 and 100
    D if final mark is greater than or equal to 70 and less than 80
    C if final mark is greater than or equal to 60 and less than 70
    P if final mark is greater than or equal to 50 and less than 60
    N if final mark is less than 50
    • A display method that prints to screen its “idNumber” on a line by itself, the student’s full name, the mark for each assessment task on one line separated by a space, and the final mark and final grade on another line by itself.
    a) Draw a class diagram for the Student class.
    b) Write the Java code defining the class Student. It should include the following:
    i. A constructor which assigns a unique number to instance variable idNumber,
    creates space for the results array and leaves the other instance variables empty.
    HINT: Use a static variable for the idNumber. Initialise idNumber in the attribute declaration part of the code, then incremented in the constructor.
    ii. Accessor methods for each instance variable.
    iii. Mutator methods for some of the instance variables. Write comments above the mutator methods justifying the inclusions of these mutator methods.
    NOTE The setResults method that can set the mark for each assessment task in the results array. This method takes two parameters: the assessment task number that indicates which assessment task result to set and the result of that particular assessment task. The method therefore sets the result of the appropriate element in the results array. However, if the value of the assessment task number sent as a parameter is outside the bounds of the array or if the result is less than zero or greater than 20, the method does not set the result for that task – it leaves the results as its present value. This method returns true if the mark sent to it was set, and false if it was not set.
    iv. A display method.
    v. A computeGrade method.
    c) Write a test driver class that demonstrates that your constructor, accessors and mutators are working correctly.

    This is my codes:
    This code is called Student:

    /**
    * @(#)Student.jav a
    *
    *
    * @author
    * @version 1.00 2008/5/17
    */


    public class Student {

    private static int ID=0;
    String fname;
    String sname;
    String ucode;
    double result[]= new double[5];
    double fmark=0;
    double t=0;
    public Student() {

    ID++;
    fname="";
    sname="";
    ucode="";
    }

    public void setfname(String f){

    fname=f;
    }
    public void setsname(String f){

    sname=f;
    }
    public void setucode(String f){

    ucode=f;
    }
    public boolean setresult(doubl e m, int t){

    if(m<0.0 || m>20.0)
    return false;
    else
    result[t]=m;
    return true;

    }
    public String getfname(){

    return fname;
    }
    public String getsname(){

    return sname;
    }
    public String getucode(){

    return ucode;
    }

    public int getID(){

    return ID;
    }
    public String computeGrade(){

    for(int i=0; i<5; i++)
    fmark+=result[i];

    t=fmark;
    t/=2;
    if(t>=80 && t<=100)
    return "HD";
    if(t>=70 && t<80)
    return "D";
    if(t>=60 && t<70)
    return "C";
    if(t>=50 && t<60)
    return "P";
    else
    return "N";


    }
    public void display(){
    System.out.prin tln("ID FULL NAME UCODE A1 A2 A3 A4 A5 fINAL MARK FINAL GRADE\n");
    System.out.prin t(getID()+" "+getfname()+ge tsname()+" "+getucode( )+" ");
    for(int i=0; i<5; i++)
    System.out.prin t(result[i]+" ");
    System.out.prin t(fmark+" "+computeGrade( ));

    }


    }


    This is the test driver code of the Student program it is called driverStu:

    /**
    * @(#)driverStu.j ava
    *
    *
    * @author
    * @version 1.00 2008/5/17
    */


    public class driverStu {

    public static void main(String args[]){
    Student s = new Student();
    s.setfname("Eri ck");
    s.setsname(" Mouinten");
    s.setucode(" FIT1002");

    if( s.setresult(25. 5,0))
    System.out.prin tln("true It was set marks");
    else
    System.out.prin tln(" False It was NOT set marks");
    s.setresult(15. 5,1);
    s.setresult(15. 5,2);
    s.setresult(15. 5,3);
    s.setresult(15. 5,4);
    s.computeGrade( );
    s.display();

    Student s1 = new Student();
    s1.setfname("Da vid");
    s1.setsname(" Carmen");
    s1.setucode(" FIT2107");

    if( s1.setresult(25 .5,0))
    System.out.prin tln("true It was set marks");
    else
    System.out.prin tln(" False It was NOT set marks");
    s1.setresult(9. 5,1);
    s1.setresult(11 .5,2);
    s1.setresult(14 .5,3);
    s1.setresult(18 .5,4);
    s1.computeGrade ();
    s1.display();


    }

    }





    Waiting your replies and thank you very much.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    None of the Students have their own unique identification code. btw I just saw you
    on Sun's Java forum. Any answers from there yet?

    kind regards,

    Jos

    Comment

    • ALKASER266
      New Member
      • May 2008
      • 7

      #3
      Originally posted by JosAH
      None of the Students have their own unique identification code. btw I just saw you
      on Sun's Java forum. Any answers from there yet?

      kind regards,

      Jos
      the Students unique ID code is incremented in the constructor.

      Any answers from there yet?

      not yet

      thanks

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by ALKASER266
        the Students unique ID code is incremented in the constructor.
        Sure it is but it is a static variable so there is only one for all Students.

        kind regards,

        Jos

        Comment

        • ALKASER266
          New Member
          • May 2008
          • 7

          #5
          what is the best way to do it then?

          thanks

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by ALKASER266
            what is the best way to do it then?

            thanks
            Just keep it as it is but add a private int id to your class and copy the current
            ID value to it at construction time; like this:

            [code=java]
            public class Student {
            private static int ID; // initialized to 0
            ...
            private int id; // private copy, one per Student instance
            ...
            public Student( ... ) {
            id= ID++; // copy current ID value and increment for the next Student
            ...
            }
            }
            [/code]

            kind regards,

            Jos

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              ps. @OP: see? people don't like massive crossposts all over the place. Not even
              across forums; don't do that anymore.

              kind regards,

              Jos

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #8
                and inclose your code with a codetag next time...
                eg. [/CODE] at the end and [CODE=JAVA] at the top of your code...

                regards,
                sukatoa

                Comment

                • Kid Programmer
                  New Member
                  • Mar 2008
                  • 176

                  #9
                  Originally posted by ALKASER266
                  Hey guyz

                  I have a prac and I am beginner and I did this code>
                  Is my code is complete and if is it not complete how i can complete it?
                  and how i can arrange it more?
                  How I can make my driver to an array that generates an array of 20 students instead of my way of driver to enter it manually? THANK YOU VERY MUCH
                  I will put the question then I will put my answer
                  It is two codes one is Student which is the main one and the other one is driverStu which is a test driver

                  This is my prac question:
                  Assume a class called Student has the following attributes and methods:
                  Attributes:
                  • a unique “idNumber”, which is stored as an integer.
                  • a firstname and surname
                  • unit code (eg FIT1002)
                  • an array called “results” of type float that stores the results of 5 assessment tasks each out of 20 marks.
                  Methods:
                  • A computeGrade method that computes and returns the final grade. The final grade is based on the final mark which is the sum of the five assessment items and the final grade is computed using the following rules:
                  HD if final mark is between 80 and 100
                  D if final mark is greater than or equal to 70 and less than 80
                  C if final mark is greater than or equal to 60 and less than 70
                  P if final mark is greater than or equal to 50 and less than 60
                  N if final mark is less than 50
                  • A display method that prints to screen its “idNumber” on a line by itself, the student’s full name, the mark for each assessment task on one line separated by a space, and the final mark and final grade on another line by itself.
                  a) Draw a class diagram for the Student class.
                  b) Write the Java code defining the class Student. It should include the following:
                  i. A constructor which assigns a unique number to instance variable idNumber,
                  creates space for the results array and leaves the other instance variables empty.
                  HINT: Use a static variable for the idNumber. Initialise idNumber in the attribute declaration part of the code, then incremented in the constructor.
                  ii. Accessor methods for each instance variable.
                  iii. Mutator methods for some of the instance variables. Write comments above the mutator methods justifying the inclusions of these mutator methods.
                  NOTE The setResults method that can set the mark for each assessment task in the results array. This method takes two parameters: the assessment task number that indicates which assessment task result to set and the result of that particular assessment task. The method therefore sets the result of the appropriate element in the results array. However, if the value of the assessment task number sent as a parameter is outside the bounds of the array or if the result is less than zero or greater than 20, the method does not set the result for that task – it leaves the results as its present value. This method returns true if the mark sent to it was set, and false if it was not set.
                  iv. A display method.
                  v. A computeGrade method.
                  c) Write a test driver class that demonstrates that your constructor, accessors and mutators are working correctly.

                  This is my codes:
                  This code is called Student:

                  /**
                  * @(#)Student.jav a
                  *
                  *
                  * @author
                  * @version 1.00 2008/5/17
                  */


                  public class Student {

                  private static int ID=0;
                  String fname;
                  String sname;
                  String ucode;
                  double result[]= new double[5];
                  double fmark=0;
                  double t=0;
                  public Student() {

                  ID++;
                  fname="";
                  sname="";
                  ucode="";
                  }

                  public void setfname(String f){

                  fname=f;
                  }
                  public void setsname(String f){

                  sname=f;
                  }
                  public void setucode(String f){

                  ucode=f;
                  }
                  public boolean setresult(doubl e m, int t){

                  if(m<0.0 || m>20.0)
                  return false;
                  else
                  result[t]=m;
                  return true;

                  }
                  public String getfname(){

                  return fname;
                  }
                  public String getsname(){

                  return sname;
                  }
                  public String getucode(){

                  return ucode;
                  }

                  public int getID(){

                  return ID;
                  }
                  public String computeGrade(){

                  for(int i=0; i<5; i++)
                  fmark+=result[i];

                  t=fmark;
                  t/=2;
                  if(t>=80 && t<=100)
                  return "HD";
                  if(t>=70 && t<80)
                  return "D";
                  if(t>=60 && t<70)
                  return "C";
                  if(t>=50 && t<60)
                  return "P";
                  else
                  return "N";


                  }
                  public void display(){
                  System.out.prin tln("ID FULL NAME UCODE A1 A2 A3 A4 A5 fINAL MARK FINAL GRADE\n");
                  System.out.prin t(getID()+" "+getfname()+ge tsname()+" "+getucode( )+" ");
                  for(int i=0; i<5; i++)
                  System.out.prin t(result[i]+" ");
                  System.out.prin t(fmark+" "+computeGrade( ));

                  }


                  }


                  This is the test driver code of the Student program it is called driverStu:

                  /**
                  * @(#)driverStu.j ava
                  *
                  *
                  * @author
                  * @version 1.00 2008/5/17
                  */


                  public class driverStu {

                  public static void main(String args[]){
                  Student s = new Student();
                  s.setfname("Eri ck");
                  s.setsname(" Mouinten");
                  s.setucode(" FIT1002");

                  if( s.setresult(25. 5,0))
                  System.out.prin tln("true It was set marks");
                  else
                  System.out.prin tln(" False It was NOT set marks");
                  s.setresult(15. 5,1);
                  s.setresult(15. 5,2);
                  s.setresult(15. 5,3);
                  s.setresult(15. 5,4);
                  s.computeGrade( );
                  s.display();

                  Student s1 = new Student();
                  s1.setfname("Da vid");
                  s1.setsname(" Carmen");
                  s1.setucode(" FIT2107");

                  if( s1.setresult(25 .5,0))
                  System.out.prin tln("true It was set marks");
                  else
                  System.out.prin tln(" False It was NOT set marks");
                  s1.setresult(9. 5,1);
                  s1.setresult(11 .5,2);
                  s1.setresult(14 .5,3);
                  s1.setresult(18 .5,4);
                  s1.computeGrade ();
                  s1.display();


                  }

                  }





                  Waiting your replies and thank you very much.
                  Please put your code in code tags. It is much easier to read.

                  Comment

                  • ALKASER266
                    New Member
                    • May 2008
                    • 7

                    #10
                    i am sorry guyz for Crossposting
                    i just wanted many ideas thats all
                    sorry and i am not doing it again
                    thanks again
                    and i will put as code format next time i am sorry
                    thanks

                    Comment

                    • ALKASER266
                      New Member
                      • May 2008
                      • 7

                      #11
                      thanks
                      i want to ask you how i can change the test drive to an array?
                      and do u think the mutator methods are correct?
                      thank you

                      Comment

                      Working...