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 5 of 5

Thread: Why does OOP hate me so much- can not get my driver to work

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why does OOP hate me so much- can not get my driver to work

    Ok, so here is my class

    public class Student
    {
      private String name;  // student name
      private int id;       // student id
      //*********************************************************
      public void setName(String n)
      {
        this.name = n;
      }
      public void setId(int id)
      {
        this.id = id;
      }
      //*********************************************************
      public String getEmailAccount()
      {
        // Include "" in concatenation to convert to strings.
        return "" + this.name.charAt(0) + this.id +
          "@park.edu";
      }
      //*********************************************************
      public boolean isValid()
      {
        return this.id >= 100000 && this.id <= 999999;
      }
    } // end class Student

    Here is my driver
    import java.util.Scanner;
     
    public class StudentDriver
    {
      public static void main(String[] args)
      {
        Scanner stdIn = new Scanner(System.in);
        Student student;  // student object
        String name;      // student name
     
        // Instantiate Student object and assign it to student.
        student = new Student();
     
        System.out.print("Enter student name: ");
        name = stdIn.nextLine();
     
        // Assign name to the student object.
        student.setName();
     
        System.out.print("Enter student id: ");
        // In a single statement, read an int for the id value,
        // and assign it to the student object.
         name.setId()= stdIn.nextInt();
     
        // If invalid id, execute the loop.
        // (Use the isValid method in the while loop heading.)
        while (!isValid)
        {
          System.out.print("Invalid student id - reenter: ");
          // In a single statement, read an int for the id value
          // and assign it to the student object.
           student.SetId()= stdIn.nextInt();
        }
     
        System.out.println("\n" + name +
          ", your new e-mail account is: \n" +
          student.getEmailAccount());                   // Get email account.
      } // end main
    } // end class StudentDriver

    And here are my errors
    C:\Users\Jennifer\Documents\school\StudentDriver.java:18: error: method setName in class Student cannot be applied to given types;
        student.setName();
               ^
      required: String
      found: no arguments
      reason: actual and formal argument lists differ in length
    C:\Users\Jennifer\Documents\school\StudentDriver.java:23: error: cannot find symbol
         name.setId()= stdIn.nextInt();
             ^
      symbol:   method setId()
      location: variable name of type String
    C:\Users\Jennifer\Documents\school\StudentDriver.java:27: error: cannot find symbol
        while (!isValid)
                ^
      symbol:   variable isValid
      location: class StudentDriver
    C:\Users\Jennifer\Documents\school\StudentDriver.java:32: error: cannot find symbol
           student.SetId()= stdIn.nextInt();
                  ^
      symbol:   method SetId()
      location: variable student of type Student
    4 errors
     
    Tool completed with exit code 1

    I can't figure out what I'm doing wrong. The comments in the program are what I need to do and my attempt at the code is beneath them. My first issue is that I can't figure out how to set the name. I believe that I need to use the student variable name because that is what I'm trying to assign the name to. However, any way I try to modify the statement it doesn't work unless I go student.setName(name). Is that really the correct way to use the code? Doesn't that sort of negate the setName Method? Do I need to do the same with the setID?

    The boolean is giving me some issues. I want to do something like while(!student.isvalid()) but will that work with the student variable name?

    Hopefully I'm making some realtively simple errors, because this is driving me crazy...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Why does OOP hate me so much- can not get my driver to work

    The setName() method requires an argument, a String, that provides the name:

    student.setName( "Sally" );

    Back away form a battle with OOP and "go with the flow." Look at the Student methods and realize what they require to be used. Also, consider the error message you posted that should have been very helpful:

    C:\Users\Jennifer\Documents\school\StudentDriver.j ava:18: error: method setName in class Student cannot be applied to given types;
    student.setName();
    ^
    required: String
    found: no arguments
    reason: actual and formal argument lists differ in length
    It's telling you that the method setName() requires a String but it didn't find one. Make sense?

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

    kissyfurs (July 14th, 2013)

  4. #3
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why does OOP hate me so much- can not get my driver to work

    So I can use student.setName (name) and that's correct? Isn't the purpose of the setName Method to set the name? Why do I have to have something in the parenthesis?

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Why does OOP hate me so much- can not get my driver to work

    You put something in the parentheses so that the setName() method knows what the student's name should be set to.

    Notice that the fact that you have a variable name in the main() method is neither here nor there to the compiler. It is not going to guess your intention and use name as the name you want for the student.

    I find that it's a good idea to document methods.

        /**
         * Sets the student's name to a given value.
         */
    public void setName(String n)
    {
        this.name = n;
    }

    In this case the comment makes it clear that the behaviour of setName() is to set the student's name to some value given when the method is called.

  6. #5
    Junior Member
    Join Date
    Jul 2013
    Location
    United States
    Posts
    16
    My Mood
    Lurking
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why does OOP hate me so much- can not get my driver to work

    I think I understand. I guess I was expecting the computer to do too much for me. I applied the corrections and updated my boolean according to my original post and it worked correctly, along with a few other changes. Thanks for your help.

Similar Threads

  1. A beginner needs help in oop
    By kinkita in forum Object Oriented Programming
    Replies: 42
    Last Post: May 13th, 2013, 11:02 AM
  2. How to learn OOP
    By Alex L in forum Object Oriented Programming
    Replies: 7
    Last Post: December 8th, 2011, 11:53 PM
  3. OOP help!
    By imaznumkay in forum Object Oriented Programming
    Replies: 3
    Last Post: July 11th, 2011, 01:43 PM
  4. OOP
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 2
    Last Post: November 29th, 2009, 10:10 PM