Creating two constructors with objects that call to other java files - java

I don't really know if my question makes sense but my assignment says:
"Write a third class, StudentRecord, that has two attributes:
Student stu;
Address addr;
and two constructors. The first constructor is given a Student object and an Address object to initialize the attributes. The second constructor is given a first name, a last name, a student ID, a gpa, a street address, a city, a state, and a zipcode and uses these to initialize the attributes"
I don't understand how exactly I'm supposed to make two constructors take info from two different java files.
Here's the code I have for the third class named "StudentRecord".
I have no doubt it's incorrect.
public class StudentRecord {
Student stu;
Address addr;
public StudentRecord() {
Student stu;
Address addr;
}
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
}
public String toString() {
return String.format(stu + "\n" + addr);
}
}
Here's the code I have for the TestStudentRecord class, all I get is
"null null" when I run the program.
public class TestStudentRecord {
public static void main(String[] args) {
StudentRecord stu1 = new StudentRecord("Jane", "Brown", 182765, 2.333, "13 Flower St.", "Pulteneyville", "NY", 14386);
System.out.println(stu1);
}
}
All I get is "null null" when I run the program instead of the toString method giving me the student info I have typed into the test class.
For those asking for the Student and Address classes, here you go:
public class Student {
// attributes of a Student
private String firstName;
private String lastName;
private int studentId;
private double gpa;
/**
* Student constructor.
* #param _fName student's first name
* #param _lName student's last name
* #param _id student's id number
* #param _gpa students GPA
*/
public Student(String _fName, String _lName, int _id, double _gpa) {
firstName = _fName;
lastName = _lName;
studentId = _id;
gpa = _gpa;
}
/**
* getFirstName - Accessor for first name
* #return the student's first name
*/
public String getFirstName() {
return firstName;
}
/**
* getLastName - Accessor for last name
* #return the student's last name
*/
public String getLastName() {
return lastName;
}
/**
* getId - Accessor for ID
* #return the student's ID
*/
public int getStudentId() {
return studentId;
}
/**
* getGpa - Accessor for gpa
* #return the student's gpa
*/
public double getGpa() {
return gpa;
}
/**
* setFirstName - Mutator for first name
* #param the new first name
*/
public void setFirstName(String _fName) {
firstName = _fName;
}
/**
* setLastName - Mutator for last name
* #param the new last name
*/
public void setLastName(String _lastName) {
lastName = _lastName;
}
/**
* setStudentId - Mutator for ID
* #param the new ID
*/
public void setStudentId(int _id) {
studentId = _id;
}
/**
* setGpa - Mutator for gpa
* #param the new gpa
*/
public void setGpa(double _gpa) {
gpa = _gpa;
}
// toString Method
public String toString() {
return String.format(getLastName() + ", " + getFirstName() + "\n" + "ID: " + getStudentId() + " GPA: %3.1f", getGpa());
}
}
public class Address {
private String street;
private String city;
private String state;
private int zip;
public Address(String _street, String _city, String _state, int _zip) {
street = _street;
city = _city;
state = _state;
zip = _zip;
}
// Accessors
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getState() {
return state;
}
public int getZip() {
return zip;
}
// Mutators
public void setStreet(String _street) {
street = _street;
}
public void setCity(String _city) {
city = _city;
}
public void setState(String _state) {
state = _state;
}
public void setZip(int _zip) {
zip = _zip;
}
// toString Method
public String toString() {
return String.format(getStreet() + "\n" + getCity() + ", " + getState() + " " + getZip());
}
}

would suggest you to please follow below approach to initialize your object through argumented construtor.
public class StudentRecord {
Student stu;
Address addr;
public StudentRecord() {
Student stu;
Address addr;
}
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
this.stu = new Student(_fName, _lName, _id, _gpa);
this.addr=new Address(_street,_city,_state,_zip);
}
public String toString() {
return String.format(stu + "\n" + addr);
}
}
As you have already overided toString method in address and student class so you will get the object.

Your constructor is like below. Empty Constructor will not assign values to your class variables.
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
}
it should be like below as mentioned by Eran.
public StudentRecord(String _fName, String _lName, int _id, double _gpa, String _street, String _city, String _state, int _zip){
this.stu = new Student(_fName, _lName, _id, _gp);
this.addr = new Address(_street, _city, _state, _zip);
}
Now your toString will have the values for stu and addr. Which will get printed.

You have to initilize the variables with this.{variable name} = {variable name}.
Also it is often better to chain the Constructors (see How do I call one constructor from another in Java?), in this way changes in classes, with many constructors, are way easier to implement.
I would also suggsest to rename your variables, because in Java they should not start with an underscore (see https://www.javatpoint.com/java-naming-conventions).
public class StudentRecord {
Student student;
Address addr;
public StudentRecord(Student student, Address addr) {
this.student = student;
this.addr = addr;
}
public StudentRecord(String fName, String lName, int id, double gpa, String street, String city, String state, int zip){
this(new Student(fName, lName, id, gpa),
new Address(street,city,state,zip));
}
public String toString() {
return String.format(student + "\n" + addr);
}
}

Related

The string value data member is skipped by compiler [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 3 years ago.
string value is not displaying all though told the computer to display Please help
import java.util.Scanner;
class Author
{
private String name;
private String email;
private char gender;
public Author (String name, String email, char gender) {
this.name=name;
this.setEmail(email);
this.gender=gender;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #return the email
*/
public String getEmail() {
return email;
}
/**
* #param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* #return the gender
*/
public char getGender() {
return gender;
}
#Override
public String toString() {
return "Author [name=" + name + ", email=" + email + ", gender=" + gender + "]";
}
}
class Book
{
private String Name;
Author auth;
private double price;
private int qty;
/**
* #param name
* #param auth
* #param price
*/
public Book(String name, Author auth, double price) {
super();
Name = name;
this.auth = auth;
this.price = price;
}
/**
* #param name
* #param auth
* #param price
* #param qty
*/
public Book(String name, Author auth, double price, int qty) {
super();
Name = name;
this.auth = auth;
this.price = price;
this.qty = qty;
}
public String getName() {
return Name;
}
public Author getAuth() {
return auth;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
#Override
public String toString() {
return "Book [Name=" + Name + ", auth=" + auth + ", price=" + price + ", qty=" + qty + "]";
}
}
class Student
{
private String Name;
private int roll;
date issueDate;
date returnDate;
/**
* #param name
* #param roll
* #param issueDate``
* #param returnDate
*/
public Student(String name, int roll, date issueDate, date returnDate) {
super();
Name = name;
this.roll = roll;
this.issueDate = issueDate;
this.returnDate = returnDate;
}
public String getName() {
return Name;
}
public int getRoll() {
return roll;
}
public date getIssueDate() {
return issueDate;
}
public date getReturnDate() {
return returnDate;
}
public void setReturnDate(date returnDate) {
this.returnDate = returnDate;
}
}
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
Author ahTeck=null;
System.out.println("How many Book are there in library ?");
int n=sc.nextInt();
Book []ob=new Book[n];
for(int i=0;i<n;++i)
{ System.out.println("Author's name");
String s=sc.nextLine();
sc.nextLine();
System.out.println("Author's Email Id");
String s1=sc.nextLine();
System.out.println("gender:");
char c = sc.next(".").charAt(0);
sc.nextLine();
System.out.println("Book Name:");
String b=sc.nextLine();
System.out.println("Book price:");
double price=sc.nextInt();
System.out.println("Book quantity");
int q=sc.nextInt();
ob[i]=new Book(b, new Author(s, s1, c),price,q);
System.out.println(ob[i]);
}
}
}
the question is there in
https://www.ntu.edu.sg/home/ehchua/programming/java/J3f_OOPExercises.html
Output is coming like
Book [Name=g, auth=Author [name=, email=e, gender=m], price=6.0, qty=6]
the displaying of author name is skipped
Ah yes. The ol' Scanneroo.
Right after entering the integer at the top, call sc.nextLine();
int n = sc.nextInt();
sc.nextLine();
Actually, you don't even have to call sc.nextLine() after entering a string or character. The thing is, when you enter a number, you hit the enter key as well. The Scanner class regards it as another token when you use nextInt(), so the number gets stored correctly, but the enter key \n is regarded as another token.
So when you call nextLine() after entering the number, the system sees that there is already a token remaining in the Scanner object, so it takes '\n' as its input. Thus, the name actually stores "\n", which is empty.

How to add multiple students to student class?

Im trying to create a student class, a course class and the main class. I am trying to add students to the course, and when students are added to the class the number of students in the course should increase, when the the code is run it should print the course details followed by the students in the course.
I have got the following code:
Main class:
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
Course class:
public class Course {
ArrayList<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
//Getters
public String getCourseName(){
return this.courseName;
}
public int getNoOfStudents(){
return this.noOfStudents;
}
public String getTeacher(){
return this.teacher;
}
//Setters
public void setCourseName(String courseName){
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents){
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher){
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with defaults
*
*/
public Course(){
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
* #param noOfStudents integer
* #param courseName String with the Course name
* #param teacher String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents){
this.courseName = courseName;
this.teacher = teacher;
noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public static void addStudent(Student newStudent){
if(studentList.size()==noOfStudents){
System.out.println("The class is full, you cannot enrol.");
}
else {
studentList.add(newStudent);
}
}
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + this.noOfStudents;
}
}
Student class:
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge(){
return this.age;
}
public String getName(){
return this.name;
}
public String getCourse(){
return this.course;
}
public String getPhoneNo(){
return this.phoneNo;
}
// Setters
public void setAge(int age){
this.age = age;
}
public void setName(String name){
if (JavaLecture3.DEBUG > 3) System.out.println("In Student.setName. Name = "+ name);
this.name = name;
}
public void setCourse(String course){
this.course = course;
}
public void setPhoneNo(String phoneNo){
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student(){
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
* #param age integer
* #param name String with the name
* #param course String with course name
* #param phoneNo String with phone number
*/
public Student(int age, String name, String course, String phoneNo){
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
* #param gender
*/
public Student(String gender){
this(); // Must be the first line!
this.gender = gender;
}
protected void finalize() throws Throwable{
//do finalization here
instances--;
super.finalize(); //not necessary if extending Object.
}
public String toString (){
return "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
Reference:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html?is-external=true
Course.getNoOfStudents should just return studentList.size(). No need to maintain a separate variable.
To print a list of students (from Course):
for(int i=0;i<studentList.size();i++) System.out.println(studentList.get(i));
Since you initialized the array list in the Course class, you should add the students to it. You should create a method in Course that adds an object to the array list such as:
public void addStudent(Student s){
studentList.add(s);
noOfStudents++;
}
To add multiple students:
public void addStudents(Student[] students){
for(int i = 0; i < students.length; i++){
studentList.add(students[i]);
}
}
You're almost there, just use the .add method made for you in the array list.
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
Student student = new Student(); // Calling default constructor here.
Course course = new Course();
student = new Student(21, "Joe", "CSE", "07447832342");
course = new Course("CSE", "Tom", 5);
course.addStudent(student);
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course);
System.out.println();
System.out.println("Student contains: "); // calls student.toString());
System.out.println("-------------------");
System.out.println(student);
}
}
public class Course {
List<Student> studentList;
private String courseName;
private String teacher;
private int noOfStudents;
// Getters
public String getCourseName() {
return this.courseName;
}
public int getNoOfStudents() {
return this.noOfStudents;
}
public String getTeacher() {
return this.teacher;
}
// Setters
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents) {
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with
* defaults
*
*/
public Course() {
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
studentList = new ArrayList<Student>();
}
/**
* Constructor with parameters
*
* #param noOfStudents
* integer
* #param courseName
* String with the Course name
* #param teacher
* String with the teacher
*/
public Course(String courseName, String teacher, int noOfStudents) {
this.courseName = courseName;
this.teacher = teacher;
this.noOfStudents = noOfStudents;
studentList = new ArrayList<Student>();
}
public void addStudent(Student newStudent) {
if (studentList.size() == noOfStudents) {
System.out.println("The class is full, you cannot enrol.");
} else {
studentList.add(newStudent);
}
}
#Override
public String toString() {
return "Course Name: " + this.courseName + " Teacher: " + this.teacher
+ " Number of Students: " + studentList.size();
}
}
public class Student {
private String name;
private int age;
public String gender = "na";
private String course;
private String phoneNo;
public static int instances = 0;
// Getters
public int getAge() {
return this.age;
}
public String getName() {
return this.name;
}
public String getCourse() {
return this.course;
}
public String getPhoneNo() {
return this.phoneNo;
}
// Setters
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
if (JavaLecture3.DEBUG > 3)
System.out.println("In Student.setName. Name = " + name);
this.name = name;
}
public void setCourse(String course) {
this.course = course;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
/**
* Default constructor. Populates name,age,gender,course and phone Number
* with defaults
*/
public Student() {
instances++;
this.age = 18;
this.name = "Not Set";
this.gender = "Not Set";
this.course = "Not Set";
this.phoneNo = "Not Set";
}
/**
* Constructor with parameters
*
* #param age
* integer
* #param name
* String with the name
* #param course
* String with course name
* #param phoneNo
* String with phone number
*/
public Student(int age, String name, String course, String phoneNo) {
this.age = age;
this.name = name;
this.course = course;
this.phoneNo = phoneNo;
}
/**
* Gender constructor
*
* #param gender
*/
public Student(String gender) {
this(); // Must be the first line!
this.gender = gender;
}
protected void finalize() throws Throwable {
// do finalization here
instances--;
super.finalize(); // not necessary if extending Object.
}
public String toString() {
return "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo;
}
}
public class JavaLecture3 {
public static final int DEBUG = 0;
public static void main(String [] args){
//Create course object
Course course = new Course("CSE", "Tom", 5);
Scanner scanner = new Scanner(System.in);
String cmd = "Yes";
while(cmd.equals("Yes")){
Student student = new Student();
System.out.print("Enter a new student? ");
cmd = scanner.next();
if (cmd.equals("Yes")){
//Read student name
System.out.print("Enter a student name: ");
String name = scanner.next();
student.setName(name);
//Read student Age
System.out.print("Enter a student age: ");
int age = scanner.nextInt();
student.setAge(age);
//Read student Course
System.out.print("Enter a student course: ");
String stdent_course = scanner.next();
student.setCourse(stdent_course);
//register the student to the class
course.addStudent(student);
}
}
scanner.close();
System.out.println("Course Information: ");
System.out.println("-------------------");
System.out.println(course.toString());
System.out.println();
}
}
I reworked a bit your toString() method from Course class. This should print the list of students attending a class.
public String toString (){
String ret_value = "Name: " + this.name + " Age: " + this.age + " Gender: "
+ this.gender + " Course: " + this.course + " Phone number: "
+ this.phoneNo + " Students attending this course:";
for (Student student: studentList) {
ret_value = ret_value + " " + Student.getName();
}
return ret_value;
}

error cannot find symbol - variable

I am new to using Java - so please forgive my ignorance. Would you be able to look at this code and let me know why I get error:
cannot find symbol - variable
when I call the constructor method. I am using BlueJ. Basically I put in the variables and then hit ok to create an object but it comes up with that error.
/**
* Write a description of class Membership here.
*
* #author (Gohar Warraich)
* #version (1.0)
*/
public class Membership
{
// instance variables - replace the example below with your own
private String firstname;
private String lastname;
private String phonenumber;
private int idnumber;
/**
* Constructor for objects of class Membership
*/
public Membership(String newfirstname, String newlastname, String newphonenumber, int newidnumber)
{
// initialise instance variables
firstname = newfirstname;
lastname = newlastname;
phonenumber = newphonenumber;
idnumber = newidnumber;
}
/**
* Accessor method of Membership
*/
public String getfirstname()
{
return firstname;
}
public String getlastname()
{
return lastname;
}
public String getphonenumber()
{
return phonenumber;
}
public int getidnumber()
{
return idnumber;
}
/**
* Mutator method of Membership
*/
public void setfirstname(String insertfirstname)
{
firstname = insertfirstname;
}
public void setlastname(String insertlastname)
{
lastname = insertlastname;
}
public void setphonenumber(String insertphonenumber)
{
phonenumber = insertphonenumber;
}
public void setid(int insertidnumber)
{
idnumber = insertidnumber;
}
public void printMembership()
{
System.out.println("The firstname is " + firstname + " The lastname is " + lastname +" The phoneNumber is "+ phonenumber +" The idNumber is " +idnumber);
}
}
#gohar, There isn't a problem in this code. It must be in your call to the constructor. I'll give you an example of what this should look like.
Membership membershipName = new Membership ("String", "String", "String", 0101)
0101 can be any int, and you can name the variable what ever you want by changing membershipName. Hope this helps. :)

Inheritance Lab

I'm working on a lab with couple super classes. When I used my compiled my TestingClass it highlighted the following as a syntax error:
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
The following is all of my code for this lab:
TESTING CLASS
public class TestingClass
{
public static void main(String[] args)
{
Person bob = new Person("Coach Bob", 27, "M");
System.out.println(bob);
Student lynne = new Student("Lynne Brooke", 16, "F", "HS95129", 3.5);
System.out.println(lynne);
Teacher mrJava = new Teacher("Duke Java", 34, "M", "Computer Science", 50000);
System.out.println(mrJava);
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
System.out.println(ima);
}
}
PERSON CLASS
public class Person
{
private String myName ; // name of the person
private int myAge; // person's age
private String myGender; // "M" for male, "F" for female
// constructor
public Person(String name, int age, String gender)
{
myName = name;
myAge = age;
myGender = gender;
}
public String getName()
{
return myName;
}
public int getAge()
{
return myAge;
}
public String getGender()
{
return myGender;
}
public void setName(String name)
{
myName = name;
}
public void setAge(int age)
{
myAge = age;
}
public void setGender(String gender)
{
myGender = gender;
}
public String toString()
{
return myName + ", age: " + myAge + ", gender: " +
myGender;
}
}
TEACHER CLASS
public class Teacher extends Person
{
// instance variables - replace the example below with your own
private String mySubject;
private double myAnnualSalary;
/**
* Constructor for objects of class Teacher
*/
public Teacher(String name, int age, String gender, String Subject, double AnnualSalary)
{
// initialise instance variables
super(name, age, gender);
mySubject = Subject;
myAnnualSalary = AnnualSalary;
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public String getSubject()
{
return mySubject;
}
public double getAnnualSalary()
{
return myAnnualSalary;
}
public void setSubject(String Subject)
{
mySubject = Subject;
}
public void setAnnualSalary(double AnnualSalary)
{
myAnnualSalary = AnnualSalary;
}
public String toString()
{
return super.toString() + "Subject:" + mySubject + "Annual Salary:" + myAnnualSalary;
}
}
STUDENT CLASS
public class Student extends Person
{
private String myIdNum; // Student Id Number
private double myGPA; // grade point average
// constructor
public Student(String name, int age, String gender,
String idNum, double gpa)
{
// use the super class' constructor
super(name, age, gender);
// initialize what's new to Student
myIdNum = idNum;
myGPA = gpa;
}
public String getIdNum()
{
return myIdNum;
}
public double getGPA()
{
return myGPA;
}
public void setIdNum(String idNum)
{
myIdNum = idNum;
}
public void setGPA(double gpa)
{
myGPA = gpa;
}
// overrides the toString method in the parent class
public String toString()
{
return super.toString() + ", student id: " + myIdNum + ", gpa: " + myGPA;
}
}
COLLEGE STUDENT CLASS
public class CollegeStudent extends Student
{
// instance variables - replace the example below with your own
private int myYear;
private String myMajor;
/**
* Constructor for objects of class dasf
*/
public CollegeStudent(String idNum, double gpa, String name, int age, String gender, int Year, String Major)
{
// initialise instance variables
super(name, age, gender, idNum, gpa);
myMajor = Major;
myYear = Year;
}
/**
* An example of a method - replace this comment with your own
*
* #param y a sample parameter for a method
* #return the sum of x and y
*/
public String getMajor()
{
return myMajor;
}
public int getYear()
{
return myYear;
}
public void setMajor(String Major)
{
myMajor = Major;
}
public void setYear(int Year)
{
myYear = Year;
}
public String toString()
{
return super.toString() + "Year:" + myYear + "Major:" + myMajor;
}
}
// Your call:
CollegeStudent ima = new CollegeStudent("Ima Frosh", 18, "F", "UCB123",
4.0, 1, "English");
// The constructor
public CollegeStudent(String idNum, double gpa, String name,
int age, String gender, int Year, String Major)
As you can see the constructor takes a lot of arguments, and the types matter when doing this.
The types it wants are: String, double, String, int, String, int, String.
However, you are passing: String, int (ok), String, String, double, int, String.
I think you just have the arguments all jumbled, try this:
CollegeStudent ima = new CollegeStudent("UCB123", 4.0, "Ima Frosh", 18,
"F", 1, "English");
..aaand at the end put it like this:
return super.toString() + ", Year: " + myYear + ", Major: " + myMajor;

Need help creating Student Info Code

I need help creating a Program
Must Include:
1. Student
a. Must have
i. Student name (first and last)
ii. Birthdate
iii. ID #
iv. Grade (Freshman, sophomore, etc.)
v. Class Schedule (an array?)
vi. 3 constructors of your choice (must include a default)
vii. Getter and Setter methods for all instance variables
1. This means that you have at least 2 methods for each instance variable in order to be able to get and set those variables.
2. The methods will be public, so that they can be used outside the class, and the instance variables will be private, so that they are not broken by the users of your class.
viii. Proper documentation for all methods
This is what i have so far
public class StudentTester
{
public static void main(String[] args)
{
Student someGuy = new Student(); //This creates a new student using the defalt constructor
Student someGal = new Student("Amanda","Soh"); //This creates a new student and automatically sets the firt and last name variables
String justAHolder = someGuy.getFullName();
String justAnotherHolder = someGal.getFullName();
System.out.print(someGal.getFullName());
someGuy.setMonth(11);
someGal.setMonth(14);
//If you uncomment the next line and try and compile the program, you will be able to see how it generates an error
//someGal.grade=12; //NO, NO, No, grade is supposed to be private!!!!!
}
/**
* Constructor for objects of class Student
*/
public Student()
{
// initialise instance variables
fName = "John";
lName = "Doe";
}
/**
* This is my second constructor example that students name
*
* #param fName This is the input for the first name
* #param lName This is the input for the last name
*/
public Student(String fName, String lName)
{
this();
this.fName = fName;
this.lName = lName;
}
//Here are the Getter Methods
/**
* This is the getter method to get the full name
*
* #return This returns the first and last name as a single String
*/
public String getFullName()
{
return fName+" "+lName;
}
//Here are the Setter Methods
/**
* This is the method to set the month of the students birthday
*
* #param newMonth The new month to set the birthdate with
*/
public void setMonth(int newMonth)
{
if(newMonth>0 && newMonth<13)
{
bDayMonth = newMonth;
}else
{
System.out.println("No one is born in Octovember!");
}
}
}
Your Student class :
package com.test;
public class Student {
public Student() {
firstName = "John";
lastName = "Doe";
birthDate = "05/11/1990";
}
public Student(String fName, String lName, String dob, String sGrade,
String[] classSchedule) {
firstName = fName;
lastName = lName;
birthDate = dob;
grade = sGrade;
schedule = classSchedule;
}
private String firstName;
private String lastName;
private String birthDate;
private String grade;
private String[] schedule;
/**
* #return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* #param firstName
* the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* #return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* #param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* #return the birthDate
*/
public String getBirthDate() {
return birthDate;
}
/**
* #param birthDate
* the birthDate to set
*/
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
/**
* #return the grade
*/
public String getGrade() {
return grade;
}
/**
* #param grade
* the grade to set
*/
public void setGrade(String grade) {
this.grade = grade;
}
/**
* #return the schedule
*/
public String[] getSchedule() {
return schedule;
}
/**
* #param schedule
* the schedule to set
*/
public void setSchedule(String[] schedule) {
this.schedule = schedule;
}
public String getFullName() {
return firstName + " " + lastName;
}
}
Then the tester class:
package com.test;
public class StudentTester {
/**
* #param args
*/
public static void main(String[] args) {
Student st1 = new Student(); // This creates a new student using the
// defalt constructor
String[] sch = { "Math 10.30", "Physics 14.00" };
Student st2 = new Student("Amanda", "Soh", "10/12/1990", "Freshman",
sch);// This creates a new student and automatically sets all
// attributes
System.out.println("First Student is: " + st1.getFullName());
System.out.println("Details of 2nd student: " + st2.getFullName() + " "
+ st2.getBirthDate() + " " + st2.getGrade() + " "
+ st2.getSchedule()[0] + " " + st2.getSchedule()[1]);
}
}

Categories