Question

Starting Out With Java early Objects by Tony Gladdis The class to be modified is found...

Starting Out With Java early Objects by Tony Gladdis

The class to be modified is found in Code Listing 6-29 section on pgs 417-418 in the book 'Starting Out With Java early Objects by Tony Gladdis the 5th Edition'

7. RetailItem Class Modification

Modify this chapter’s RetailItem class (which uses an inner class named CostData) to include accessor and mutator methods for getting and setting an item’s wholesale and retail cost. Demonstrate the methods in a program.

Thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

Please find the answer below:

Program
---------

// Retail Item Java Class

package chapter6;

import java.text.DecimalFormat;

public class RetailItem
{
   private String description; // Item description
   private int itemNumber;      // Item number
   private CostData cost;       // Cost data
  
   /**
   * RetailItem class constructor
   **/
   public RetailItem(String desc, int itemNum, double wholesale, double retail)
   {
       description = desc;
       itemNumber = itemNum;
       cost = new CostData(wholesale, retail);
   }

   /**
   * RetailItem class toString method
   */
   public String toString() {
       String str; // To hold a descriptive string.
       // Create a DecimalFormat object to format output.
       DecimalFormat dollar = new DecimalFormat("#,##0.00");
       // Create a string describing the item.
       str = "Description: " + description
               + "\nItem Number: " + itemNumber
               + "\nWholesale Cost: $"
               + dollar.format(cost.getWholesale())
               + "\nRetail Price: $"
               + dollar.format(cost.getRetail());
       // Return the string.
       return str;
   }

   /**
   * CostData Inner Class
   */
   private class CostData
   {
       public double wholesale, // Wholesale cost
       retail;     // Retail price
      
       public double getWholesale() {
           return wholesale;
       }

       public void setWholesale(double wholesale) {
           this.wholesale = wholesale;
       }

       public double getRetail() {
           return retail;
       }

       public void setRetail(double retail) {
           this.retail = retail;
       }

       /**
       * CostData class constructor
       */
       public CostData(double w, double r)
       {
           wholesale = w;
           retail = r;
       }
   }
}


//Demo Class

package chapter6;

/**
* This program demonstrates the RetailItem class,  
* which has an inner class.
*/
public class InnerClassDemo
{
   public static void main(String[] args)
   {
       // Create a RetailItem object.
       RetailItem item = new RetailItem("Candy bar", 17789,0.75, 1.5);
       // Display the item's information.
       System.out.println(item);
    }
}


Output:
---------

Description: Candy bar
Item Number: 17789
Wholesale Cost: $0.75
Retail Price: $1.50

Screenshot:
------------

eclipse-photon-workspace - StartingOutWithJava/src/chapter6/InnerClassDemo,java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help Quick Access : Console X JuJUnit E. Problems @Javadoc [D. Declaration «terminated> InnerClassDemo [Java Application] C:\Program Files (x86)、Javaure 1.8.0-151 Description: Candy bar Item Number: 17789 Wholesale Cost: $0.75 Retail Price: $1.50 1 package chapter6; 2 4 This program demonstrates the RetailItem class, w 5 which has an inner class. 7 public class InnerClassDemo 9 public static void main(String[ 1 args 6 Ju 10 // Create a Retailltem object. RetailItem itemnew Retailltem( Candy bar // Display the items information System.out.println(item); 12 13 14 15 16 17 18


Notes:
Added Methods:

public double getWholesale() {
   return wholesale;
}

public void setWholesale(double wholesale) {
   this.wholesale = wholesale;
}

public double getRetail() {
   return retail;
}

public void setRetail(double retail) {
   this.retail = retail;
}


Hope this is helpful.
Let me know if you need more information or have any doubts/clarification in the comments.
Kindly, like the solution if you find it useful.
Thanks,

Add a comment
Know the answer?
Add Answer to:
Starting Out With Java early Objects by Tony Gladdis The class to be modified is found...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Chapter 14 starting out with c++ from control structures through objects Feet Inches Modification Modify the...

    Chapter 14 starting out with c++ from control structures through objects Feet Inches Modification Modify the FeetInches class discussed in this chapter so it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program.

  • JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects...

    JAVA 1. Write a class called Rectangle that represents a rectangular two-dimensional region. Your Rectangle objects should have the following fields and methods: int width, int height Where width and height are the dimensions of the rectangle. Write accessor methods (i.e. getWidth(), getHeight()) that retrieve the values stored in the fields above. And mutator methods (i.e. setWidthl), setHeight() ) that change the field's values. Finally create a method called getAreal) that returns the area of the rectangle. Like we did...

  • Java

    Task #1 Creating a New Class1. In a new file, create a class definition called Television.2. Put a program header (comments/documentation) at the top of the file// The purpose of this class is to model a television// Your name and today's date3. Declare the 2 constant fields listed in the UML diagram.4. Declare the 3 remaining fields listed in the UML diagram.5. Write a comment for each field indicating what it represents.6. Save this file as Television.java.7. Compile and debug....

  • Write a program in java. You are tasked with writing an application that will keep track...

    Write a program in java. You are tasked with writing an application that will keep track of Insurance Policies. Create a Policy class called InsurancePolicies.java that will model an insurance policy for a single person. Use the following guidelines to create the Policy class: • An insurance policy has the following attributes: o Policy Number o Provider Name o Policyholder’s First Name o Policyholder’s Last Name o Policyholder’s Age o Policyholder’s Smoking Status (will be “smoker” or “non-smoker”) o Policyholder’s...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT