for home work assignment we have to
Implement a class Product. A product has a name and a price, for example new Product("Toaster", 29.95). Supply methods getName, getPrice, and reducePrice. Supply a program ProductPrinter that makes two products, prints the name and price, reduces their prices by $5.00, and then prints the prices again.

/**
   A product with a name and a price.
*/
public class Product
{ 
   private String n;
   private double p;

   /**
      Constructs a product with a given name and price.
      @param n the name
      @param p the price
   */
   public Product(String n, double p)
   {  
      n = "toaster";
      p = 29.95;
   }

   /**
      Gets the product name.
      @return the name
   */
   public String getName()
   {  
   String name = "toaster";
    return name;
   }

   /**
      Gets the product price.
      @return the price
   */
   public double getPrice()
   {  
     double price = 29.95;
      return price;
   }

   /**
      Reduces the product price.
      @param amount the amount by which to reduce the price
   */
   public void reducePrice(double amount)
   {  
     
    double price = price - amount;
   }
}
public class ProductPrinter
{
	public static void main(String[] args)
	{
		Product p = new Product();
		p.getName();
		p.getPrice();
		System.out.println(p);
	}
}

and i get this
compile
fail
run
fail
Compiling Main Program
fail
Compiling 1 source file to /var/folders/N+/N+SKIiskEveKsGCwnqC83++++TI/-Tmp-/asnmt668102usr1966762-submit-1292801884349
/var/folders/N+/N+SKIiskEveKsGCwnqC83++++TI/-Tmp-/asnmt668102usr1966762-submit-1292801884349/ProductPrinter.java:5: cannot find symbol
symbol  : constructor Product()
location: class Product
Product p = new Product();
            ^
1 error
/var/folders/N+/N+SKIiskEveKsGCwnqC83++++TI/-Tmp-/check8852985623170383351.xml:324: Compile failed; see the compiler error output for details.
Running Main Program
fail

Recommended Answers

All 3 Replies

Your constructor is supposed to take two arguments. On line 5 you are not supplying any arguments. Besides that most of your code is also wrong.

Your Public class is public, and therefore in it's own file. Maybe this will help. See especially, Compiling Multiple Source Files.

Your constructor is supposed to take two arguments. On line 5 you are not supplying any arguments. Besides that most of your code is also wrong.

Correct.

As slimmy has pointed, your constructor takes two arguments

public Product(String n, double p)   
{        n = "toaster";     
 p = 29.95;   }

It is advisable that if you overload a constructor (in this instance the NEW constructor) you create a default constructor that accepts no arguments.

maybe you can add this.

public Product()   {        
n = "default name";      
p = 0;   }

With this you can write

Product p = new Product();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.