Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Question on a method?

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question on a method?

    I am doing project, that deals with an array and I was having trouble with one of the methods called calcuateGPA. The method goes through an array of courses and makes the appropriate calculation. Here is the code and method below :



    /** @author CCochran */
    /** @version Java Version 6 Update 30 */


    /** Creates Transcript class which stores information about classes a student has taken */

    public class Transcript
    {

    /** Instance filed for gpa */

    private double gpa;
    /** Instance field for courses variable */
    private Course[] courses;
    /** Instance field for studentid variable */
    private int studentID;
    /** Instance field for courseid */

    private String courseID;

    /** Instance field for numbergrade */

    private double numbergrade;

    /** Instance field for lettergrade */

    private String lettergrade;

    /** Instance field for studentname variable */

    private String studentName;

    /** Instance field for count variable */

    private int count;

    /** Parameterized constructor

    @param studentID studentname */


    public Transcript(int studentID, String studentName)
    {

    this.studentID = studentID;
    this.studentName = studentName;


    }


    /** Method that adds a course to the transcript

    @param courseID lettergrade */

    public void addCourse(String courseID, String lettergrade)
    {

    Course[] courses = new Course[10];


    courseID = " ";

    lettergrade = " ";



    }

    /** Method that updates the course in the transcript
    @param courseID lettergrade */
    public void updateCourse(String courseID, String lettergrade)
    {
    int count = 10;

    Course[] courses = new Course[10] ;

    this.courseID = courseID;

    this.lettergrade = lettergrade;

    }


    /** Method that calculates the gpa */
    private void calculateGPA()
    {

    Courses[] courses = new Course[10];


    }


    /** Method that returns the gpa of the student
    @return the gpa */

    public double getGPA()
    {

    return gpa;


    }

    /** Method that gets and returns the course information in a string for the student
    @return course */

    public String getCourse()
    {

    return(courseID + " " + lettergrade + " " + numbergrade);

    }

    /** Method that returns a string
    @return string */


    public String toString()
    {

    return(studentID + "\n"+ studentName + " " + "\n" + gpa +"\n"+ courseID + " " + lettergrade + " " + numbergrade);


    }

    }
    Last edited by cam25; June 16th, 2012 at 11:07 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on a method?

    Please explain your problem or ask a question.
    Show the program's output and explain what is wrong with it and show what it should be.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on a method?

    Quote Originally Posted by Norm View Post
    Please explain your problem or ask a question.
    Show the program's output and explain what is wrong with it and show what it should be.

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    This is the output :12345
    Cameron Cochran
    0.0
    null null 0.0 . It prints out null for the gpa, courseID , lettergrade and numbergrade variables. Im trying to figure out how to get the calculategpa and updatecourse methods to work.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on a method?

    It prints out null for the gpa, courseID , lettergrade and numbergrade variables.
    I see 2 null values that are being printed, but your there are 4 variables in the list of variables you gave???

    Look at the code and see why those variables do not have valid non-null values. Where does the code assign them values?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on a method?

    Quote Originally Posted by Norm View Post
    I see 2 null values that are being printed, but your there are 4 variables in the list of variables you gave???

    Look at the code and see why those variables do not have valid non-null values. Where does the code assign them values?
    Thanks and I have one more question on the calcuategpa method. Would I use a while loop to go through the array to make the calculation, or would I use a different way?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on a method?

    If the data is in an array you'd use a for loop. If the data is in another kind of collection you'd probably use a while loop.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Question on a method?

    Well in each one of your methods involving your courses, you are resetting it to a null value, so you never actually add any data into it.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question on a method?

    Quote Originally Posted by Parranoia View Post
    Well in each one of your methods involving your courses, you are resetting it to a null value, so you never actually add any data into it.
    When I test using the toString method it returns that issue . How would I call the information from the other class into this class?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Question on a method?

    Where is the information that you want to access? What variable in what class?
    In what method in what class do you want to access that information?

    One way is for the class with the information to have a method that can be called that will return the info.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Question about using the toString method.
    By iDizzle in forum Object Oriented Programming
    Replies: 12
    Last Post: April 9th, 2012, 09:24 AM
  2. Basic Question Regarding Object/Method
    By aandcmedia in forum Object Oriented Programming
    Replies: 3
    Last Post: February 26th, 2012, 02:42 PM
  3. Substring Retrieval Method Question
    By Nuggets in forum Java Theory & Questions
    Replies: 12
    Last Post: January 31st, 2012, 12:16 AM
  4. [SOLVED] Quick method/array question...?
    By kari4848 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 1st, 2011, 09:48 PM
  5. Bissection Method Baseball Question
    By nickcanada in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 15th, 2010, 07:49 PM