Results 1 to 8 of 8

Thread: Constructors

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Constructors

    I'm taking inheritance now and some things look confusing. I'm trying to do the following but the code, in particular, the constructor in subclasses are pointed to be wrong. The constructor in LongestWord is not correct, could smb help me how to pass the filename into the constructor?
    Java Code:
    import java.util.Scanner;
    import java.io.File;
    
    public abstract class FileProcessor {
    
    	protected Scanner input;
    	
    	public FileProcessor(String filename) throws Exception
    	{
    		this.input = new Scanner(new File(filename));
    	}
    		public void processAll()
    	{
    		while(input.hasNext())
    		{
    			processToken();
    		}
    		this.input.close();
    	}
    	public abstract void processToken();
    	public abstract String reportResult();
    }
    
    import java.util.Scanner;
    
    
    public class LongestWord extends FileProcessor {
    	
    	String longestWord = "";
    	String firstWord = "";
    	int number = 0;
    	
    	Scanner kbd = new Scanner(System.in);
    	int sum = 0;
    	public LongestWord(String input) throws Exception
    	{
    		super(input);
    	}
    	public void processToken()
    	{
    		int nextNumber = 0;
    		
    		while(input.hasNext())
    		{
    			int number1 = 0;
    			if(input.hasNextInt())
    			{
    				nextNumber =  input.nextInt();
    			    sum += nextNumber;
    			}
    			    else 
    				input.next();
    			firstWord = kbd.next();
    			if(firstWord.length() > input.next().length())
    			{
    				longestWord = firstWord;
    			}
    				
    		}
    		
    	}
    	
    	public String reportResult() {
    		
    		return "Output" + " "  + longestWord  + sum;
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,007
    Rep Power
    34

    Default Re: Constructors

    constructor in subclasses are pointed to be wrong.
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Psychrom is offline Member
    Join Date
    Feb 2015
    Posts
    4
    Rep Power
    0

    Default Re: Constructors

    place the
    Java Code:
    throws Exception
    after "FileProcessor" in
    Java Code:
    public class LongestWord extends FileProcessor{
    and
    Java Code:
     public abstract class FileProcessor {
    in your code.

    Your constructors need to match their classes, up to the class name.
    All of your constructors for your non-abstracted class should look similar to
    Java Code:
    public LongestWord(//any parameters here){

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    27

    Default Re: Constructors

    Quote Originally Posted by Psychrom View Post
    place the
    Java Code:
    throws Exception
    after "FileProcessor" in
    Java Code:
    public class LongestWord extends FileProcessor{
    Nope.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Psychrom is offline Member
    Join Date
    Feb 2015
    Posts
    4
    Rep Power
    0

    Default Re: Constructors

    I'm also fairly certain that abstract classes cannot have defined methods.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Constructors

    Quote Originally Posted by Psychrom View Post
    I'm also fairly certain that abstract classes cannot have defined methods.
    And you would be incorrect! Fairly certain implies some doubt. So, when in doubt, try it out.

    Regards,
    Jim
    Last edited by jim829; 02-27-2015 at 09:21 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    13

    Default Re: Constructors

    Quote Originally Posted by jim829 View Post
    When in doubt, just try it out.

    Regards,
    Jim
    It rolls off the tongue almost like a song, that's how logical that is.

    Pre-java 8 we would be talking about interfaces here, but since Java 8 even interfaces can have pre-defined methods.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Constructors

    Be extremely careful with calling methods defined in a sub class from the constructor of the super class, i.e. the sub class object isn't properly defined yet when the constructor of the super class executes.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Help with constructors
    By monopolyman900 in forum New To Java
    Replies: 1
    Last Post: 03-04-2013, 12:21 AM
  2. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 11:05 AM
  3. Need help with constructors
    By tpfaff in forum New To Java
    Replies: 10
    Last Post: 10-22-2010, 04:33 AM
  4. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 12:10 AM
  5. Constructors?
    By annna in forum New To Java
    Replies: 3
    Last Post: 01-27-2010, 10:51 PM

Posting Permissions

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