this is the homework I have to do

This homework is due in week 3.
You must work on the program outside the formal laboratory sessions. The program must be ready to be executed at the start of the scheduled laboratory session. Remember you may be asked to explain your program!

Write a class called Module to represent a University’s teaching module. The attributes of each module are:

Module name : a string, e.g. Programming 2
Module ID : a string, e.g. CG0048
Lecture Room : a string, e.g. Pandon S1
Module Tutor : a string, e.g. Alan Maughuan

The class should include:

Constructors:
Module() : sets all values to empty strings
Module(String inName, String inModuleID)
Module(String inName, String inModuleID, String inRoom, String inModuleTutor)

Methods:
String getModuleName()
String getModuleID()
String getRoom()
void setRoom(String inRoom)
String getModuleTutor()
void setModuleTutor(String inModuleTutor)
String toString()

You should also write a separate class with a main method to test the Module class. This driver must execute all the constructors and methods defined above.
Marking

Mark Criteria
5 0.5 For each correct constructor or method as specified above
5 For a driver that clearly demonstrates the correct operation of Module

This is the code I have for it at the moment. I keep getting return type required errors. can you help?

class Homeworkone
{
private String moduleName = "Programming 2";
private String moduleId = ". CG0048";
private String lectureRoom = "Pandon S1";
private String moduleTutor = "Alan Maughuan";
// constructors
Module()
{
moduleName="";
moduleId="";
lectureRoom="";
muduleTutor="";

}

Module (String inName,String inModuleId )
{
moduleName=inName;
moduleId= inModuleId;

}

Module (String inName, String inModuleId, String inRoom, String inModuleTutor)
{
moduleName=inName;
moduleID= inModuleId;
lectureRoom=inRoom;
moduleTutor=inModuleTutor;

}

// mutator methods
void setRoom(String inRoom)
{
inName = inRoom; // sets value of name
}
void setModuleTutor(String inModuleTutor)
{
moduleTutor = inModuleTutor; // sets value of hwkMark
}

String getInName()
{
return inName;
}

String getModuleTutor()
{
return moduleTutor;
}

int getRoom()
{
return lectureRoom;
}

}
// other methods
public String toString()
{
return inName +" " + inRoom +" " +lectureroom+ " " + moduleTutor;
}


}

Recommended Answers

All 14 Replies

Hi ,

In that program what you think are constructors are not constructors at all .

There are treated as methods since it doesn't satisfy constructor's rule and methods must have return type and that's why you are getting that error.

To learn about constructor and how to create that
use this link

To clarify, a constructor will have the same name as the class and not define any return type. If it does not have the same name as the class, then it is just a method and must have a return type defined. If your methods are not returning anything, you still need to specify "void" as the return type.

To create the constructors, change all those "Module" names to "Homeworkone".

okay guys I found out about the constructors and now have 2 errors to do with my toString command. I highlighted in bold where I got errors. The error said class, interface or enum expected. Also with the question I have been given am I doing this completly wrong. I know I need to change the constructor to module which I will do but for now I want it to run. After it runs I can concentrate on the driver. So any help would be greatly appreciated. Thanks


class Homeworkone
{
private String moduleName = "Programming 2";
private String moduleId = ". CG0048";
private String Room = "Pandon S1";
private String moduleTutor = "Alan Maughuan";
// constructors
Homeworkone()
{
moduleName="";
moduleId="";
Room="";
muduleTutor="";

}

Homeworkone (String inName,String inModuleId )
{
moduleName=inName;
moduleId= inModuleId;

}

Homeworkone (String inName, String inModuleId, String inRoom, String inModuleTutor)
{
moduleName=inName;
moduleId= inModuleId;
Room=inRoom;
moduleTutor=inModuleTutor;

}

// mutator methods
void setRoom(String inRoom)
{
Room = inRoom; // sets value of name
}
void setModuleTutor(String inModuleTutor)
{
moduleTutor = inModuleTutor; // sets value of Module tutor
}

String getInName()
{
return inName;
}

String getModuleTutor()
{
return moduleTutor;
}

int getRoom()
{
return Room;
}

}
// other methods
public String toString()
{
return inName +" " + inRoom +" " +Room+ " " + moduleTutor;
}


}

ye have an extra parenthesis there above //other methods, so you have in effect finished your class there!!! get rid of it and ye should be fine mate.

commented: thnx that was a big help +1

okay guys thanks for all your help. I have started creating the driver and have become stuck. I cant get it to print out the different pieces of infomation as defined in my strings (in bold).

