خرید بک لینک

Vote count: 0

I have a project that Ive made so far and here are my desired outputs Test 1 Test 1 and Test 2 Here are the directions."Write a class called Book that contains instance data for the title, author, and pages. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString methods that retus a string like below:

Author: name of the author in one word

Title: title in one word

Pages: number of pages

example:

Author: Lewis

Title: Java

Pages: 806

Write a BookTester class to take author, title, pages from keyboard, create a book object and print the above to the screen. How would I get my answer to show the expected outputs shown in my pictures Test1 and Test 2? Also, what am I missing?

    public class Book {
    String author;
    String title;
    int pages;

    public Book(String a, String t, int p) {
    author = a;
    title = t;
    pages = p;
}

    public String toString() {
    String output;
    output = ("Author: " + author + "n");
    output = ("Title: " + title + "n");
    output = ("Pages: " + pages + "n");
    retu output;
}

}

    import java.util.*;
    public class BookTester {
    public static void main(String[] args) {
    String author;
    String title;
    int pages;
    Scaer scan = new Scaer(System.in);
    author = scan.nextLine();
    title = scan.nextLine();
    pages = scan.nextInt();
    Book book1 = new Book(author, title, pages);
    System.out.println(book1);
     }
     }

asked 2 mins ago

1 Answer

Vote count: 0

public String toString() {
    String output ="";
    output += "Author: " + author + "n";
    output += "Title: " + title + "n";
    output += "Pages: " + pages + "n";
    retu output;
}

You should cancat (+) each value. Otherwise last value will be retued.

answered 1 min ago

- - , .

برچسب: نویسنده: استخدام کار تاريخ: شنبه 24 بهمن 1394 ساعت: 14:15

صفحه بندی