Hi everyone,
I'm working on a project for a class that has a Product, Book, and ProductApp class. I have to make certain changes to them, and somewhere along the way I'm screwing up and it's not producing output anymore, so I'm hoping to go through step by step and hopefully have some of you guys either tell me it's correct, or give me suggestion on where to go next. First thing is, here is the coding that I was given for the three classes so you can see my starting point
ProductApp Class
Product class
Book class
I'm going to go step by step in what I do, but I'll list the whole assignment so you guys know what all I have to do by the end of the project.
Step 1: Add an abstract method, getDisplayText(), to Product, with no parameters and have it return a String object. Also, delete the existing toString() method in Product.
Step 2: Rename the toString() method in Book to getDisplayText(), and modify the getDisplayText method so that when it is called, it generates the output of the original program.
Step 3: Modify Product and ProductApp to make sure that when the modified ProductApp is executed, it generates the same output as the original program.
Anyways, after having gone through step one, here is the modified coding I came up with for the Product method. Is this correct, or can someone at least point me in the correct direction if it is wrong.
It executes, but the output is all wrong, but I'm guessing that's because the other steps haven't been completed yet.
I'm working on a project for a class that has a Product, Book, and ProductApp class. I have to make certain changes to them, and somewhere along the way I'm screwing up and it's not producing output anymore, so I'm hoping to go through step by step and hopefully have some of you guys either tell me it's correct, or give me suggestion on where to go next. First thing is, here is the coding that I was given for the three classes so you can see my starting point
ProductApp Class
public class ProductApp { public static void main(String args[]) { Book b = new Book(); b.setCode("mjs6"); b.setDescription("Murach's Java SE 6"); b.setPrice(19.3); b.setAuthor("Joel Murach"); System.out.println(b.toString()); } }
Product class
import java.text.NumberFormat; public class Product { private String code; private String description; private double price; public Product() { code = ""; description = ""; price = 0; } public void setCode(String code) { this.code = code; } public String getCode(){ return code; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public String getFormattedPrice() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(price); } public String toString() { return "Code: " + code + "\n" + "Description: " + description + "\n" + "Price: " + this.getFormattedPrice() + "\n"; } }
Book class
public class Book extends Product { private String author; public Book() { super(); author = ""; } public void setAuthor(String author) { this.author = author; } public String getAuthor(){ return author; } public String toString() { return super.toString() + "Author: " + author + "\n"; } }
I'm going to go step by step in what I do, but I'll list the whole assignment so you guys know what all I have to do by the end of the project.
Step 1: Add an abstract method, getDisplayText(), to Product, with no parameters and have it return a String object. Also, delete the existing toString() method in Product.
Step 2: Rename the toString() method in Book to getDisplayText(), and modify the getDisplayText method so that when it is called, it generates the output of the original program.
Step 3: Modify Product and ProductApp to make sure that when the modified ProductApp is executed, it generates the same output as the original program.
Anyways, after having gone through step one, here is the modified coding I came up with for the Product method. Is this correct, or can someone at least point me in the correct direction if it is wrong.
import java.text.NumberFormat; public abstract class Product { private String code; private String description; private double price; public Product() { code = ""; description = ""; price = 0; } public void setCode(String code) { this.code = code; } public String getCode(){ return code; } public void setDescription(String description) { this.description = description; } public String getDescription() { return description; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } public String getFormattedPrice() { NumberFormat currency = NumberFormat.getCurrencyInstance(); return currency.format(price); } abstract String getDisplayText(); }
It executes, but the output is all wrong, but I'm guessing that's because the other steps haven't been completed yet.