Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Post Inheritance help?

    I am using a program called BlueJ for Java. When I compile for errors, it says there is nothing wrong but when I run it, I only get the first Gui of where I can choose what "Genre" I want. It is supposed to go on after that and print out a list of recommended books based on the genre. This is a portion of the code but after it there are just more things to print books....

    Java Code:
    import javax.swing.*;
    import java.util.Arrays;
    
    public class ReadingMaterial
    {
        String title, author;
        String posVal;
        public void mainMethod()
        {
            String[] possibleValue = { "Fiction", "Non-Fiction", "Textbook", "Magazine" };
    
            Object selectedValue = JOptionPane.showInputDialog(null,
                 "Choose one", "Input",
                 JOptionPane.INFORMATION_MESSAGE, null,
                 possibleValue, possibleValue[0]);
                 String posVal = Arrays.toString(possibleValue);
        }
        public class FictionBook extends ReadingMaterial
        {
            public void Fiction()
            {
                if (posVal == "Fiction")
                {
                    System.out.println("Title: In Cold Blood");
                    System.out.println("ISBN: 0679745580");
                    System.out.println("Author: Capote, Truman");
                    System.out.println("Title: When I Found You");
                    System.out.println("ISBN: 161109979X");
                    System.out.println("Author: Hyde, Catherine");
                    System.out.println("Title: The Girl on a Train: The Novel");
                    System.out.println("ISBN: 1594633665");
                    System.out.println("Author: Hawkins, Paula");
                }
    }
    }
    Last edited by JosAH; 02-03-2015 at 01:42 PM. Reason: added [code] ... [/code] tags

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

    Default Re: Inheritance help?

    How do you execute the code for testing? There is no main() method.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Inheritance help?

    It creates an object and then the "mainMethod" is a method.

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

    Default Re: Inheritance help?

    What is "it" ?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Inheritance help?

    It is BlueJ.......You right click on a box that represents a class and then click on "ReadingMaterial" and an object is created as well as a red box. Right click the red box and the code is executed.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Inheritance help?

    It sounds like the problem is with the use of BlueJ, not a java programing problem.

    The posted code is not complete for executing as a java program.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Inheritance help?

    Fine. Here is the whole thing. Thanks in advance. And sorry for the whole thing.

    import javax.swing.*;
    import java.util.Arrays;

    public class ReadingMaterial
    {
    String title, author;
    String posVal;
    public void mainMethod()
    {
    String[] possibleValue = { "Fiction", "Non-Fiction", "Textbook", "Magazine" };

    Object selectedValue = JOptionPane.showInputDialog(null,
    "Choose one", "Input",
    JOptionPane.INFORMATION_MESSAGE, null,
    possibleValue, possibleValue[0]);
    String posVal = Arrays.toString(possibleValue);
    }
    public class FictionBook extends ReadingMaterial
    {
    public void Fiction()
    {
    if (posVal == "Fiction")
    {
    System.out.println("Title: In Cold Blood");
    System.out.println("ISBN: 0679745580");
    System.out.println("Author: Capote, Truman");
    System.out.println("Title: When I Found You");
    System.out.println("ISBN: 161109979X");
    System.out.println("Author: Hyde, Catherine");
    System.out.println("Title: The Girl on a Train: The Novel");
    System.out.println("ISBN: 1594633665");
    System.out.println("Author: Hawkins, Paula");
    }
    }
    }
    public class NonBook extends ReadingMaterial
    {
    public void NonFiction()
    {
    if (posVal == "Non-Fiction")
    {
    System.out.println("A Short History of Nearly Everything");
    System.out.println("ISBN: 076790818X");
    System.out.println("Author: Bryson,Bill");
    System.out.println("Title: Spilled Milk");
    System.out.println("ISBN: 0615835600");
    System.out.println("Author: Randis, K.L.");
    System.out.println("Title: Ten Days In A Mad-House");
    System.out.println("ISBN: 146369539X");
    System.out.println("Author: Bly, Nellie");
    }
    }
    }
    public class Textbook extends ReadingMaterial
    {
    public void Textbooks()
    {
    if (posVal == "Textbook")
    {
    System.out.println("Campbell Biology");
    System.out.println("ISBN: 0321558235");
    System.out.println("Author: Reece, Jane");
    System.out.println("Title: Textbook of Neonatal Resuscitation");
    System.out.println("ISBN: 1581104987");
    System.out.println("Author: American Heart Association");
    System.out.println("Title: Delmar's Standard Textbook of Electricity");
    System.out.println("ISBN: 1111539154");
    System.out.println("Author: Herman, Stephen L.");
    }
    }
    }
    public class Magazine extends ReadingMaterial
    {
    public void Mag()
    {
    if (posVal == "Magazine")
    {
    System.out.println("The Heretic Magazine");
    System.out.println("ISBN: --");
    System.out.println("Publisher: The Heretic Publishing");
    System.out.println("Title: Lightspeed Magazine");
    System.out.println("ISBN: --");
    System.out.println("Author: Adams, John");
    System.out.println("Title: The Modern Magazine");
    System.out.println("ISBN: 1780672985");
    System.out.println("Author: Leslie, Jeremy");
    }
    else
    {
    System.out.println("Genre not found. Please try again.");
    }
    }
    }
    }

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

    Default Re: Inheritance help?

    Use equals and not == for testing strings. Although I am not certain that solves your problem, it is a problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Inheritance help?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Inheritance help?

    On top of what Jim says:

    1) you ask a value 'selectedValue' of the user. But you don't use it anywhere in your code.

    2) You have four inner classes (FictionBook, NonBook, etc.); you don't use them anywhere right now. As an example: the FictionBook.Fiction() method is -never- called.


    It looks like you don't need those inner classes at all, why did you create them? Is that part of an assignment?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

    Default Re: Inheritance help?

    Yes, this is part of an assignment.

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

    Default Re: Inheritance help?

    Well then you're trying to do too much in one go - remove the inner classes for now and first try to create some code which at least outputs what you want. Then when you have that, try to split up that code into separate classes. After you re-read the chapters/paragraphs of your study book about using classes and methods and about inheritance.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    Join Date
    Feb 2015
    Posts
    6
    Rep Power
    0

Similar Threads

  1. hi need help with inheritance
    By lisa.abraham in forum New To Java
    Replies: 7
    Last Post: 07-21-2012, 07:01 AM
  2. Inheritance
    By NewToJava_99 in forum New To Java
    Replies: 2
    Last Post: 05-20-2012, 08:03 PM
  3. Help with inheritance
    By waker3210 in forum Java Gaming
    Replies: 6
    Last Post: 05-19-2012, 08:27 PM
  4. JPA Inheritance
    By videanuadrian in forum Advanced Java
    Replies: 0
    Last Post: 01-11-2011, 06:30 AM
  5. Inheritance example
    By kris4u4ever in forum New To Java
    Replies: 3
    Last Post: 03-21-2009, 02:53 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
  •