How to use a variable of one class in another in Java?

I'm just working on some things as a practice for an exam that I came up with, but one thing I can’t make out with is using a variable belonging to one class in another class.

I have a course class and a Student class. The class course stores all the different courses, and what I just want is to use the name of the course in the Student class.

Here is my course class:

public class Course extends Student { // instance variables - replace the example below with your own private Award courseAward; private String courseCode; public String courseTitle; private String courseLeader; private int courseDuration; private boolean courseSandwich; /** * Constructor for objects of class Course */ public Course(String code, String title, Award award, String leader, int duration, boolean sandwich) { courseCode = code; courseTitle = title; courseAward = award; courseLeader = leader; courseDuration = duration; courseSandwich = sandwich; } } 

And here is the Student:

 public class Student { // instance variables - replace the example below with your own private int studentNumber; private String studentName; private int studentPhone; private String studentCourse; /** * Constructor for objects of class Student */ public Student(int number, String name, int phone) { studentNumber = number; studentName = name; studentPhone = phone; studentCourse = courseTitle; } } 

Am I using ' extends ' as part of the course? Or is it not necessary?

In my constructor for Student, I am trying to assign the "title" of the Course class to the variable "studentCourse". But I just can't figure out how to do this!

Thank you in advance for your help, I look forward to hearing from you!

Thanks!

+6
source share
12 answers

Am I using continuation as part of the course? Or is it not necessary?

Unfortunately, no, if you want to know if the inheritance is correct or not, replace extends with is-a . Is the course a student? The answer is no. This means that your Course should not expand Student

A student can attend Course , so the Student class can have a member variable of type Course . You can define a list of courses if your model indicates that (a student can attend several courses).

Here is a sample code:

 public class Student{ //.... private Course course; //... public void attendCourse(Course course){ this.course = course; } public Course getCourse(){ return course; } } 

You may now have the following:

 Student bob = new Student(...); Course course = new Course(...); bob.attendCourse(course); 
+11
source

I assume the course is not a student, so inheritance between these classes is probably bad.

+4
source

You must declare them public.

It is best to keep them secret and encode a public getter for this variable. eg:

 public Award getCourseAward(){ return this.courseAward; } 
+4
source

Course should not be extended by Student . If you want to access the courseTitle Course field, you need to pass the link to the Course object to Student , and then make the course. CourseTitle.

+2
source

You cannot access private class attributes from another, this is one of the main principles of OOP: encapsulation. You must provide a method for accessing this attribute that you want to publish outside the class. The general approach is only setter / getters - getters if you want your class to be immutable. See here: http://en.wikipedia.org/wiki/Mutator_method#Java_example

+2
source

It makes no sense to arbitrarily extend classes. A student is not a course, or vice versa, so you cannot expand them that way.

What you need to do:

first create a course:

  Course aCourse = new Course(..); 

create student:

  Student aStudent = new Student(..); 

assign a course to a student:

  aStudent.setCourse(aCourse.title); 
+2
source

Student extension using Couse , because they are not of the same type. An extension of one class to another occurs when it specializes in a more general (in a sense) one.
The solution would be to pass courseTitle as an argument to the Student constructor

+2
source

There should be three separate objects: course, student, and registration. Enrollment connects the student to the course, the course has many students, and the student can enroll in many courses. None of them should expand each other.

+2
source

You may not need to add a course name to the student. What I would do is add students to some data structure in the course. It is cleaner and lessens the connection between the course and the student. It will also allow you to have students in more than one course. For instance:

 public class Course extends Student{ private Award courseAward; private String courseCode; public String courseTitle; private Student courseLeader;//change to a student Object private int courseDuration; private boolean courseSandwich; private Set<Student> students;//have course hold a collection of students /** * Constructor for objects of class Course */ public Course(String code, String title, Award award, Student leader, int duration, boolean sandwich){ courseCode = code; courseTitle = title; courseAward = award; courseLeader = leader; courseDuration = duration; courseSandwich = sandwich; this.students=new HashSet<Student>(); } public boolean addStudent(Student student){ return students.add(student); } public Set<Student> getStudents(){ return students; } 

}

+2
source

At first,

You are expanding the Students class in the course class, which means that the student class receives all the properties of the coruse class. Thus, the student class does not have the courseTitle property.

Secondly, yes, this is optional - you need to do the following:

