12 Replies - 10853 Views - Last Post: 23 February 2009 - 08:24 AM Rate Topic: -----

#1 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

get/set methods with constructor

Post icon  Posted 22 February 2009 - 05:49 PM

so this is my code and the point is to learn get/set methods. the part where i get confused is in assigning the name and number using get methods and the idea of a constructor. what am i doing wrong?

public class BicycleTest {

	public static void main( String[] args ) 
	{
	
				// Create 1 Bicycle reference variable
	
		Bicycle bike;
		
	// Create 1 String reference variable for the owner's name
	
		String name;
		
	// Create 1 integer variable for the license number
	
		int number;
		
	// Assign your full name and a license number to the String and
	// integer variables
	
		name = "Josh";
		number = 12345;
		
	
		
	// Create a Bicycle object with the Bicycle constructor 
	// Use the variables you created as arguments to the constructor
	
		Bicycle bike = new Bicycle("Josh", 12345);
	
	
	  // Output the owner's name and license number in printf statements
	// using the object reference and the get methods.  
	// For example: bike.getOwnerName()
		
			System.out.printf("The owner is %s\n", bike.getownerName());
			System.out.printf("The license number is %d\n", bike.getlicenseNumber());
		
	
	
			bike.setownerName(); {
				ownerName = "Kenny McCormick";
			}
		
	  
	
			System.out.printf("The owner is %s\n", bike.getownerName());
			System.out.printf("The license number is %d\n", bike.getlicenseNumber()); 
}

}


these are the error messages that i keep getting:

bike is already defined in main(java.lang.String[])
cannot find symbol method getlicenseNumber()
cannot find symbol method getownerName()
cannot find symbol method setownerName()
cannot find symbol variable ownerName
cannot find symbol variable licenseNumber

This post has been edited by pyr0b0y: 22 February 2009 - 06:17 PM


Is This A Good Question/Topic? 0
  • +

Replies To: get/set methods with constructor

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get/set methods with constructor

Posted 22 February 2009 - 06:06 PM

Can't really help you if you don't post your Bike class code

and this looks weird to me

			bike.setownerName(); {
				ownerName = "Kenny McCormick";
			}



this would like more "standard"/"catholic" as we are dealing with Irish name :)

bike.setOwner("Kenny McCormick");
Was This Post Helpful? 0
  • +
  • -

#3 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: get/set methods with constructor

Posted 22 February 2009 - 06:08 PM

bicycle class code:

/*
	File: Bicycle.java
*/

public class Bicycle {

	// Instance field
	private String ownerName;
	private int licenseNumber;
	
	// Constructor
	public Bicycle( String name, int license ) {
		ownerName = name;
	  licenseNumber = license;
	}
   
	// Returns the name of this bicycle's owner
	public String getOwnerName( ) {
		
		return ownerName;
	}

	// Assigns the name of this bicycle's owner
	public void setOwnerName( String name ) {
	
		ownerName = name;
	}   

	// Returns the license number of this bicycle
	public int getLicenseNumber( ) {
		
		return licenseNumber;
	}

	// Assigns the license number of this bicycle
	public void setLicenseNumber( int license ) {
	
		licenseNumber = license;
	}	
 
 }


by the way, one of the instructions is to change the owner to that name...its an assignment my teacher gave me

This post has been edited by pyr0b0y: 22 February 2009 - 06:09 PM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get/set methods with constructor

Posted 22 February 2009 - 06:18 PM

Exactly what I said:

Your class Bike as a setter for the owner:

 // Assigns the name of this bicycle's owner
  public void setOwnerName( String name ) {   
		ownerName = name;
 }


Was This Post Helpful? 0
  • +
  • -

#5 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: get/set methods with constructor

Posted 22 February 2009 - 06:20 PM

im sorry, i just started taking java programming and im kind of lost, i want to know how to correctly use get/set methods and constructors. ive looked online and in a couple of my textbooks for a couple hours and it is not making sense. i should be able to use

 bike.setownerName();


right?

This post has been edited by pyr0b0y: 22 February 2009 - 06:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get/set methods with constructor

Posted 22 February 2009 - 06:23 PM

this would actually use the set method for the Byke owner and change the owner name

bike.setOwner("Kenny McCormick");
Was This Post Helpful? 0
  • +
  • -

#7 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: get/set methods with constructor

Posted 22 February 2009 - 06:23 PM

Well first of all you don't have a constructor. Your constructor right now is set to null... If you want to have a constructor it would go above your main and would include the same name as your class.

haha woops responded to that a little late...

This post has been edited by markhazlett9: 22 February 2009 - 06:26 PM

Was This Post Helpful? 0
  • +
  • -

#8 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: get/set methods with constructor

Posted 22 February 2009 - 06:26 PM

this is the guide that i am supposed to follow:

/*
	File: BicycleTest.java
*/

public class BicycleTest {

	public static void main( String[] args ) 
	{
		
				
	// Create 1 Bicycle reference variable
	
		
	// Create 1 String reference variable for the owner's name
	
		
		
	// Create 1 integer variable for the license number
	
		
	// Assign your full name and a license number to the String and
	// integer variables
	
		
	// Create a Bicycle object with the Bicycle constructor 
	// Use the variables you created as arguments to the constructor
		
			
	
	  // Output the owner's name and license number in printf statements
	// using the object reference and the get methods.  
	// For example: bike.getOwnerName()
		
		
		
	// Change the owner's name to Kenny McCormick using setOwnerName 
	
			
		
	  // Output the owner's name and license number in printf statements
	// using the Bicycle object reference and the get methods. 
	
		
		
	}   		
}


so the constructor would be

Bicycle bike = new Bicycle();


is that correct? if not can you provide and example using constructor and a set method? and why does it say to start by defining a reference variable? do i need to add the constructor at the top then?

This post has been edited by pyr0b0y: 22 February 2009 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get/set methods with constructor

Posted 22 February 2009 - 07:21 PM

Here is an example with all the setters and getters

// An example
// The class bike has an onwer as a String and a licenseId
public class Bike {
	String owner;
	int licenseId;
	// the constructor of Bike receives an Owner and a LicenseId
	Bike(String owner, int licenseId) {
		this.owner = owner;
		this.licenseId = licenseId;
	}
	// bike has setter and getter for Onwe and linceId
	public int getLicenseId() {
		return licenseId;
	}
	public void setLicenseId(int licenseId) {
		this.licenseId = licenseId;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	// bike has a toString method to display what it contains
	public String toString() {
		return "Bike is owned by " + getOwner() + " license is: " + getLicenseId();
	}
	// a main method to test all that stuff
	public static void main(String[] arg) {
		// create a bike
		Bike b = new Bike("Jogn Smith", 123456);
		// display it
		System.out.println("Bike 1: " + b);
		// change onwer
		b.setOwner("Barak Obama");
		System.out.println("Bike 2: " + b);
		// change license
		b.setLicenseId(314159);
		System.out.println("Bike 3: " + b);
	}
}



which yields

Bike 1: Bike is owned by Jogn Smith license is: 123456
Bike 2: Bike is owned by Barak Obama license is: 123456
Bike 2: Bike is owned by Barak Obama license is: 314159
Was This Post Helpful? 1
  • +
  • -

#10 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: get/set methods with constructor

Posted 22 February 2009 - 08:16 PM

thanks, and just to double check, if i needed to incorporate the get methods into the output, it would be

System.out.printf("The owner is %s\n", b.getowner);


by stating b.getowner, im calling owner, which is "barak obama" correct?

and if i wanted to get the name from input, it would be

Scanner input = new Scanner (System.in);
		 System.out.println("Please enter the new owner's name: ");
		 String theInput = input.nextline();
		 b.setowner(theInput);


so the second line of code will read the input of the user then the third line will store the input as the new owner right?

This post has been edited by pyr0b0y: 22 February 2009 - 08:36 PM

Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: get/set methods with constructor

Posted 22 February 2009 - 08:25 PM

View Postpyr0b0y, on 22 Feb, 2009 - 07:16 PM, said:

thanks, you actually do not know how much that helped me out, the textbook doesnt do so great of a job. i really appreciate you helping me out with that....and its a good thing because i have an assignment where i need to use this concept. THANKS! :D

You're welcome
Was This Post Helpful? 0
  • +
  • -

#12 pyr0b0y  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 44
  • Joined: 22-February 09

Re: get/set methods with constructor

Posted 22 February 2009 - 11:41 PM

ok so i rewrote the code
/*
	File: BicycleTest.java
*/

public class BicycleTest {

