Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling with java  RSS feed

 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a fairly new java student. Near the end of last semester we kinda jumped into UML, constructors and making classes in separate files and all of that pretty quick and I was already struggling with it. My professor doesn't have time to help students after class because he has to meet with 1st and 2nd semester freshmen to ensure they're on track for graduation with their classes and stuff, which eats up all of his office hours and he's the only java instructor. It's a community college, so it is what it is. Over the break I was going to try to go through my book and try to get more practice, but ended up in a pretty bad car accident and now I feel like I'm starting out behind the 8 ball. I seem to have forgotten some of the stuff we did last semester, and have been going back through my book (Tony Gaddis - Starting Out With Java 6e) trying to remember some of this, and am now snowed in with an assignment to work on. I've got some of it done, but am stuck. This is what I'm supposed to make:

Instructions:
Implement the following class:
-----------------------------------
Inventory
-----------------------------------
- quantity : int
- width : double
- cost : double
- name : String
-----------------------------------
+ Inventory()
+ Inventory (q : int, w : double, c : double, n : String)
+ getTotalCost() : double
+ display() : void
-----------------------------------
Only create the fields listed above.
Create sets and gets for each field.
All constructors should perform their traditional role. Only use set methods within the constructor.
getTotalCost returns the product of the quantity multiplied by the cost.
display should display all the fields of the object as well as the result of getTotalCost. Only use get methods within display.
Use all methods at least once in either the Inventory or InventoryDemo
Create a InventoryDemo class which uses the Inventory class. (Each class is in its own file.)
Create main.
Create an inventory item i1 using the no argument constructor.
Use display to display the results.
Create an inventory item i2 using values of: quantity =20, width=2, cost=3.25 and name = Regular
Use display to display the results.
Create a loop that decrements the quantity of i2 by 1, starting at 10 and ending at 1 (includes 10 & 1).
Perform the following steps within the loop for each iteration:
If the totalcost is greater than 20, display the special message “Reduce inventory”.
If the totalcost is 20 or less, do not display a special message.
Use display to display the result of every iteration.
Create an inventory item i3 which is an array containing 3 items.
Use the no argument constructor to load information into each item of i3.
Place display inside of a loop to display the results of array i3.



This is what I have so far:

Inventory.java



InventoryDemo.java



I'm having a hard time remembering how to do with with two files. I thought that when I used command prompt (it's pointing at the correct location for the files) I was supposed to do javac InventoryDemo.java and it would compile both InventoryDemo and Inventory, but instead all I got was a list of 46 errors and I don't understand what the problem is. I wanted to go ahead and try to compile this much before continuing on.

The other problem I'm having, is that I'm not sure how to tackle the last part of the assignment:
If the totalcost is greater than 20, display the special message “Reduce inventory”.
If the totalcost is 20 or less, do not display a special message.
Use display to display the result of every iteration.
Create an inventory item i3 which is an array containing 3 items.
Use the no argument constructor to load information into each item of i3.
Place display inside of a loop to display the results of array i3.



This is the list of errors if it would help:
C:\Users\######\Documents\Java Programs>javac InventoryDemo.java
.\Inventory.java:39: error: <identifier> expected
public Inventory {
^
.\Inventory.java:40: error: invalid method declaration; return type required
setQuantity(0);
^
.\Inventory.java:40: error: illegal start of type
setQuantity(0);
^
.\Inventory.java:41: error: invalid method declaration; return type required
setWidth(0.0);
^
.\Inventory.java:41: error: illegal start of type
setWidth(0.0);
^
.\Inventory.java:42: error: invalid method declaration; return type required
setCost(0.0);
^
.\Inventory.java:42: error: illegal start of type
setCost(0.0);
^
.\Inventory.java:43: error: invalid method declaration; return type required
setName("");
^
.\Inventory.java:43: error: illegal start of type
setName("");
^
.\Inventory.java:47: error: class, interface, or enum expected
public Inventory(int q, double w, double c, String n) {
^
.\Inventory.java:49: error: class, interface, or enum expected
setWidth(w);
^
.\Inventory.java:50: error: class, interface, or enum expected
setCost(c);
^
.\Inventory.java:51: error: class, interface, or enum expected
setName(n);
^
.\Inventory.java:52: error: class, interface, or enum expected
}
^
.\Inventory.java:58: error: class, interface, or enum expected
public double getTotalCost(){
^
.\Inventory.java:60: error: class, interface, or enum expected
return tot;
^
.\Inventory.java:61: error: class, interface, or enum expected
}
^
.\Inventory.java:64: error: class, interface, or enum expected
public void display(){
^
.\Inventory.java:65: error: illegal character: '\'
String show = "Quantity: " + getQuantity() + \n +
^
.\Inventory.java:66: error: illegal character: '\'
"Width: " + getWidth() + \n +
^
.\Inventory.java:67: error: illegal character: '\'
"Cost: " + getCost() + \n +
^
.\Inventory.java:69: error: class, interface, or enum expected
return show;
^
.\Inventory.java:70: error: class, interface, or enum expected
}
^
.\Inventory.java:76: error: class, interface, or enum expected
public void setQuantity(int q1) {
^
.\Inventory.java:78: error: class, interface, or enum expected
}
^
.\Inventory.java:80: error: class, interface, or enum expected
public int getQuantity() {
^
.\Inventory.java:82: error: class, interface, or enum expected
}
^
.\Inventory.java:86: error: class, interface, or enum expected
public void setWidth(double w1) {
^
.\Inventory.java:88: error: class, interface, or enum expected
}
^
.\Inventory.java:90: error: class, interface, or enum expected
public double getWidth() {
^
.\Inventory.java:92: error: class, interface, or enum expected
}
^
.\Inventory.java:96: error: class, interface, or enum expected
public void setCost(double c1) {
^
.\Inventory.java:98: error: class, interface, or enum expected
}
^
.\Inventory.java:100: error: class, interface, or enum expected
public double getCost() {
^
.\Inventory.java:102: error: class, interface, or enum expected
}
^
.\Inventory.java:106: error: class, interface, or enum expected
public void setName(String n1) {
^
.\Inventory.java:108: error: class, interface, or enum expected
}
^
.\Inventory.java:110: error: class, interface, or enum expected
public String getName() {
^
.\Inventory.java:112: error: class, interface, or enum expected
}
^
InventoryDemo.java:7: error: no suitable constructor found for Inventory(no arguments)
Inventory i1 = new Inventory();
^
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
InventoryDemo.java:8: error: cannot find symbol
i1.display();
^
symbol: method display()
location: variable i1 of type Inventory
InventoryDemo.java:9: error: no suitable constructor found for Inventory(int,double,double,String)
Inventory i2 = new Inventory(20, 2.0, 3.25, "Regular");
^
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
constructor Inventory.Inventory(<any>) is not applicable
(actual and formal argument lists differ in length)
InventoryDemo.java:10: error: cannot find symbol
i2.display();
^
symbol: method display()
location: variable i2 of type Inventory
InventoryDemo.java:13: error: cannot find symbol
i2.setQuantity(i);
^
symbol: method setQuantity(int)
location: variable i2 of type Inventory
InventoryDemo.java:14: error: cannot find symbol
if (i2.getTotalCost() <= 20.0)
^
symbol: method getTotalCost()
location: variable i2 of type Inventory
InventoryDemo.java:16: error: cannot find symbol
System.out.println(i2.display());
^
symbol: method display()
location: variable i2 of type Inventory
46 errors

C:\Users\######\Documents\Java Programs>


Line numbers for the errors may be incorrect because I removed the comment documentation header I had to put at the top because it includes my name and personal info.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two hints.

One. When you have a large list of errors -- only work on the first few. Syntax errors can confuse the compiler, and sometimes, it generates a long list of false positives (along with missing some errors).

Two. Regarding the first error. How do you declare a constructor? Did you happen to forget something?

Henry
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH! It needs parenthesis, even though it's no-arg. I was confused with that error message because it said it was an identifier issue, so I wasn't sure if I didn't capitalize something or what. So that brought it down a lot. Also fixed the multi line thing.

C:\Users\######\Documents\Java Programs>javac InventoryDemo.java
InventoryDemo.java:16: error: 'void' type not allowed here
System.out.println(i2.display());
^
.\Inventory.java:59: error: cannot find symbol
int tot = getQuantity * getCost;
^
symbol: variable getQuantity
location: class Inventory
.\Inventory.java:59: error: cannot find symbol
int tot = getQuantity * getCost;
^
symbol: variable getCost
location: class Inventory
.\Inventory.java:69: error: incompatible types: unexpected return value
return show;
^
4 errors

C:\Users\######\Documents\Java Programs>



So what are those errors about? I feel like there needs to be a whole lesson or something just on learning to read java errors. Some of them are hard to understand what it's talking about.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicole Anon wrote:
C:\Users\######\Documents\Java Programs>javac InventoryDemo.java
InventoryDemo.java:16: error: 'void' type not allowed here
System.out.println(i2.display());
^
So what are those errors about?


Well, instead of looking at it from an error perspective, can you tell us what is that line supposed to do?

Henry
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, instead of looking at it from an error perspective, can you tell us what is that line supposed to do?


It's supposed to call the display method in inventory.java and print out for each iteration of the loop.


 
Paul Clapham
Sheriff
Posts: 22480
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


But this line doesn't make any sense in a method which is declared to return void (i.e. to not return anything).
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the tips and such. I've finally gotten rid of errors and at least have output lol. I'm just stuck on the last part:

Create an inventory item i3 which is an array containing 3 items.
Use the no argument constructor to load information into each item of i3.
Place display inside of a loop to display the results of array i3.



I've got that much sorted, but how would I use the constructor to load info into each item?
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array you've created can hold three items at indices 0 through 2 (inclusive):



All you have left to do is assign to each of those "buckets" an Inventory object created by calling the no argument constructor:



You could make this cleaner by using a loop instead.

There's also a "shortcut" for initializing arrays where they are declared. This is handy when you know exactly what (and how many things) will be in the array:

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Smith wrote:Thank you all for the tips and such.

Well here's one more: Don't write so much code that you get a pile of errors when you compile.

My usual rule of thumb is to compile:
  • every time I declare a class.
  • every time I add a field.
  • every time I add a method (or constructor).
  • every 10 lines I write inside a method.
  • It may sound like a pain, but believe me, it saves a lot of frustration of the type you just went through.

    The compiler is NOT forgiving, so the more you can do to keep your code clean, the easier things will be.

    HIH

    Winston
     
    Campbell Ritchie
    Marshal
    Posts: 55687
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:. . . The compiler is NOT forgiving, . . .
    But the compiler tries to help you. It tries to give you errors which will guide you to the right solution and it tries to find things before they cause your program to crash at runtime. So learn about error messages and they will help you with your programs.

    Note unwarranted anthropomorphising of the compiler
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12542
    48
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:My usual rule of thumb is to compile:
  • every time I declare a class.
  • every time I add a field.
  • every time I add a method (or constructor).
  • every 10 lines I write inside a method.


  • Winston

    The only thing I'd say different is I compile every 2-3 lines inside a method. But I think you've been programming longer than me, so you're probably better/more confident.

    And this was one of the hardest lesson to learn, yet one of the most beneficial ones to me.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!