Results 1 to 3 of 3
- 10-04-2010, 08:01 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
[SOLVED] Need some help with this program please.
Hello everyone, im somewhat new to the java scene and only have little experince in java. Im having a problem with this program where 2/3 of the parts work while the third one doesn't return the right data.
import java.text.*;
/**
*Program to test the Pizza class by creating Pizza objects and displaying there information
*@author
*@version 1.0
*/
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());
}
}
^^^ THIS PART SHOULD NOT BE CHANGED
and this is what i have so far.
public class Pizza{
private double diameter;
private double price;
private double d;
private double p;
private double Area;
private double pricePerSquareInch;
private double Diameter;
private double Price;
private double pi;
public Pizza() {
Diameter = 6.0;
Price = 2.25;
}
public Pizza(double d, double p){
d = diameter;
p = price;
}
public Pizza(Pizza pi){
d = pi.getDiameter();
p = pi.getPrice();
}
public double calculateArea(){
Area = Math.PI * (Diameter/2) * (Diameter/2);
return Area;
}
public double pricePerSquareInch(){
pricePerSquareInch = Price / Area;
return pricePerSquareInch;
}
public double getDiameter(){
return Diameter;
}
public double getPrice(){
return Price;
}
public void setDiameter(double d){
Diameter = d;
}
public void setPrice(double p){
Price = p;
}
}
I cannot get the second line to show properly. It only returns 0's for everything. Any help will be much appreciated and thanks in advance.Last edited by fezman1337; 10-04-2010 at 09:30 PM.
- 10-04-2010, 08:11 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Your second constructor is wrong.
but --> Naming ConventionsJava Code:public Pizza(double d, double p) { Diameter = d; Price = p; }
- 10-04-2010, 08:16 PM #3
Member
- Join Date
- Oct 2010
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
Execute A program from a Program!
By Moncleared in forum Advanced JavaReplies: 2Last Post: 02-22-2009, 04:17 PM -
Executing a program within a program
By gibsonrocker800 in forum New To JavaReplies: 5Last Post: 05-12-2008, 08:24 AM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks

Bookmarks