Results 1 to 4 of 4
  1. #1
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default 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

    Java Code:
    if(userArray[i].getRole().equals("instructor")){
                System.out.println("courses: "+ userArray[i]);
             }
    but eclipse is telling me that this method doesn't exist for class User.
    in class User i have

    Java Code:
    public abstract class User {
       public String username;
       public String password;
       
       public abstract String getUsername();
       public abstract String getPassword();
       public abstract String getRole();
       
       
    }
    i didnt put a method in there as admins don't have courses.

    should i still be defining a method in the abstract class for courses?

    or am i doing this completely wrong?

    thanks

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by maknib View Post
    i want to print out the courses so i tried

    Java Code:
    if(userArray[i].getRole().equals("instructor")){
                System.out.println("courses: "+ userArray[i]);
             }
    That code tries to print out the user (if it's an instructor). I don't see any courses in your code at all.

    eclipse is telling me that this method doesn't exist for class User.
    Which method? Post up the full error message text.

    i didnt put a method in there as admins don't have courses.
    What method?

    should i still be defining a method in the abstract class for courses?
    or am i doing this completely wrong?
    I don't know, the only place in your code that courses are mentioned is in a text string that is printed.

    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.

  3. #3
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    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.

    What method?



    I don't know, the only place in your code that courses are mentioned is in a text string that is printed.

    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.

    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

    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)
    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:



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

    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();
          }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,009
    Rep Power
    20

    Default

    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

  1. abstract classes
    By renju krishnan in forum New To Java
    Replies: 1
    Last Post: 09-29-2010, 08:31 AM
  2. question about abstract classes
    By TheFlying_Boy in forum New To Java
    Replies: 6
    Last Post: 07-08-2009, 07:19 AM
  3. interface vs abstract classes
    By rosh72851 in forum New To Java
    Replies: 7
    Last Post: 11-16-2008, 08:22 PM
  4. Inverfaces vs Abstract Classes
    By ravian in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •