Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Book title:Java Programming, author:Liang, pages:1320, price: $145.0
- Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99
- Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25
- Book title:Born a Crime, author:Noah, pages:304, price: $17.33
- Book title:null, author:null, pages:0, price: $0.0
- Book title:null, author:null, pages:0, price: $0.0
- Books after completing library and 40% discount
- Book title:Java Programming, author:Liang, pages:1320, price: $58.0
- Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $7.9959999999999996
- Book title:The Hobbit, author:Tolkien, pages:320, price: $3.7
- Book title:Born a Crime, author:Noah, pages:304, price: $6.9319999999999995
- Book title:Dark Territory, author:Kaplan, pages:352, price: $4.496
- Book title:Born to Run, author:Springsteen, pages:508, price: $4.868
- Here is the most expensive book after discounts
- Book title:Java Programming, author:Liang, pages:1320, price: $58.0
- public class Book {
- //instance data members
- private String title;
- private String author;
- private int pages;
- private double price;
- //public static int set to 0
- public static int numBooks = 0;
- //parameterized constructor
- Book(String title, String author, int pages, double price){
- this.title=title;
- this.author=author;
- this.pages=pages;
- this.price=price;
- numBooks++;
- }
- //default constructor
- Book(){
- title = author = null;
- price = pages = 0;
- numBooks++;
- }
- //getters and setters
- void setTitle(String title) {
- this.title=title;
- }
- void setAuthor(String author) {
- this.author=author;
- }
- void setPages(int pages) {
- this.pages=pages;
- }
- void setPrice(double price) {
- this.price=price;
- }
- String getTitle() {
- return title;
- }
- String getAuthor() {
- return author;
- }
- int getPages() {
- return pages;
- }
- double getPrice() {
- return price;
- }
- //toString method to return string showing state of a book instance
- @Override
- public String toString() {
- String str = "Book title:" + title + ", author:" + author +
- ", pages:" + pages + ", price: $" + price;
- return str;
- }
- }
- public class TestBook {
- public static void main(String[] args) {
- // array to hold 6 book objects specify data in first 4
- Book[] bookArray=new Book[6];
- bookArray[0]=new Book("Java Programming","Liang",1320,145.00);
- bookArray[1]=new Book("Horton Hears a Who!","Dr.Seuss",72,19.99);
- bookArray[2]=new Book("The Hobbit","Tolkien",320,9.25);
- bookArray[3]=new Book("Born a Crime","Noah",304,17.33);
- bookArray[4]=new Book();
- bookArray[5]=new Book();
- for(Book b:bookArray)
- System.out.println(b.toString());
- System.out.println("nBooks after completing library and 40% discount");
- finishArray(bookArray);
- Book expensive = reduceBooks(bookArray);
- for(Book b:bookArray)
- System.out.println(b.toString());
- System.out.println("nHere is the most expensive book after discountsn" + expensive.toString());
- }
- //void method, use setter method to fill in last 2 books in array
- static void finishArray(Book[]array) {
- array[4].setTitle("Dark Territory");
- array[4].setAuthor("Kaplan");
- array[4].setPages(352);
- array[4].setPrice(11.24);
- array[5].setTitle("Born to Run");
- array[5].setAuthor("Springsteen");
- array[5].setPages(508);
- array[5].setPrice(12.17);
- }
- static Book reduceBooks(Book[]array) {
- Book mostExpensiveBook=null;
- //reduce price of every book by 40%
- for (int i=0; i<6; i++) {
- array[i].setPrice(array[i].getPrice()*0.40);
- if(mostExpensiveBook==null)
- mostExpensiveBook=array[i];
- else if(mostExpensiveBook.getPrice()<array[i].getPrice())
- mostExpensiveBook = array[i];
- }
- return mostExpensiveBook;
- }
Add Comment
Please, Sign In to add comment