7 Replies - 316 Views - Last Post: 09 October 2011 - 02:37 PM Rate Topic: -----

#1 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

errors in my main class

Posted 09 October 2011 - 03:33 AM


public class CameraInventory2
{

    
    // The main entry
    public static void main(String[] args)
    {
        
        // create the array
        Camera[] camera = new Camera[3];
        cameras[0] = new Camera(1, "Nokia", 20, 30.0);
        cameras[1] = new Camera(2, "Polaroid", 10, 21.0);
        cameras[2] = new Camera(3, "Canon", 15, 32.0);
        
        Inventory2 invent = new Inventory2("cameras");
        
        // sort the inventory
        invent.sortProd();
        
        for (int i = 0; i < invent.getSize(); i++)
        {
            // get the product
            Camera prod = invent.get(i);
            
            // display the product
            System.out.println("Item Number: " + prod.getItemNumber());
            System.out.println("Product Name: " + prod.getName());
            System.out.println("Number Of Units: " + prod.getUnits());
            System.out.printf("Price Per Unit: $%,.2f\n", prod.getPrice());
            System.out.printf("The value of the inventory: $%,.2f\n\n", prod.inventoryValue());
        }
        
        // display the total value
        System.out.printf("The total value: $%,.2f\n", invent.totalValue());
       

    }

}


CameraInventory2.java:13: error: cannot find symbol
cameras[0] = new Camera(1, "Nokia", 20, 30.0);
^
symbol: variable cameras
location: class CameraInventory2
CameraInventory2.java:14: error: cannot find symbol
cameras[1] = new Camera(2, "Polaroid", 10, 21.0);
^
symbol: variable cameras
location: class CameraInventory2
CameraInventory2.java:15: error: cannot find symbol
cameras[2] = new Camera(3, "Canon", 15, 32.0);
^
symbol: variable cameras
location: class CameraInventory2
CameraInventory2.java:17: error: constructor Inventory2 in class Inventory2 cann
ot be applied to given types;
Inventory2 invent = new Inventory2("cameras");
^
required: Camera[]
found: String
reason: actual argument String cannot be converted to Camera[] by method invoc
ation conversion
4 errors

I cannot seem to resolve these errors, and would be grateful for a deeper understanding as to why.

Is This A Good Question/Topic? 0
  • +

Replies To: errors in my main class

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5230
  • View blog
  • Posts: 27,016
  • Joined: 10-May 07

Re: errors in my main class

Posted 09 October 2011 - 03:40 AM

** Topic moved to Java **

Please don't post questions under the tutorials section. That the designated area for posting tutorials.

If you have any questions regarding where to post questions or topics, feel free to shoot me a private message, thank you.
Was This Post Helpful? 0
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: errors in my main class

Posted 09 October 2011 - 03:50 AM

Look at your declaration: Camera[] camera = new Camera[3]; you call your variable cameara, they you access it as cameras.
The last two errors are because your constructor of Camera class has not match on here and we need to see the class to solve it. But you can solve it your self, look at the definition of your constructor, does it match with how you call it here?
Was This Post Helpful? 1
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: errors in my main class

Posted 09 October 2011 - 04:05 AM

Why? Understanding of and attention to the details.

I believe you've taken your code from a previous assignment and renamed certain parts of it - like the class name for starters - but you haven't made corresponding changes to the rest of the code. Since you don't understand the impacts of your changes, the reuse approach is causing you to begin with a confusing mess rather than a shortcut to a solution to your next assignment.

How to recover? The error messages are quite helpful if you read them and think about what they're telling you:

There are 4 errors. The first 3 are all related to the variable 'cameras' being undefined. What is it? The 4th error is telling you that your attempt to create an instance of Inventory2 has failed, because you didn't comply with the constructor's data requirements.

More errors will result, but keep plugging the holes until you get to runnable code.
Was This Post Helpful? 1
  • +
  • -

#5 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: errors in my main class

Posted 09 October 2011 - 01:12 PM

import java.util.Arrays;
import java.util.Comparator;

