Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 22nd, 2009, 06:57 PM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Java Constructor issue

hi...my first post! so im learning java in college and am kind of getting confused. i need to assign name and number to 2 reference variables with the get/set method...ive tried lots of different ways of doing this, but i cant seem to get it because i keep coming up with syntax errors. these are the two files we are using as reference, i have to build the code from the BicycleTest.java file:

/*
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;
}

}



THIS IS THE FILE THAT TELLS ME HOW TO DO EVERYTHING.


/*
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.

}
}

THIS IS THE FILE THAT I FOLLOW TO BUILD MY CODE.


/*
File: BicycleTest.java
*/

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

bike.setownerName = "Josh";
bike.setlicenseNumber = 12345;

// Create a Bicycle object with the Bicycle constructor
// Use the variables you created as arguments to the constructor

//IM TOTALLY CONFUSED ABOUT WHAT TO DO HERE!!!!



// 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());

// Change the owner's name to Kenny McCormick using setOwnerName

bike.setownerName(); {
ownerName = "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 owner is %s\n", bike.getownerName());
System.out.printf("The license number is %d\n", bike.getlicenseNumber());
}
}
also it would be really nice if you could point me to a really good website that explains this in detail. the textbook that were are using is small java programming by deitel...i also have head first java which i like since it is visual. Thanks!
Reply With Quote
  #2 (permalink)  
Old February 23rd, 2009, 03:30 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Code:
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
          /* WRONG WRONG WRONG
         bike.setownerName = "Josh";
         bike.setlicenseNumber = 12345;
         Those are *NOT* the String and integer variables you just declared!!
         Technically, they aren't variables at all.  They are object properties.
          */
      name = "Josh"; // Assign your full name...to the String...variable
     number = 12345; // Assign you...license number to the...integer variable

     // Create a Bicycle object with the Bicycle constructor 
     // Use the variables you created as arguments to the constructor 
     bike = new Bicycle( name, number ); // just *EXACTLY* as required

     ...
The *ONLY* way to create an object in Java is via the new operator. Period.

And yes, you could have done
Code:
     bike = new Bicycle( ); // create an empty Bicycle object
     bike.setownerName = name; // fill in the two fields
     bike.setlicenseNumber = number;
but (1) that doesn't do what you were asked to do and (2) if the empty ("default") constructor for Bicycle is private, you wouldn't be able to create the empty object in the first place.

Does that help at all?
Reply With Quote
  #3 (permalink)  
Old February 26th, 2009, 03:47 PM
Registered User
 
Join Date: Feb 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default help!!!

[quote=Old Pedant;235365][code]
u must initialize the obj, check this out....
version 1


public static void main( String[] args )
{

Bicycle bike = new Bicycle( " Name", lic );// mark 1
/*here your got an initialize Object okay, here you can create an instance of the class Bicycle , an do your things...*/

/*also you can input your date from the IDE's console , the same for eclipse the sema for Net Beans... there are many ways of do that but here i going to show you a good one Scanner remember include this class on the top okay */

Scanner scan = new Scanner(System.in);
//this is a class that allows you to input data in this example you can substitute the //mark 1 doing this

String name = scan.next();//automatically get the name entered from the console
int lice = scan.nextInt();// the same for integer data types ok

//then your new y pruve mark 1 .....

Bicycle bike = new Bicycle( " name", lice );

// when you do this you can update your field an everithing you want to do

bike.setownerName("Neo");
/*changin name, but remember this is a mathod on you Bike class mus be declare like this
public void setownerName(String newName){

ownername = newName;
]

cause java works by reference the memory zone when your object in located , thats means if you change the variable name by using this method also change the variable on the Bike class...

my english is not good by i am a programer and is a plasure to help you

.-
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java library path issue elixiradeel Apache Tomcat 0 June 5th, 2008 07:59 AM
Constructor nayeem69 .NET Framework 2.0 1 July 17th, 2007 12:33 AM
java security certificate issue getsreenuk Apache Tomcat 0 March 3rd, 2007 05:49 AM
Constructor ramess C++ Programming 1 March 18th, 2006 07:23 PM
VB constructor snowbelly VB How-To 2 August 18th, 2004 04:42 AM



All times are GMT -4. The time now is 06:17 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.