Guest User

Untitled

a guest
Nov 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. Book title:Java Programming, author:Liang, pages:1320, price: $145.0
  2.  
  3. Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $19.99
  4.  
  5. Book title:The Hobbit, author:Tolkien, pages:320, price: $9.25
  6.  
  7. Book title:Born a Crime, author:Noah, pages:304, price: $17.33
  8.  
  9. Book title:null, author:null, pages:0, price: $0.0
  10.  
  11. Book title:null, author:null, pages:0, price: $0.0
  12.  
  13. Books after completing library and 40% discount
  14. Book title:Java Programming, author:Liang, pages:1320, price: $58.0
  15.  
  16. Book title:Horton Hears a Who!, author:Dr.Seuss, pages:72, price: $7.9959999999999996
  17.  
  18. Book title:The Hobbit, author:Tolkien, pages:320, price: $3.7
  19.  
  20. Book title:Born a Crime, author:Noah, pages:304, price: $6.9319999999999995
  21.  
  22. Book title:Dark Territory, author:Kaplan, pages:352, price: $4.496
  23.  
  24. Book title:Born to Run, author:Springsteen, pages:508, price: $4.868
  25.  
  26. Here is the most expensive book after discounts
  27.  
  28. Book title:Java Programming, author:Liang, pages:1320, price: $58.0
  29.  
  30. public class Book {
  31. //instance data members
  32. private String title;
  33. private String author;
  34. private int pages;
  35. private double price;
  36. //public static int set to 0
  37. public static int numBooks = 0;
  38.  
  39. //parameterized constructor
  40. Book(String title, String author, int pages, double price){
  41. this.title=title;
  42. this.author=author;
  43. this.pages=pages;
  44. this.price=price;
  45. numBooks++;
  46. }
  47.  
  48. //default constructor
  49. Book(){
  50. title = author = null;
  51. price = pages = 0;
  52. numBooks++;
  53. }
  54.  
  55. //getters and setters
  56. void setTitle(String title) {
  57. this.title=title;
  58. }
  59. void setAuthor(String author) {
  60. this.author=author;
  61. }
  62. void setPages(int pages) {
  63. this.pages=pages;
  64. }
  65. void setPrice(double price) {
  66. this.price=price;
  67. }
  68. String getTitle() {
  69. return title;
  70. }
  71. String getAuthor() {
  72. return author;
  73. }
  74. int getPages() {
  75. return pages;
  76. }
  77. double getPrice() {
  78. return price;
  79. }
  80. //toString method to return string showing state of a book instance
  81. @Override
  82. public String toString() {
  83. String str = "Book title:" + title + ", author:" + author +
  84. ", pages:" + pages + ", price: $" + price;
  85. return str;
  86. }
  87. }
  88.  
  89. public class TestBook {
  90.  
  91. public static void main(String[] args) {
  92. // array to hold 6 book objects specify data in first 4
  93. Book[] bookArray=new Book[6];
  94. bookArray[0]=new Book("Java Programming","Liang",1320,145.00);
  95. bookArray[1]=new Book("Horton Hears a Who!","Dr.Seuss",72,19.99);
  96. bookArray[2]=new Book("The Hobbit","Tolkien",320,9.25);
  97. bookArray[3]=new Book("Born a Crime","Noah",304,17.33);
  98. bookArray[4]=new Book();
  99. bookArray[5]=new Book();
  100. for(Book b:bookArray)
  101. System.out.println(b.toString());
  102. System.out.println("nBooks after completing library and 40% discount");
  103. finishArray(bookArray);
  104. Book expensive = reduceBooks(bookArray);
  105. for(Book b:bookArray)
  106. System.out.println(b.toString());
  107. System.out.println("nHere is the most expensive book after discountsn" + expensive.toString());
  108. }
  109. //void method, use setter method to fill in last 2 books in array
  110. static void finishArray(Book[]array) {
  111. array[4].setTitle("Dark Territory");
  112. array[4].setAuthor("Kaplan");
  113. array[4].setPages(352);
  114. array[4].setPrice(11.24);
  115.  
  116. array[5].setTitle("Born to Run");
  117. array[5].setAuthor("Springsteen");
  118. array[5].setPages(508);
  119. array[5].setPrice(12.17);
  120. }
  121. static Book reduceBooks(Book[]array) {
  122. Book mostExpensiveBook=null;
  123. //reduce price of every book by 40%
  124. for (int i=0; i<6; i++) {
  125. array[i].setPrice(array[i].getPrice()*0.40);
  126. if(mostExpensiveBook==null)
  127. mostExpensiveBook=array[i];
  128. else if(mostExpensiveBook.getPrice()<array[i].getPrice())
  129. mostExpensiveBook = array[i];
  130. }
  131. return mostExpensiveBook;
  132. }
Add Comment
Please, Sign In to add comment