Results 1 to 13 of 13
Thread: Inheritance help?
- 02-03-2015, 02:40 PM #1
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
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 02:42 PM. Reason: added [code] ... [/code] tags
- 02-03-2015, 03:00 PM #2
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.
- 02-03-2015, 03:22 PM #3
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
Re: Inheritance help?
It creates an object and then the "mainMethod" is a method.
- 02-03-2015, 03:26 PM #4
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Inheritance help?
What is "it" ?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 02-03-2015, 03:29 PM #5
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
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.
- 02-03-2015, 03:32 PM #6
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.
- 02-03-2015, 03:50 PM #7
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
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.");
}
}
}
}
- 02-03-2015, 04:00 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Inheritance help?
Use equals and not == for testing strings. Although I am not certain that solves your problem, it is a problem.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 02-03-2015, 04:18 PM #9
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.
- 02-03-2015, 05:59 PM #10
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 02-04-2015, 01:06 PM #11
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
Re: Inheritance help?
Yes, this is part of an assignment.
- 02-04-2015, 01:31 PM #12
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 02-04-2015, 01:35 PM #13
Member
- Join Date
- Feb 2015
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
hi need help with inheritance
By lisa.abraham in forum New To JavaReplies: 7Last Post: 07-21-2012, 08:01 AM -
Inheritance
By NewToJava_99 in forum New To JavaReplies: 2Last Post: 05-20-2012, 09:03 PM -
Help with inheritance
By waker3210 in forum Java GamingReplies: 6Last Post: 05-19-2012, 09:27 PM -
JPA Inheritance
By videanuadrian in forum Advanced JavaReplies: 0Last Post: 01-11-2011, 07:30 AM -
Inheritance example
By kris4u4ever in forum New To JavaReplies: 3Last Post: 03-21-2009, 03:53 PM
Bookmarks