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 18 of 18

Thread: writing a class

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default writing a class

    I wrote almost the entire class other than one thing that i'm confused about which is this:
    public Pizza(Pizza pi) {
        }

    the rest of the coding is done. when i test it it says that calculateArea() is required:double...arguments found: no arguments
    this is my class:
     
    public class Pizza {
     
        private static double diameter;
        private static double price;
     
        public Pizza() {
            diameter = 6.0;
            price = 2.25;
     
        }
     
        public Pizza(double d, double p) {
            diameter = d;
            price = p;
        }
     
        public Pizza(Pizza pi) {
        }
     
        public double calculateArea(double area) {
     
            area = diameter / 2;
            return area;
        }
     
        public double pricePerSquareInch(double priceSqIn, double area) {
            area = diameter / 2;
            priceSqIn = price / area;
     
            return priceSqIn;
        }
     
        public double getDiameter() {
            return diameter;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setDiameter(double d) {
            diameter = d;
        }
     
        public void setPrice(double p) {
            price = p;
        }
    }
    and i'm using this to test it:
    import java.text.*;
     
     
     public class CheapPizza{
     
     	public static void main(String[] arg){
     		//declare pizza objects
     		Pizza p1 = new Pizza();
     		Pizza p2 = new Pizza(9.0, 3.10);
     		Pizza p3 = new Pizza(p2);
     
     		//change some info on p3
     		p3.setPrice(4.00);
     		p3.setDiameter(12.0);
     
     		//formatting
     		NumberFormat nf = NumberFormat.getInstance();
     		NumberFormat nf2 = NumberFormat.getInstance();
     		NumberFormat cf = NumberFormat.getCurrencyInstance();
     
     		nf.setMaximumFractionDigits(2);
     		nf2.setMaximumFractionDigits(4);
     
     		//display the information
     		System.out.println("Diameter of Pizza     Area of Pizza     Price     Price/SQ Inch");
     		System.out.printf("%15.2f      %13.2f       " + cf.format(p1.getPrice()) + "      %13.4f\n", p1.getDiameter(), p1.calculateArea(), p1.pricePerSquareInch());
     		System.out.printf("%15.2f      %13.2f       " + cf.format(p2.getPrice()) + "      %13.4f\n", p2.getDiameter(), p2.calculateArea(), p2.pricePerSquareInch());
     		System.out.printf("%15.2f      %13.2f       " + cf.format(p3.getPrice()) + "      %13.4f\n", p3.getDiameter(), p3.calculateArea(), p3.pricePerSquareInch());
     
     	}
     }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    I wrote almost the entire class other than one thing that i'm confused about which is this:
    public Pizza(Pizza pi) {
        }
    It looks like you're trying to write a copy constructor where you usually copy the properties of one object into the newly created Pizza object. But you're doing nothing with the parameter, pi, and so it looks like your work is incomplete. What are your instructions regarding what you're supposed to do here?

    the rest of the coding is done. when i test it it says that calculateArea() is required:double...arguments found: no arguments
    this is my class:
    You should know the drill by now: if you have an error, please post the entire error message and indicate which line(s) of code is the offending line.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    for this part the instruction is: "copy constructor the creates an exact copy of the passed pizza object pi."
    public Pizza(Pizza pi) {
        }
    the error message that im getting is this:
    method calculateArea in class Pizza cannot be applied to given types;
      required: double
      found: no arguments
      reason: actual and formal argument lists differ in length
     
    method pricePerSquareInch in class Pizza cannot be applied to given types;
      required: double,double
      found: no arguments
      reason: actual and formal argument lists differ in length
    ----
    (Alt-Enter shows hints)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    for this part the instruction is: "copy constructor the creates an exact copy of the passed pizza object pi."
    public Pizza(Pizza pi) {
        }
    OK, create "an exact copy..." the newly created Pizza's fields should match those of the Pizza passed into the constructor.

    So first off: what are the two fields I'm talking about?
    How would you set the fields in the new Pizza (you're already doing something similar in your other constructors)?
    How would you set these fields with the values from the Pizza object that is passed into the constructor, pi?
    Think on these questions, and you'll likely solve this problem.

    the error message that im getting is this:
    method calculateArea in class Pizza cannot be applied to given types;
      required: double
      found: no arguments
      reason: actual and formal argument lists differ in length
    The error tells you what's wrong -- you're calling calculateArea by passing in no arguments -- calculateArea(), while the constructor expects a double parameter -- calculateArea(someDoubleParameter).

    So either you're calling the method wrong, or you've defined the method wrong. The thinking point here is this: does this method really need a parameter? Are you using the value passed into the parameter for any purpose? Or in other words, is the value of the parameter ever on the *left* hand side of an expression?

    method pricePerSquareInch in class Pizza cannot be applied to given types;
      required: double,double
      found: no arguments
      reason: actual and formal argument lists differ in length
    You have the same issues for this problem, and the solution is the same as well.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    for the first part... are the two fields(diameter, price)?
    is this what i'm suppose to do?
    public Pizza(Pizza pi) {
           diameter = 12.0;
           price = 4.00;
        }

    and for the other one is this how its supposed to be?!!
    public double pricePerSquareInch(double priceSqIn, double area) {
            area = diameter / 2;
            pr = price / area;
            pr = priceSqIn;
            return priceSqIn;

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    for the first part... are the two fields(diameter, price)?
    is this what i'm suppose to do?
    public Pizza(Pizza pi) {
           diameter = 12.0;
           price = 4.00;
        }
    No. You're pulling out "magic numbers" from thin air, 12.00 and 4.00, and you're totally ignoring the values held by pi. Why not use pi when setting diameter and price?

    and for the other one is this how its supposed to be?!!
    public double pricePerSquareInch(double priceSqIn, double area) {
            area = diameter / 2;
            pr = price / area;
            pr = priceSqIn;
            return priceSqIn;
    No. Again, do you really need parameters for this method???

  7. #7
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    did i get it right this time? haha
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
        }
    and i think i got this one right too:
    public double pricePerSquareInch() {
            double area = diameter / 2;
            double priceSqIn = price / area;
            return priceSqIn;
        }
    the only thing now is that my output isn't correct
    its suppose to look like this:
    Untitled.jpg

    but mine looks like this:
    run:
    Diameter of Pizza     Area of Pizza     Price     Price/SQ Inch
              12.00               6.00       $4.00             0.6667
              12.00               6.00       $4.00             0.6667
              12.00               6.00       $4.00             0.6667
    BUILD SUCCESSFUL (total time: 0 seconds)

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    did i get it right this time? haha
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
        }
    In the code above, you're getting the values from pi, but not doing anything with them you're simply getting them and discarding them. You want to *use* those values to set the state of the new Pizza object. Please don't just post code, but try to think about what each line is doing before creating the line. Programming is an exercise in thinking, not guessing.

    and i think i got this one right too:
    public double pricePerSquareInch() {
            double area = diameter / 2;
            double priceSqIn = price / area;
            return priceSqIn;
        }
    That looks better, although my formula for area is vastly different: Math.PI * radius * radius. All you're calculating with diameter / 2.0 is the radius.

    Getting closer though.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    thanks
    got this one fixed
    public double calculateArea() {
            double radius = diameter / 2;
            double area = Math.PI * radius * radius;
            return area;
        }

    for this one:
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
     
     
        }
    what i know is that this is where i link public class Pizza(); with public class CheapPizza();
    I'm just not understanding how its going to work
    I tried a few things my answer was 12.0 for the diameter which is p3
    can you please explain it a bit more?
    thanks and sorry :/

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    thanks
    got this one fixed
    public double calculateArea() {
            double radius = diameter / 2;
            double area = Math.PI * radius * radius;
            return area;
        }

    for this one:
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
     
     
        }
    what i know is that this is where i link public class Pizza(); with public class CheapPizza();
    I'm just not understanding how its going to work
    I tried a few things my answer was 12.0 for the diameter which is p3
    can you please explain it a bit more?
    thanks and sorry :/
    For a copy constructor you should copy the state of the other object. For instance:

    class Foo {
       private int baz;
       private int bar;
     
       public Foo(Foo other) {
          // get the values held by the other class's fields
          // *** and use them to set the current fields
          this.baz = other.getBaz();
          this.bar = other.getBar();
       }
     
       public int getBar() {
          return bar;
       }
     
       public int getBaz() {
          return baz;
       }
     
       // plus setters,....
    }

  11. #11
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    based on the example i think this is how it should be done... but its still giving me the same output :/
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
     
            Pizza p1 = pi;
            Pizza p2 = pi;
            Pizza p3 = pi;
     
            Pizza.diameter = p1.getDiameter();
            Pizza.price = p1.getPrice();
     
            p1.setDiameter(6.00);
            p1.setPrice(2.25);
     
            Pizza.diameter = p2.getDiameter();
            Pizza.price = p2.getPrice();
     
            p2.setDiameter(9.00);
            p2.setPrice(3.10);
     
            Pizza.diameter = p3.getDiameter();
            Pizza.price = p3.getPrice();
     
            p3.setDiameter(12.0);
            p3.setPrice(4.00);
     
     
        }


  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Quote Originally Posted by m7abraham View Post
    based on the example i think this is how it should be done... but its still giving me the same output :/
    public Pizza(Pizza pi) {
     
            pi.getDiameter();
            pi.getPrice();
     
            Pizza p1 = pi;
            Pizza p2 = pi;
            Pizza p3 = pi;
     
            Pizza.diameter = p1.getDiameter();
            Pizza.price = p1.getPrice();
     
            p1.setDiameter(6.00);
            p1.setPrice(2.25);
     
            Pizza.diameter = p2.getDiameter();
            Pizza.price = p2.getPrice();
     
            p2.setDiameter(9.00);
            p2.setPrice(3.10);
     
            Pizza.diameter = p3.getDiameter();
            Pizza.price = p3.getPrice();
     
            p3.setDiameter(12.0);
            p3.setPrice(4.00);
     
     
        }
    This is your constructor? This doesn't make any sense at all. Again throwing code against the wall at random and seeing what sticks is not a viable way to program. Please delete this and re-look at my last post.
    Last edited by curmudgeon; September 30th, 2012 at 03:31 PM.

  13. #13
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    ok sorry...
    just one question
    whats "this" in the program

  14. #14
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    This is the current object. The one you're creating in this constructor.

  15. #15
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    is this correct?
    public Pizza(Pizza pi) {
     
            diameter = pi.diameter;
            price = pi.price;
        }

    sorry im soo confused for some reason :/

  16. #16
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: writing a class

    Muuuuuuuuuuuuuuch better!

  17. #17
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    haha yeah took a lot of brain storming!!!
    but i still get the same output for some reason
    this is what my program looks like now:
    /* This class calculates the area of a pizza and 
     * the price per square inch
     * 
     * Mikhail Abraham
     * 9/28/2012
     * JDK Version 7.0
     */
    public class Pizza {
     
        private static double diameter;
        private static double price;
     
        public Pizza() {
            diameter = 6.0;
            price = 2.25;
     
        }
     
        public Pizza(double d, double p) {
            diameter = d;
            price = p;
        }
     
        public Pizza(Pizza pi) {
     
            diameter = pi.getDiameter();
            price = pi.getPrice();
     
        }
     
        public double calculateArea() {
            double radius = diameter / 2;
            double area = Math.PI * radius * radius;
            return area;
        }
     
        public double pricePerSquareInch() {
            double radius = diameter / 2;
            double area = Math.PI * radius * radius;
            double priceSqIn = price / area;
            return priceSqIn;
        }
     
        public double getDiameter() {
            return diameter;
        }
     
        public double getPrice() {
            return price;
        }
     
        public void setDiameter(double d) {
            diameter = d;
        }
     
        public void setPrice(double p) {
            price = p;
        }
    }

  18. #18
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: writing a class

    this is the output:
    run:
    Diameter of Pizza     Area of Pizza     Price     Price/SQ Inch
              12.00             113.10       $4.00             0.0354
              12.00             113.10       $4.00             0.0354
              12.00             113.10       $4.00             0.0354
    BUILD SUCCESSFUL (total time: 6 seconds)

Similar Threads

  1. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  2. Replies: 3
    Last Post: June 17th, 2012, 06:22 PM
  3. [SOLVED] Writing a program with arrays and class methods... PLEASE HELP?
    By syang in forum What's Wrong With My Code?
    Replies: 17
    Last Post: August 8th, 2011, 07:02 AM
  4. Help writing a Template Class
    By mamipapi in forum Object Oriented Programming
    Replies: 2
    Last Post: October 5th, 2010, 09:34 PM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM