Results 1 to 4 of 4
Thread: Help with constructors
- 04-09-2008, 12:07 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 1
- Rep Power
- 0
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");
}
}
}
- 04-09-2008, 03:35 AM #2
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:
Java Code:carA.invoice;
See you around!Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 04-09-2008, 08:56 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
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..
- 04-09-2008, 08:59 AM #4
Welcome javarishi to the Java Forums!
Please take a moment to review the FAQ to get you started.
See you around! :)Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
Similar Threads
-
Initializing variables using constructors
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 09:28 PM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 04:15 PM
Bookmarks