A+ Answers



1.      Given the following data definition class, write one line of Java code that creates one object using a specific constructor to create a new book titled, "I love OO!" that sells for 9.95.
public class Book {

   
private String isbn;

   
private double cost;

   
private boolean isNew;

   

   
public Book() {

      
this.isNew = true;

   }

   

   
public Book(String isbn) {

      
this();

      
this.isbn = isbn;

   }

   

   
public Book(String isbn, double cost) {

      
this();

      
this.isbn = isbn;

      
this.cost = cost;

   }

   

   
public String getIsbn() { return this.isbn; }

   
public double getCost() { return this.cost; }

   
public boolean isNew() { return this.isNew; }

   

   
public void setIsbn(String isbn) {

      
this.isbn = isbn;

   }

   

   
public boolean setCost(double cost) {

      
if (cost >= 0) {

         
this.cost = cost;

         
return true;

      }

      
else {

         
return false;

      }

   }

   

   
public void setIsNew(boolean isNew) {

      
this.isNew = isNew;

   }

   

   
public boolean discount(double percentage) {

      
if (percentage > 0) {

        setCost(
this.getCost() - this.getCost() * (percentage/100));

        
return true;

      }

      
else {

         
return false;

      }

   }


2.      Consider the data definition class identified below. Assume an object has been created using the default constructor. List each instance variable, and state the value of that instance variable.
public class Book {

   
private String isbn;

   
private double cost;

   
private boolean isNew;

   
private int quantity;  
   public Book() {

      
this.isNew = true;

   }

   

   
public Book(String isbn) {

      
this();

      
this.isbn = isbn;

   }

   

   
public Book(String isbn, double cost) {

      
this();

      
this.isbn = isbn;

      
this.cost = cost;

   }

   

   
public String getIsbn() { return this.isbn; }

   
public double getCost() { return this.cost; }

   
public boolean isNew() { return this.isNew; }

   

   
public void setIsbn(String isbn) {

      
this.isbn = isbn;

   }

   

   
public boolean setCost(double cost) {

      
if (cost >= 0) {

         
this.cost = cost;

         
return true;

      }

      
else {

         
return false;

      }

   }

   

   
public void setIsNew(boolean isNew) {

      
this.isNew = isNew;

   }

   

   
public boolean discount(double percentage) {

      
if (percentage > 0) {

        setCost(
this.getCost() - this.getCost() * (percentage/100));

        
return true;

      }

      
else {

         
return false;

      }

   }

}
3.      Consider the following implementation class code used to create an object and then call the constructor.
Book b1 = new Book();

b1.Book();
Using course terminology, briefly (1-2 sentences) explain why this code will generate a syntax error.
4.      Consider the data definition class identified below. Using terminology learned in the course, briefly (1-2 sentences) explain the purpose of the line of code that states:  this();
public class Book {

   
private String isbn;

   
private double cost;

   
private boolean isNew;

   

   
public Book() {

      
this.isNew = true;

   }

   

   
public Book(String isbn) {

      
this();

      
this.isbn = isbn;

   }

   

   
public Book(String isbn, double cost) {

      
this();

      
this.isbn = isbn;

      
this.cost = cost;

   }

   

   
public String getIsbn() { return this.isbn; }

   
public double getCost() { return this.cost; }

   
public boolean isNew() { return this.isNew; }

   

   
public void setIsbn(String isbn) {

      
this.isbn = isbn;

   }

   

   
public boolean setCost(double cost) {

      
if (cost >= 0) {

         
this.cost = cost;

         
return true;

      }

      
else {

         
return false;

      }

   }

   

   
public void setIsNew(boolean isNew) {

      
this.isNew = isNew;

   }

   

   
public boolean discount(double percentage) {

      
if (percentage > 0) {

        setCost(
this.getCost() - this.getCost() * (percentage/100));

        
return true;

      }

      
else {

         
return false;

      }

   }

}
5.      Consider the constructors of the data definition class identified below. Using course terminology, list one thing wrong with the first constructor as it is written.
public class Book {

   
private String isbn;

   
private double cost;

   
private boolean isNew;

   
private int quantity;   
   public void Book() {

      
this.isNew = true;

   }

   

   
public void Book(String isbn) {

      
this();

      
this.isbn = isbn;

   }

   

   
public void Book(String isbn, double cost) {

      
this.cost = cost; 
      this();

      
this(isbn); 

   }

   

   
public String getIsbn() { return this.isbn; }

   
public double getCost() { return this.cost; }

   
public boolean isNew() { return this.isNew; }

   

   
public void setIsbn(String isbn) {

      
this.isbn = isbn;

   }

   

   
public boolean setCost(double cost) {

      
if (cost >= 0) {

         
this.cost = cost;

         
return true;

      }

      
else {

         
return false;

      }

   }

   

   
public void setIsNew(boolean isNew) {

      
this.isNew = isNew;

   }

   

   
public boolean discount(double percentage) {

      
if (percentage > 0) {

        setCost(
this.getCost() - this.getCost() * (percentage/100));

        
return true;

      }

      
else {

         
return false;

      }

   }

}
Given the following data definition class, write one line of Java code that creates one object using the default constructor
public class Book {

  
 private String isbn;

  
 private double cost;

  
 private boolean isNew;

  
 

  
 public Book() {

      
this.isNew = true;

  
 }

  
 

  
 public Book(String isbn) {

      
this();

      
this.isbn = isbn;

  
 }

  
 

  
 public Book(String isbn, double cost) {

      
this();

      
this.isbn = isbn;

      
this.cost = cost;

  
 }

  
 

  
 public String getIsbn() { return this.isbn; }

  
 public double getCost() { return this.cost; }

  
 public boolean isNew() { return this.isNew; }

  
 

  
 public void setIsbn(String isbn) {

     
 this.isbn = isbn;

  
 }

  
 

  
 public boolean setCost(double cost) {

     
 if (cost >= 0) {

        
 this.cost = cost;

        
 return true;

     
 }

     
 else {

        
 return false;

     
 }

  
 }

  
 

  
 public void setIsNew(boolean isNew) {

     
 this.isNew = isNew;

  
 }

  
 

  
 public boolean discount(double percentage) {

     
 if (percentage > 0) {

       
 setCost(this.getCost() - this.getCost() * (percentage/100));

       
 return true;

     
 }

     
 else {

        
 return false;

     
 }

  
 }

}


6.      Using terminology learned in the course, briefly (1-2 sentences) explain why a programmer would create both a default and a specific constructor in a data definition class.
7.      Given the following data definition class and implementation class shell, write a create() method that will create and return an object to model a Book with an ISBN number of 12345.

public class Book {

   
private String isbn;

   
private double cost;

   
private boolean isNew;

  

   
public Book() {

      
this.isNew = true;

   }

  

   
public Book(String isbn) {

      
this();

      
this.isbn = isbn;

   }

  

   
public Book(String isbn, double cost) {

      
this();

      
this.isbn = isbn;

      
this.cost = cost;

   }

  

   
public String getIsbn() { return this.isbn; }

   
public double getCost() { return this.cost; }

   
public boolean isNew() { return this.isNew; }

  

   
public void setIsbn(String isbn) {

      
this.isbn = isbn;

   }

  

   
public boolean setCost(double cost) {

      
if (cost >= 0) {

         
this.cost = cost;

         
return true;

      }

      
else {

         
return false;

      }

   }

  

   
public void setIsNew(boolean isNew) {

      
this.isNew = isNew;

   }

  

   
public boolean discount(double percentage) {

      
if (percentage > 0) {

        setCost(
this.getCost() - this.getCost() * (percentage/100));

        
return true;

      }

      
else {

         
return false;

      }

   }

}



import javax.swing.JOptionPane;
public class Bookstore {

   
public static void main(String[] args) {

      Book myBook = create();

   }