#Need help with classes in Java very basic

143 messages · Page 1 of 1 (latest)

fresh bone
#

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.

sour monolithBOT
#

This post has been reserved for your question.

Hey @fresh bone! Please use /close or the Close Post button 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.

brittle parrot
#

You need delete import Java.lang.record and reimport you Record class.

fresh bone
#

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?

brittle parrot
#

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();

fresh bone
#

idk what that is?

fresh bone
gray tree
#

import your.package.Record;

fresh bone
#

how do i import a specific file

#

both files are already in the same folder

gray tree
#

can you share the files

#

do they have package declarations at the top?

#

and how are you running them

fresh bone
#

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?

brittle parrot
#

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

fresh bone
#

i have the ADT for that but have not implemented it yet

brittle parrot
#

emm You should implement BSTDictionaryADT first, and be sure that get method return Record is you defined.

indigo haven
#

"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

brittle parrot
#

Yes you should avoid to use the word that was defined in jdk as system key words.

indigo haven
#

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

fresh bone
#

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

fresh bone
#

wouldnt it still return the wrong object Record

brittle parrot
#

ok upload your package,i will check what's wrong

fresh bone
#

as inthe whole folder?

brittle parrot
#

yes

indigo haven
#

I mean, it works fine on some IDEs. But that kind of layout is rather confusing on how to build compilation lines

brittle parrot
#

You're right,these engineering regulations are things that teachers never tell us, but they are very important in work project.

fresh bone
#

how do i submit a whole folder

brittle parrot
#

Maybe use compression tool to compress folder

fresh bone
#

this is a zip with all provided code the files i sent earlier were added by me

brittle parrot
#

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.

fresh bone
#

how do i do this?

#

are you on vscode?

brittle parrot
#

No. I use IDEA and I recommend you use it.

sour monolithBOT
#

💤 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.

fresh bone
#

ok so how do i solve the issue im sorry i dont understand the instruction

sour monolithBOT
gray tree
#

oh hmm

#

try writing import Record;

fresh bone
#

well its supposed to come from my class in file Record.java

gray tree
#

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

fresh bone
#

ok

gray tree
#

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?

fresh bone
gray tree
#

🙃

fresh bone
#

didn’t get that far

gray tree
#

who wrote this code?

#

is this like "starter" code for an assignment?

fresh bone
#

which part i wrote, Record, BSTNode and Key

#

the rest was provided files for an assignment yes

#

so yes starter code for an assignment 😭

gray tree
#

you need to tell your professor about this so they can let you either

  • Move things into packages
  • Rename Record to something else
fresh bone
#

im on call with a teaching assistant right now and he cant figure anything out either

gray tree
#

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

fresh bone
#

aasted a whole day on this

gray tree
#

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

fresh bone
fresh bone
gray tree
#

how are you running your code?

#

what editor are you using

#

what is "not working" exactly

fresh bone
#

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?

fresh bone
fresh bone
gray tree
#

If im real about it - VSCode is not great for Java ATM

#

it is getting better but slowly

#

download the community edition (link is in the black area)

#

and make a blank new project

fresh bone
#

one more question

#

whats the difference between the BinarySearchTree and the BSTDictionary classes

#

arent they applying the same methods

gray tree
#

im doing thanksgiving prep

pearl palm
#

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.

fresh bone
gray tree
#

okay looking

fresh bone
gray tree
#

I don't see a BinarySearchTree class

fresh bone
gray tree
#

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

fresh bone
gray tree
#

doesn't look like it