Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Java abstract

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java abstract

    Could someone help me figure out what am I missing? It not printing out "Math" in my main class. Thanks

    public abstract class Homework
    {   
        private int pagesRead; 
        private String typeHomework;
     
        public Homework()
        {
            pagesRead = 0 ;
            typeHomework = "";
        }
        public void setPage(int page)
        {
            pagesRead = page;
        }
     
        public int getPage()
        {
            return pagesRead;
        }
     
     
        public void setType(String type)
        {
            typeHomework = type;
        }
     
        public String getType()
        {
            return typeHomework;
        }
     
        public abstract void createAssignment(int p);
     
     
    }

    public class MyMath extends Homework
    {
        public MyMath()
        {
        }
     
        public void createAssignment(int p)
        {
            setPage(p);
            setType("Math");    
        }
     
        public String toString()
        {
            return getType() + " - " + getPage();
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstract

    Where is the main class, what is the name of the main class and where are the methods that are supposed to cause "Math" to be printed.

    There is no main() method in the posted code. A main() method is needed to be able to start execution of a class with the java command.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract

    public class testHomework
    {
        public static void main(String[]args)
    {
            MyMath math = new MyMath();
     
     
            System.out.println(math.toString());
     
        }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstract

    What prints out when the code is executed? Why do you expect anything different to be printed?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract

    It print out nothing. Since I "setType("Math"); " won't it print out "Math"?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java abstract

    It print out nothing
    Are you sure? This line must print something:
    System.out.println(math.toString());

    In what method is setType("Math") called? Then where is that method called?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java abstract

    I forgot about calling the method, I thought it would call itself for some reason. Thanks you so much!

Similar Threads

  1. Java Abstract class and Interface issues in a code
    By John234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 09:51 PM
  2. [SOLVED] Abstract method in a non-abstract class
    By bassie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 2nd, 2012, 11:27 AM
  3. GUI error: is not abstract and does not override abstract method
    By djl1990 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 21st, 2011, 01:26 PM
  4. Java: interfaces and abstract classes
    By pinansonoyon in forum Object Oriented Programming
    Replies: 1
    Last Post: May 6th, 2010, 10:17 AM
  5. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM