Question:
programming java basics picture question?
anonymous
2008-09-08 06:23:45 UTC
when compiling, im getting the error "cannot find symbol - constructor Picture(java.lang.String)"

public class Picture
{
public void show()
{
String fileName;
fileName = FileChooser.pickAFile();

Picture picture1;
picture1 = new Picture();
picture1.show();

Picture picture2;
picture2 = new Picture(fileName);
picture2.show();
}
}
Four answers:
Michael P
2008-09-08 06:43:28 UTC
This is a problem with your Picture class. You are creating 2 variables called picture1 and picture2. When you instantiate the second variable (picture2) you are passing a string into the constructor. The picture class you are showing us only has a show() public method and no constructor that takes a String.



To fix this you will need to do 1 of 2 things:

1. Remove the filename from the constructor when you instantiate the Picture class. Example: Picture picture2 = new Picture();



2. Add a constructor to the class that will accept a String but you will need to move your FileChooser step into this constructor and make the fileName string global within the class.



Hope this helps.
geonautika
2008-09-08 19:09:03 UTC
Basically your class does not have constructor(s). A constructor is a method that allocate memory and build the class instance.



The ways you called your class

1. picture1 = new Picture();

2. picture2 = new Picture(fileName);

require 2 constructors. However, Java compiler is able to generate "default constructor" is none is give. So for #1 -- Java generates it.



But the compiler does not know how to create constructor for #2 since it has to accept a parameter. Here you have to create this constructor yourself.
deonejuan
2008-09-08 06:51:33 UTC
The JVM is complaining because of the constructor. The class does not define a constructor in your code so the JVM makes a default constructor. Your code is like this to the Java Runtime:



public class Picture {



public Picture() {

// this is the default constructor

// either loads some vars

// or call a method

}



public Picture( String fname ) {

// here is your custom constructor

// and this is polymorphism at work

// you have two constructors, java

// looks for the constructor that will "fit"

String fileName = fname;

File file = new File( fileName );

}



// then some methods

private void method1() {

// do something

}



} // end of class



To make this a standalone, executable class, you need a main method. So, you have a few more things to consider for what you are trying to do.
?
2016-10-24 10:29:47 UTC
HTML is straightforward. in truth, that's not even technically programming. once you come back to getting to allure to close Python, C or C++, you'll comprehend this. HTML/CSS (you opt on CSS for formatting jointly with shade, top, etc.) would take some weeks to get to an intermediate aspect and comprehend all the tags, the benefits and downsides of particular tags, deprecated (out of date) tags and good HTML prepare. be conscious that you are able to also decide on to study JavaScript or ASP/Hypertext Preprocessor which will take a lot more desirable time. For Python, C or C++, it is going to takes weeks to get an particularly effortless comprehend-how, months in the previous you're everywhere close to proficient at it to make something first rate and likely years to change into totally adept the position you are able to make finished courses with out practise. for sure, this may certainly determination relying on how a lot dedication you've.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...