Question
Please write this in java! thank you here is the Example Code to help with this assignment. Can you make it similar? public class TextBook
Please write this in java! thank you
here is the Example Code to help with this assignment. Can you make it similar?
public class TextBook implements Comparable { private String title; private String author; private String publisher; public TextBook(String textTitle, String auth, String pub) { title = textTitle; author = auth; publisher = pub; } public TextBook(TextBook object2) { title = object2.title; author = object2.author; publisher = object2.publisher; } // clone method public Object clone() { return new TextBook(this); } // finalize method you need to invoke garbage collection to test this method //System.gc() public void finalize() { System.out.println("TextBook object has been destroyed"); } // hashCode method generates a unique number for each objects based on the field values public int hashCode() { // Using Joshua Bloch's recipe for hashCode // start with prime number 17 and multiply hashCode() of each fields with prime number 37 // idea is to generate a unique number int result = 17; result = 37 * result + title.hashCode(); result = 37 * result + author.hashCode(); result = 37 * result + publisher.hashCode(); return result; } // equals method "==" operator is only good for comparing primitive data type // for any reference objects we need to create equals method instead public boolean equals(TextBook obj) { boolean status; if (title.equals(obj.title) && author.equals(obj.author) && publisher.equals(obj.publisher)) status = true; else status =false; return status; } // compareTo method is coming from Interface Comparable
public class Instructor implements Comparable { private String lastName; // Last name private String firstName; // First name private String officeNumber; // Office number /** This constructor initializes the last name, first name, and office number. @param lname The instructor's last name. @param fname The instructor's first name. @param office The office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** The copy constructor initializes the object as a copy of another Instructor object. @param object2 The object to copy. */ public Instructor(Instructor object2) { lastName = object2.lastName; firstName = object2.firstName; officeNumber = object2.officeNumber; } public Object clone() { return new Instructor(this); } public void finalize() { System.out.println("Instructor object has been destroyed"); } public int hashCode() { // Using Joshua Bloch's recipe for hashCode int result = 17; result = 37 * result + lastName.hashCode(); result = 37 * result + firstName.hashCode(); return result; } public boolean equals(Instructor obj) { boolean status; if (lastName.equals(obj.lastName) && firstName.equals(obj.firstName) && officeNumber.equals(obj.officeNumber)) status = true; else status =false; return status; } public int compareTo(Object obj) { int result = 0; Instructor i = (Instructor)obj; if (firstName.equals(i.firstName) && lastName.equals(i.lastName)) result = 0; else if (firstName.equals(i.firstName) && lastName.compareTo(i.lastName) public void set(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } public String toString() { String str = "Last Name: " + lastName + " First Name: " + firstName + " Office Number: " + officeNumber; return str; } }
Carpet Calculator The Westfield Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square foot of carpet. For example, the area of floor that is 12 feet long and 10 feet wide is 120 square feet. To cover that floor with carpet that costs $8 per square foot would COM $960.(12108=960. First, you should create a class named RoomDimension that has two fields: one for the length of the room and one for the width. The RoomDimension class should have a method that returns the area of the room. (The area of the room is the room's length multiplied by the room's width.) Next you should create a RoomCarpet class that has a RoomDimension object as a field. It should also have a field for the cost of the carpet per square foot. The RoomCarpet class should have a method that returns the total cost of the carpet. In addition to the above specs given please add the following methods in both classes: - Copy constructor - toString() method - clone() method - hashCode() method - equals() method - compareTo() method - finalize() method Once you have written these classes, use them in an application to test all methods you have created. Create at least three RoomCarpet objects and add them into an ArrayList Deliverables: - RoomDimension.java (30\%) - RoomCarpet.java (30\%) CarpetCalculatorApp.java (30\%) - Screenshot(s)- 10\%
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started