The problem does... Create a class named Book containg the following private dat
ID: 3640604 • Letter: T
Question
The problem does...Create a class named Book containg the following private data elements:
int numberOfPages
boolean hardBack
String title
double price
implement the following public methods:
Book(int numberOfPages, boolean hardBack, String title, double price)
Accessor and mutator methods for each attribute
boolean equals() - this method must compare attributes in the following order:
numberOfPages
price
hardBack
title
String toString()
Make use of the @Override annotation for all overridden methods
THIS IS WHAT I HAVE SO FAR.
class Book
{
private int numberOfPages;
private boolean hardBack;
private String title;
private double price;
//constructors
Book()
{
title = "";
numberOfPages = 0;
hardBack = true;
price = 0.0;
}
Book(int n, boolean h, String t, double p)
{
numberOfPages = n;
hardBack = h;
title = t;
price = p;
}
int getPages()
{
return numberOfPages;
}
void setPage(int n)
{
numberOfPages = n;
}
boolean getBack()
{
return hardBack;
}
void setBack(boolean h)
{
hardBack = h;
}
String getTitle()
{
return title;
}
void setTitle(String t) {
title = t; } d
ouble getPrice()
{
return price;
}
void setPrice( double p)
{
price = p;
}
}
ANY HELP WILL BE HIGHLY APPRECIATED!
Explanation / Answer
please rate - thanks
I'd help with toString, but don't have the information you need in the string
import java.util.*;
public class main
{public static void main(String[] args) {
Book Book(5,true,"one",5);
Book two=new Book(5,true,"one",5);
if(one.equals(two))
System.out.println("same");
else
System.out.println("different");
Book three=new Book(5,true,"tthe",5);
if(one.equals(three))
System.out.println("same");
else
System.out.println("different");
}
}
----------------------------------
class Book
{
private int numberOfPages;
private boolean hardBack;
private String title;
private double price;
//constructors
Book()
{
title = "";
numberOfPages = 0;
hardBack = true;
price = 0.0;
}
Book(int n, boolean h, String t, double p)
{
numberOfPages = n;
hardBack = h;
title = t;
price = p;
}
int getPages()
{
return numberOfPages;
}
void setPage(int n)
{
numberOfPages = n;
}
boolean getBack()
{
return hardBack;
}
void setBack(boolean h)
{
hardBack = h;
}
String getTitle()
{
return title;
}
void setTitle(String t) {
title = t; }
double getPrice()
{
return price;
}
void setPrice( double p)
{
price = p;
}
boolean equals(Book other)
{if(price==other.price)
if(numberOfPages==other.numberOfPages)
if(hardBack==other.hardBack)
if(title.equalsIgnoreCase(other.title))
return true;
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.