Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code?

    THIS IS THE QUESTION
    public class Computer
    {
    private String brand; //asus, acer, hp etc
    private double price; //1699.00
    }

    Using class definition above, write definition of following methods
    -Normal constructor to initialize all attribute
    -Accessor for each attribute
    -Mutator for each attribute
    -Method calDiscount(double) to calculate and return discount amount
    method receives the discount percentage rate from the application program.
    The discount computed as:
    discount amount = price x discount percentage rate
    -toString method to display the information of object

    Using java, write main() to perform following task
    -Declare an array of computer objects. Get size of array from user
    -Get input values from user and create the objects
    -Display the information of the objects
    -Calculate and display total discount amount
    -Count and display the number of "Acer" computers

    THIS IS MY CLASS DEFINITION :

    public class Computer
    {
    private String brand;
    private double price;

    //normal constructor
    public Computer(String b, double p)
    {
    brand=b;
    price=p;
    }

    //accessor
    public String getBrand()
    {
    return brand;
    }
    public double price()
    {
    return price;
    }

    //mutator
    public void setComputer(String b, double p)
    {
    brand=b;
    price=p;
    }

    //processor
    public double calDiscount()
    {
    double discount;
    double dRate=0;
    discount=price*dRate;
    return discount;
    }

    //toString
    public String toString()
    {
    return "The computer brand is: "+brand +"\nThe price is:RM "+price;
    }
    }//end class



    THIS IS MY APPLICATION PROGRAM:

    import javax.swing.*;
    public class ComputerApp
    {
    public static void main(String[]args)
    {
    int n=0;
    Computer comp[]=new Computer[n];
    for(int i=0; i<n; i++)
    {
    String brand=JOptionPane.showInputDialog("Enter the computer's brand: ");
    double price=Double.parseDouble(JOptionPane.showInputDial og("Enter the price:RM "));
    double dRate=Double.parseDouble(JOptionPane.showInputDial og("Enter the discount rate: "));

    comp[i].setComputer(brand, price);
    }//end for

    for(int i=0; i<n; i++)
    {
    comp[i].toString();
    System.out.println(comp[i].toString());
    }

    double discount=0, dRate;
    for(int i=0; i<n; i++)
    {
    discount=discount+comp[i].getCalDiscount;
    }
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,304
    My Mood
    Mellow
    Thanks
    252
    Thanked 778 Times in 763 Posts

    Default Re: What's wrong with my code?

    Please post your code in code tags.

    What do you mean what's wrong with your code? Is there something wrong with it? Are you getting errors or is the program not behaving as you'd like? Please give details, post errors you want help with, ask specific questions. If you just want your program graded, hand it in to the instructor.

Similar Threads

  1. There are something wrong with my code...
    By khanhnq in forum Object Oriented Programming
    Replies: 0
    Last Post: January 13th, 2013, 09:04 PM
  2. What Am I doing Wrong In This Code?
    By RadiusUnknown in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 14th, 2012, 04:00 PM
  3. what is wrong with my code? need help
    By balmung123 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2012, 05:12 AM
  4. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 29th, 2012, 04:33 PM
  5. Please what is wrong with my code
    By LordDavid in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 28th, 2012, 10:08 PM

Tags for this Thread