简体   繁体   中英

Calling the info from one class to another class?

I am relatively new to programming and an working with setters and getters at the moment.

I have something set up where I have a student class that has information about said student, including their first, middle, and last name, their student ID, and their major.

I need to set it so that, if their student ID is less than zero, it automatically sets it to -1. I also need to set the major to undecided if they do not input anything.

I also need to override the toString method and print all of this information out.

I feel like I have the first part with the names down, I am not sure about the rest of it however. I am not sure how I am supposed to use the toString method while also using setters and getters.

Below is my Student class that does all of the work.

import java.util.Objects;
import java.util.Scanner;
public class Student {
        String first;
        String middle;
        String last;
        String major = "Undecided";
        static int studentID = -1;

        public Student(String first, String middle, String last) {
                Objects.requireNonNull(first);
                Objects.requireNonNull(last);
        }

        public void setFirst(String A) {
                first = A;
        }

        public void setMiddle(String B) {
                middle = B;
         }

        public void setLast(String C) {
                last = C;
        }

        private String getFirst() {
                return first;
        }

        private String getMiddle() {
                return middle;

        }

        private String getLast() {
                return last;
        }

        private String getMajor() {
            return major;
            
        }
        public void setMajor(){
        
            
        
        static void register(int a){
                if (a < 0) {
                    studentID = a;
                } else {
                    studentID = getID(a);
                }
        }

        private static int getID(int a) {
                if (studentIDInput < 0) {
                    studentID = -1;
                } else {
                    studentID = a;
                }
            return studentID;
        }

        public static void main(String[] args) {
                String first = "abc";
                String middle = "def";
                String last = "ghi";

                Scanner sc = new Scanner(System.in);
                String majorInput = sc.next();
                int studentIDInput = sc.nextInt();

                Student student1 = new Student(first, middle, last);

                System.out.println(student1.getFirst().toString() + " " + student1.getMiddle().toString() + " " + student1.getLast().toString() + '\n' + "Major:" + " " + student1.getMajor().toString() + '\n' );
        }
        
        @Override
        public String toString() {
            return ;
            
        }

}

I have also included the Driver class just for reference.


public class Driver {
        static String first;
        static String middle;
        static String last;


        public static void main(String[] args){
                Student student1 = new Student(first, middle, last);
                student1.setFirst("Mikayla");
                student1.setMiddle("Rose");
                student1.setLast("Knox");


        }
}

You have this constructor:

public Student(String first, String middle, String last) {
    Objects.requireNonNull(first);
    Objects.requireNonNull(last);
}

It does its job of checking that first and last name are not null , but it does not do anything with the values besides checking. The constructor's job is to construct the object, ie, initialize its member variables. When your constructor is done, you should have a usable object, without having to call any setters in it.

You need to add that:

public Student(String first, String middle, String last) {
    Objects.requireNonNull(first);
    Objects.requireNonNull(last);
    this.first = first;
    this.middle = middle;
    this.last = last;
}

Note that you don't need to use setters here as code within the class can access member variables directly. You can use setters if you want, though.

As for toString : this is a method mainly used in debugging, and it displays some helpful information about the object it's called on. You could implement it like below, with a bit of ?: to make sure to only print the middle name if it's not null :

@Override
public String toString() {
    return first + " " + (middle != null ? middle + " " : "") + last;
}

I'll leave it to you to also include major and ID.

On using a Scanner : You use a Scanner to get input from somewhere, like the from the user. You don't need it in toString or any setters or getters. These are all methods that should be very simple and not deal with I/O classes like Scanner .

If you are using constructors, you do not really need setters. Try something like this:

class Student {
    private String first;
    private String middle;
    private String last;
    private String major;
    private int studentID;
    
    public Student(String first, String middle, String last) {
        this(first, middle, last, "undecided", -1);
    }
    
    public Student(String first, String middle, String last, String major, int studentID) {
        this.first = first;
        this.middle = middle;
        this.last = last;
        this.major = major;
        this.studentID = studentID;
    }
    
    @Override
    public String toString() {
        return "first: " + first + "\nmiddle: " + middle + "\nlast: " + last + "\nmajor: " + major + "\nid: " _ studentID;
    }
}

This way, when you create a new Student object with 3 parameters, the last 2 are automatically set to "undecided" and -1. If there is a case when you have the ID and not the major (or the other way around), you can add more constructors.

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