11 Replies - 448 Views - Last Post: 15 February 2009 - 01:40 PM Rate Topic: -----

#1 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Help with one line of program

Post icon  Posted 15 February 2009 - 12:10 PM

I am receiving an error:
invalid method declaration; return type required
Book(String in_title, String in_publisher, String in_author, int in_copies, int in_dpublication, int in_book_num, int in_price)
^
Can you provide any insight? Thanks.

import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;

public class BookCH5
{
private String title;
private String publisher;
private String author;
private int copies;
private int dpublication = 1900;
private int book_num;
private int price;

Book(String in_title, String in_publisher, String in_author, int in_copies, int in_dpublication, int in_book_num, int in_price)
{
title = in_title;
publisher=in_publisher;
author=in_author;
copies=in_copies;
dpublication=in_dpublication;
book_num=in_book_num;
price=in_price;
}
public String get_title()
{
return title;
}
public String get_publisher()
{
return publisher;
}
public String get_author()
{
return author;
}
public void setCopies(int copies)
{
copies = 100;
}
public int get_copies()
{
return copies;
}
public void setYear(int dpublication)
{
dpublication = 1900;
}
public int getdpublication()
{
return dpublication;
}
public int get_book_num()
{
return book_num;
}
public void setPrice(double price)
{
price = 100;
}
public int get_price()
{
return price;
}
{
String display1 = "This program will ask you to enter the isbn numbers, price and number of copies in stock for ten Books then ask you whether or not you would like to view the records sorted or unsorted.";
String display2 = "Thank you. Click OK to display the unsorted records.";
String display3 = "Click OK to display the records sorted by stock number.";
String display4 = "Click OK to display the records sorted by title.";
String ques1= "Please enter the isbn number of the Book.";
String ques2= "Please enter the price of the Book.";
String ques3= "Please enter the number of copies in stock.";
String ques4= "Please enter the year of publication.";
String ques5= "Please enter the title of the book.";
String ques6= "Please enter the author of the book.";
String bookName;
String title= "";
String publisher= "";
String author= "";
int counter = 0;
int rand_num;
int rand_year;
int copies;
double rand_price;


Book[] someBooks = new Book[10];

JOptionPane.showMessageDialog(null, display1, "Program Notification", JOptionPane.INFORMATION_MESSAGE);

for(counter=0; counter<10;++counter)
{
bookName=JOptionPane.showInputDialog(null,ques1);
rand_num=(int)(Math.random() * 9999);
bookName=JOptionPane.showInputDialog(null,ques2);
rand_price=(Math.random() * 100);
bookName=JOptionPane.showInputDialog(null,ques3);
copies=(int)(Math.random() * 100);
bookName=JOptionPane.showInputDialog(null,ques4);
rand_year=(int)(Math.random() * 1900);
title=JOptionPane.showInputDialog(null,ques5);
author=JOptionPane.showInputDialog(null,ques6);
someBooks[counter] = new Book(bookName, rand_num, rand_price, copies, rand_year, title, author);
//CONSTRUCTOR IS UNDEFINED(String, int, double, int, int, String, String)
}
JOptionPane.showMessageDialog(null, display2, "Program Notification", JOptionPane.INFORMATION_MESSAGE);

for(counter = 0; counter<10; ++counter)
{
System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title());
}
JOptionPane.showMessageDialog(null,display3, "Program Notification", JOptionPane.INFORMATION_MESSAGE);
System.out.println("\n\n");

for(counter = 0; counter<10;++counter)
{
System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title());
}
JOptionPane.showMessageDialog(null,display4, "Program Notification",JOptionPane.INFORMATION_MESSAGE);
System.out.println("\n\n");

for(counter = 0; counter<10;++counter)
{
System.out.println(someBooks[counter].get_copies() + "\t\t" + someBooks[counter].get_title());
}
System.exit(0);
}
}

Is This A Good Question/Topic? 0
  • +

Replies To: Help with one line of program

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 12:18 PM

change your class name to Book
Was This Post Helpful? 0
  • +
  • -

#3 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Help with one line of program

Posted 15 February 2009 - 12:29 PM

If I do that then I get the error below

class Book is public, should be declared in a file named Book.java
public class Book

THanks.
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 12:33 PM

yes, you will also have to change the source file's name to Book
Was This Post Helpful? 0
  • +
  • -

#5 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Help with one line of program

Posted 15 February 2009 - 12:35 PM

Okay, I figured that part out - it was because I did not save the program with that particular name.

Now I receive 1 more error and I am stuck.

Book.java:103: cannot find symbol
symbol : constructor Book(java.lang.String,int,double,int,int,java.lang.String,java.lang.String)
location: class Book
someBooks[counter] = new Book(bookName, rand_num, rand_price, copies, rand_year, title, author);
^
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 12:39 PM

make sure that you pass the arguments to your constructor in the same format and order that you have declared above
Was This Post Helpful? 0
  • +
  • -

#7 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Help with one line of program

Posted 15 February 2009 - 12:47 PM

I thought I did. I have first BookName, then rand_num, then rand_price, then copies, then rand_year, then title, then author.
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 01:05 PM

ok, i had to clean up a lot of things in your code to get the basic stuff to work, so i took some stuff out..now you can modify it to your needs
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;

public class Book
{
	private String title;
	private String publisher;
	private String author;
	private int copies;
	private int dpublication;
	private int book_num;
	private double price;

	public Book(String in_title, String in_publisher, String in_author, int in_copies, int in_dpublication, int in_book_num, double in_price)
	{
		title = in_title;
		publisher=in_publisher;
		author=in_author;
		copies=in_copies;
		dpublication=in_dpublication;
		book_num=in_book_num;
		price=in_price;
	}

	public String get_title()
	{
		return title;
	}

	public String get_publisher()
	{
		return publisher;
	}

	public String get_author()
	{
		return author;
	}

	public void setCopies(int copies)
	{
		copies = 100;
	}

	public int get_copies()
	{
		return copies;
	}

	public void setYear(int dpublication)
	{
		dpublication = 1900;
	}

	public int getdpublication()
	{
		return dpublication;
	}

	public int get_book_num()
	{
		return book_num;
	}

	public void setPrice(double price)
	{
		price = 100;
	}

	public double get_price()
	{
		return price;
	}

	public static void main(String[]args)
	{
		String bookName;
		String title;
		String publisher;
		String author;
		int counter = 0;
		int rand_num;
		int rand_year;
		int copies;
		double rand_price;
		Book[] someBooks = new Book[10];
		for(counter=0; counter<10;counter++)
		{
			rand_num=(int)(Math.random() * 9999);
			rand_price=(Math.random() * 100);
			copies=(int)(Math.random() * 100);
			rand_year=(int)(Math.random() * 1900);
			title=JOptionPane.showInputDialog(null,"what's the title of the book?");
			author=JOptionPane.showInputDialog(null,"who is the author?");
			publisher=JOptionPane.showInputDialog(null,"who is the publisher?"); 
			someBooks[counter] = new Book(title,publisher ,author, rand_num,copies, rand_year,rand_price);
		}
	}
} 


This post has been edited by mostyfriedman: 15 February 2009 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#9 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Help with one line of program

Posted 15 February 2009 - 01:22 PM

Thank you. Now it keeps looping to input information. Is that because it wants 100 books entered?
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 01:27 PM

its because you made a loop to enter information of 10 books and store then in an array
Was This Post Helpful? 0
  • +
  • -

#11 JavaLost  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Help with one line of program

Posted 15 February 2009 - 01:29 PM

Oh. Thank you.
Was This Post Helpful? 0
  • +
  • -

#12 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Help with one line of program

Posted 15 February 2009 - 01:40 PM

no problem ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1