class Homeworkone
{
public String moduleName = "Programming 2";
public String moduleId = ". CG0048";
public String Room = "Pandon S1";
public String moduleTutor = "Alan Maughuan";

// constructor

Homeworkone()
{
moduleName = "";
moduleId = "";
Room="";
moduleTutor="";

}

Homeworkone (String inName,String inModuleId )
{
moduleName=inName;
moduleId= inModuleId;

}

Homeworkone (String inName, String inModuleId, String inRoom, String inModuleTutor)
{
moduleName=inName;
moduleId= inModuleId;
Room=inRoom;
moduleTutor=inModuleTutor;

}

// mutator methods
void setRoom(String inRoom)
{
Room = inRoom; // sets value of name
}
void setModuleTutor(String inModuleTutor)
{
moduleTutor = inModuleTutor; // sets value of module tutor
}

String getInName()
{
return moduleName;
}

String getModuleTutor()
{
return moduleTutor;
}

String getRoom()
{
return Room;
}


// other methods
public String toString()
{
return moduleName +" " + moduleId +" " +Room+ " " + moduleTutor;
}


}

This is my driver file

class ModuleDriver
{
public static void main(String args[])
{
Homeworkone m1 = new Homeworkone();

m1.setRoom("pandon S1\n");
m1.setModuleTutor("Alan Maughuan");
System.out.println( m1.toString());

}// end of main()
}

Because everyone of your constructors are redefining them.

What happens? Does it print anything at all, wrong data or just blank? Any errors?

Also, if you wrap your code inside code tags, it'll be a lot easier for us to read it. [c o d e] my code here[/c o d e]

okay i didnt know ther were any code tags.

what it is ment to print out is

Module name : Programming 2
Module ID : CG0048
Lecture Room : Pandon S1
Module Tutor : Alan Maughuan

At the moment I can get the programme to print out
Pandon S1
Alan Maughuan

Now with set methods I could do it but the methods for my the other parts IE moduleId and moduleName I dont know how to print them out.
I have to use System.out.println to display the information. The instructions for the homework are:

This homework is due in week 3.
You must work on the program outside the formal laboratory sessions. The program must be ready to be executed at the start of the scheduled laboratory session. Remember you may be asked to explain your program!

Write a class called Module to represent a University’s teaching module. The attributes of each module are:

Module name : a string, e.g. Programming 2
Module ID : a string, e.g. CG0048
Lecture Room : a string, e.g. Pandon S1
Module Tutor : a string, e.g. Alan Maughuan

The class should include:

Constructors:
Module() : sets all values to empty strings
Module(String inName, String inModuleID)
Module(String inName, String inModuleID, String inRoom, String inModuleTutor)

Methods:
String getModuleName()
String getModuleID()
String getRoom()
void setRoom(String inRoom)
String getModuleTutor()
void setModuleTutor(String inModuleTutor)
String toString()

You should also write a separate class with a main method to test the Module class. This driver must execute all the constructors and methods defined above.
Marking

Mark Criteria
5 0.5 For each correct constructor or method as specified above
5 For a driver that clearly demonstrates the correct operation of Module

So I have all the methods and constructor compiling fine but I just cant seem to get moduleId and module name to print out in my driver file. this is the driver file

class ModuleDriver
{
public static void main(String args[])
{
Homeworkone m1 = new Homeworkone();

m1.setRoom("pandon S1\n");
m1.setModuleTutor("Alan Maughuan");
System.out.println( m1.toString());

}// end of main()
}

just cant seem to get moduleId and module name to print out in my driver file. this is the driver file

Because, as Masijade already told you above, you have set all your variables to empty strings in that constructor and you haven't set values for them in your "driver".

I wouldnt be excercising all the methods if I define it in the driver and thats the problem I have. The driver has to use all the methods. If I create the strings in the driver class then it doesnt execute the methods. I really am confused about how I do this now.

I wouldnt be excercising all the methods if I define it in the driver and thats the problem I have. The driver has to use all the methods. If I create the strings in the driver class then it does execute the methods. I really am confused about how I do this now.

What? Define it in the driver? You aren't "defining" anything in the driver - you are using the class you have built. Currently, you only use one of the constructors. You need to use the others as well. Depending on which constructor you use, the amount of additional information you need to set on the object will change.

Your class variables should not be public and you should initialize them to appropriate default values - not specific strings that you happen to want to print. If you want those variables to hold those values, you should set them via the accessors or the constructors. The class itself should be generic and have no information relating to any one particular module.

thnx m8. I think I got it now.
This is the final code I have.
driver class

class ModuleDriver
{
  public static void main(String args[])
  {
     Module m1 = new Module();
      m1.moduleId =  "Module Id: CG0048\n";
      m1.moduleName =" Module name: Programming 2\n";
      m1.setRoom("Lecture Room: pandon S1\n");
      m1.setModuleTutor("Module Tutor: Alan Maughuan");
     System.out.println( m1.toString());
     
  }// end of main()
}

module class

class Module
{ 
 
  public String moduleName = "";
  public String moduleId = "";
 public String Room="";
 public String moduleTutor="";
  
 
  // constructor
  
  Module()
  {
  moduleName = "";
  moduleId = "";
  Room="";
  moduleTutor="";
  
  }
  
  Module (String inName,String inModuleId )
  {
     moduleName=inName;
     moduleId= inModuleId;
    
  }
  
