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

Thread: Quick Question: Did I understand these directions correctly?

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Quick Question: Did I understand these directions correctly?

    I'm new to java. I just need a little clarification.

    1. Create a class called Student that has name, grade, house as instance variables

    2. Create a constructor that take these (3) items as parameters and sets them to your private instance/global variables


    My code:

    public class Student {
     
    	String name;
    	int grade;
    	String house;
     
     
     
     
    	public Student(String studentName, int studentGrade, String studentHouse)
    	{
    		name = studentName;
    		grade = studentGrade;
    		house = studentHouse;
     
    	}


    The one thing that I'm confused about is that I looked at the java tutorial for making constructors. It was a bicycle example. The instance variables for the Bicycle class (public int cadence, public int gear, public int speed) were NOT the same as the parameters in the constructor(int startCadence, int startSpeed, int startGear). Why is this? My assignment seems to want me to make my constructor parameters the same as my instance variables.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Quick Question: Did I understand these directions correctly?

    The same names you mean? You could pass this as a parameter, not as a formal parameter, but when calling it:

    Student s = new Student("Bob", 100, "Smith");

    It doesn't have to be the same name when calling it as an object.

    Actually, they do have to be same data type and in the same order.

    Also, you can't do this in a class for instance:

    public JSeparatedMenu(int value)
    {


    }

    public JSeparatedMenu(int value2)
    {


    }

    Though the two constructors have different variable names, they both have only one variable, an int one in each case. It won't compile as it already says I have a constructor that takes an int as its only parameter.
    Last edited by javapenguin; June 3rd, 2012 at 03:06 PM.

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

    msinc210 (June 3rd, 2012)

Similar Threads

  1. Quick question
    By l1nk3 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2012, 02:02 PM
  2. quick question, please HELP!
    By Nemus in forum What's Wrong With My Code?
    Replies: 14
    Last Post: September 29th, 2011, 01:23 PM
  3. Quick Question
    By sird00dguyman in forum Java Theory & Questions
    Replies: 2
    Last Post: August 4th, 2011, 06:14 PM
  4. Another Quick Question
    By Jacksontbh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 1st, 2011, 07:18 AM
  5. Hi, quick question
    By curras in forum Member Introductions
    Replies: 1
    Last Post: March 21st, 2011, 03:21 PM