|
-
Feb 1st, 2005, 12:37 PM
#1
Thread Starter
Addicted Member
Need help bad!!
I am having problems with an assignment in my advanced java class.
Here it is. I am supposed to use something like an arraylist or linked list to make a dynamically changing program that holds the products for a video company. The problem is the fact that I have never used an arraylist or linked list so I dont know where to start.
Code:
You are part of a team of programmers that is developing a point of sale application for a local video store called SCC Pictures and Stuff. The application needs a class that holds a list of the products offered for sale. The list of products can vary in number. Create the ProductList class. Include a data structure that holds Product objects. Include the following methods:
add(Product p) – add a product to the list
add(String id, String name, double price) – create a Product and add to the list
size() - return how many are in the list
remove(int index) – remove a product at position index.
get(int index) – return the Product at position index.
find(String find) – return a Product based on the ID#.
Create a list of Product objects in the constructor of the ProductList class.
Download the Product class from the micros ftp site – it is in the assignments directory.
ProductList class includes:
Dynamic data structure as an attribute
Constructor adds Products to the list
add method s
size method
remove method
get Method
find method
Here is the product class were are supposed to use.
Code:
public class Product{
private String code;
private String name;
private double price;
public Product(){
}
public Product(String code, String name, double price)
{
this.code = code;
this.name = name;
this.price = price;
}
public void setCode(String c)
{
code = c;
}
public String getCode()
{
return code;
}
public void setName(String d)
{
description = d;
}
public String getName()
{
return name;
}
public void setPrice(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
}
<Moderator added green checkmark to the first post>
Last edited by NoteMe; Feb 17th, 2005 at 06:59 AM.
-
Feb 1st, 2005, 02:04 PM
#2
Thread Starter
Addicted Member
Re: Need help bad!!
OK, before anyone says, " WE WONT DO YOUR ASSIGNMENT!!!!" i dont expect you to. I just want some help with the code that i am writing. Here it is. Any help would be appreciated.
Code:
package assignment;
import java.util.*;
public class ProductList extends Product{
public ProductList() {
}
public Object add(Product p)
{//add a product to the list
products.add(p);//put product object in array list
}
public String add(String id, String name, double price)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod(i) = new Product(id,name,price);
products.add(prod(i));
}
}
public int size()
{//return how many are in the list
int size;
size = products.size();
return size;
}
public int get(int index)
{//return the Product at position index
products.get(index);
return index;
}
public int remove(int index)
{//remove the Product at position index
products.remove(index);
return index;
}
public String find(String find)
{//return a Product based on the ID#
return find;
}
public static void main(String[] args)
{
ArrayList products = new ArrayList();
Product prod1 = new Product("5248","trumpet" , 50);
Product prod2 = new Product("5420","Saxaphone" , 100);
for(int i=0; i <products.size(); i++)
{
Product p = (Product)products.get(i);
System.out.println(p.toString());
}
Iterator it = products.iterator();
while(it.hasNext())
{
Product p = (Product)it.next();
System.out.println(p.toString());
}
}
}
-
Feb 1st, 2005, 03:38 PM
#3
Re: Need help bad!!
Well, what exactly is your problem?
There's documentation for ArrayList. I'm also sure that if you search for that term on the forum, you can find some usage examples.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 1st, 2005, 04:06 PM
#4
Thread Starter
Addicted Member
Re: Need help bad!!
sorry, it didnt post the error part of that message Corned Bee
I get the message "Cannot resolve symbol class Product"
-
Feb 1st, 2005, 04:44 PM
#5
Frenzied Member
Re: Need help bad!!
Is the product class in the same directory as your other class? And did you compile it? I can't remember if you would have to compile it or not..
-
Feb 1st, 2005, 04:45 PM
#6
Frenzied Member
Re: Need help bad!!
Code:
public void setName(String d)
{
description = d;
}
You have this in the product class, but I don't see any variable named description.
-
Feb 1st, 2005, 05:03 PM
#7
Thread Starter
Addicted Member
Re: Need help bad!!
 Originally Posted by System_Error
Is the product class in the same directory as your other class? And did you compile it? I can't remember if you would have to compile it or not..
yeah, Im using a program called JCreator Pro and it automatically makes it into its own directory and makes a project of it.
I tried compiling and got the Cannot Resolve System Symbol Product error
-
Feb 1st, 2005, 10:40 PM
#8
Dazed Member
Re: Need help bad!!
I see a package directive at the top of the ProductList class but not the Product class.
-
Feb 2nd, 2005, 03:12 AM
#9
Re: Need help bad!!
Yeah, pretty sure it's a directory issue.
It should look like this:
Code:
source_root\
-> Product.java
-> assignment\
-> ProductList.java
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 3rd, 2005, 10:01 AM
#10
Thread Starter
Addicted Member
Re: Need help bad!!
Ok, i got around the "Cannot resolve symbol class Product" error by removing the package and just putting them in the same directory.
Now I am getting the error "non-static variable products cannot be referenced from a static context."
here is an update on my code. I have placed comments with the // near the bottom where the compiler has the problem.
Code:
/**
* Title: Assign 2
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/
import java.util.*;
public class ProductList extends Product{
ArrayList products = new ArrayList();
public ProductList() {
Product prod1 = new Product("5248","trumpet" , 50);
Product prod2 = new Product("5420","Saxaphone" , 100);
}
public void add(Product p)
{//add a product to the list
products.add(p);//put product object in array list
}
public void add(String id, String name, double price)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod1 = new Product(id,name,price);
products.add(prod1);
}
}
public int size()
{//return how many are in the list
return products.size();
}
public Object get(int index)
{//return the Product at position index
return products.get(index);
}
public void remove(int index)
{//remove the Product at position index
products.remove(index);
}
public Object find(String find)
{//return a Product based on the ID#
Iterator it = products.iterator();
while(it.hasNext())
{
Product q = (Product)it.next();//returns an object
if (q.getCode().equals(find))
{
return q;
}
}
return null;
}
public static void main(String[] args)
{
for(int i=0; i < products.size(); i++){//non-static variable products cannot be referenced from a static
//context
Product p = (Product)products.get(i);//non-static variable products cannot be referenced from a static
//context
System.out.println(p.toString());
}
Iterator it = products.iterator();//non-static variable products cannot be referenced from a static
//context
while(it.hasNext()){
Product p = (Product)it.next();
System.out.println(p.toString());
}
}
}
-
Feb 3rd, 2005, 10:21 AM
#11
Thread Starter
Addicted Member
Re: Need help bad!!
An update
I thought it might have something to do with my arraylist.
So i copied and pasted it from the top of my class down into the main method, now I dont get the error, but it also isnt printing anything because there is technically nothing in the arrayList since it doesnt do anything with the rest of the program. Any ideas?
-
Feb 3rd, 2005, 11:09 AM
#12
Re: Need help bad!!
You can't directly access the various variables from main(). You have to create an object of your class first.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 3rd, 2005, 11:27 AM
#13
Thread Starter
Addicted Member
Re: Need help bad!!
OK, i figured out what i was doing wrong. I didnt have an it() method. Now it works. Thanks
Code:
public Iterator it()
{
return products.iterator();
}
here is the finished code for both my methods.
first Product.java
Code:
/**
* Title: Assign 2
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/
public class Product{
private String code;
private String name;
private double price;
public Product(){
}
public Product(String code, String name, double price)
{
this.code = code;
this.name = name;
this.price = price;
}
public void setCode(String c)
{
code = c;
}
public String getCode()
{
return code;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void setPrice(double p)
{
price = p;
}
public double getPrice()
{
return price;
}
public String toString()
{
return code + "\t" + name + "\t" + price + "\t";
//return code + " " + name + " " + price;
}
}
and now my productList.java
Code:
/**
* Title: Assign 2
* @author Matthew Davis
* School SECC
* Class Advanced Java
*/
import java.util.*;
public class ProductList extends Product{
ArrayList products = new ArrayList();
public ProductList() {
}
public void add(Product p)
{//add a product to the list
products.add(p);
/*String key = ((Product)p).getCode();
if(find(key) == null)
{
products.add(p);
}*/
}
public void add(String id, String name, double price)
{//create a Product and add it to the list
for(int i = 0 ; i<products.size(); i++)
{
Product prod1 = new Product(id,name,price);
products.add(prod1);
}
}
public int size()
{//return how many are in the list
return products.size();
}
public Object get(int index)
{//return the Product at position index
return products.get(index);
}
public void remove(int index)
{//remove the Product at position index
products.remove(index);
}
public Object find(String find)
{//return a Product based on the ID#
Iterator it = products.iterator();
while(it.hasNext())
{
Product q = (Product)it.next();
if (q.getCode().equals(find))
{
return q;
}
}
return null;
}
public Iterator it()
{
return products.iterator();
}
public static void main(String[] args)
{
ProductList pr = new ProductList();
Product prod1 = new Product("5248","trumpet" , 50);
Product prod2 = new Product("5420","Saxaphone" , 100);
pr.add(prod1);
pr.add(prod2);
Iterator it = pr.it();
while (it.hasNext())
{
Product p = (Product)it.next();
System.out.println(p.toString());
}
}
}
Last edited by ddmeightball; Feb 3rd, 2005 at 11:55 AM.
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
|