 public class Course { private Award courseAward; private String courseCode; public String courseTitle; private String courseLeader; private int courseDuration; private boolean courseSandwich; public Course(String code, String title, Award award, String leader, int duration, boolean sandwich) { courseCode = code; courseTitle = title; courseAward = award; courseLeader = leader; courseDuration = duration; courseSandwich = sandwich; } } public class Student { private int studentNumber; private String studentName; private int studentPhone; // This is where you keep the course object associated to student public Course studentCourse; public Student(int number, String name, int phone, Course course) { studentNumber = number; studentName = name; studentPhone = phone; studentCourse = course; } } 

A usage example would be something like this:

 Course course = new Course("ASD", "TITLE", null, "ME", 50, true); Student student = new Student(1, "JOHN", "5551234", course); 

And then, get the course information you need from the student, for example:

 student.studentCourse.courseTitle; 

Since now student.studentCourse will be an object of the course with all its properties.

Greetings

+2
source

As mentioned, stay away from "extensions" for this. In general, you should not use it if the "is-a" relationship makes sense.

You should probably provide getters for the course class methods:

 public class Course { ... public String getTitle() { return title; } } 

And then, if the Student class needs it, it will somehow keep the course (which depends on you in your design) and call getter:

 public class Student { private Set<Course> courses = new HashSet<Course>(); public void attendCourse(Course course) { courses.add(course); } public void printCourses(PrintStream stream) { for (Course course : courses) { stream.println(course.getTitle()); } } } 
+1
source

Below is the solution to the problem, and if you want to check the code below on your computer, create a file called Test.java and paste the following codes:

package com;

 class Course { private Award courseAward; private String courseCode; public String courseTitle; private String courseLeader; private int courseDuration; private boolean courseSandwich; public Course(String code, String title, Award award, String leader, int duration, boolean sandwich) { courseAward = award; courseCode = code; courseTitle = title; courseLeader = leader; courseDuration = duration; courseSandwich = sandwich; } public Award getCourseAward() { return courseAward; } public void setCourseAward(Award courseAward) { this.courseAward = courseAward; } public String getCourseCode() { return courseCode; } public void setCourseCode(String courseCode) { this.courseCode = courseCode; } public String getCourseTitle() { return courseTitle; } public void setCourseTitle(String courseTitle) { this.courseTitle = courseTitle; } public String getCourseLeader() { return courseLeader; } public void setCourseLeader(String courseLeader) { this.courseLeader = courseLeader; } public int getCourseDuration() { return courseDuration; } public void setCourseDuration(int courseDuration) { this.courseDuration = courseDuration; } public boolean isCourseSandwich() { return courseSandwich; } public void setCourseSandwich(boolean courseSandwich) { this.courseSandwich = courseSandwich; } } class Student { private int studentNumber; private String studentName; private int studentPhone; private Course studentCourse; /** * Constructor for objects of class Student */ public Student(int number, String name, int phone, Course course) { studentNumber = number; studentName = name; studentPhone = phone; studentCourse = course; } public int getStudentNumber() { return studentNumber; } public void setStudentNumber(int studentNumber) { this.studentNumber = studentNumber; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public int getStudentPhone() { return studentPhone; } public void setStudentPhone(int studentPhone) { this.studentPhone = studentPhone; } public Course getStudentCourse() { return studentCourse; } public void setStudentCourse(Course studentCourse) { this.studentCourse = studentCourse; } } class Award{ private long awardId; private String awardName; Award(long awardId, String awardName){ this.awardId = awardId; this.awardName = awardName; } public long getAwardId() { return awardId; } public void setAwardId(long awardId) { this.awardId = awardId; } public String getAwardName() { return awardName; } public void setAwardName(String awardName) { this.awardName = awardName; } } public class Test{ public static void main(String ar[]){ // use your all classes here } } 
+1
source

Source: https://habr.com/ru/post/919275/


All Articles