Results 1 to 11 of 11
  1. #1
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Java Help Please

    Ok so im having trouble with this problem.


    OBJECTIVE: A.) To differentiate between declaring and assigning variables

    B.) To be able to assign values to variables using dot notation



    Using tonight’s code example please write a program that prints out your Student statistics including:
    firstName
    lastName
    numberOfClasses
    program
    You should create a method that prints the above information
    Save the file under the name Student and compile it. (It won’t run because it doesn’t have a main())
    Write a second class called Registrar. Registrar should have one method called addStudent. In the addStudent() method create 2 new Student objects, assign values to their member variables using dot notation, and call their printInfo() method using dot notation
    Then write the main() method. In the main() create/instatiate a registrar object and call its addStudent() method.
    Save, Compile and Run Registrar

    this is what i have so far.....


    public class Student {
    String firstName;
    String lastName;
    int numberOfClasses;

    public Student() {
    firstName = "";
    lastName = "";
    numberOfClasses = 0;
    }
    public String printInfo() {
    String info = firstName + "\t " + lastName + "\tclasses: " + numberOfClasses";

    return info;
    }
    {
    //>=======
    public class Registrar {

    public static void main ( String [] args ) {
    new Registrar().addStudent();
    }
    private void addStudent() {
    Student warmbody_1 = new Student();
    warmbody_1.firstName ="Sara";
    warmbody_1.lastname = "Pawlen";
    warmbody_1.numberOfClasses = 1;

    Student warmbody_2 = new Student();
    warmbody_2.firstName = "Cowboy";
    warmbody_2.lastName = "Bob";
    warmbody_2.numberOfClasses = 22;

    System.out.println( warmbody_1.printInfo() );
    System.out.println( warmbody_2.printInfo() );
    }
    }

    its giving me an error with the line (String info = firstName + "\t " + lastName + "\tclasses: " + numberOfClasses";) and i cannot figure out why. Please help!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What is the error? Often these things are quite descriptive.

    (In this case it is that the "-signs don't match. They have to come in pairs, but you have a stray one at the end.)

  3. #3
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    Student.java:15: unclosed string literal
    String info = firstName + "\t " + lastName + "\tclasses: " + numberOfClasses";
    ^
    Student.java:19: reached end of file while parsing
    {
    ^

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    > Student.java:15: unclosed string literal

    Thanks. Things like "\t" and "\tclasses:" are called string literals. The compiler is complaining about the one at the end of the line. Just remove it.

    The other error refers to the stray { that appears at the end of Student.java. Again it should be removed.

  5. #5
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    im not sure what needs to be removed though.

  6. #6
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    ok i fixed the problem with the string literals but then i remove the } it gives me another error telling me to remove the other } then i remove that and it gives me an error with the return info;

  7. #7
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    Student.java:16: reached end of file while parsing
    {
    ^
    Registrar.java:12: cannot find symbol
    symbol : variable lastname
    location: class Student
    warmbody_1.lastname = "DiBiasio";
    ^

  8. #8
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    i fixed he lastname problem.

    it just the return info

  9. #9
    md69holla is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default

    nevermind i found out what was wrong. i had a open bracket instead of a closing bracket.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I'm glad you've got it sorted out. Mostly these things are a matter of patiently working through each of the errors generated by the compiler. (actually understanding the problem, not just trying to work around it.)

  11. #11
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •