Results 1 to 4 of 4
Thread: Abstract Classes.
- 05-12-2011, 12:48 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Abstract Classes.
Hey guys.
for my assignment i must use an abstract class called User.
i also have admin and instructor classes which extend User.
i have a User array which is storing all my objects either admin or instructor objects and IF they are instructors they have courses.
i want to print out the courses so i tried
but eclipse is telling me that this method doesn't exist for class User.Java Code:if(userArray[i].getRole().equals("instructor")){ System.out.println("courses: "+ userArray[i]); }
in class User i have
i didnt put a method in there as admins don't have courses.Java Code:public abstract class User { public String username; public String password; public abstract String getUsername(); public abstract String getPassword(); public abstract String getRole(); }
should i still be defining a method in the abstract class for courses?
or am i doing this completely wrong?
thanks
- 05-12-2011, 12:57 PM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
That code tries to print out the user (if it's an instructor). I don't see any courses in your code at all.
Which method? Post up the full error message text.eclipse is telling me that this method doesn't exist for class User.
What method?i didnt put a method in there as admins don't have courses.
I don't know, the only place in your code that courses are mentioned is in a text string that is printed.should i still be defining a method in the abstract class for courses?
or am i doing this completely wrong?
Perhaps if you explained clearly and concisely what it is you're trying to achieve, it might become clearer.
Incidentally, to save a lot of confusion, please use the Java naming conventions - class names start with an UPPERCASE letter, variable and method names start with a lowercase letter.
- 05-12-2011, 01:16 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
Sorry.. all classes are below.
im reading a txt file of users. i have to determine if they are instructors or admins and create objects of them.
i am trying to print out the courses property on given to instructors but i get
i didn't make one in the abstract User class because admin doesn't use courses. so now i'm wondering if i have done this completely wrong :confused:xception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getCourses() is undefined for the type User
at RequestApp.main(RequestApp.java:27)
User class
Java Code:public abstract class User { public String username; public String password; public abstract String getUsername(); public abstract String getPassword(); public abstract String getRole(); }
Instructor class
Java Code:public class Instructor extends User { private String courses; private String role = "instructor"; Instructor(String line) { String[] userDetails = line.split("\\s+\\|\\s+"); if (userDetails.length == 4) { username = userDetails[0]; password = userDetails[1]; courses = userDetails[3]; } else if (userDetails.length == 3) { username = userDetails[0]; password = userDetails[1]; courses = "none"; } } public String getUsername(){ return username; } public String getPassword(){ return password; } public String getRole(){ return role; } public String getCourses(){ return this.courses; } }
Admin class
Main..Java Code:public class Admin extends User { private String role = "admin"; Admin(String line) { String[] userDetails = line.split("\\s+\\|\\s+"); username = userDetails[0]; password = userDetails[1]; } public String getUsername(){ return username; } public String getPassword(){ return password; } public String getRole(){ return role; } }
Java Code:mport java.io.*; public class RequestApp { private static String[] users; private static String[] requests; private static User[] userArray; public static void main(String[] args) throws IOException { // read login text file and create user objects try { ReadFile file = new ReadFile(); userArray = file.openFile("login.txt"); } catch (IOException e) { System.out.println(e.getMessage()); } for(int i = 0; i < userArray.length; i++){ System.out.println("username: " + userArray[i].getUsername()); System.out.println("password: " + userArray[i].getPassword()); System.out.println("role: " + userArray[i].getRole()); if(userArray[i].getRole().equals("instructor")){ System.out.println("courses: "+ userArray[i].getCourses()); } System.out.println(); }
- 05-12-2011, 02:30 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Can I suggest overriding the toString() method in your User and Instructor classes?
The User toString() will build a String containing the username/password/role.
The Instructor class will call the User toString (super.toString()) and append the course data to it.
Your printout code will then simply loop round the user list calling System.out.println(userArray[i].toString()).
Similar Threads
-
abstract classes
By renju krishnan in forum New To JavaReplies: 1Last Post: 09-29-2010, 08:31 AM -
question about abstract classes
By TheFlying_Boy in forum New To JavaReplies: 6Last Post: 07-08-2009, 07:19 AM -
interface vs abstract classes
By rosh72851 in forum New To JavaReplies: 7Last Post: 11-16-2008, 08:22 PM -
Inverfaces vs Abstract Classes
By ravian in forum New To JavaReplies: 1Last Post: 11-28-2007, 09:53 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks