简体   繁体   中英

(identifier expected) getter/setter and objects

I've got a problem with my programm. When i try to compile following i just receive the message:

Tutorium.java:15: error: <identifier> expected
    public void settName(vorlesung.lectureName) {
                                              ^

So my Code:

Tutorium.java

public class Tutorium {
private Vorlesung vorlesung;
public String tName;
private int tNumber;


public int gettNumber() {
    return this.tNumber;
}

public String gettName() {
    return this.tName;
}

public void settName(vorlesung.lectureName) {
    this.tName = vorlesung.lectureName;
}

public String toString() {
    return (this.tName + ", " + this.tNumber);
}

public Tutorium(int tNumber){
    this.tNumber = tNumber; } }

Vorlesung.java

public class Vorlesung {
public String lectureName;
private int lectureNumber;
private int lecture;
private Dozent dozent;
private String lecturerlName;

public String getlectureName(){
    return this.lectureName;
}

public int lectureNumber(){
    return this.lectureNumber;
}

public int lecture(){
    return this.lecture;
}

public String getlecturer(){
    this.lecturerlName = dozent.lecturerlName;
    return this.lecturerlName;
}

public String toString() {
    return (this.lectureName + ", " + this.lectureNumber);
}

public Vorlesung(String lectureName, int lecture) {
    this.lectureName = lectureName;
    this.lecture = lecture +1;
    this.lectureNumber = this.lecture -1;
    this.lecturerlName = lecturerlName;
}}

My Main-Method:

public class MainVorlesung {
public static void main(String[] args) {
    Student student = new Student("STUDENTNAME", "STUDENTLASTNAME", 178, 1);
    Vorlesung vorlesung = new Vorlesung("Programmieren", 13341);
    Tutorium tutorium = new Tutorium(3);
    Dozent dozent = new Dozent("LECTURERFIRSTNAME", "LECTURERLASTNAME", 815);

    System.out.println(student.toString());
    System.out.println(vorlesung.toString());
    System.out.println(tutorium.toString());
    System.out.println(dozent.toString());

}}

My goal is to set the value of tName equal the value of vorlesung.lectureName.

Why can't i do this that way?

I appreciate every help. :) Thanks

For methods, the arguments that you pass in must have a declared value.

In this case, a String. So you need to change your method to this:

public void settName(String newLectureName) {
    this.tName = newLectureName;
}

Read more about what a java method is and how to create one here: http://www.tutorialspoint.com/java/java_methods.htm

Change settName to

public void settName(String name) {
    this.tName = name;
}

Since your goal is:

My goal is to set the value of tName equal the value of vorlesung.lectureName.

You should get rid of the setName method entirely since it will depend entirely on the vorlesung field and so should not be changeable. You should also get rid of the tName field, and instead change getName() to:

public class Tutorium {
    private Vorlesung vorlesung;
    // public String tName;  // get rid of
    private int tNumber;



    public String gettName() {
        if (vorlesung != null) {
            return vorlesung.getlecturer();
        }

        return null; // or throw exception
    }

    // *** get rid of this since you won't be setting names
    // public void settName(Vorlesung vorlesung) {
    //     this.tName = vorlesung.lectureName;
    // }

I have just now noticed that your Tutorium class does not have and absolutely needs a setVorlesung(...) method.

public void setVorlesung(Vorlesung vorlesung) {
    this.vorlesung = vorlesung;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM