Results 1 to 3 of 3
  1. #1
    mudit222 is offline Member
    Join Date
    Apr 2010
    Posts
    16
    Rep Power
    0

    Default error in java program..

    ques - Create Product having following attributes: Product ID, Name, Category ID and UnitPrice. Create ElectricalProduct having the
    following additional attributes: VoltageRange and Wattage. Add a behavior to change the Wattage and price of the electrical
    product. Display the updated ElectricalProduct details.

    program made by me-


    import java.io.*;


    class Product
    {
    int productId;
    String productName;
    int categoryId;
    double unitPrice;
    Product(double a)
    {
    unitPrice = a;
    }
    }
    class ElectricalProduct extends Product
    {
    int voltageRange;
    int wattage;
    void changeWattage(int watt)
    {
    wattage = watt;
    }
    void changePrice(double b)
    {
    super(b);
    }
    void show()
    {
    System.out.println("The Product ID is : " +productId);
    System.out.println("The Product NAME is : " +productName);
    System.out.println("The Category ID is : " +categoryId);
    System.out.println("The Voltage Range is : " +voltageRange);
    System.out.println("The Change Wattage is : " +wattage);
    System.out.println("The Change UnitPrice is : " +unitPrice);

    }



    }

    class NewProduct
    {
    public static void main(String [] args) throws IOException
    {
    double newprice;
    int w;
    Product p = new Product();
    ElectricalProduct ep = new ElectricalProduct();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the Product ID : ");
    p.productId = Integer.parseInt(br.readLine());
    System.out.println("Enter the Product Name : ");
    p.productName = br.readLine();
    System.out.println("Enter the Category ID : ");
    p.categoryId = Integer.parseInt(br.readLine());
    System.out.println("Enter the Unit Price : ");
    p.unitPrice = Double.parseDouble(br.readLine());
    System.out.println("Enter the Voltage Range : ");
    ep.voltageRange = Integer.parseInt(br.readLine());
    System.out.println("Enter the New Wattage : ");
    w = Integer.parseInt(br.readLine());
    ep.changeWattage(w);
    System.out.println("Enter the New Unit Price : ");
    newprice = Double.parseDouble(br.readLine());
    ep.changePrice(newprice);
    System.out.println("The UPDATED ELECTRICAL PRODUCT DETAILS ARE ");
    ep.show();

    }

    }



    rectify the errors in this program...

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    One problem I'm seeing is this:
    Java Code:
    void changePrice(double b)
    {
    super(b);
    }
    Instead of updating the price, you're creating a new object. This is code reusability, but not in the right way. Set the price in changePrice() method and call that method in the constructor.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by mudit222 View Post
    rectify the errors in this program...
    That's not how this works.
    You need to tell us what errors you have.
    Not simply dump your code here and say "fix it".

Similar Threads

  1. Replies: 4
    Last Post: 12-13-2010, 10:00 PM
  2. Error running java program using URL
    By gio123bg in forum New To Java
    Replies: 6
    Last Post: 06-30-2009, 06:26 PM
  3. error running java program
    By bdasilva in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 01:46 AM
  4. Same error msg with every program Java Textpad
    By peterdfl in forum New To Java
    Replies: 7
    Last Post: 04-17-2009, 04:49 AM
  5. How to get error codes using java program
    By kasipandian in forum Web Frameworks
    Replies: 10
    Last Post: 05-25-2008, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •