Please Help stuck on Inheritances and abstract classes.
I am not asking you to do my homework. I am legitlly stuck on this and cannot figure out how to fix it. It seems that i cannot get the packages to see each other. Thanks for any Help.
Using Jgrasp
Code :
public abstract class Book
{
private String title;
private double price;
public abstract void setPrice();
public double getPrice()
{
return price;
}
public void setTitle(String bk_title)
{
title = bk_title;
}
public String getTitle()
{
return title;
}
}
Code :
import Book.java;
public class Fiction extends Book
{
Fiction fic = new Fiction();
public void setPrice(double bk_price)
{
price = bk_price;
}
fic.getPrice()
{
return price;
}
}
Code :
public class Nonfiction extends Book
{
Nonfiction nonfic = new Nonfiction();
nonfic.setPrice(double)
{
price = 37.99;
}
nonfic.getPrice()
{
return price;
}
}
Code :
import Book.java;
import Fiction.java;
import NonFiction.java;
public class UseBook {
public static void main(String[]args)
{
System.out.println("price for fiction: $" + price);
System.out.println("price for nonfiction: $" + bk_price);
}
}
Here are the errors:
UseBook.java:1: error: package Book does not exist
import Book.java;
^
UseBook.java:2: error: package Fiction does not exist
import Fiction.java;
^
UseBook.java:3: error: package NonFiction does not exist
import NonFiction.java;
^
UseBook.java:9: error: cannot find symbol
System.out.println("price for fiction: $" + price);
^
symbol: variable price
location: class UseBook
UseBook.java:10: error: cannot find symbol
System.out.println("price for nonfiction: $" + bk_price);
^
symbol: variable bk_price
location: class UseBook
5 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Re: Please Help stuck on Inheritances and abstract classes.
See Using Package Members (The Java™ Tutorials > Learning the Java Language > Packages)
If all classes are in the same package, you do not need to explicitly import the classes. And further, the import is by class name, not by file source name (MyClass rather than MyClass.java)