The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Jul 2017 |
| Last Sign in: | 313 Weeks Ago, 5 Days Ago |
| Questions Answered: | 15833 |
| Tutorials Posted: | 15827 |
MBA,PHD, Juris Doctor
Strayer,Devery,Harvard University
Mar-1995 - Mar-2002
Manager Planning
WalMart
Mar-2001 - Feb-2009
I am having trouble even getting started on this one. Its for Netbeans. Attached are the instructions and the code for the separate files and also a screen cap of what it looks like in netbeans. Examples of w
Â
In this exercise, you’ll create a Product application like the one presented in this chapter that uses inheritance. However, you will add an additional kind of product: compact discs.
Create a new subclass named CompactDisc
1. Open the project named ch08_ex2_Product that’s in the ex_starts directory. Then, review the code.
2. Add a class named CompactDisc that inherits the Product class. This new class should work like the Book and Software classes, but it should include public get and set methods for a private instance variable named artist. In addition, its toString method should append the artist name to the end of the string.
Modify the ProductDB class so it returns a CompactDisc object
3. Modify the ProductDB class so it creates at least one CompactDisc object. For example, this object might contain the following information:
Code: sgtp
Description: Sgt. Pepper's Lonely Hearts Club Band Price: $15.00
Artist: The Beatles
Add a protected variable
4. Open the Product class and change the access modifier for the count variable from public to protected.
5. Run the application to make sure that it works correctly and that the count is maintained properly
Â
-------------------------------------------------------------------------------------------------------------------
book:
public class Book extends Product
{
               private String author;
Â
               public Book()
               {
                               super();
                               author = "";
                               count++;
               }
Â
               public void setAuthor(String author)
               {
                               this.author = author;
               }
Â
               public String getAuthor(){
                               return author;
               }
Â
       @Override
               public String toString()
               {
                               return super.toString() +
                                               "Author:     " + author + "\n";
               }
}
-------------------------------------------------------------------------------------------------------------------
Â
Product:
Â
import java.text.NumberFormat;
Â
public class Product
{
   private String code;
   private String description;
   private double price;
   public static int count = 0;
Â
   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);
   }
Â
   @Override
   public String toString()
   {
       return "Code:       " + code + "\n" +
              "Description: " + description + "\n" +
              "Price:      " + this.getFormattedPrice() + "\n";
   }
Â
   public static intgetCount()
   {
       return count;
   }
}
Â
-------------------------------------------------------------------------------------------------------------------
Â
product app:Â
Â
import java.util.Scanner;
Â
public class ProductApp
{
   public static void main(String args[])
   {
       // display a weclome message
System.out.println("Welcome to the Product Selector\n");
Â
       // perform 1 or more selections
       Scanner sc = new Scanner(System.in);
       String choice = "y";
       while (choice.equalsIgnoreCase("y"))
       {
System.out.print("Enter product code: ");
           String productCode = sc.next(); // read the product code
sc.nextLine();Â // discard any other data entered on the line
Â
           // get the Product object
           Product p = ProductDB.getProduct(productCode);
Â
           // display the output
System.out.println();
           if (p != null)
System.out.println(p.toString());
           else
System.out.println("No product matches this product code.\n");
Â
System.out.println("Product count: " + Product.getCount() + "\n");
Â
           // see if the user wants to continue
System.out.print("Continue? (y/n): ");
           choice = sc.nextLine();
System.out.println();
       }
   }
}
-------------------------------------------------------------------------------------------------------------------
Â
ProductDB:
Â
public class ProductDB
{
   public static Product getProduct(String productCode)
   {
       // In a more realistic application, this code would
       // get the data for the product from a file or database
       // For now, this code just uses if/else statements
       // to return the correct product data
Â
       Product p = null;
Â
       if (productCode.equalsIgnoreCase("java") ||
productCode.equalsIgnoreCase("jsps") ||
productCode.equalsIgnoreCase("mcb2"))
       {
           Book b = new Book();
           if (productCode.equalsIgnoreCase("java"))
           {
b.setCode(productCode);
b.setDescription("Murach's Beginning Java");
b.setPrice(49.50);
b.setAuthor("Andrea Steelman");
           }
           else if (productCode.equalsIgnoreCase("jsps"))
           {
b.setCode(productCode);
b.setDescription("Murach's Java Servlets and JSP");
b.setPrice(49.50);
b.setAuthor("Andrea Steelman");
           }
           else if (productCode.equalsIgnoreCase("mcb2"))
           {
b.setCode(productCode);
b.setDescription("Murach's Mainframe COBOL");
b.setPrice(59.50);
b.setAuthor("Mike Murach");
           }
           p = b; // set Product object equal to the Book object
       }
       else if (productCode.equalsIgnoreCase("txtp"))
       {
           Software s = new Software();
s.setCode("txtp");
s.setDescription("TextPad");
s.setPrice(27.00);
s.setVersion("4.7.3");
           p = s; // set Product object equal to the Software object
       }
       return p;
   }
}
-------------------------------------------------------------------------------------------------------------------
Â
software.java
Â
public class Software extends Product
{
               private String version;
Â
               public Software()
               {
                               super();
                               version = "";
                               count++;
               }
Â
               public void setVersion(String version)
               {
                               this.version = version;
               }
Â
               public String getVersion(){
                               return version;
               }
Â
       @Override
               public String toString()
               {
                               return super.toString() +
                                               "Version:    " + version + "\n";
               }
}
Â
Â
hat the code for each file is like would be nice if possible, thanks.
Attachments:
----------- He-----------llo----------- Si-----------r/M-----------ada-----------m -----------Tha-----------nk -----------you----------- fo-----------r y-----------our----------- in-----------ter-----------est----------- an-----------d b-----------uyi-----------ng -----------my -----------pos-----------ted----------- so-----------lut-----------ion-----------. P-----------lea-----------se -----------pin-----------g m-----------e o-----------n c-----------hat----------- I -----------am -----------onl-----------ine----------- or----------- in-----------box----------- me----------- a -----------mes-----------sag-----------e I----------- wi-----------ll -----------be -----------qui-----------ckl-----------y