The Artima Developer Community
Sponsored Link

Java Answers Forum
A classt hat will Model a student and University

1 reply on 1 page. Most recent reply: Nov 15, 2002 1:20 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Ian Andrew

Posts: 3
Nickname: sunkey
Registered: Nov, 2002

A classt hat will Model a student and University Posted: Nov 15, 2002 10:45 AM
Reply to this message Reply
Advertisement
Below is a lab program to be submitted by Nov. 22, 02:

Write a program that will model a University. Write a class to model a Student, each student has a name and an address. Implement a method called deplayStudent that displays the information of a student in a window. Use the static method showInputDialog in the class JOptionPane, found in the javax.swing package. Each University has a list of Students that that are enrolled. Using an array to model the list of Students, implement a University class that has 50 students enrolled. Add to your university class, a method displayAllStudents that will call the displayStudent method for all the students enrolled.

I HAVE TRIED TO WRITE THE PROGRAM MYSELF BUT KEEP GETTING ERRORS THAT I CANNOT DEBUG. I AM NOT SURE HOW IMPLEMENT THE SOLUTION. I AM THERFORE SEEKING ASSISTANT IN COMPLETING IT IN TIME TO BE HANDED IN.

HERE WHAT I TRIED TO DO:

/* This class models the Student Object. It takes as
input two Strings the name and the address and
constructs a Student Object.
*/

import javax.swing.*;

public class Student
{
String student_name;
String student_address;

public Student(String name, String address)
{
this.student_name = name;
this.student_address = address;
}
public void displayStudent()
{
JOptionPane.showMessageDialog(null, "Name :" +
student_name + "/n" + "Address : " +
student_address);
}

public void AddStudent(Student st)
{
student_list[next_student++];
}

} // End class Student

===========================================================

* Class to model the University */

public class University
{
Student [] student_list = new Student[50];
int next_index = 0 ;

public void displayAllStudents()
{
for (int st_pos = 0; st_pos <
student_list.length; st_pos++)

student_list[st_pos].displayStudent();
}

public static void main(String [] args)
{
String inputname, inputaddress;
int counter = 0;

do
{
TextIO.put("Student Name : ");
inputname = TextIO.getlnString();

TextIO.put("\n\n Student Address : ");
inputaddress = TextIO.getlnString();

student_info[counter] = new Student
(inputname, inputaddress);

displayStudent();

TextIO.put("\n Enter Another Student Y/N ? ");
counter++;

} while ( counter < 50 || TextIO.getlnBoolean() );


TextIO.putln("\n\n\nOKcounter Student'S' were
entered. Goodbye.");

displayAllStudent();

}

}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: A classt hat will Model a student and University Posted: Nov 15, 2002 1:20 PM
Reply to this message Reply
You have this method:
public void AddStudent(Student st)
{
   student_list[next_student++];
}


But you have not defined student_list (which you appear to be treating as an array, not a List, by the way) or next_student and even if you did, you have an incomplete statement. I assume you really want to have something like this:
public void AddStudent(Student st)
{
   studentList.Add(st);
}

Where studentList is a List object, such as LinkedList or ArrayList (or even Vector).

By the way, the idea of a student containing a student list seems a bit strange. Maybe you really want the student list and the AddStudent() method to be in the University class.

I would use a for in place of your do/while, also. Other than that, I didn't look too closely at your University class. If you surround your code with [java] and [/java] mark-up tags when you post here, it will be much more readable, you know.

Flat View: This topic has 1 reply on 1 page
Topic: how do u add two arrays Previous Topic   Next Topic Topic: Honesty is the Best Policy

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use