Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
class Book {

int stockNum;
String author;
String title;
double price;
int pages;

public int getStockNum() {
return stockNum;
}
public void setStockNum(int stockNum) {
this.stockNum = stockNum;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPages() {
return pages;
}
public void setPages(int pages) {
this.pages = pages;
}

public Book() {
super();
}
public Book(int stockNum, String author, String title, double price, int pages) {
super();
this.stockNum = stockNum;
this.author = author;
this.title = title;
this.price = price;
this.pages = pages;
}

@Override
public String toString() {
return "Stock Number: " + stockNum + ", Author: " + author + ", Title: " + title + ", Price: " + price
+ ", Pages: " + pages;
}





}

class TextBook extends Book{
private int gradelevelbook;

public int getGradelevelbook() {
return gradelevelbook;
}

public void setGradelevelbook(int gradelevelbook) {
this.gradelevelbook = gradelevelbook;
}







public class BookApplication {

public void main(String[] args) {

Book book1 = new Book();
book1.setAuthor("Chad");
book1.setPages(390);
book1.setPrice(345);
book1.setStockNum(2);
book1.setTitle("Intro to C++");

TextBook tb1=new TextBook();

tb1.setGradelevelbook(3);
tb1.setAuthor("dennis ritchie");
tb1.setPages(566);
tb1.setPrice(200);
tb1.setStockNum(5);
tb1.setTitle("Java beginer");

System.out.println(book1.getAuthor()+" "+book1.getPages()+" "+book1.getPrice()+" "+book1.getStockNum()+" "+book1.getTitle());
System.out.println(tb1.getAuthor()+" "+tb1.getPages()+" "+tb1.getPrice()+" "+tb1.getPrice()+" "+tb1.getStockNum()+" "+tb1.getTitle()+" "+tb1.getGradelevelbook());
}

}
}

What I have tried:

Every time I run this code I get an error code saying it's missing the "Public static void main" but when I add it, it creates more error codes. What should I do?
Posted
Updated 25-Apr-20 12:02pm

Your main problem is the fact that the braces are out of kilter.

Your class 'TextBook' is missing a closing brace which means the compiler thinks that your 'BookApplication' class is part of the 'TextBook' class.

Once you correct the brace problem you can then set the main method signature to include the static keyword which will be required for the main entrypoint (see corrected code below
Java
class TextBook extends Book {
	private int gradelevelbook;

	public int getGradelevelbook() {
		return gradelevelbook;
	}

	public void setGradelevelbook(int gradelevelbook) {
		this.gradelevelbook = gradelevelbook;
	}
}                                       // THIS BRACE WAS MISSING

public class BookApplication {

		public static void main(String[] args) {           // CORRECTED METHOD SIGNATURE TO INCLUDE STATIC

			Book book1 = new Book();
			book1.setAuthor("Chad");
			book1.setPages(390);
			book1.setPrice(345);
			book1.setStockNum(2);
			book1.setTitle("Intro to C++");

			TextBook tb1 = new TextBook();

			tb1.setGradelevelbook(3);
			tb1.setAuthor("dennis ritchie");
			tb1.setPages(566);
			tb1.setPrice(200);
			tb1.setStockNum(5);
			tb1.setTitle("Java beginer");

			System.out.println(book1.getAuthor() + " " + book1.getPages() + " " + book1.getPrice() + " "
					+ book1.getStockNum() + " " + book1.getTitle());
			System.out.println(tb1.getAuthor() + " " + tb1.getPages() + " " + tb1.getPrice() + " " + tb1.getPrice()
					+ " " + tb1.getStockNum() + " " + tb1.getTitle() + " " + tb1.getGradelevelbook());
		}
	}
                // REMOVED EXCESS BRACE FROM HERE
 
Share this answer
 
v2
It means you forgot a keyword:
Java
public class BookApplication {

public void main(String[] args) {

Book book1 = new Book();
Try this:
Java
public class BookApplication {

public static void main(String[] args) {

Book book1 = new Book();
Your app needs one - and only one - main method, and it must be declared as static so that it is not associated with any specific instance of a class.
Then it becomes the "first method" that is executed in your code when the app starts.
 
Share this answer
 
Comments
NikonMakeup 25-Apr-20 14:57pm    
This is the error code I keep getting:

Error: Main method not found in class Book, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Formatting code may be purely aesthetic, but it makes it a lot easier to read and find things like misplaced and/or missing closing brackets.
Besides some simple indents, a couple of comments I added to your code should show you where your problem lies
Java
class Book {
   int stockNum;
   String author;
   String title;
   double price;
   int pages;

   public int getStockNum() { return stockNum; }
   public void setStockNum(int stockNum) { this.stockNum = stockNum; }

   public String getAuthor() { return author; }
   public void setAuthor(String author) { this.author = author; }

   public String getTitle() { return title; }
   public void setTitle(String title) { this.title = title; }

   public double getPrice() { return price; }
   public void setPrice(double price) { this.price = price; }

   public int getPages() { return pages; }
   public void setPages(int pages) { this.pages = pages; }

   public Book() { super(); }

   public Book(int stockNum, String author, String title, double price, int pages) {
      super();
      this.stockNum = stockNum;
      this.author = author;
      this.title = title;
      this.price = price;
      this.pages = pages;
   }

   @Override public String toString() {
      return "Stock Number: " + stockNum + ", Author: " + author + ", Title: " + title + ", Price: " + price + ", Pages: " + pages;
   }
}


class TextBook extends Book{
   private int gradelevelbook;

   public int getGradelevelbook() { return gradelevelbook; }
   public void setGradelevelbook(int gradelevelbook) { this.gradelevelbook = gradelevelbook; }

// missing closing brace here.... 
  // results in this being part of Textbook class
   public class BookApplication {
      public void main(String[] args) {
         Book book1 = new Book();
         book1.setAuthor("Chad");
         book1.setPages(390);
         book1.setPrice(345);
         book1.setStockNum(2);
         book1.setTitle("Intro to C++");

         TextBook tb1=new TextBook();
         tb1.setGradelevelbook(3);
         tb1.setAuthor("dennis ritchie");
         tb1.setPages(566);
         tb1.setPrice(200);
         tb1.setStockNum(5);
         tb1.setTitle("Java beginer");

         System.out.println(book1.getAuthor()+" "+book1.getPages()+" "+book1.getPrice()+" "+book1.getStockNum()+" "+book1.getTitle());
         System.out.println(tb1.getAuthor()+" "+tb1.getPages()+" "+tb1.getPrice()+" "+tb1.getPrice()+" "+tb1.getStockNum()+" "+tb1.getTitle()+" "+tb1.getGradelevelbook());
      }
   }

} // extra brace here
 
Share this answer
 
Comments
NikonMakeup 25-Apr-20 21:44pm    
I tried that but it still doesn't work
MadMyche 25-Apr-20 21:53pm    
What did you try?
NikonMakeup 26-Apr-20 10:19am    
Your suggestion

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900