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 4 of 4

Thread: Help changing my programm to take in more data

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help changing my programm to take in more data

    Hey Guys I have this program below written. I want to change it so the array size isn't fixed but rather automatically increase the array size by creating a new larger array and copying contents of the current array to it. I also want to implement the dropStudent method and add a new method named clear() that removes all students from the course. Write a test program that creates a course, adds three students, removes one and displays the students in the course. Thank you in advance for your help I'm more of a Python programmer and new to Java!



    public class Course {
    private String courseName;
    private String[] students = new String[100];
    private int numberOfStudents;

    public Course(String courseName) {
    this.courseName = courseName;
    }

    public void addStudent(String student) {
    students[numberOfStudents] = student;
    numberOfStudents++;
    }

    public String[] getStudents() {
    return students;
    }

    public int getNumberOfStudents() {
    return numberOfStudents;
    }

    public String getCourseName() {
    return courseName;
    }

    public void dropStudent(String student) {
    // Left as an exercise in Exercise 9.9
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,524
    My Mood
    Mellow
    Thanks
    156
    Thanked 615 Times in 602 Posts

    Default Re: Help changing my programm to take in more data

    Use ArrayList.

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Help changing my programm to take in more data

    If you're forced to use an array, then you need to swap the position of the element to remove with the last non-null element. When you have swapped, you nullify the last element and shrink the student count by 1.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,469
    My Mood
    Grumpy
    Thanks
    0
    Thanked 159 Times in 150 Posts

    Default Re: Help changing my programm to take in more data

    The clear method is very simple. Just create a new array.
    Improving the world one idiot at a time!

Similar Threads

  1. Ip pinging programm with tables, realy need help
    By reinisla in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 5th, 2013, 02:54 PM
  2. Programm does not start. Help Please!
    By efluvio in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 8th, 2013, 01:55 PM
  3. problem while changing file data
    By badhen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 01:19 PM
  4. Creating a programm
    By TheBoss84 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 23rd, 2011, 02:36 PM
  5. Updating TableModel if changing data on DataBase(MySQL)
    By Farrukhjon in forum AWT / Java Swing
    Replies: 2
    Last Post: September 13th, 2011, 08:59 AM

Tags for this Thread