	public static void main( String[] args ) {
	
	// Create 1 Bicycle reference variable
	
		Bicycle myBike;
		
	// Create 1 String reference variable for the owner's name
	
		String ownerName;
		
	// Create 1 integer variable for the license number
	
		int licenseNumber;
		
	// Assign your full name and a license number to the String and
	// integer variables
	
		ownerName = "Josh Mac";
		licenseNumber = 12345;
		
	// Create a Bicycle object with the Bicycle constructor 
	// Use the variables you created as arguments to the constructor
	
		 Bicycle bike = new Bicycle(ownerName, licenseNumber);
		 
	  // Output the owner's name and license number in printf statements
	// using the object reference and the get methods.  
	// For example: bike.getOwnerName()
	
		System.out.printf("The owner's name is %s\n", bike.getOwnerName());
		System.out.printf("The license number is %d\n", bike.getLicenseNumber());
		
	// Change the owner's name to Kenny McCormick using setOwnerName 
	
		bike.setOwnerName("Kenny McCormick");
		
	  // Output the owner's name and license number in printf statements
	// using the Bicycle object reference and the get methods.
	
		System.out.printf("The new owner is %s\n", bike.getOwnerName());
		System.out.printf("The license number is %d\n", bike.getLicenseNumber());  

	}
}


the only thing i cant understand is why after i set the new name, i still get the output as the orginal. after i set the new name, when i call OwnerName(), it should display the new name.

This post has been edited by pyr0b0y: 22 February 2009 - 11:55 PM

Was This Post Helpful? 0
  • +
  • -

#13 Lucrosen  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 21
  • Joined: 11-February 09

Re: get/set methods with constructor

Posted 23 February 2009 - 08:24 AM

I went through your code using these 2 files
public class BicycleTest {

	public static void main( String[] args ) {
	
	// Create 1 Bicycle reference variable
	
		Bicycle myBike;
		
	// Create 1 String reference variable for the owner's name
	
		String ownerName;
		
	// Create 1 integer variable for the license number
	
		int licenseNumber;
		
	// Assign your full name and a license number to the String and
	// integer variables
	
		ownerName = "Josh Mac";
		licenseNumber = 12345;
		
	// Create a Bicycle object with the Bicycle constructor 
	// Use the variables you created as arguments to the constructor
	
		 Bicycle bike = new Bicycle(ownerName, licenseNumber);
		 
	  // Output the owner's name and license number in printf statements
	// using the object reference and the get methods.  
	// For example: bike.getOwnerName()
	
		System.out.printf("The owner's name is %s\n", bike.getOwner());
		System.out.printf("The license number is %d\n", bike.getLicenseId());
		
	// Change the owner's name to Kenny McCormick using setOwnerName 
	
		bike.setOwner("Kenny McCormick");
		
	  // Output the owner's name and license number in printf statements
	// using the Bicycle object reference and the get methods.
	
		System.out.printf("The new owner is %s\n", bike.getOwner());
		System.out.printf("The license number is %d\n", bike.getLicenseId());  

	}
}

and
// An example
// The class bike has an onwer as a String and a licenseId
public class Bicycle {
	String owner;
	int licenseId;
	// the constructor of Bike receives an Owner and a LicenseId
	Bicycle(String owner, int licenseId) {
		this.owner = owner;
		this.licenseId = licenseId;
	}
	// bike has setter and getter for Onwe and linceId
	public int getLicenseId() {
		return licenseId;
	}
	public void setLicenseId(int licenseId) {
		this.licenseId = licenseId;
	}
	public String getOwner() {
		return owner;
	}
	public void setOwner(String owner) {
		this.owner = owner;
	}
	// bike has a toString method to display what it contains
	public String toString() {
		return "Bike is owned by " + getOwner() + " license is: " + getLicenseId();
	}
	// a main method to test all that stuff
	public static void main(String[] arg) {
		// create a bike
		Bike b = new Bike("Jogn Smith", 123456);
		// display it
		System.out.println("Bike 1: " + b);
		// change onwer
		b.setOwner("Barak Obama");
		System.out.println("Bike 2: " + b);
		// change license
		b.setLicenseId(314159);
		System.out.println("Bike 3: " + b);
	}
}

I didn't get any errors. after i did just a little debugging. there were 2 different names for set and get methods. It correctly displayed the names before and after the change
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1