 Module (String inName, String inModuleId, String inRoom, String inModuleTutor)
  {
      moduleName=inName;
      moduleId= inModuleId;
      Room=inRoom;
      moduleTutor=inModuleTutor;
    
  }
  
  
  // mutator methods
  void setRoom(String inRoom)
  {
    
    Room = inRoom; // sets value of name
  }
  void  setModuleTutor(String inModuleTutor)
  {
    moduleTutor = inModuleTutor; // sets value of module tutor
  }
  
  String getInName()
  {
     String moduleName = "Programming 2";
    return moduleName;
  }

  String  getModuleTutor()
  {
    return moduleTutor;
  }
  
  String getRoom()
  {
    return Room;
  }
 String getModuleId()
 {
  String moduleId  = " CG0048";
 return moduleId;
 }
  
  // other methods
  public String toString()
  {
    return moduleName +" " + moduleId +" " +Room+ " " + moduleTutor; 
  }


}

I think it meets the marking criteria.
do you think that meets the marking criteria?
the marking criteria:-

You must work on the program outside the formal laboratory sessions. The program must be ready to be executed at the start of the scheduled laboratory session. Remember you may be asked to explain your program!

Write a class called Module to represent a University’s teaching module. The attributes of each module are:

Module name : a string, e.g. Programming 2
Module ID : a string, e.g. CG0048
Lecture Room : a string, e.g. Pandon S1
Module Tutor : a string, e.g. Alan Maughuan

The class should include:

Constructors:
Module() : sets all values to empty strings
Module(String inName, String inModuleID)
Module(String inName, String inModuleID, String inRoom, String inModuleTutor)

Methods:
String getModuleName()
String getModuleID()
String getRoom()
void setRoom(String inRoom)
String getModuleTutor()
void setModuleTutor(String inModuleTutor)
String toString()

You should also write a separate class with a main method to test the Module class. This driver must execute all the constructors and methods defined above.
Marking

Mark Criteria
5 0.5 For each correct constructor or method as specified above
5 For a driver that clearly demonstrates the correct operation of Module

class ModuleDriver
{
  public static void main(String args[])
  {
     Module m1 = new Module();
      m1.moduleId =  "Module Id: CG0048\n";
      m1.moduleName =" Module name: Programming 2\n";
      m1.setRoom("Lecture Room: pandon S1\n");
      m1.setModuleTutor("Module Tutor: Alan Maughuan");
     System.out.println( m1.toString());
     
  }// end of main()
}

I think it meets the marking criteria.
do you think that meets the marking criteria?
the marking criteria:-

No, far from it. You have only used one constructor. You need to show usage of all of the other constructors as well. Also, you are setting the public properties directly. Those need to be made private and you should set them with the accessor methods.

I finally got it :-D Thanks a lot you really helped.
driver class

class ModuleDriver
{
  public static void main(String args[])
  {
  	// shows use of constructor with arguements inName and inModuleId 
     Module m1 = new Module("Module Name:Programming 2\n", "Module Id: CG0048\n");
          System.out.println( m1.toString());
 // shows Mutator methods 
      m1.setRoom("Lecture Room: pandon S1\n");
      m1.setModuleTutor("Module Tutor: Alan Maughuan");
     System.out.println( m1.toString());
 // show accessor methods
 String room = m1.getRoom();
 String moduletutor = m1.getModuleTutor();
 System.out.println("\n"+room+moduletutor);
 
 // No argument contructor
 Module m2 = new Module();
          System.out.println( m2.toString());
 // full arguement constructor
 Module m3 = new Module("ModuleName: Programming 2\n","Module Id: CG0048\n","Lecture Room: pandon S1\n","Module Tutor: Alan Maughuan");
  System.out.println( m3.toString());
  }// end of main()
}

Module class

class Module
{ 
 
  private String moduleName = "";
  private String moduleId = "";
 private String Room="";
 private String moduleTutor="";
  
 
  // constructor
  
  Module()
  {
  moduleName = "";
  moduleId = "";
  Room="";
  moduleTutor="";
  
  }
  
  Module (String inName,String inModuleId )
  {
     moduleName=inName;
     moduleId= inModuleId;
    
  }
  
 Module (String inName, String inModuleId, String inRoom, String inModuleTutor)
  {
      moduleName=inName;
      moduleId= inModuleId;
      Room=inRoom;
      moduleTutor=inModuleTutor;
    
  }
  
  
  // mutator methods
  void setRoom(String inRoom)
  {
    
    Room = inRoom; // sets value of name
  }
  void  setModuleTutor(String inModuleTutor)
  {
    moduleTutor = inModuleTutor; // sets value of module tutor
  }
  
  String getInName()
  {
     String moduleName = "Programming 2";
    return moduleName;
  }

  String  getModuleTutor()
  {
    return moduleTutor;
  }
  
  String getRoom()
  {
    return Room;
  }
 String getModuleId()
 {
  String moduleId  = " CG0048";
 return moduleId;
 }
  
  // other methods
  public String toString()
  {
    return moduleName +" " + moduleId +" " +Room+ " " + moduleTutor; 
  }


}
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.