Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Class, Parent Help

    Ok need your help again.

    My instructions are to create a class named Book that contains data fields for title and number of pages. Include get and set methods for these fields. Next, create a subclass named TextBook, which contains additional field that holds a grade level for the TextBook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class.

    Book.java

    PHP Code:
    //Book.java by Tyler 11/10/2012
    
    import java.util.*;
    
    public class Book
    
    {
    private int pageNum;
    private String title;
    Date today = new Date();
    
    	public Book(int pn, String name)
    	{
    	pageNum = pn;
    	title = name;
    	}
    
    	//Setter/Getter for pageNum
    	public int getpageNum()
    	{
    	return pageNum;
    	}
    	
    	public void setpageNum(int pn)
    	{
    	pageNum = pn;
    	}
    	//Setter/Getter for Title
    	public String getTitle()
    	{
    	return title;
    	}
    	
    	public void setTitle(String name)
    	{
    	title = name;
    	}
    	
    	public void display()
    	{
    	System.out.println("Book.java" + "\nby Tyler " + today);
    	System.out.println("Book Title: " + title + "\nNumber of Pages: " + pageNum);
    	}
    }
    TextBook.java
    PHP Code:
    //TextBook.java by Tyler 11/10/2012
    
    public class TextBook extends Book
    {
    private String gradeLevel;
    
    public TextBook(int pageNum, String title, String gl)
    	{
    	super(pageNum, title);
    	gradeLevel = gl;
    	}
    	
    	//Setter/Getter for gradeLevel
    	public String getGradeLevel()
    	{
    	return gradeLevel;
    	}
    	
    	public void setGradeLevel(String g)
    	{
    	gradeLevel = g;
    	}
    
    	public void display()
    	{
    	super.display();
    	System.out.println("Grade Level: " + gradeLevel);
    	}
    
    }
    DemoBook.java

    PHP Code:
    //DemoBook.java by Tyler 11/10/2012
    
    public class DemoBook
    {
    	public static void main(String[] args)
    	{
    	Book oneBook = new Book(250, "Awesome Story");
    	TextBook oneTextBook = new TextBook(350, "Awesomer Story", 12);
    	
    	oneBook.display();
    	oneTextBook.display();
    	}
    }
    Everything has compiled fine. However when I try to run DemoBook.java from the Windows cmd prompt I get an error that says:

    C:\Users\Tyler\Documents\School Work\CIS280\Project 6>java DemoBook.java
    Exception in thread "main" java.lang.NoClassDefFoundError: DemoBook/java
    Caused by: java.lang.ClassNotFoundException: DemoBook.java
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: DemoBook.java. Program will exit.
    I have no idea what this error means. Ive only seen this when you try to run a program without a main. However this has a main. Not sure whats wrong or where to look.

  2. #2
    Join Date
    Oct 2012
    Posts
    20
    Rep Power
    0

    Default Re: Class, Parent Help

    Fixed it. Changed the setter/getter in TextBook from calling in a String G to String GL and it worked.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Class, Parent Help

    And, presumably, ran it using:
    java DemoBook
    rather than:
    java DemoBook.java
    which is incorrect.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  3. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. Replies: 7
    Last Post: 05-06-2009, 12:42 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
  •