Newbie to Java programming - Problems with Array assignment


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Newbie to Java programming - Problems with Array assignment

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Posts
    1

    Question Newbie to Java programming - Problems with Array assignment

    I'm taking an intro to Java course and currently we're taking about arrays. I have the two java files below that I need for the assignment to work, but I'm not sure what to do next with my code.

    Here's what the assignment says to do:

    Create a 'CollegeCourse class. The class contains fields for the course ID (for example, "History"), credit hours (for example, 3) and a letter grade (for example, 'A'). Include get() and set() accessor methods for each field.

    Create a 'Student' class containing an ID number and an array of five CollegeCourse objects. Create a get() and set() method for the Student ID number.
    Also, create a get() method that returns one of the student's CollegeCourses; the method would take an integer argument and return the CollegeCourse in the position(0 through 4).
    Next, create a set() method that sets the value of one of the student's CollegeCourses; the method would take two arguments -- a CollegeCourse and an integer representing the CollegeCourse's position (o through 4).

    -----------------------
    //CollegeCourse.java file below:

    class CollegeCourse
    {
    private String courseID;
    private int creditHours;
    private char letterGrade;

    public CollegeCourse(String course, int credits, char grades)
    {
    courseID = course;
    creditHours = credits;
    letterGrade = grades;
    }

    //Set Functions

    public void setcourseID(String name)
    {
    courseID = name;
    }
    public void setcreditHours(int num)
    {
    creditHours = num;
    }
    public void setletterGrade(char letter)
    {
    letterGrade = letter;
    }

    //Get Functions

    public String getCourseID()
    {
    return courseID;
    }
    public int getCreditHours()
    {
    return creditHours;
    }
    public char getLetterGrades()
    {
    return letterGrade;
    }
    }

    ==========================
    public class Student
    {
    public static void main(String[] args)
    {
    showValues(courses[0]);
    }

    public Student(int ID)
    {
    idNumber = ID;
    courses[0] = new CollegeCourse ("History", 0, 'A');
    courses[1] = new CollegeCourse ("Math", 0, 'A');
    courses[2] = new CollegeCourse ("English", 0, 'A');
    courses[3] = new CollegeCourse ("Music", 0, 'A');
    courses[4] = new CollegeCourse ("Science", 0, 'A');
    }

    public int getCourse()
    {
    return idNumber;
    }
    public static void showValues(Show c)
    {
    System.out.println c.getCourse();
    }
    }


    Thanks

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    What happens when you compile these files?

    Your ShowValues() method needs help - you need to iterate through the elements of your array. for (int i = 0; i < courses.length; ++i) ...

    The statement in your ShowValues() method will not work - you need to pass one or more parameter to the System.out.println() method - it takes a string. Your getCourse() method returns a string. So you need to pass the getCourse() method of the CollegeCourse object as the parameter for println().

    Anything else going on?

  3. #3
    Join Date
    Sep 2005
    Posts
    74
    I've looked over your code, compiled and it had a ton of errors. So I went through and fixed them..although I feel it's better for you to fix them and learn from them (although after I give you these hints if your really stuck, just ask and I'll post the working code).

    Hints:

    -Indentation is your friend =p
    -Get rid of the showValues class..you don't need it (just put the print statement in main(), instead of calling for it)
    - Your object initializing statements won't work, because you don't have a reference to that class anywhere (ex: static CollegeCourse courses[]...)
    - You are also going to need a polymorphic temp object to hold/print whatever course value the user (or you) determine (ex: 4, the temp variable would take on the object at index 4 in the array, and print)
    -You need a for loop to initialize those objects (it goes in main), so put those object initializing statements inside that loop
    - I think your program calls for a setCourse method..if so make one
    - Don't know if your spec calls for this or not, but your going to need some way to get input from the user (or just use a dummy variable for this)

    I think that's about it.

  4. #4
    Join Date
    Oct 2006
    Posts
    1
    Quote Originally Posted by Dark Rain
    I've looked over your code, compiled and it had a ton of errors. So I went through and fixed them..although I feel it's better for you to fix them and learn from them (although after I give you these hints if your really stuck, just ask and I'll post the working code).

    Hints:

    -Indentation is your friend =p
    -Get rid of the showValues class..you don't need it (just put the print statement in main(), instead of calling for it)
    - Your object initializing statements won't work, because you don't have a reference to that class anywhere (ex: static CollegeCourse courses[]...)
    - You are also going to need a polymorphic temp object to hold/print whatever course value the user (or you) determine (ex: 4, the temp variable would take on the object at index 4 in the array, and print)
    -You need a for loop to initialize those objects (it goes in main), so put those object initializing statements inside that loop
    - I think your program calls for a setCourse method..if so make one
    - Don't know if your spec calls for this or not, but your going to need some way to get input from the user (or just use a dummy variable for this)

    I think that's about it.

    man, i can;t get this to work either, can you post the corrected code.
    thanks

Similar Threads

  1. Java vs. .Net. A questionnaire
    By Basil in forum .NET
    Replies: 1
    Last Post: 05-13-2005, 06:46 AM
  2. DevX does seem one sideded
    By Rob Abbe in forum Talk to the Editors
    Replies: 44
    Last Post: 01-13-2003, 02:57 PM
  3. Replies: 2
    Last Post: 12-13-2001, 09:55 PM
  4. Outsource your Java & C++ programming to Novosoft!
    By Novosoft in forum web.announcements
    Replies: 0
    Last Post: 02-14-2001, 03:50 AM
  5. About Java programming courses in Chicago/Suburbs
    By Muhammad Alam in forum Java
    Replies: 1
    Last Post: 03-24-2000, 09:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center