Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A java problem: LibraryDemo Class Create a class called LibraryDemo. LibraryDemo

ID: 3915890 • Letter: A

Question

A java problem:

LibraryDemo Class

Create a class called LibraryDemo. LibraryDemo class should have

Variables: bookName, author, ISBN, price, lost(boolean), dueDate(Date class), totalCost, totalLost(in price) *some of them are instance variables and some are static variables

Constructor:

• default constructor(no arg): Name and author set to “Unknown”

• constructor which takes 3 parameters for bookName, author, and price *think about how would you control totalCost; do you need to give default values to the rest of the variables?

Methods:

• setters and getters o when a book is marked as lost, how would it impact totalLost; in which setters and getters should you use deep copy?

• toString

use main to check you code (do NOT change what’s in main)

public static void main(String[] args) {

System.out.println("Part1");

LibraryDemo book1 = new LibraryDemo();

LibraryDemo book2 = new LibraryDemo("JAVA Web Services in a Nutshell", "Kim Topley", 39.95);

LibraryDemo book3 = new LibraryDemo("Core JAVA", "Cay S. Horstmann", 54.99);

LibraryDemo book4 = new LibraryDemo("Headfirst Android Development", "Dawn Griffiths", 69.99);

System.out.println(book1);

System.out.println(book2);

System.out.println(book3);

System.out.println(book4);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Adding Harry Potter:");

book1.setBookName("harry potter and the order of the phoenix");

book1.setAuthor("J. K. Rowling");

book1.setPrice(29.99);

System.out.println(book1);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Someone lost a book:");

book2.setLost(true);

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Now Let's check dueDate:");

Date date1 = new Date("April",1,2018);

Date date2;

book1.setDueDate(date1);

date1.setMonth("May");

System.out.println(book1.dueDate);

date2 = book1.getDueDate();

date2.setYear(2019);

System.out.println(book1.dueDate);


My code so far: I have two classes, one called "Date" and one called "LibraryDemo"

**************************** Date Code

package library_demo;

import java.util.Scanner;

public class Date {

private String month;

private int day;

private int year;

public Date(String string, int i, int j) {

// TODO Auto-generated constructor stub

}

public Date() {

// TODO Auto-generated constructor stub

}

public void setDate(int month, int day, int year)

{

if (dateOK(month, day, year))

{

this.month=monthString(month);

this.day = day;

this.year = year;

}

else

{

System.out.println("Fatal Error");

System.exit(0);

}

}

public void setMonth(int monthNumber) {

if ((monthNumber <= 0) || (monthNumber > 12)) {

System.out.println("Fatal error");

System.exit(0);

}

else

month = monthString(monthNumber);

}

private boolean dateOK(int monthInt, int dayInt, int yearInt) {

return ( (monthInt >= 1)&& (monthInt <= 12) && dayInt>=1)&& (dayInt <=31) && (yearInt >= 1000) && (yearInt <=9999);

}

private String monthString(int monthNumber) {

switch (monthNumber) {

case 1: return "January";

case 2: return "February";

case 3: return "March";

case 4: return "April";

case 5: return "May";

case 6: return "June";

case 7: return "July";

case 8: return "August";

case 9: return "September";

case 10: return "October";

case 11: return "November";

case 12: return "December";

default:

System.out.println("Invalid month");

System.exit(0);

return "Error";

}

}

public void setYear(int i) {

// TODO Auto-generated method stub

}

public void setMonth(String string) {

// TODO Auto-generated method stub

}

}

package library_demo;

******************************************* LibraryDemo Code

public class LibraryDemo {

private String bookName, author;

private int ISBN, count;

private double price;

public static double totalCost, totalLost;

private boolean lost;

Date dueDate = new Date();

public LibraryDemo() {

bookName = "unknown";

author = "unknown";

}

public int calculateTotalCost(int count) {

while (price > 0) {

totalCost += price;

count++;

}

return count;

}

public int calculateTotalLost(int count) {

while (price > 0) {

totalLost += price;

count++;

}

return count;

}

public LibraryDemo(String bookName, String author, double price) {

}

public String getBookName() {

return bookName;

}

public void setBookName(String bookName) {

this.bookName = bookName;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public int getISBN() {

return ISBN;

}

public void setISBN(int iSBN) {

ISBN = iSBN;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public static double getTotalCost() {

return totalCost;

}

public static void setTotalCost(double totalCost) {

LibraryDemo.totalCost = totalCost;

}

public static double getTotalLost() {

return totalLost;

}

public static void setTotalLost(double totalLost) {

LibraryDemo.totalLost = totalLost;

}

public boolean isLost() {

return lost;

}

public void setLost(boolean lost) {

this.lost = lost;

}

public Date getDueDate() {

return dueDate;

}

public void setDueDate(Date dueDate) {

this.dueDate = dueDate;

}

@Override

public String toString() {

return "LibraryDemo [bookName=" + bookName + ", author=" + author + ", price=" + price + "]";

}

public static void main(String[] args) {

System.out.println("Part1");

LibraryDemo book1 = new LibraryDemo();

LibraryDemo book2 = new LibraryDemo("JAVA Web Services in a Nutshell", "Kim Topley", 39.95);

LibraryDemo book3 = new LibraryDemo("Core JAVA", "Cay S. Horstmann", 54.99);

LibraryDemo book4 = new LibraryDemo("Headfirst Android Development", "Dawn Griffiths", 69.99);

System.out.println(book1);

System.out.println(book2);

System.out.println(book3);

System.out.println(book4);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Adding Harry Potter:");

book1.setBookName("harry potter and the order of the phoenix");

book1.setAuthor("J. K. Rowling");

book1.setPrice(29.99);

System.out.println(book1);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Someone lost a book:");

book2.setLost(true);

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Now Let's check dueDate:");

Date date1 = new Date("April", 1, 2018);

Date date2;

book1.setDueDate(date1);

date1.setMonth("May");

System.out.println(book1.dueDate);

date2 = book1.getDueDate();

date2.setYear(2019);

System.out.println(book1.dueDate);

}

}

My output: Why am I not getting the correct output? How do I need to change my code to get the correct output?

Part1
LibraryDemo [bookName=unknown, author=unknown, price=0.0]
LibraryDemo [bookName=null, author=null, price=0.0]
LibraryDemo [bookName=null, author=null, price=0.0]
LibraryDemo [bookName=null, author=null, price=0.0]
Total Price: $0.0
Total Lost: $0.0

Adding Harry Potter:
LibraryDemo [bookName=harry potter and the order of the phoenix, author=J. K. Rowling, price=29.99]
Total Price: $0.0
Total Lost: $0.0

Someone lost a book:
Total Lost: $0.0

Now Let's check dueDate:
library_demo.Date@11028347
library_demo.Date@11028347

Explanation / Answer

The error here is in parameterized constructor you are not setting the values. By fixing that you get the values. Please check the modified code below.

public class LibraryDemo {

private String bookName, author;

private int ISBN, count;

private double price;

public static double totalCost, totalLost;

private boolean lost;

Date dueDate = new Date();

public LibraryDemo() {

bookName = "unknown";

author = "unknown";

}

public int calculateTotalCost(int count) {

while (price > 0) {

totalCost += price;

count++;

}

return count;

}

public int calculateTotalLost(int count) {

while (price > 0) {

totalLost += price;

count++;

}

return count;

}

public LibraryDemo(String bookName, String author, double price) {

this.bookName=bookName;

this.author=author;

this.price=price;

}

public String getBookName() {

return bookName;

}

public void setBookName(String bookName) {

this.bookName = bookName;

}

public String getAuthor() {

return author;

}

public void setAuthor(String author) {

this.author = author;

}

public int getISBN() {

return ISBN;

}

public void setISBN(int iSBN) {

ISBN = iSBN;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public static double getTotalCost() {

return totalCost;

}

public static void setTotalCost(double totalCost) {

LibraryDemo.totalCost = totalCost;

}

public static double getTotalLost() {

return totalLost;

}

public static void setTotalLost(double totalLost) {

LibraryDemo.totalLost = totalLost;

}

public boolean isLost() {

return lost;

}

public void setLost(boolean lost) {

this.lost = lost;

}

public Date getDueDate() {

return dueDate;

}

public void setDueDate(Date dueDate) {

this.dueDate = dueDate;

}

@Override

public String toString() {

return "LibraryDemo [bookName=" + bookName + ", author=" + author + ", price=" + price + "]";

}

public static void main(String[] args) {

System.out.println("Part1");

LibraryDemo book1 = new LibraryDemo();

LibraryDemo book2 = new LibraryDemo("JAVA Web Services in a Nutshell", "Kim Topley", 39.95);

LibraryDemo book3 = new LibraryDemo("Core JAVA", "Cay S. Horstmann", 54.99);

LibraryDemo book4 = new LibraryDemo("Headfirst Android Development", "Dawn Griffiths", 69.99);

System.out.println(book1);

System.out.println(book2);

System.out.println(book3);

System.out.println(book4);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Adding Harry Potter:");

book1.setBookName("harry potter and the order of the phoenix");

book1.setAuthor("J. K. Rowling");

book1.setPrice(29.99);

System.out.println(book1);

System.out.println("Total Price: $" + getTotalCost());

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Someone lost a book:");

book2.setLost(true);

System.out.println("Total Lost: $" + getTotalLost());

System.out.println();

System.out.println("Now Let's check dueDate:");

Date date1 = new Date("April", 1, 2018);

Date date2;

book1.setDueDate(date1);

date1.setMonth("May");

System.out.println(book1.dueDate);

date2 = book1.getDueDate();

date2.setYear(2019);

System.out.println(book1.dueDate);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote