Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: public expect statement error

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default public expect statement error

    The assignment is to :
    /*
    * 59. Write a class encapsulating the concept of a student, assuming a student
    * has the following attributes: a name, a social security number, and a GPA
    * (for instance, 3.5). Include a constructor, the assessors and mutators, and
    * methods to String and equals. Write a client class to test all the methods
    * in your class.
    */
    I have this code but it is giving me an error at
        public Student(String n, int soc, float g) {
            name = n;
            soc = soc;
            float = g;
        }
    Says that float = g is not a expect statement ";" and says soc = soc can't be to itself. I saw a way to fix this in my book but now i can't remember where to find what i should do to fix the soc= soc. Not sure at all how to fix the float = g;

    Here is the entirety of the code
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package natashaepperson_chapter7_exercise_59;
     
     
    public class Student{
        private String name;
        private int SSN;
        private Float GPA;
     
        public Student(String n, int soc, float g) {
            name = n;
            soc = soc;
            float = g;
        }
    public String getName() {
        return name;
    }
    // Create 2 more getter methods for SSN and GPA.
     
    protected void setName(String s) 
    {
    this.name = s;
    }
    // Create two more setter methods for SSN and GPA.
     
    // I have it returning each item on its own line.
    @Override public String toString() 
    {
        return(name + "\n" + SSN + "\n" + GPA);
    }
    public boolean equals(Student s) {
        if(!(s instanceof Student)) {
            return true;
    } return false;
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: public expect statement error

    this.soc = soc;

    And float is a keyword and can't be used as a variable name.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    nepperso (November 23rd, 2013)

  4. #3
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: public expect statement error

    hrm it's not letting me use this.soc = soc; gives error this symbol variable doesn't exist. But it's in the public Student(String n, int soc, float g) {
    i think maybe it should be public Student(String n, int SSN, float g) { ? change the int soc to SSN? that's what it's referencing right?

    Okay i changed the code a bit:
    /*
     * 59. Write a class encapsulating the concept of a student, assuming a student
     * has the following attributes: a name, a social security number, and a GPA 
     * (for instance, 3.5). Include a constructor, the assessors and mutators, and 
     * methods to String and equals. Write a client class to test all the methods 
     * in your class.
     * Natasha Epperson
     */
    package natashaepperson_chapter7_exercise_59;
     
     
    public class Student{
        private String name;
        private int SSN;
        private Float GPA;
     
        public Student(String n, int s, float g) {
            name = n;
            this.SSN = s;
     
        }
    public String getName() {
        return name;
    }
    // Create 2 more getter methods for SSN and GPA.
     
    protected void setName(String s) 
    {
    this.name = s;
    }
    // Create two more setter methods for SSN and GPA.
     
    // I have it returning each item on its own line.
    @Override public String toString() 
    {
        return(name + "\n" + SSN + "\n" + GPA);
    }
    public boolean equals(Student s) {
        if(!(s instanceof Student)) {
            return true;
    } return false;
    }
    }

    But when I run it, while it provides no errors it doesn't do anything, just says successful build.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: public expect statement error

    You're right. I didn't really look at your code. And you probably mean:

    private float GPA;

    and then

    GPA = g;

    The capitalized Float will work thanks to autoboxing, but I don't think that's what you meant to use.

    Single letter variable names are annoying. Stop it. Give your variables decent names.

    A class with no main() method should not be expected to do something. Do you have a driver class to test it? Something with a main() method?

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    nepperso (November 23rd, 2013)

  7. #5
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: public expect statement error

    ahh okay. no i hadn't used a driver class to test it. I wasn't sure how to do that yet I mean i know how to use import java class method and use main () methods. But not sure how to call a class i created yet?

    And that seems to fix it so that i'm not getting errors:
    package natashaepperson_chapter7_exercise_59;
     
     
    public class Student{
        private String name;
        private int SSN;
        private Float GPA;
     
        public Student(String n, int s, float g) {
            name = n;
            this.SSN = s;
            GPA = g;
     
        }
    public String getName() {
        return name;
    }
    // Create 2 more getter methods for SSN and GPA.
     
    protected void setName(String s) 
    {
    this.name = s;
    }
    // Create two more setter methods for SSN and GPA.
     
    // I have it returning each item on its own line.
    @Override public String toString() 
    {
        return(name + "\n" + SSN + "\n" + GPA);
    }
    public boolean equals(Student s) {
        if(!(s instanceof Student)) {
            return true;
    } return false;
    }
    }

  8. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,733
    My Mood
    Mellow
    Thanks
    232
    Thanked 706 Times in 692 Posts

    Default Re: public expect statement error

    If the test class is in the same package (and why wouldn't it be?) then you don't need to import anything.

    The test class will have a main() method that will either call other methods or will itself create Student objects, something like:

    Student studentX = new Student( "Name", someInteger, someFloat );

    And then use all of the Student methods to verify that they work or that they need fixing. There should be outputs to describe what is being tested with the results. For example, testing the equals() method, the tester would say that the equals() method is being tested and then output the results of studentX.equals( studentX ) and studentX.equals( studentY ) and perhaps using studentY as the subject and studentX as the parameter. Test the class methods thoroughly and describe exactly what is being tested and what the results are.

  9. The Following User Says Thank You to GregBrannon For This Useful Post:

    nepperso (November 23rd, 2013)

  10. #7
    Member
    Join Date
    Sep 2013
    Posts
    35
    My Mood
    Goofy
    Thanks
    18
    Thanked 0 Times in 0 Posts

    Default Re: public expect statement error

    awesome okay thanks !

Similar Threads

  1. public void run () error message
    By Niquinho in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 23rd, 2012, 01:22 AM
  2. My JPanels don's stack the way I expect them to
    By amcqueen in forum AWT / Java Swing
    Replies: 4
    Last Post: September 15th, 2011, 09:08 AM
  3. Need Expect Help and Advice
    By Addo kofi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 29th, 2011, 08:41 PM
  4. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  5. Public class help/error
    By Plural in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 05:22 PM

Tags for this Thread