Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 12:07 AM
Member
 
Join Date: Apr 2008
Posts: 1
Minime is on a distinguished road
Help with constructors
I can't get the Car constructor to work it says I need a ; after the car() in the code here it is:
import java.text.DecimalFormat;
import java.io.*;

public class Car {
public static void main(String args[]) {
String Color = "",
Make = "",
Model = "";
int year = 0;
double invoice;
double price;
double getProfit;
double X;

//void showCar()

Car carA = new Car();
Car(){
Make = "Ford";
Model = "Escort";
year = 1997;
invoice = 10500;
price = 14500;
Color = "blue";
}



BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
String inputValue;

DecimalFormat number2Digits = new DecimalFormat ("0");


try
{

//Input
System.out.print("Please enter Car Color: ");
Color = reader.readLine();

System.out.print("Please enter Car Make: ");
Make = reader.readLine();

System.out.print("Please enter Car Model: ");
Model = reader.readLine();

System.out.print("Please enter year of Car: ");
inputValue = reader.readLine();
year = Integer.parseInt(inputValue);

System.out.print("Please enter Invoice Price: ");
inputValue = reader.readLine();
invoice = Double.parseDouble(inputValue);

System.out.print("Please enter Actuall Price: ");
inputValue = reader.readLine();
price = Double.parseDouble(inputValue);



//Processing
{
X = invoice - price;
}

//Output
System.out.println("\t CarA");
System.out.println("Car Color: " + Color); //Print Color
System.out.println("Car Make: " + Make); //Print Make
System.out.println("Car Model: " + Model);//Print Model
System.out.println("Year: " + number2Digits.format(year));//Print year
System.out.println("Invoice Price: " + number2Digits.format(invoice));//Print invoice
System.out.println("Actuall Price: " + number2Digits.format(price));//Print price
System.out.println("Profit =" + number2Digits.format(X));//Print difference


}
catch (Exception e)
{
System.out.println("Exception occurred");
}

}


}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2008, 03:35 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Welcome to the forums. First, please read the FAQ before you post again.

What looks like a constructor is placed inside of main(). Move that(and make it public) and the class's fields to outside of main(). And finally, the calls to the class member fields are made from a static context- you need to reference them instead. In other words, you make a Car object: carA, but you don't use it. To access a member field, use the "." operator via:
Code:
carA.invoice;
Post your updated attempt if you get stuck.

See you around!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-09-2008, 08:56 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
No Methods Or Constructors Or Statics Inside A Method
Hi Minime,
Dont Put Any Constructors InSide Any Method. Metods Reside In Stack Memory. Objects Which Will Be Created By Invoking Constructors Will Reside In Heap Memory..
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-09-2008, 08:59 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Welcome javarishi to the Java Forums!

Please take a moment to review the FAQ to get you started.

See you around!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Initializing variables using constructors Java Tip Java Tips 0 01-13-2008 09:28 PM
constructors khamuruddeen New To Java 2 12-01-2007 04:15 PM


All times are GMT +3. The time now is 06:47 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org