New & Frustrated over implementing methods


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: New & Frustrated over implementing methods

  1. #1
    Eric Guest

    New & Frustrated over implementing methods


    Well, it is now my third week of class ans up until now the program assignments
    were fairly straight foward. Now I am encountering difficulty defining a
    new class and applying methods to it. I analyzed other example programs which
    has similar methods but I still come up short. Even though I already turned
    in this assignment, I am frustrated that I did not figure it out in time.
    Any pointers would be appreciated. Here is what I wrote:

    /**
    Program Assignment #3: P3.8, page 137 of the text

    A program to test the Product program. The TestProduct program asks the user
    for the name of two products and their prices, prints them to the screen,
    reduces their price by $5.00, and then prints them to the screen again.
    This program contains the Product class which constructs products, keeping
    track of their name and price. The methods supplied within it either retrieve
    a name, retrieve a price, subtract an amount from the original price, or
    prints the price to the screen.

    This also uses ConsoleReader class from the text.

    CS 46A, Spring, 2001
    @author E. Fraim
    @version Feb. 19, 2001
    */

    public class TestProduct
    { public static void main(String[] args)
    { Product product1 = new Product();
    Product product2 = new Product();
    ConsoleReader console = new ConsoleReader(System.in);

    System.out.println("Enter a product name: ");
    String prod1 = console.readLine();
    System.out.println("Enter the price of the product: ");
    double price1 = console.readDouble();

    product1.getPrice();
    product1.setPrice(5);
    product1.printProduct();

    System.out.println("Enter a second product name: ");
    String prod2 = console.readLine();
    System.out.println("Enter the price of the product: ");
    double price2 = console.readDouble();


    }
    }
    class Product
    {
    public Product()
    { name = "";
    price = 0;
    }

    public String getName()
    { return name;
    }

    public double getPrice(double price)
    { return price;
    }

    public void setPrice(double amount)
    { price = price - amount;
    }

    public void printProduct()
    { System.out.println(price);
    }

    private String name;
    private double price;
    }


  2. #2
    Kyle Gabhart Guest

    Re: New & Frustrated over implementing methods


    "Eric" <eric@spokesmanbicycles.com> wrote:
    >
    >Well, it is now my third week of class ans up until now the program assignments
    >were fairly straight foward. Now I am encountering difficulty defining a
    >new class and applying methods to it. I analyzed other example programs

    which
    >has similar methods but I still come up short. Even though I already turned
    >in this assignment, I am frustrated that I did not figure it out in time.
    >Any pointers would be appreciated. Here is what I wrote:
    >
    >/**
    >Program Assignment #3: P3.8, page 137 of the text
    >
    >A program to test the Product program. The TestProduct program asks the

    user
    >for the name of two products and their prices, prints them to the screen,
    >reduces their price by $5.00, and then prints them to the screen again.
    > This program contains the Product class which constructs products, keeping
    >track of their name and price. The methods supplied within it either retrieve
    >a name, retrieve a price, subtract an amount from the original price, or
    >prints the price to the screen.
    >
    >This also uses ConsoleReader class from the text.
    >
    > CS 46A, Spring, 2001
    > @author E. Fraim
    > @version Feb. 19, 2001
    >*/
    >
    >public class TestProduct
    >{ public static void main(String[] args)
    > { Product product1 = new Product();
    > Product product2 = new Product();
    > ConsoleReader console = new ConsoleReader(System.in);
    >
    > System.out.println("Enter a product name: ");
    > String prod1 = console.readLine();
    > System.out.println("Enter the price of the product: ");
    > double price1 = console.readDouble();
    >
    > product1.getPrice();
    > product1.setPrice(5);
    > product1.printProduct();
    >
    > System.out.println("Enter a second product name: ");
    > String prod2 = console.readLine();
    > System.out.println("Enter the price of the product: ");
    > double price2 = console.readDouble();
    > }
    >}
    >class Product
    >{
    > public Product()
    > { name = "";
    > price = 0;
    > }
    >
    > public String getName()
    > { return name;
    > }
    >
    > public double getPrice(double price)
    > { return price;
    > }
    >
    > public void setPrice(double amount)
    > { price = price - amount;
    > }
    >
    > public void printProduct()
    > { System.out.println(price);
    > }
    >
    > private String name;
    > private double price;
    >}



    Eric,

    It appears that you misunderstand the concept of Java objects and how methods
    are invoked on them. Let me summarize what the code you wrote does and then
    compare it to what your code was supposed to do.

    Your code does the following:
    1. Three objects are created: 2 Product objects, 1 ConsoleReader Object
    2. A string is read from the console representing the product name and stored
    in a String object referenced by the variable "product1".
    3. Another string is read from the console representing the product price
    and stored in the variable of type double called "price1".
    4. Then three methods are called on the first Product object (created in
    step #1) created earlier.
    5. Then steps 2-4 are repeated again with the second Product object.

    The problem, is that the three methods you are calling on the Product object
    are supposed to process the data retrieved from the console, but the Product
    object has no way of accessing this data!

    The solution, is to create the Product object using the data you have collected
    from the console. Besides, what is a "Product" except a logical entity that
    has a name and a price!

    Here is a sample process that will accomplish what you are trying to do:
    1. A ConsoleReader object is created.
    2. A string is read from the console representing the product name and stored
    in a String object referenced by the variable "product1".
    3. Another string is read from the console representing the product price
    and stored in the variable of type double called "price1".
    4. A Product object is created using the values stored in the two variables,
    product1 and price1.
    5. Then three methods are called on the Product object created in step #3
    6. Then steps 2-5 are repeated again with the second Product object.

    Here is an idea of what the code might look like:

    public static void main(String[] args)
    {
    ConsoleReader console = new ConsoleReader(System.in);
    Product product1, product2;
    String productName;
    double productPrice;

    System.out.println("Enter a product name: ");
    String productName = console.readLine();
    System.out.println("Enter the price of the product: ");
    double productPrice = console.readDouble();

    product1 = new Product( productName, productPrice );
    product1.getPrice();
    product1.setPrice(5);
    product1.printProduct();

    System.out.println("Enter a product name: ");
    String productName = console.readLine();
    System.out.println("Enter the price of the product: ");
    double productPrice = console.readDouble();

    product2 = new Product( productName, productPrice );
    product2.getPrice();
    product2.setPrice(5);
    product2.printProduct();
    }//end main()

    Then change the Product constructor to accept the two parameters passed to
    it by the above code:

    Product( String _name, double _price )
    {
    name = _name;
    price = _price;
    } //end Product( String, double )


    I hope that this is helpful. If there is anything that isn't entirely clear,
    don't hesitate to post another question or e-mail me directly.

    Happy Coding!

    Cordially,

    Kyle Gabhart
    DevX Java Pro


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center