Question
Asked Feb 12, 2020
2 views

write a java program which Create a Product object: “Laptop”, $799.99
• Create a Product object: “Printer”, $219.99
• Print the information about the products
• Change the price of the laptop to $999
• Change the price of the printer to $329.99
• Print the information about the products

check_circle

Expert Answer

Step 1

Below are the java program and its approach: -

Approach: -

  • Creating a class product and declaring the variables as per the requirement.
  • Creating a parameterized constructor for the product class.
  • Getter and setter functions are used to set the name and cost of the item.
  • Overriding the toString() method to return the specified values of the objects.
  • Declaring the main class and inside the main, the main() method is defined.
  • Creating the objects of Product class and passing the values as arguments.
  • The parameterized constructor will call automatically after creating the objects.
  • Displaying the initial values of the objects.
  • Resetting the cost of each item by calling the setCost() method and passing the new cost as a parameter.
  • Displaying the new values of the objects.     
Step 2

Java program: -

//product class is defined

class Product

{

    //declaring the variables

    String item;        //to store the name of the product

    double cost;        //to store the cost of the product

    //parameterized constructor

    Product(String item, double cost)

    {

        //this keyword points the current object

        /*this keyword removes the confusion among class attributes

        and parameter with the same name */

        this.item = item;      

        this.cost = cost;      

    }

    //getter function to get the name of item

    public String getItem()

    {

        //returns item

        return item;

    }

    //setter function to set the name of item

    public void setItem(String item)

    {

        this.item = item;

    }

    //getter function to get the cost of item

    public double getCost()

    {

        //returns the cost

        return cost;

    }

    //setter function to set the cost of item

    public void setCost(double cost)

    {

        this.cost = cost;

    }

    @Override

  &nbs...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Create a complex query based on two or more tables (SCHOOL, DEPARTMENT, PROFESSOR, TECHNOLOGY, COURS...

A: Create three tables for the given question-For School-create table SCHOOL(SCHOOL_CODE varchar(100),D...

question_answer

Q: To reduce the communication cost of remote applications with high access frequency, it is required t...

A: Queries are: App1: select hours * pay_rate as payment from works_on, project where plocation = "NEW_...

question_answer

Q: in Python create a program that Must include Python comments for each of the items show below. Calcu...

A: Program DesignThe following python program functions as specified in the question except for the Tur...

question_answer

Q: Express 384 in IEEE 32-bit floating-point format.

A: Representing 384 in IEEE 32-bit floating-point format:Step 1: Convert the given number “384” to its ...

question_answer

Q: Create a query that uses any THREE of the tables (SCHOOL, DEPARTMENT, PROFESSOR, TECHNOLOGY & CO...

A: Creating the three tables for the given question-For Department-create table DEPARTMENT(DEPARTMENT_N...

question_answer

Q: Write a short program in C++ languge to compute the number of hours in a non-leap year. Compile and ...

A: A C++ program to calculate total hours in a non-leap year.There are 365 days in a non-leap year.Ther...

question_answer

Q: Express -1/32 in IEEE 32-bit floating-point format.

A: Representing -1/32 in IEEE 32-bit floating-point format:Step 1: Convert the given number “1/32” to i...

question_answer

Q: Is there a way to condense my statements? I'm also getting a "bad operand type" error. I haven't lea...

A: The error you were getting was due to improper arguments in the print statements. The rectified Java...

question_answer

Q: Given two processes in the READY state, one that is CPU-bound and one that is I/O- bound, which proc...

A: Process having high priority for running nextThe Input/output bound process should be given a high p...