HelpBeginner in Java- Need Help With Project

Posts 1–4 of 4 · Page 1 of 1
Beginner in Java- Need Help With Project
Hi everyone!

First- thank you for taking a look at this forum, any help is greatly appreciated.

Here is what the instructions say:

Create a Java NetBeans project named StudentClient with the main class named StudentClient. Add a service class to the project named Student. This program will encapsulate the concept of a student, assuming a student has the following attributes (instance variables): a name, a social security number and a GPA (e.g., 3.5).For the Student class you will write the constructors, accessors and mutators, and toString and equals methods. In the client application class you will instantiate student class objects and write statements (method calls) to test the methods in your student service class.

I have 0 ideas on how to start this, and this is my biggest project (grade wise), so if you have any idea on how I can start it I will love you forever (no homo)

PS: I have another project in which I will upload the code to pastebin, I want to make sure I did that properly or not, and I will post a link back here along with the instructions in pastebin
This is what I came up with. If you need me to explain anything feel free to message me.

Code:
public class StudentClient {

    public static void main(String[] args) {
	Student mark = new Student("Mark", 555555555, 3.6);
        System.out.println(mark.toString());

        Student leslie = new Student();
        leslie.equals(mark);
        leslie.setName("Leslie");
        System.out.println(leslie.toString());
    }
}
Code:
public class Student {

    private String name;
    private int socialSecurityNumber;
    private double gpa; // grade point average

    private static final String DEFAULT_NAME = "Unregistered";
    private static final int DEFAULT_SOCIAL_SECURITY_NUMBER = 111111111;
    private static final int HIGHEST_SOCIAL_SECURITY_NUMBER = 999999999;
    private static final double LOWEST_GPA = 0.0;
    private static final double HIGHEST_GPA = 4.0;

    public String toString() {
        String information = "Name: " + name;
        information += "\n" + "Social Security Number: " + socialSecurityNumber;
        information += "\n" + "G.P.A: " + gpa;

        return information;
    }

    public void equals(Student student) {
        this.name = student.getName();
        this.socialSecurityNumber = student.getSocialSecurityNumber();
        this.gpa = student.getGpa();
    }

    // ================================== CONSTRUCTORS =========================================== //

    public Student() {
        this(DEFAULT_NAME, DEFAULT_SOCIAL_SECURITY_NUMBER, LOWEST_GPA);
    }

    public Student(String name) {
        this(name, DEFAULT_SOCIAL_SECURITY_NUMBER, LOWEST_GPA);
    }

    public Student(int socialSecurityNumber) {
        this(DEFAULT_NAME, socialSecurityNumber, LOWEST_GPA);
    }

    public Student(double gpa) {
        this(DEFAULT_NAME, DEFAULT_SOCIAL_SECURITY_NUMBER, gpa);
    }

    public Student(String name, int socialSecurityNumber) {
        this(name, socialSecurityNumber, LOWEST_GPA);
    }

    public Student(String name, double gpa) {
        this(name, DEFAULT_SOCIAL_SECURITY_NUMBER, gpa);
    }

    public Student(int socialSecurityNumber, double gpa) {
        this(DEFAULT_NAME, socialSecurityNumber, gpa);
    }

    public Student(String name, int socialSecurityNumber, double gpa) {

        // check to see if blank string was passed as the name
        // if it was then set name to default value otherwise set this.name = to string passed
        if (name.equals("")) {
            this.name = DEFAULT_NAME;
        }else {
            this.name = name;
        }

        // check that social is between 0 and 999999999
        // if not set it equal to default value
        if (socialSecurityNumber >= DEFAULT_SOCIAL_SECURITY_NUMBER && socialSecurityNumber <= HIGHEST_SOCIAL_SECURITY_NUMBER) {
            this.socialSecurityNumber = socialSecurityNumber;
        } else {
            this.socialSecurityNumber = DEFAULT_SOCIAL_SECURITY_NUMBER;
        }

        // ensure that the gpa passed to the method is between 0.0 and 4.0 inclusive
        // else set it equal to lowest value
        if (gpa >= LOWEST_GPA && gpa <= HIGHEST_GPA) {
            this.gpa = gpa;
        } else {
            this.gpa = LOWEST_GPA;
        }
    }

    // ============================ Getter and Setters ===================================== //

    public String getName() {
        return this.name;
    }

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

    public int getSocialSecurityNumber() {
        return this.socialSecurityNumber;
    }

    public void setSocialSecurityNumber(int socialSecurityNumber) {
        this.socialSecurityNumber = socialSecurityNumber;
    }

    public double getGpa() {
        return this.gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
}
- - - Updated - - -

Here was the output from the code above:

Name: Mark
Social Security Number: 555555555
G.P.A: 3.6
Name: Leslie
Social Security Number: 555555555
G.P.A: 3.6

Process finished with exit code 0
Quote Originally Posted by Cosmo_ View Post
This is what I came up with. If you need me to explain anything feel free to message me.

Code:
public class StudentClient {

    public static void main(String[] args) {
	Student mark = new Student("Mark", 555555555, 3.6);
        System.out.println(mark.toString());

        Student leslie = new Student();
        leslie.equals(mark);
        leslie.setName("Leslie");
        System.out.println(leslie.toString());
    }
}
Code:
public class Student {

    private String name;
    private int socialSecurityNumber;
    private double gpa; // grade point average

    private static final String DEFAULT_NAME = "Unregistered";
    private static final int DEFAULT_SOCIAL_SECURITY_NUMBER = 111111111;
    private static final int HIGHEST_SOCIAL_SECURITY_NUMBER = 999999999;
    private static final double LOWEST_GPA = 0.0;
    private static final double HIGHEST_GPA = 4.0;

    public String toString() {
        String information = "Name: " + name;
        information += "\n" + "Social Security Number: " + socialSecurityNumber;
        information += "\n" + "G.P.A: " + gpa;

        return information;
    }

    public void equals(Student student) {
        this.name = student.getName();
        this.socialSecurityNumber = student.getSocialSecurityNumber();
        this.gpa = student.getGpa();
    }

    // ================================== CONSTRUCTORS =========================================== //

    public Student() {
        this(DEFAULT_NAME, DEFAULT_SOCIAL_SECURITY_NUMBER, LOWEST_GPA);
    }

    public Student(String name) {
        this(name, DEFAULT_SOCIAL_SECURITY_NUMBER, LOWEST_GPA);
    }

    public Student(int socialSecurityNumber) {
        this(DEFAULT_NAME, socialSecurityNumber, LOWEST_GPA);
    }

    public Student(double gpa) {
        this(DEFAULT_NAME, DEFAULT_SOCIAL_SECURITY_NUMBER, gpa);
    }

    public Student(String name, int socialSecurityNumber) {
        this(name, socialSecurityNumber, LOWEST_GPA);
    }

    public Student(String name, double gpa) {
        this(name, DEFAULT_SOCIAL_SECURITY_NUMBER, gpa);
    }

    public Student(int socialSecurityNumber, double gpa) {
        this(DEFAULT_NAME, socialSecurityNumber, gpa);
    }

    public Student(String name, int socialSecurityNumber, double gpa) {

        // check to see if blank string was passed as the name
        // if it was then set name to default value otherwise set this.name = to string passed
        if (name.equals("")) {
            this.name = DEFAULT_NAME;
        }else {
            this.name = name;
        }

        // check that social is between 0 and 999999999
        // if not set it equal to default value
        if (socialSecurityNumber >= DEFAULT_SOCIAL_SECURITY_NUMBER && socialSecurityNumber <= HIGHEST_SOCIAL_SECURITY_NUMBER) {
            this.socialSecurityNumber = socialSecurityNumber;
        } else {
            this.socialSecurityNumber = DEFAULT_SOCIAL_SECURITY_NUMBER;
        }

        // ensure that the gpa passed to the method is between 0.0 and 4.0 inclusive
        // else set it equal to lowest value
        if (gpa >= LOWEST_GPA && gpa <= HIGHEST_GPA) {
            this.gpa = gpa;
        } else {
            this.gpa = LOWEST_GPA;
        }
    }

    // ============================ Getter and Setters ===================================== //

    public String getName() {
        return this.name;
    }

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

    public int getSocialSecurityNumber() {
        return this.socialSecurityNumber;
    }

    public void setSocialSecurityNumber(int socialSecurityNumber) {
        this.socialSecurityNumber = socialSecurityNumber;
    }

    public double getGpa() {
        return this.gpa;
    }

    public void setGpa(double gpa) {
        this.gpa = gpa;
    }
}
- - - Updated - - -

Here was the output from the code above:

Name: Mark
Social Security Number: 555555555
G.P.A: 3.6
Name: Leslie
Social Security Number: 555555555
G.P.A: 3.6

Process finished with exit code 0
Wow I can't thank you enough!! I will try the code out once I get home. )

Btw nice user name my dogs name is also Cosmo :P
No problem and good luck!
Posts 1–4 of 4 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us