hey guys,

im a bit new to this. pls assist me to correct this code :(.

public class studentSystem
{
 public static void main(String args[])
 {
 	//Student sw = new Module("");
 	Student sw = new Student("Sefakor","1112348");
	System.out.println(sw);
	
	System.out.println(sw.getName()); //prints Sefakor Selormey
	System.out.println(sw.getId()); //prints 1112348	
	
	sw.setName("May Stow");
	
	System.out.println(sw.getName());
	
 }
}
public class Student
{
	private String name;
	private int id;
	private Module[] modules;
	
	public Student(String name, int id)
	{
		this.name = name;
		this.id = id;
		this.modules = new Module[3];
	}
	
	public void setModule(int index, Module module)
	{
		this.modules[index] = module;
	}
	
	public void printModule()
	{
		for(int i = 0; i < this.modules.length; i++)
		{
			System.out.println(this.modules[i]);
		}
	}
	
	
	public String toString()
	{
		return "The student's name is: "; 
		return this.name;
		return "and the id number is " + this.id;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public int getId()
	{
		return this.id;
	}
	
	public void setName(String name)
	{
		this.name = name;
	}
	
	public void setId(int id)
	{
		this.id = id;
	}
}

1. What are the errors that you are getting??

2. Note that in your main method you have this

Student sw = new Student("Sefakor","1112348");

but your method student has this

public Student(String name, int id)

Notice that id is int but you passed it with a "".


3. You have a method String toString(). Java has a method toString() so if you can, pls use another method name. Inside this toString method, you have 3 return statements. When you issue a return inside a method, it will return back to the caller and codes below this return statement will not be executed.

If you want to return the whole string in the toStrign method, you can just declare another variable like wholeString = "the name is" + getName + "id is " + getId.
Then you can just return this string.

Hope this helps.

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.