Hi sorry beginner coder here and I am not good at explaining things but I needed help and was wondering why I keep getting this error no matter how I format or rearrange the date name and title. so I just was wondering if anyone can help on what order I am suppose put the name dates and titles in? I am using BlueJ compiler.
Here is my code for the issue that I am having:
public BookStore()
{
inventory = new Book[100];
inventory[0] = new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
inventory[1] = new Book(2013, "THE GREAT GATSBY", "F. Scott Fitzgerald");
I keep getting this error no suitable constructor found for Book(java.lang.String,java.lang.String,int,int,int,java.lang.String) constructor Book.Book() is not applicable; (actual and formal arguments list differ in length); constructor Book.Book(Author,Date,java.lang.String) is not applicable (actual and formal argument lists differ in length)
Here is the Book class:
private static final String DEFAULT_TITLE = "Untitled";
private Author author;
private Date published;
private String title;
public Book()
{
this.author = new Author();
this.published = new Date();
this.title = DEFAULT_TITLE;
} // end constructor
public Book(Author author, Date published, String title)
{
setAuthor(author);
setDatePublished(published);
setTitle(title);
} // end constructor
public Author getAuthor()
{
return author;
} // end accessor
public Date getDatePublished()
{
return published;
} // end accessor
public String getTitle()
{
return title;
} // end accessor
public void setAuthor(Author author)
{
this.author = (null == author ? new Author() : author);
} // end accessor
public void setDatePublished(Date published)
{
this.published = (null == published ? new Date() : published);
} // end accessor
public void setTitle(String title)
{
this.title = (null == title ? DEFAULT_TITLE : title);
} // end accessor
public String getAuthorName()
{
return author.getName().getFullName();
} // end method
public String getDayOfTheWeekBookWasPublished()
{
return published.getDayOfTheWeek();
} // end method
public void printDetails()
{
System.out.print(getAuthorName());
System.out.print("(");
System.out.print(author.getName().getInitials());
System.out.print(") wrote ");
System.out.print(title);
System.out.print(" on ");
System.out.print(getDayOfTheWeekBookWasPublished());
System.out.print(", ");
System.out.print(Date.getMonthName(published.getMonth()));
System.out.print(" ");
System.out.print(published.getDay());
System.out.print(", ");
System.out.print(published.getYear());
Name pseudonym = author.getPseudonym();
if (null != pseudonym)
{
System.out.print(", under the pseudonym ");
System.out.print(pseudonym.getFullName());
}
System.out.println();
} // end method
} // end class
When you want to create a Book object, you need to use one of the defined constructors. You have two options:
public Book()
public Book(Author author, Date published, String title)
The first one creates a book with a default author, date and title. The second one receives them as parameters. Assuming the second one is the one you need, you now know:
You need to call new Book(...) with three arguments. Not more, not less.
The first argument has to be an object of type Author. Not a string, not a number, an Author.
The second argument needs to be a Date object.
The third argument needs to be a String.
Now, here is your call:
new Book( "James", "Joyce",2013,1,1, 2013,1,1, 2013,1,1, "ULYSSES");
In this call, you pass twelve arguments! And they are just strings and numbers, not an Author, a Date and a String.
So, you need to create an Author object and a Date object and pass those. For example:
Author bookAuthor = new Author(...);
Date bookDate = new Date(...);
inventory[0] = new Book( bookAuthor, bookDate, "Ulysses" );
You can do the same without the extra variables:
inventory[0] = new Book( new Author(...), new Date(...), "Ulysses" );
Now, you should apply the same logic to the new Author(...) and new Date(...) calls.
Check which constructors you have in your code.
Find which one is the most suitable for the object you want to create.
Pass it the exact number of arguments of the exact type that is defined. If you need, use a call to new ... to create an object of the required type.
Related
Im trying to create a new instance in my book array, i've researched on the copyof method and thats supposedly how you do it, however, when I run the code that supposedly does it, i dont get any error or anything but it just doesnt show up? the output shows the other 3 books but not the added harry potter one.
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Book book1 = new Book("The Alchemist", "Paulo Coelho", "HarperCollins", "Adventure");
Book book2 = new Book("The Great Gatsby", "F. Scott Fitzgerald", "Charles Scribner\'s Sons", "Fiction");
Book book3 = new Book("The Catcher in the Rye", "J. D. Salinger", "Little, Brown and Company", "Fiction");
// Create an instance of the class Author
Author author1 = new Author("Paulo", "Coelho");
Author author2 = new Author("F. Scott", "Fitzgerald");
Author author3 = new Author("J. D.", "Salinger");
// Create an instance of the class Publisher
Publisher publisher1 = new Publisher("HarperCollins");
Publisher publisher2 = new Publisher("Charles Scribner\'s Sons");
Publisher publisher3 = new Publisher("Little, Brown and Company");
String[] authors = {author1.toString(), author2.toString(), author3.toString()};
String[] publishers = {publisher1.toString(), publisher2.toString(), publisher3.toString()};
String[] books = {book1.toString(), book2.toString(), book3.toString()};
addBook(authors, publishers, books, "J. K. Rowling", "Bloomsbury", "Harry Potter and the Philosopher\'s Stone", "trash");
//System.out.println(Arrays.toString(books));
//displayBooks(books);
// displayAuthors(authors);
//displayPublishers(publishers);
}
public static void addBook(String[] authors, String[] publishers, String[] books, String author, String publisher, String book, String genre){
authors = Arrays.copyOf(authors, authors.length + 1);
authors[authors.length - 1] = author;
publishers = Arrays.copyOf(publishers, publishers.length + 1);
publishers[publishers.length - 1] = publisher;
books = Arrays.copyOf(books, books.length + 1);
books[books.length - 1] = book;
}
public static void displayAuthors(String[] authors){
for(String author : authors){
System.out.println(author);
}
}
public static void displayPublishers(String[] publishers){
for(String publisher : publishers){
System.out.println(publisher);
}
}
public static void displayBooks(String[] books) {
for (String book : books) {
System.out.println(book);
}
}
}
Constructor for book:
public class Book {
String title;
String author;
String publisher;
String genre;
public Book(String title,String author,String publisher,String genre) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.genre = genre;
}
public String toString() {
return this.title + " | " + this.author + " | " + this.publisher + " | " + this.genre;
}
}
I've tried
System.out.println(Arrays.toString(books));
There is no error, so maybe it does happen but when i go to print the array itself it doesnt show up
(I'd like all 4 books to show instead of the original 3)
In your addBook method:
authors = Arrays.copyOf(authors, authors.length + 1);
authors[authors.length - 1] = author;
authors is your own local variable. You create a new array here (with copyOf), and assign it to this local copy. You then change something in this array you just made, and then your method ends. With it, your local variable poofs out of existence, and with that, no code has any reference to this copied array. The garbage collector eventually removes it.
The entire method therefore does nothing.
Generally, don't have static anything when you start out (other than your main, which should contain just one line: new MyClass().go(), or possibly new MyClass().go(args) if you care about the args; no further use of static allowed until you're a bit further along in your java career.
If you want things to survive, they have to be fields.
This is probably a bad question, but i have constructed a DVD object in java:
DVD myDVD = new DVD (11.17 , 9 , 120 , " Howl ’s Moving Castle " , " Hayao Miyazaki " );
I have a toString to print the whole object, but I've been asked to print the director (Hayao Miyazaki) of the object without the rest, is there a way to do this?
If you need any more information in order to help, please comment. Thanks
create a get method for the director
public String getDirector(){
return director;
}
System.out.print(myDVD.getDirector());
Assuming your code is Java code, in your DVD class, you can override the toString method in order to print what you want:
public class DVD {
private String director;
//more fields and stuff
#Override
public String toString() {
return director;
}
}
If you already have a toString implementation and need another one, you can add another method to get the director:
public String getDirector(){
return director;
}
and print it:
System.out.print(myDVD.getDirector());
Or you may want a method to do the printing itself:
public void printDirector() {
System.out.println(director);
}
You could make it as simple as writing a new method printDirector() which would do just that, OR...
You could leave the responsibility of printing the information to some other class, and make the DVD object responsible only for providing its information:
public class Movies {
public class DVD {
private director;
public DVD(String director) {
this.director = director;
}
public String getDirector() {
return director;
}
}
public static void main(String... arg) {
DVD howl = new DVD("Miyazaki");
String director = howl.getDirector();
SomePrinterClass.print(director);
}
}
Ultimately that's a design decision, either will produce the same result.
I need to create a variable of either data type on runtime (via scanner class)
This is what my assignment asked
"A selling arrangement could be an offered price OR an auction date"
This is what i have created but not sure if it is correct..
public class SellingArrangement {
private Object dateOrPrice;
public SellingArrangement()
{
}
public void setDateOrPrice(String price)
{
dateOrPrice = new Object();
dateOrPrice = price;
}
public void setDateOrPrice(Double price)
{
dateOrPrice = new Object();
dateOrPrice = price;
}
I have done something similar before (when an API may return JSON or XML)
However, you have two sets of two choices here - the input can either be a String or a Double, and that input can either represent a Date or a Price.
Instead of using an Object, I would just create two separate fields and populate the correct one using two separate constructors like this.
public class SellingArrangement {
private Date date;
private Price price;
public SellingArrangement(String input)
{
if ( // String is a price ) {
this.price = new Price(input);
}
if ( // String is a date ) {
this.date = new Date(input)
}
}
public SellingArrangement(Double input)
{
if ( // Double is a price ) {
this.price = new Price(input);
}
if ( // Double is a date ) {
this.date = new Date(input)
}
}
}
Of course, I am assuming you can figure out some way to validate whether the String or Double you are getting as input is a date or a price, and that you have constructors which will take a String / Double for each type. Treat this as pseudocode...
However as others have mentioned in comments if you dont have to do this with a single class, it would be better to use another method entirely...
I'm having issues creating the following two methods using an ArrayList object:
existTextbook(): checks whether a given textbook is in the catalogue. existTextbook() accepts the title and the author and returns true or false. True if the textbook is in the catalogue, false otherwise.
deleteTexbook(): deletes a textbook from the catalogue. deleteTextbook() accepts a textbook title as parameter and deletes the textbook if it exists.
Searching the Java API, the closest method I can find for the first method is the contains method but it takes an object as a parameter, not a String object within the Textbook object like the title or author. The same is true for the remove method for the second method taking an object of the ArrayList as a parameter.
Any hints on how to have a method look at each Textbook object String title or author, then return true if a match is found, or to delete the Textbook object containing the Textbook object String title or author?
Here's my code so far:
Textbook Class
package Ex1;
import java.text.NumberFormat;
public class Textbook
{
private String category, title, author;
private int year;
private double price;
public Textbook (String category, String title, String author, int year,
double price)
{
this.category = category;
this.title = title;
this.author = author;
this.year = year;
this.price = price;
}
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = "Category: " + category + "\n";
description += "Title: " + title + "\n";
description += "Author: " + author + "\n";
description += "Year: " + year + "\n";
description += "Price: " + fmt.format(price) + "\n" + "\n";
return description;
}
}
Catalogue Class
package Ex1;
import java.util.ArrayList;
public class Catalogue
{
private ArrayList <Textbook> catalogue;
public Catalogue ()
{
catalogue = new ArrayList<Textbook>();
}
public void addTextbook (Textbook t)
{
catalogue.add(t);
}
public boolean existTextbook(String title, String author)
{
}
public void deleteTextbook(String title)
{
}
public String toString()
{
return catalogue.toString();
}
}
Driver Class
package Ex1;
public class Drivermain
{
public static void main(String[] args)
{
Textbook javaBook = new Textbook ("Computer Science",
"Java Software Solutions", "Lewis/Loftus", 2015, 163.45);
Textbook dataBook = new Textbook ("Computer Science",
"Data Structures and Algorithm Analysis in Java,",
"Mark A. Weiss", 2015, 181.90);
Textbook calcBook = new Textbook ("Mathematics",
"Calculus Plus NEW MyMathLab", "Briggs/Cochran/Gillett",
2015, 236.90);
Textbook osBook = new Textbook ("Computer Science",
"Operating Systems: Internals and Design Principles",
"William Stallings", 2015, 205.70);
Textbook historyBook = new Textbook ("History",
"History of the Canadian Peoples: Beginnings to 1867, Vol. 1",
"Conard/Finkel/Fyson", 2015, 96.90);
Catalogue bookCatalogue = new Catalogue();
bookCatalogue.addTextbook(javaBook);
bookCatalogue.addTextbook(dataBook);
bookCatalogue.addTextbook(calcBook);
bookCatalogue.addTextbook(osBook);
bookCatalogue.addTextbook(historyBook);
System.out.println(bookCatalogue);
bookCatalogue.existTextbook("Java Software Solutions", "Lewis/Loftus");
bookCatalogue.deleteTextbook("Java Software Solutions");
}
}
I think instead of using methods from collections, you may want to deal with looking in your Arraylist yourself.
I'm not using a for each loop (just a for loop) because for delete it will cause a concurrent modification exception.
package Ex1;
import java.util.ArrayList;
public class Catalogue
{
private ArrayList <Textbook> catalogue;
public Catalogue ()
{
catalogue = new ArrayList<Textbook>();
}
public void addTextbook (Textbook t)
{
catalogue.add(t);
}
public boolean existTextbook(String title, String author)
{
for(int i =0; i<catalogue.Size(); i++){
Textbook t = catalogue.get(i);
//you'll want getter and setter methods
if(t.author.equals(author)&&t.title.equals(title))
return truel
}
}
public void deleteTextbook(String title)
{
for(int i =0; i<catalogue.Size(); i++){
Textbook t = catalogue.get(i);
if(t.title.equals(title)){
catalogue.remove(i);
}
}
}
public String toString()
{
return catalogue.toString();
}
}
Happy Coding! Leave a comment if you have any questions.
Instead of directly using those methods you may consider just looping through the catalogue ArrayList yourself and testing if the current object matches the title (and author).
It might be overkill, but you could make Textbook implement Comparable or write a Comparator.
Hello I am new to Java and NetBeans and am in Advanced classes in which my classes are 5 weeks long, so it is a lot to learn a new code language in 5 weeks. Anyways I have an assignment to Create a class named Movie that holds a movie name and rating. Provide methods to get and set both the movie name and rating. Create a class named TestMovie that creates three Movie instances with different values for name and rating and prints the information about each movie. I have done the code and it is passing the build fine but my professor wants a screen shot of the program working and running but I can't get NetBeans to bring that up. The chapter on building the test project was ripped out of my book. Can I get some help or pointers here is the code I have done:
package movie;
/**
*
* #author Jason
*/
public class Movie {
String movieRating;
public Movie(String rated, String mtitle) {
this.mrating = rated;
this.title = mtitle;
}
public void setRating(String Rating) {
movieRating = Rating;
}
// Get the rating
public String getRating() {
return movieRating;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
#Override
public String toString() {
return "Movie" + " title=" + getTitle() + " rating=" + getRating();
}
public static void main(String args[]) {
Movie mv = new Movie("", "");
mv.toString();
}
private String title;
private String mrating;
}
You can just run a test on the console, that is, create a MovieTest class with only a main method and create three instances/objects of Movie (Movie m1, m2, m3; OR Movie[] movies;). Assign them values either in the constructor or with the set methods then print them out with the method print or println in System.out.
Something along the lines of:
public class MovieTest {
public static void main(String[] args) {
Movie[] movies = new Movie[] {new Movie("R1", "T1"), new Movie("R2", "T2"), new Movie("R3", "T3)";
for (Movie i : movies) {
System.out.println(i.toString());
}
}
}
Then end by screenshooting the results.
As an alternative to the other answers suggesting printing the output to the console, with the Netbeans' UI editor you can easily create a window with a label showing the result, which makes it slightly more fancy.
You can get details on how to this here. Here's an image from that page:
The full working code is here. As you can see, it's just a few extra lines.
Your application prints no output, because you invoke toString(), but you don't print the result of it.
An example to create 3 Movie instances with data,
print them out, and then make a screenshot of your console app.
public static void main(String args[]) {
List<Movie> movieList = new ArrayList<Movie>(3);
Movie mv1 = new Movie("very Good", "Testfilm 1");
movieList add(mv1);
mv1 = new Movie("good", "Testfilm 2");
movieList add(mv1);
mv1 = new Movie("not good", "Testfilm 2");
movieList add(mv1);
for (Movie m : movieList) {
System.out.println(m.toString());
}
}