I need help with a java program im writing I have a class called Record which im trying to use in another class, I dont know if I'm doing something wrong but it seems that Vscode is using a standrd Record class (Java.lang.record). I dont know how to change it sothat it uses the custom class I made, I cannot change the name of the class and both files are in the same folder.
#Need help with classes in Java very basic
143 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fresh bone! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
You need delete import Java.lang.record and reimport you Record class.
i have not imported anything in this particular file
how do i delete an import that was never there
and i thought if the files are in the same folder you dont need to import them?
You can use your own Record class by put package path in font of Record like : com.you.package.path.Record record = new com.you.package.path.Record();
are u in eecs 2030
idk what that is?
is there any way without hardcoding a path into everyline it has to work on other computers
Put an import at the top of the file
import your.package.Record;
can you share the files
do they have package declarations at the top?
and how are you running them
record.java, stores a key from class key
in binarysearchtree to get a node with a given key i need to pull the record from the node and the get the key
but it says getKey() not defined for Record
and when i scroll over Record witgh my cursor it says Java.util.record
all files are in the same folder so it should use Record.java no?
Maybe you import java.lang.record in BSTDictionary.java ? So when you get record from dictionary that is java.lang.record not your defined Record class.
I can check if you upload your BSTDictionary.java
emm You should implement BSTDictionaryADT first, and be sure that get method return Record is you defined.
"I cannot change the name" is just not gonna cut it.
There would be a solution if you were using packages. But as you don't, you don't have any way to counter the fact that java.lang classes are auto-imported
Yes you should avoid to use the word that was defined in jdk as system key words.
Record with a capital R is just the supertype of all records. There would be much more compilation failures trying to use a keyword like a class name
unfortunately I dont get to choose its an assignment for a class and we have to follow the instructions
I could lose marks for messing with the name as they probably have test cases which they use to run everybodys code with the the same name files
can i ask what difference that would make?
wouldnt it still return the wrong object Record
ok upload your package,i will check what's wrong
as inthe whole folder?
yes
I mean, it works fine on some IDEs. But that kind of layout is rather confusing on how to build compilation lines
You're right,these engineering regulations are things that teachers never tell us, but they are very important in work project.
how do i submit a whole folder
Maybe use compression tool to compress folder
Ok, first you need modify java.lang.Record to Record all the class and interface
You should organize the directory structure of you project, you should not pull all the files in one directory.
No. I use IDEA and I recommend you use it.
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
ok so how do i solve the issue im sorry i dont understand the instruction
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Where does Record come from?
oh hmm
try writing import Record;
well its supposed to come from my class in file Record.java
if that doesn't work this might be a strange corner case or the language
i need to read the language spec
defintiely one fix is to put things in a package
so
package program;
public class Record{
private Key theKey;
private String data;
public Record (Key K, String theData){
this.theKey = K;
this.data = theData;
}
public Key getKey(){
return theKey;
}
public String getDataItem(){
return data;
}
}
and then
import program.Record;
public class BSTNode {
private Record record;
private BSTNode leftChild;
private BSTNode rightChild;
private BSTNode parent;
public BSTNode(Record r) {
this.record = r;
this.leftChild = null;
this.rightChild = null;
this.parent = null;
}
public Record getRecord() {
return record;
}
public void setRecord (Record d){
this.record =d;
}
public BSTNode getLeftChild(){
return leftChild;
}
public BSTNode getRightChild() {
return rightChild;
}
public BSTNode getParent() {
return parent;
}
public void setLeftChild(BSTNode u) {
this.leftChild = u;
}
public void setRightChild(BSTNode u) {
this.rightChild = u;
}
public void setParent(BSTNode u) {
this.parent = u;
}
public boolean isLeaf(){
boolean retBool = false;
if (this.leftChild == null && this.rightChild ==null && this.getRecord() == null){
retBool = true;
}
return retBool;
}
}
but give me a minute to read the language spec
ok
if you want to read along
@fresh bone so uhh
this is impossible to fix unless you move code into packages
🤷♂️ 😬
good time to learn how to do that I guess
how are you compiling/running your code right now?
im not i haven’t actually run anything yet
🙃
didn’t get that far
which part i wrote, Record, BSTNode and Key
the rest was provided files for an assignment yes
so yes starter code for an assignment 😭
you need to tell your professor about this so they can let you either
- Move things into packages
- Rename
Recordto something else
im on call with a teaching assistant right now and he cant figure anything out either
I am 95% sure there is no way to fix this other than those two ways
...or compiling with a lower release
so like javac --release 11 -d out *.java
that would actually work
im literally begging them to just let me rename it
aasted a whole day on this
can you use the --release flag?
when you compile
that should do it
javac --release 11 -d out *.java
TestDict.java:7: error: cannot find symbol
BSTDictionary dictionary = new BSTDictionary();
^
symbol: class BSTDictionary
location: class TestDict
TestDict.java:7: error: cannot find symbol
BSTDictionary dictionary = new BSTDictionary();
^
symbol: class BSTDictionary
location: class TestDict
TestDict.java:151: error: cannot find symbol
dictionary = new BSTDictionary();
^
symbol: class BSTDictionary
location: class TestDict
3 errors
did you make a BSTDictionary yet?
class BSTDictionary implements BSTDictionaryADT {
public Record get (Key k) { return null; }
public void put (Record d) throws DictionaryException {}
public void remove (Key k) throws DictionaryException { }
public Record successor (Key k) { return null; }
public Record predecessor (Key k) { return null; }
public Record smallest () { return null; }
public Record largest () { return null; }
}
I made this dummy BSTDictionary
then compiled with
javac --release 11 -d out *.java
and it works fine
actually..
it works fine regardless?
are you sure you are doing this right?
its all working for me locally
no
honestly idk why its not working it should work fine without having to do anything the TA says nobody else had this problem
how are you running your code?
what editor are you using
what is "not working" exactly
ok so i was on vscode
and for some reason you cant see the file structure properly in the explorer on vscode
but my TA opened it up on eclipse and saw that Record.java was in the folder with everything else but was not in "default package"
idk how or why as I created it and saved it the same way
do you know why this couldve happened?
not working as in not using my Record class
VScode
If im real about it - VSCode is not great for Java ATM
it is getting better but slowly
Try using https://www.jetbrains.com/idea/download
download the community edition (link is in the black area)
and make a blank new project
one more question
whats the difference between the BinarySearchTree and the BSTDictionary classes
arent they applying the same methods
@ me in an hour or so
im doing thanksgiving prep
If youre interested, you should download Eclipse. I find it much better to work with Java in than visual studio code. If you have any questions feel free to DM me.
i appreciate all the help
okay looking
i have eclipse and actually used it in the past, I started using VScode for C and i just forgot everything about eclipse lol
I don't see a BinarySearchTree class
ah
so BinarySearchTree is the generic structure
BSTDictionary seems to make a dictionary on top of it
so inside BSTDictionary you likely will have to use BinarySearchTree
do i have to use generic types in it then?
doesn't look like it