4 Replies - 3836 Views - Last Post: 31 October 2011 - 11:23 AM Rate Topic: -----

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Call Info from Other Classes to be Written

Posted 31 October 2011 - 10:21 AM

I am attempting to write information to a file, but this is the the first time I've tried something like this. Below is an excerpt of my code with relevant information. The University class is my main class. After writing the information regarding University itself (ie name, current semester, # of students/instructors/courses), I need to write specific information regarding students, instructors, and courses. Other classes I have written are "Person", "Instructor extends Person", "Student extends Person", "GradStudent extends Student", "UnderGrad extends Student", and "Courses". I am confused on how to call information from other classes so that I can write it all to a file.

public class University {
	// Relevant Properties
	private String name;
	private String currentTerm; 
	private int year; 			
	
	private Course[] courses;
	private Student[] students;
	private Instructor[] instructors;
	
	// This is where my Constructors are in the project (not included here)

public String toString() {
		String str = "University Name: " + name;
		str += "\nCurrent Semester: " + currentTerm + " " + year;
		//str += "\nCurrent year: " + year;
		str += "\nNumber of Students: " + students.length;
		str += "\nNumber of Instructors: " + instructors.length;
		str += "\nNumber of Courses: " + courses.length;
		return str;
	}

        // Is this written correctly? This is the first time I've tried writing info to a file.
	public void writeToFile (PrintWriter pw, UndergradStudent student) throws IOException {
		pw.println("University Name: " + getName());
		pw.println("Current Semester: " + getCurrentTerm());
		pw.println("Number of Students: " + students.length);
		pw.println("Number of Instructors: " + instructors.length);
		pw.println("Number of Courses: " + courses.length);
/*After this, how do I call info from other classes so that it can be written?
for example, how do I call the "name" property of the class GradStudent which extends Student? 
                */ 
		}
}


This post has been edited by ninjawesome222: 31 October 2011 - 10:24 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Call Info from Other Classes to be Written

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Call Info from Other Classes to be Written

Posted 31 October 2011 - 10:26 AM

You will need to create an object of the 'GradStudent' and call the name property from that instance.
Was This Post Helpful? 0
  • +
  • -

#3 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 106
  • Joined: 27-January 10

Re: Call Info from Other Classes to be Written

Posted 31 October 2011 - 11:07 AM

Suppose this is my main class:

public class Main {

private SecondClass secondClass;

//methods
}



and my second class is

public class SecondClass {

private int num;

//methods
}



How can my Main class call the num variable from SecondClass so that methods in main can manipulate it?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,288
  • Joined: 27-December 08

Re: Call Info from Other Classes to be Written

Posted 31 October 2011 - 11:21 AM

You should use getter/setter methods as appropriate in your SecondClass.
public class SecondClass{

   private int x;

   public int getX(){ return x; }
   public void setX(int x){ this.x = x; }
}



Remember that private variables are only accessible in the class they were defined in.

More reading on class design: http://www.dreaminco...arallel-arrays/
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Call Info from Other Classes to be Written

Posted 31 October 2011 - 11:23 AM

Please avoid Duplicate threads
*Topic Merged*
You will need to have a getter method for your instance variable, then use the getter method using your object to get a value from another object`s class
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1