import java.util.Scanner;

public class Course {
	private String courseTitle;
	private double feesPerStudent;
	private int noOfStudents;
	private String[] studentName = new String[100];
	private int courseCount;

    public Course(String courseTitle, double feesPerStudent) {
    	this.courseTitle = courseTitle;
    	this.feesPerStudent = feesPerStudent;
    }
    
    public void setCourseTitle(String courseTitle){
    	this.courseTitle = courseTitle;
    }
    public String getCourseTitle(){
    	return courseTitle;
    }
    
    public void setFeesPerStudent(double feesPerStudent){
    	this.feesPerStudent = feesPerStudent;
    }
    public double getFeesPerStudent(){
    	return feesPerStudent;
    }
    
    public void setNoOfStudents(int noOfStudents){
    	this.noOfStudents = noOfStudents;
    }
    public int getNoOfStudents(){
    	return noOfStudents;
    }
    
    public void setStudentName(String[] studentName){
    	this.studentName = studentName;
    }
    public String[] getStudentName(){
    	return studentName;
    }
    
    public void setCourseCount(int courseCount){
    	this.courseCount = courseCount;
    }
    public int getCourseCount(){
    	return courseCount;
    }
    
    public double calcFeesCollected(){
    	return getFeesPerStudent() * getNoOfStudents();
    }
    
    public void addStudent(String studName){
    	Scanner scan = new Scanner(System.in);
	    //System.out.print("Student Name: ");
	    //studName = scan.next();
	    studentName[noOfStudents] = studName;
	    ++noOfStudents;

    }
    
    public String toString(){
    	return "\nCourse Title: " + getCourseTitle() + "\nFees Per Student: RM" + getFeesPerStudent() + "0\nNumber Students: " + getNoOfStudents();
    }
}

class TestCourse{
	
	public static void main(String[] args){
		Course course = new Course("Introduction to Computers", 250);
		
		course.addStudent("Ali Said");
		course.addStudent("Wong Ken");
		course.addStudent("Peter Lim");
		
		System.out.println(course.toString());
		System.out.print("Student Name: ");
		for(int i = 0; i < 3; i++){
			System.out.print(course.getStudentName());
			System.out.printf("\t\t\t\n");
		}
		System.out.println("Total Fees: " + course.calcFeesCollected());
	}
}

since my studentName declared as array with size 100, and i want to get the studentName with array type, how?

Recommended Answers

All 9 Replies

you're making it way to difficult on yourself ...
why creating an entire class to deal with the basic functions of an array, which you can take care of with about 3 or 4 lines of code in your main class?

String[] studentName = course.getStudentName();

Now iterate through this array.

Use an Enhanced For loop. Its better.

I think you are now printing the entire array of students thrice.

@above. I feel it is an assignment for them to create a course class.

well ... if so, take a closer look at the addStudent method, and see if you can spot anything out of the ordinary in there?

I think that, if it's a task he has to submit code for, it might serve him well to re-think his code a bit, and not just on the matter of retrieving information from the array.

also, for a task, I would deem it way more plausible if he were to create a class of Students of which he had to create an array. might be easier if we also knew what he's trying to achieve.

@above. I feel it is an assignment for them to create a course class.

Unlikely. With these sort of assignments you are expected to create certain object type and show how you can use it with collections.

So better get that array out of Course class, and create Course array in main method where you can then do as follows

Course[] course = new Course[100];
course[0] = new Course("Java Development", "Peter");
course[1] = new Course("Web Development", "stultuske");
//etc
String[] studentName = course.getStudentName();

Now iterate through this array.

Use an Enhanced For loop. Its better.

I think you are now printing the entire array of students thrice.

@above. I feel it is an assignment for them to create a course class.

awh, i solved the problem, thanks dude.
by the way, everytime we take variable from another class must create an variables to hold it?

that depends on the situation. are you instantiating a new variable, or ar you changing an already stored value?

but, as both I and Peter_Budo also mentioned, we strongly believe your approach is wrong and not the way you are intended to do it.

also, check your addStudent method. why do you create an instance of the Scanner class there? you're just reserving resources you're never intending to use.

oh, i though the addStudent method have to let user input, so i do it..
then after i know i did wrong, so i put the // there :)

but what does your assignment tell about creating a class to deal with the array tasks?

it seems a lot more likely you were ment to create an object of the type Student, and afterwards, in your mainclass, to create an array (or collections) containing only elements of the type Student

Alright, Thanks Dude.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.