• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Based on UML class diagram

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see my code

it is based on this UML

LabExer2  
___________________________________________
- itemName: String  
- itemPrice: double  
- itemQuantity: int  
- amountDue: double  
___________________________________________
+ stetItemName(String newItemName): void  
+ setTotalCost(int quantity, double price): void  
+ getItemName(): String  
+ getTotalCost(): double  
+ readInput(): void  
+ writeOutput(): void  
___________________________________________



The errors were

/LabExer2.java:12: error: not a statement
 this.quantity, price = quantity, price; }
     ^
/LabExer2.java:12: error: ';' expected
 this.quantity, price = quantity, price; }
              ^
/LabExer2.java:12: error: ';' expected
 this.quantity, price = quantity, price; }
                                ^
/LabExer2.java:12: error: not a statement
 this.quantity, price = quantity, price; }
                                  ^
/LabExer2.java:17: error: ';' expected
 return quantity, price; }
                ^
/LabExer2.java:17: error: not a statement
 return quantity, price; }
                  ^
/LabExer2.java:20: error: '{' expected
public class main() {
                ^
/LabExer2.java:26: error: <identifier> expected
System.out.println("Enter the name of the item you are purchasing.");
                 ^
/LabExer2.java:26: error: illegal start of type
System.out.println("Enter the name of the item you are purchasing.");
                  ^
/LabExer2.java:29: error: <identifier> expected
System.out.println("Enter the quantity and price separated by a space.");
                 ^
/LabExer2.java:29: error: illegal start of type
System.out.println("Enter the quantity and price separated by a space.");
                  ^
/LabExer2.java:33: error: <identifier> expected
public class void writeOutput() {
 
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A comma is not an operator in Java.  You'll need to make to separate assignment statements out of this, one for quantity and one for price.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but that was like that in the given UML diagram
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing that beginners consistently don't get about writing code is how critically important it is to properly format your code. I ran your code through Eclipse to format it and if you study it carefully you'll start to see quite a few errors. Also proper formatting will likely get your post more attention because it's easier to read.
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have so many errors that I might suggest starting over. When you do, only write 3-5 lines of code and get it to compile. Only after it compiles add another 3-5 lines of code and get that to compile. Repeat.
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zylith Zen wrote:but that was like that in the given UML diagram

The only thing the UML show is what arguments need to be passed to each method. It tells you nothing about how the method makes use of those arguments.
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zylith Zen wrote:
LabExer2  
___________________________________________
- itemName: String  
- itemPrice: double  
- itemQuantity: int  
- amountDue: double  
___________________________________________
+ stetItemName(String newItemName): void  
+ setTotalCost(int quantity, double price): void  
+ getItemName(): String  
+ getTotalCost(): double  
+ readInput(): void  
+ writeOutput(): void  
___________________________________________

I think that "LabExer2" is the name of your assignment but your class name, based on the wording of its members, should probably be "Item".

Note that ALL the methods called for in this UML must be part of your Item class and not part of any Main class as you were attempting.

You can have more than one class in a .java file but only one can be declared public, in your case Main which contains the main() method should be declared public and Item should not.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i changed it to this

 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First question: DOES IT COMPILE?

A method can only return one value.

Your closing brace must be on a line of its own lined up with the first letter of the method name.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im sorry im not english so what do you mean compile?
this is what came  out when i run it

/main.java:28: error: <identifier> expected
System.out.println ("Enter the name of the item you are purchasing.");
                 ^
/main.java:28: error: illegal start of type
System.out.println ("Enter the name of the item you are purchasing.");
                   ^
/main.java:31: error: <identifier> expected
System.out.println("Enter the quantity and price separated by a space.");
                 ^
/main.java:31: error: illegal start of type
System.out.println("Enter the quantity and price separated by a space.");
                  ^
/main.java:35: error: <identifier> expected
public class void writeOutput() {
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two accepted brace styles:
K&R (Kernigan & Ritchie)
Alman
Indentation should consist of multiples of 4 spaces.

Personally I think that Alman makes it easier for beginners to pick out errors in enclosing blocks of code.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

Personally I think that Alman makes it easier for beginners to pick out errors in enclosing blocks of code.



Oh yeah it does. thanks
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zylith Zen wrote:im sorry im not english so what do you mean compile?
this is what came  out when i run it


Are you using an IDE like Eclipse?

If you are compiling using a command line it looks something like this:
javac Main.java

this builds a file called Main.class which is executed by:
java Main

If you are using an IDE most likely the compile step is done automatically for you.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i use notepad and an Online Java Compiler IDE
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiler errors, like these, should also be enclosed in Code Tags when posting here so that the caret (^) points to the correct thing.

Here, I find nothing wrong with those lines so I'm guessing  that the line or lines that came before it were incomplete and now the compiler is confused.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also one of my problems are where to put readInput() and writeOutput

it was in the instructions that readInput() method will be used to accept user input through the Scanner class

and the writeOutput() should display the an output like this

You are purchasing 3 bag(s) at 1,475.5 each
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the UML indicates, readInput() and writeOutput() are methods inside your Item class.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im sorry but how. and are my inputs under those classes correct? im really sorry its like 3 am here i might pass out later.

btw this is what i got so far



and the output should be something like this

Enter the name of the item you are purchasing.
bag
Enter the quantity and price separated by a space.
3 1475.50
You are purchasing 3 bag(s) at 1475.50 each.
Amount due is 4426.50
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the closing brace for readInput() too soon. You need to include lines 40-45 in the method. Then the ENTIRE method should be put in Item after line 28.

You also need to move the entire writeOutput() method inside Item following the readInput() method.

Line 27: you can't return two values. What does the UML say you should return?

3 a.m.? You need some sleep. I'm in the Mountain Standard Time zone (Denver, Colorado) I'll be around when I'm not sleeping.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Like this?

 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just before writeOutput() you have an open brace. That should be moved two lines down.
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
readInput() and writeOutput() are METHODS not CLASSES. Just like the other methods in your Item class.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it still says

/main.java:29: error: <identifier> expected
   class void readInput()
        ^
/main.java:42: error: <identifier> expected
   class void writeOutput()
        ^
/main.java:42: error: ';' expected
   class void writeOutput()
                           ^
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically, main() should be an empty method at this point. The UML doesn't tell us what to put in main. Do you have other instructions?
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Zylith Zen wrote:it still says

/main.java:29: error: <identifier> expected
   class void readInput()
        ^
/main.java:42: error: <identifier> expected
   class void writeOutput()
        ^
/main.java:42: error: ';' expected
   class void writeOutput()
                           ^

As I said - NO CLASS
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the whole instructions

Note: The readinput() method will be used to accept user input through the Scanner class. This is
done by:
a. Writing import java.util.*; on top of the code, before the line for the class name
b. Instantiating an object of the Scanner class, Scanner s = new Scanner (System.in);
C. Storing the input to the variable name based on data type
For String: s.nextLine()
For int: s.nextInt()
For double: s.nextDouble()
The writeOutput() method will be used to display an output similar to the sample below.

You are purchasing 3 bag(s) at 1475.5 each.


Sample output:

Enter the name of the item you are purchasing.
bag
Enter the quantity and price separated by a space.
3 1475.50
You are purchasing 3 bag(s) at 1475.5 each.
Amount due is 4426.50
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that doesn't really indicate what goes into main(). Because read and write are both Item methods it follows that main() would first need to make an Item and then use that Item to call read the write.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


and these are the errors

/main.java:12: error: cannot find symbol
     this.newitemName = newItemName;
         ^
 symbol: variable newitemName
/main.java:26: error: cannot find symbol
     return quantity;
            ^
 symbol:   variable quantity
 location: class Item
/main.java:27: error: cannot find symbol
     return price;
            ^
 symbol:   variable price
 location: class Item
/main.java:39: error: cannot find symbol
       System.out.println("Amount due is " + TotalCost);
                                             ^
 symbol:   variable TotalCost
 location: class Item
/main.java:43: error: cannot find symbol
     System.out.println("You are purchasing " + quantity + " " + newItemName + "(s) at " + price + " each.");
                                                ^
 symbol:   variable quantity
 location: class Item
/main.java:43: error: cannot find symbol
     System.out.println("You are purchasing " + quantity + " " + newItemName + "(s) at " + price + " each.");
                                                                 ^
 symbol:   variable newItemName
 location: class Item
/main.java:43: error: cannot find symbol
     System.out.println("You are purchasing " + quantity + " " + newItemName + "(s) at " + price + " each.");
                                                                                           ^
 symbol:   variable price
 location: class Item
/main.java:44: error: cannot find symbol
       double TotalCost = quantity*price;
                          ^
 symbol:   variable quantity
 location: class Item
/main.java:44: error: cannot find symbol
       double TotalCost = quantity*price;
                                   ^
 symbol:   variable price
 location: class Item


Well. im gonna fix this later. after some sleep.
thanks for your help.
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, making progress.
Line 27 - can't return two values.
You have used wrong variable names as pointed out by several of the error messages.

I'm off to get some lunch.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. Your  method name says it will return the total cost, so given the various fields in the Item class can you come up with a way to  compute the total cost and return that value?
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing you're going to bump into but haven't yet because it's not a compilation error: readInput() is now, correctly, a method of the Item class. As such it has direct access to the various member variables (fields). Here you are creating a new LOCAL variable "newItemName" and populating it with user input, but this won't update the value of the current object being processed which is the variable named "item" inside the main() method. So, what you'll need to do is not create a whole new variable but use the one in our current object. (Etc. for all your inputs in this method.)
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very good. On to my next comment.
 
Zylith Zen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why was the output null and 0
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See my other comment about readInput().

--got to step away for awhile
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 8593
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I may have steered you slightly off  course, I believe this belongs in writeOutput().
 
reply
    Bookmark Topic Watch Topic
  • New Topic