This site requires JavaScript, please enable it in your browser!
Greenfoot back
al_griff
al_griff wrote ...

2012/3/18

Struggling

al_griff al_griff

2012/3/18

#
Hey there guys, So for school in SDD I've been given a task and it goes as follows; design a class called Book that will receive the ISBN, author, title and price of a book. I've been struggling quite a bit with this, and I can't quite figure out how to get the author and title into the class' attributes. I tried using the data type "char" but every time I'd use a space it'd ask for ";" So I was just wondering if there was a data type that would allow me to just type the Author's name and the title of the book. I'm sorry if this isn't very well articulated, I'm not very skilled with Greenfoot. Cheers, Alex
danpost danpost

2012/3/18

#
How about a 'String' variable or two.
al_griff al_griff

2012/3/18

#
I tried using the data type "String" "string" "strings" "Strings" like I found on a few different websites about Java data types, but they didn't seem to work
danpost danpost

2012/3/18

#
You can learn all about data types an how to use them in the Java tutorials. The second section labelled in red on the left is the place to start.
danpost danpost

2012/3/18

#
Show me some example of how you tried to use them.
al_griff al_griff

2012/3/18

#
Ok, so here's what I have... "public class Book extends Actor { public char author; public char title; public double ISBN; public int price; public Book (char inAuthor, char inTitle, double inISBN, int inPrice) { author = inAuthor; title = inTitle; ISBN = inISBN; price = inPrice; } public void act() { // Add your action code here. } public void setAuthor (char inAuthor) { } } "
danpost danpost

2012/3/18

#
You need to change
public char author;
    public char title;
to
public String author;
public String title;
Then, change
public  Book (char inAuthor, char inTitle, double inISBN, int inPrice)
to
public Book(String inAuthor, String inTitle, double inISBN, int inPrice)
Also, change
public void setAuthor(char inAuthor)
to
public void setAuthor(String inAuthor)
Finally, when you create an instance of a Book entry, make sure String variable are sent to the constructor and the setAuthor methods.
al_griff al_griff

2012/3/18

#
Cheers man, I'll give that a go. I'll probably be back though...
al_griff al_griff

2012/3/18

#
ok, so I change all that stuff, but I am now getting this when I try to put the values in. Link
danpost danpost

2012/3/18

#
OK, two things: (1) When entering in a String variable, it needs to be in-between a set of double-quotes ("Book Name"); (2) You will then get a message stating 'possible loss of precision'; this will be due to you entering a number with two decimal places into a variable of type 'int' (integers do not contain decimal places). You probably need to change it to a 'double' variable, or you could just enter cents ('2550', instead of '25.50' ).
al_griff al_griff

2012/3/18

#
Just want to quickly say thanks for all your help by the way. Now, I have to get the scenario to select and print the Title and Author of any book with the price over $50. I have absolutely no idea how to do this. Would you be able to point me in the right direction??
danpost danpost

2012/3/18

#
In the world class code:
for (Book book : getObjects(Book.class))
{
    if (book.price > 5000) System.out.println("'" + book.title + "' by " + book.author);
}
You need to login to post a reply.