public class CameraInventory2
{

    
    // The main entry
    public static void main(String[] args)
    {
        
        // create the array
        Camera[] camera = new Camera[3];
        camera[0] = new Camera(1, "Nokia", 20, 30.0);
        camera[1] = new Camera(2, "Polaroid", 10, 21.0);
        camera[2] = new Camera(3, "Canon", 15, 32.0);
        
        Inventory invent = new Inventory(Camera[] camera);
        
        // sort the inventory
        invent.sortProd();
        
        for (int i = 0; i < invent.getSize(); i++)
        {
            // get the product
            Camera prod = invent.get(i);
            
            // display the product
            System.out.println("Item Number: " + prod.getItemNumber());
            System.out.println("Product Name: " + prod.getName());
            System.out.println("Number Of Units: " + prod.getUnits());
            System.out.printf("Price Per Unit: $%,.2f\n", prod.getPrice());
            System.out.printf("The value of the inventory: $%,.2f\n\n", prod.inventoryValue());
        }
        
        // display the total value
        System.out.printf("The total value: $%,.2f\n", invent.totalValue());
       

    }

}


class Camera
{
    // Declare instance variables  
    private int itemNumber; 
    private String name; 
    private int units; 
    private double price;

    // Default constructor
    public Camera()
    {
        itemNumber = 0;
        name = "";
        units = 0;
        price = 0;        
    }
    
    // Parameters constructor
    public Camera(int itemNumber, String name, int units, double price)
    {
        this.itemNumber = itemNumber;
        this.name = name;
        this.units = units;
        this.price = price;
    }
    
    // calculate inventory value
    public double inventoryValue()
    {
        return units * price;
    }

    // accessors and modifiers
    public int getItemNumber()
    {
        return itemNumber;
    }
    
    public void setItemNumber(int itemNumber)
    {
        this.itemNumber = itemNumber;
    }
    
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
    
    public int getUnits()
    {
        return units;
    }
    
    public void setUnits(int units)
    {
        this.units = units;
    }
    
    public double getPrice()
    {
        return price;
    }
    
    public void setPrice(double price)
    {
        this.price = price;
    }    
  
}


class Inventory
{
    private Camera[] camera;
    
    // Constructor
    public Inventory(Camera[] camera)
    {
        this.camera = camera;
    }
    
    // Calculate the value of the entire inventory
    public double totalValue()
    {
        double total = 0;       
        for (int i = 0; i < camera.length; i++)
            total += camera[i].inventoryValue();
        return total;
    }
    
    // Sort the array items by the name of the product
    public void sortProd()
    {
        Comparator<Camera> comp = new Comparator<Camera>() {
            public int compare(Camera o1, Camera o2) {
                return o1.getName().compareTo(o2.getName());
            }            
        };
        
        Arrays.sort(camera, comp);
    }
    
    // Get the size of the inventory
    public int getSize()
    {
        return camera.length;
    }
    
    // Get product of the inventory
    public Camera get(int idx)
    {
        return camera[idx];
    }
}



C:\Users\Nathan\Documents\IT215 JAVAT PROGRAMMING\Week (6)\New folder>javac Came
raInventory2.java
CameraInventory2.java:18: error: '.class' expected
Inventory invent = new Inventory(Camera[] camera);
^
CameraInventory2.java:18: error: ';' expected
Inventory invent = new Inventory(Camera[] camera);
^
2 errors

here is the full amount of my updated code, and it's errors, but I'm having a hard time getting a grasp on why these are popping up.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: errors in my main class

Posted 09 October 2011 - 01:14 PM

You need to pass the variable camera only. You don't need to pass Camera[] camera, only camera to the constructor.
Was This Post Helpful? 1
  • +
  • -

#7 b.netana  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 92
  • Joined: 11-September 11

Re: errors in my main class

Posted 09 October 2011 - 02:34 PM

Thank you macosxnerd101 that helped me finish off my project. I think my biggest problem is my own lack of experience, and the fact that I seem to be moving a bit too quickly from one concept to the next. I thank my lucky stars that I have this community to help guide me through.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10445
  • View blog
  • Posts: 38,682
  • Joined: 27-December 08

Re: errors in my main class

Posted 09 October 2011 - 02:37 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1