By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,736 Members | 1,982 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,736 IT Pros & Developers. It's quick & easy.

Help setting up a Class

P: n/a
PB
i have a class called school.java and i cant figure out how to construct
another class called course.java that adds 2 people to a course. Can anyone
help me out on how to set up constructors and such. Here is school.java

import java.text.DecimalFormat;

public class School
{
//-----------------------------------------------------------------
// Creates some Student objects, adds them to a course, and
// prints them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Address school = new Address ("800 Lancaster Ave.", "Villanova",
"PA", 19085);

Address jHome = new Address ("21 Jump Street", "Lynchburg",
"VA", 24551);
Student2 john = new Student2 ("John", "Smith", jHome, school,
75, 88, 78);

Address mHome = new Address ("123 Main Street", "Euclid", "OH",
44132);
Student2 marsha = new Student2 ("Marsha", "Jones", mHome, school,
100, 98, 94);

Course zoology = new Course("Zoology 101");
zoology.addStudent(john);
zoology.addStudent(marsha);

zoology.roll();

DecimalFormat formatter = new DecimalFormat("0.0#");
System.out.println();
System.out.println ("-------------------------------------");
System.out.print ("Overall Test Average: ");
System.out.println (formatter.format(zoology.average()));
}
}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
What you have is the correct way to do it.
If you want a ctor that takes N number of students.
You would probably want to create a Course ctor that takes a collection

public class Course {

public Course(List aCollectionOfStudents){

studentColl.addAll(aCollOfStudents);
}
private List studentColl = new ArrayList();

}

PB wrote:
i have a class called school.java and i cant figure out how to construct
another class called course.java that adds 2 people to a course. Can anyone
help me out on how to set up constructors and such. Here is school.java

import java.text.DecimalFormat;

public class School
{
//-----------------------------------------------------------------
// Creates some Student objects, adds them to a course, and
// prints them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Address school = new Address ("800 Lancaster Ave.", "Villanova",
"PA", 19085);

Address jHome = new Address ("21 Jump Street", "Lynchburg",
"VA", 24551);
Student2 john = new Student2 ("John", "Smith", jHome, school,
75, 88, 78);

Address mHome = new Address ("123 Main Street", "Euclid", "OH",
44132);
Student2 marsha = new Student2 ("Marsha", "Jones", mHome, school,
100, 98, 94);

Course zoology = new Course("Zoology 101");
zoology.addStudent(john);
zoology.addStudent(marsha);

zoology.roll();

DecimalFormat formatter = new DecimalFormat("0.0#");
System.out.println();
System.out.println ("-------------------------------------");
System.out.print ("Overall Test Average: ");
System.out.println (formatter.format(zoology.average()));
}
}


Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.