|
-
January 24th, 2012, 07:48 PM
#1
Stupid beginner Mistake, Can you help?
Hello everyone. So I am trying to learn how to program in Java. I am completely self taught, so I predict I will be consulting this site a lot more from now on.
The program I have to make is:
Code:
(Invoice Class) Create a class called Invoice that a hardware store might use to represent
an invoice for an item sold at the store. An Invoice should include four pieces of information as
instance variables—a part number (type String), a part description (type String), a quantity of the
item being purchased (type int) and a price per item (double). Your class should have a constructor
that initializes the four instance variables. Provide a set and a get method for each instance variable.
In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e.,
multiplies the quantity by the price per item), then returns the amount as a double value. If the
Making a Difference 101
quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to
0.0. Write a test application named InvoiceTest that demonstrates class Invoice’s capabilities.
So this is what I wrote:
Code:
public class Invoice{
private String number;
public void setNumber(String num)
{
number = num;
}
public String getNumber()
{
return number;
}
private String description;
public void setDescription(String desc)
{
description = desc;
}
public String getDescription()
{
return description;
}
private int Amount;
public void setAmount(int amo)
{
Amount = amo;
}
public int getAmount()
{
return Amount;
}
private double Price;
public void setPrice(double pri)
{
Price = pri;
}
public double getPrice()
{
return Price;
}
private double InvoiceAmount;
public void setInvoiceAmount(double IA)
{
InvoiceAmount = (Price*Amount);
}
public double getInvoiceAmount()
{
return InvoiceAmount;
}
}
And the test is
Code:
import java.util.Scanner;
public class InvoiceTest{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);
Invoice Drill = new Invoice();
Invoice Hose = new Invoice();
Invoice Wood = new Invoice();
Invoice Bulb = new Invoice();
Drill.setPrice = 94.00;
Hose.setPrice = 4.50;
Wood.setPrice = 8.32;
Bulb.setPrice = -3.9999;
Drill.setNumber = QWERTY32;
Hose.setNumber = QWOPLOL9;
Wood.setNumber = JIZZPNTZ;
Bulb.setNumber = YUNOCOST;
Drill.setDescription = Jizz;
Hose.setDescription = in;
Wood.setDescription = my;
Bulb.setDescription = pants;
if (Drill.Price < 0)
Drill.setPrice = 0;
if (Hose.Price < 0)
Hose.setPrice = 0;
if (Wood.Price < 0)
Wood.setPrice = 0;
if (Bulb.Price < 0)
Wood.setPrice = 0;
System.out.printf("\n\nPrice for Drills is: $%.2f.\n\nPrice for Hoses is: $%.2f.\n\nPrice for Wood is: $%.2f.\n\nPrice for Bulbs is: $%.2f.",
Drill.getPrice, Hose.getPrice, Wood.getPrice, Bulb.getPrice);
System.out.println();
System.out.println();
System.out.printf("Drill: %s", Drill.getDescription);
System.out.println();
System.out.printf("Hose: %s", Hose.getDescription);
System.out.println();
System.out.printf("Wood: %s", Wood.getDescription);
System.out.println();
System.out.printf("Bulb: %s", Bulb.getDescription);
System.out.println();
System.out.print("How many Drills would you like? ");
Drill.setAmount = input.nextInt();
System.out.println();
System.out.print("How many Hoses would you like? ");
Hose.setAmount = input.nextInt();
System.out.println();
System.out.print("How many Tiles of Wood would you like? ");
Wood.setAmount = input.nextInt();
System.out.println();
System.out.print("How many bulbs would you like? ");
Bulb.setAmount = input.nextInt();
System.out.println();
System.out.println("Number Object Amount Total");
System.out.print("%s Drill %.2f $%.2f", Drill.getNumber,
Drill.getAmount, Drill.getInvoiceAmount);
System.out.println();
System.out.print("%s Hose %.2f $%.2f", Hose.getNumber,
Hose.getAmount, Hose.getInvoiceAmount);
System.out.println();
System.out.print("%s Wood %.2f $%.2f", Wood.getNumber,
Wood.getAmount, Wood.getInvoiceAmount);
System.out.println();
System.out.print("%s Bulb %.2f $%.2f", Bulb.getNumber,
Bulb.getAmount, Bulb.getInvoiceAmount);
System.out.println();
System.out.print("Grand Total: $%.2f", Sum);
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bulb.InvoiceAmount;
}
}
I would appreciate any help possible. I am getting some errors while compiling. Mostly some problem with symbols and my objects:
Code:
C:\Programs>javac Invoice.java InvoiceTest.java
InvoiceTest.java:21: error: cannot find symbol
Drill.setPrice = 94.00;
^
symbol: variable setPrice
location: variable Drill of type Invoice
InvoiceTest.java:22: error: cannot find symbol
Hose.setPrice = 4.50;
^
symbol: variable setPrice
location: variable Hose of type Invoice
InvoiceTest.java:23: error: cannot find symbol
Wood.setPrice = 8.32;
^
symbol: variable setPrice
location: variable Wood of type Invoice
InvoiceTest.java:24: error: cannot find symbol
Bulb.setPrice = -3.9999;
^
symbol: variable setPrice
location: variable Bulb of type Invoice
InvoiceTest.java:26: error: cannot find symbol
Drill.setNumber = QWERTY32;
^
symbol: variable setNumber
location: variable Drill of type Invoice
InvoiceTest.java:26: error: cannot find symbol
Drill.setNumber = QWERTY32;
^
symbol: variable QWERTY32
location: class InvoiceTest
InvoiceTest.java:27: error: cannot find symbol
Hose.setNumber = QWOPLOL9;
^
symbol: variable setNumber
location: variable Hose of type Invoice
InvoiceTest.java:27: error: cannot find symbol
Hose.setNumber = QWOPLOL9;
^
symbol: variable QWOPLOL9
location: class InvoiceTest
InvoiceTest.java:28: error: cannot find symbol
Wood.setNumber = JIZZPNTZ;
^
symbol: variable setNumber
location: variable Wood of type Invoice
InvoiceTest.java:28: error: cannot find symbol
Wood.setNumber = JIZZPNTZ;
^
symbol: variable JIZZPNTZ
location: class InvoiceTest
InvoiceTest.java:29: error: cannot find symbol
Bulb.setNumber = YUNOCOST;
^
symbol: variable setNumber
location: variable Bulb of type Invoice
InvoiceTest.java:29: error: cannot find symbol
Bulb.setNumber = YUNOCOST;
^
symbol: variable YUNOCOST
location: class InvoiceTest
InvoiceTest.java:31: error: cannot find symbol
Drill.setDescription = Jizz;
^
symbol: variable setDescription
location: variable Drill of type Invoice
InvoiceTest.java:31: error: cannot find symbol
Drill.setDescription = Jizz;
^
symbol: variable Jizz
location: class InvoiceTest
InvoiceTest.java:32: error: cannot find symbol
Hose.setDescription = in;
^
symbol: variable setDescription
location: variable Hose of type Invoice
InvoiceTest.java:32: error: cannot find symbol
Hose.setDescription = in;
^
symbol: variable in
location: class InvoiceTest
InvoiceTest.java:33: error: cannot find symbol
Wood.setDescription = my;
^
symbol: variable setDescription
location: variable Wood of type Invoice
InvoiceTest.java:33: error: cannot find symbol
Wood.setDescription = my;
^
symbol: variable my
location: class InvoiceTest
InvoiceTest.java:34: error: cannot find symbol
Bulb.setDescription = pants;
^
symbol: variable setDescription
location: variable Bulb of type Invoice
InvoiceTest.java:34: error: cannot find symbol
Bulb.setDescription = pants;
^
symbol: variable pants
location: class InvoiceTest
InvoiceTest.java:36: error: Price has private access in Invoice
if (Drill.Price < 0)
^
InvoiceTest.java:37: error: cannot find symbol
Drill.setPrice = 0;
^
symbol: variable setPrice
location: variable Drill of type Invoice
InvoiceTest.java:39: error: Price has private access in Invoice
if (Hose.Price < 0)
^
InvoiceTest.java:40: error: cannot find symbol
Hose.setPrice = 0;
^
symbol: variable setPrice
location: variable Hose of type Invoice
InvoiceTest.java:42: error: Price has private access in Invoice
if (Wood.Price < 0)
^
InvoiceTest.java:43: error: cannot find symbol
Wood.setPrice = 0;
^
symbol: variable setPrice
location: variable Wood of type Invoice
InvoiceTest.java:45: error: Price has private access in Invoice
if (Bulb.Price < 0)
^
InvoiceTest.java:46: error: cannot find symbol
Wood.setPrice = 0;
^
symbol: variable setPrice
location: variable Wood of type Invoice
InvoiceTest.java:50: error: cannot find symbol
Drill.getPrice, Hose.getPrice, Wood.getPrice, Bulb.getPrice);
^
symbol: variable getPrice
location: variable Drill of type Invoice
InvoiceTest.java:50: error: cannot find symbol
Drill.getPrice, Hose.getPrice, Wood.getPrice, Bulb.getPrice);
^
symbol: variable getPrice
location: variable Hose of type Invoice
InvoiceTest.java:50: error: cannot find symbol
Drill.getPrice, Hose.getPrice, Wood.getPrice, Bulb.getPrice);
^
symbol: variable getPrice
location: variable Wood of type Invoice
InvoiceTest.java:50: error: cannot find symbol
Drill.getPrice, Hose.getPrice, Wood.getPrice, Bulb.getPrice);
^
symbol: variable getPrice
location: variable Bulb of type Invoice
InvoiceTest.java:54: error: cannot find symbol
System.out.printf("Drill: %s", Drill.getDescription);
^
symbol: variable getDescription
location: variable Drill of type Invoice
InvoiceTest.java:56: error: cannot find symbol
System.out.printf("Hose: %s", Hose.getDescription);
^
symbol: variable getDescription
location: variable Hose of type Invoice
InvoiceTest.java:58: error: cannot find symbol
System.out.printf("Wood: %s", Wood.getDescription);
^
symbol: variable getDescription
location: variable Wood of type Invoice
InvoiceTest.java:60: error: cannot find symbol
System.out.printf("Bulb: %s", Bulb.getDescription);
^
symbol: variable getDescription
location: variable Bulb of type Invoice
InvoiceTest.java:65: error: cannot find symbol
Drill.setAmount = input.nextInt();
^
symbol: variable setAmount
location: variable Drill of type Invoice
InvoiceTest.java:69: error: cannot find symbol
Hose.setAmount = input.nextInt();
^
symbol: variable setAmount
location: variable Hose of type Invoice
InvoiceTest.java:73: error: cannot find symbol
Wood.setAmount = input.nextInt();
^
symbol: variable setAmount
location: variable Wood of type Invoice
InvoiceTest.java:77: error: cannot find symbol
Bulb.setAmount = input.nextInt();
^
symbol: variable setAmount
location: variable Bulb of type Invoice
InvoiceTest.java:81: error: cannot find symbol
System.out.print("%s Drill %.2f $%.2f", Drill.getNumber,
^
symbol: variable getNumber
location: variable Drill of type Invoice
InvoiceTest.java:82: error: cannot find symbol
Drill.getAmount, Drill.getInvoiceAmount);
^
symbol: variable getAmount
location: variable Drill of type Invoice
InvoiceTest.java:82: error: cannot find symbol
Drill.getAmount, Drill.getInvoiceAmount);
^
symbol: variable getInvoiceAmount
location: variable Drill of type Invoice
InvoiceTest.java:85: error: cannot find symbol
System.out.print("%s Hose %.2f $%.2f", Hose.getNumber,
^
symbol: variable getNumber
location: variable Hose of type Invoice
InvoiceTest.java:86: error: cannot find symbol
Hose.getAmount, Hose.getInvoiceAmount);
^
symbol: variable getAmount
location: variable Hose of type Invoice
InvoiceTest.java:86: error: cannot find symbol
Hose.getAmount, Hose.getInvoiceAmount);
^
symbol: variable getInvoiceAmount
location: variable Hose of type Invoice
InvoiceTest.java:89: error: cannot find symbol
System.out.print("%s Wood %.2f $%.2f", Wood.getNumber,
^
symbol: variable getNumber
location: variable Wood of type Invoice
InvoiceTest.java:90: error: cannot find symbol
Wood.getAmount, Wood.getInvoiceAmount);
^
symbol: variable getAmount
location: variable Wood of type Invoice
InvoiceTest.java:90: error: cannot find symbol
Wood.getAmount, Wood.getInvoiceAmount);
^
symbol: variable getInvoiceAmount
location: variable Wood of type Invoice
InvoiceTest.java:93: error: cannot find symbol
System.out.print("%s Bulb %.2f $%.2f", Bulb.getNumber,
^
symbol: variable getNumber
location: variable Bulb of type Invoice
InvoiceTest.java:94: error: cannot find symbol
Bulb.getAmount, Bulb.getInvoiceAmount);
^
symbol: variable getAmount
location: variable Bulb of type Invoice
InvoiceTest.java:94: error: cannot find symbol
Bulb.getAmount, Bulb.getInvoiceAmount);
^
symbol: variable getInvoiceAmount
location: variable Bulb of type Invoice
InvoiceTest.java:97: error: cannot find symbol
System.out.print("Grand Total: $%.2f", Sum);
^
symbol: variable Sum
location: class InvoiceTest
InvoiceTest.java:100: error: cannot find symbol
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bu
lb.InvoiceAmount;
^
symbol: variable Sum
location: class InvoiceTest
InvoiceTest.java:100: error: InvoiceAmount has private access in Invoice
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bu
lb.InvoiceAmount;
^
InvoiceTest.java:100: error: InvoiceAmount has private access in Invoice
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bu
lb.InvoiceAmount;
^
InvoiceTest.java:100: error: InvoiceAmount has private access in Invoice
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bu
lb.InvoiceAmount;
^
InvoiceTest.java:100: error: InvoiceAmount has private access in Invoice
Sum = Drill.InvoiceAmount + Hose.InvoiceAmount + Wood.InvoiceAmount + Bu
lb.InvoiceAmount;
^
58 errors
Sorry, I know this is probably trivial for this kind of forum. But I really have no one else to turn to for help. ANY help would be appreciated. Thank you very much! Have a nice day.
-
January 24th, 2012, 09:29 PM
#2
Re: Stupid beginner Mistake, Can you help?
I defined (Sum) I just realized I hadn't (as a double). Still pretty much the same problems.
-
January 25th, 2012, 02:38 AM
#3
Re: Stupid beginner Mistake, Can you help?
Check up on how to call methods, you are using assing operation as if directly setting variable values.
SomeClass someInstance=new SomeClass(some pararms, or none);
someInstance.someVariable=newValue; // assigning variable value
someInstance.someSetMethod(newValue) //calling a method.
Consult Sun´s java tutorials. There are also a number of great online books available, such as "Tninking in Java".
-
January 26th, 2012, 07:40 AM
#4
Re: Stupid beginner Mistake, Can you help?
Oh man.. this syntax belongs to no language...
Here is my suggestion. Buy yourself a book, there would be example programs to start with, write those programs (if you're not interested in studying the subjective) compile them and test them. But don't copy paste those programs..
we use setPrice(8.32) to call set price function not setPrice = 8.32; get it??? You need a book!!! go to a library....
-
January 28th, 2012, 07:41 AM
#5
Re: Stupid beginner Mistake, Can you help?
we use setPrice(8.32) to call set price function not setPrice = 8.32; get it??? You need a book!!! go to a library....
Hey, give the OP a break, he/she is asking their first question here apart from which Londbrok has already given the OP this advice but in a much more sympathetic way.
If the OP doesn't take the advice and repeatedly posts similar questions then by all means take a more direct approach but they deserve a chance before being ridiculed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|