Question

In: Computer Science

Assume you have created the following data definition class: public class Book {    private String title;...

Assume you have created the following data definition class:

public class Book {
   private String title;
   private double cost;
  
   public String getTitle() { return this.title; }
   public double getCost() { return this.cost; }
  
   public void setTitle(String title) {
      this.title = title;
   }

// Mutator to return true/false instead of using exception handling
public boolean setCost(double cost) {
      if (cost >= 0 && cost < 100) {
         this.cost = cost;
         return true;
      }
      else {
         return false;
      }
   }
  
   public double calculateSalePrice(double tax) {
      return this.getCost() * (1+tax);
   }
}

Change the mutator, setTitle(String title), to be a mutator that returns a Boolean if a title could be set, if it contains at least 10 characters

Solutions

Expert Solution

public class Book {
    private String title;
    private double cost;

    public String getTitle() {
        return this.title;
    }

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

    public boolean setTitle(String title) {
        if (title.length() >= 10) {
            this.title = title;
            return true;
        }
        return false;
    }

    // Mutator to return true/false instead of using exception handling
    public boolean setCost(double cost) {
        if (cost >= 0 && cost < 100) {
            this.cost = cost;
            return true;
        } else {
            return false;
        }
    }

    public double calculateSalePrice(double tax) {
        return this.getCost() * (1 + tax);
    }
}

Related Solutions

Assume you have created the following data definition class: public abstract class Organization { private String...
Assume you have created the following data definition class: public abstract class Organization { private String name;    private int numEmployees;    public static final double TAX_RATE = 0.01;    public String getName() { return this.name; }    public int getNumEmployees() { return this.numEmployees; }         public abstract double calculateTax() {       return this.numEmployees * TAX_RATE;    } } In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:   ...
Programming Exercise Implement the following class design: class Tune { private:    string title; public:    Tune();    Tune( const string &n );      const string & get_title() const; }; class Music_collection { private: int number; // the number of tunes actually in the collection int max; // the number of tunes the collection will ever be able to hold Tune *collection; // a dynamic array of Tunes: "Music_collection has-many Tunes" public: // default value of max is a conservative...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public...
this won't compile package com.test; public class CatalogItem { private String title; private double price; public CatalogItem(String title, double price) { super(); this.title = title; this.price = price; } public String getTitle() { return title; } public double getPrice() { return price; } } //Book.java package com.test; public class Book extends CatalogItem { private String author; private int ISBN; public Book(String title, double price, String author, int iSBN) { super(title, price); this.author = author; ISBN = iSBN; } public String...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public...
1. public class MyString { private char[] data; MyString(String string) { data = string.toCharArray(); } public int compareTo(MyString other) { /* code from HW1 here */ } public char charAt(int i) { return data[i]; } public int length() { return data.length; } public int indexOf(char c) { /* code from HW1 here */ } public boolean equals(MyString other) { /* code from HW1 here */ } /* * Change this MyString by removing all occurrences of * the argument character...
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
Consider the following class definition:                   public class Parent {               private
Consider the following class definition:                   public class Parent {               private int varA;               protected double varB;               public Parent(int a, double b){ varA = a; varB = b;               }               public int sum( ){                    return varA + varB;               } public String toString( ){                    return "" + varA + "   " + varB;               }         } Consider that you want to extend Parent to Child. Child will have a third int instance data varC....
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class StringNode { private String item; private StringNode next; } public class StringLL { private...
public class StringNode { private String item; private StringNode next; } public class StringLL { private StringNode head; private int size; public StringLL(){ head = null; size = 0; } public void add(String s){ add(size,s); } public boolean add(int index, String s){ ... } public String remove(int index){ ... } } In the above code add(int index, String s) creates a StringNode and adds it to the linked list at position index, and remove(int index) removes the StringNode at position...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT