Results 1 to 4 of 4
  1. #1
    Minime is offline Member
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default 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");
    }

    }


    }

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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;
    Post your updated attempt if you get stuck.

    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)

  3. #3
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default 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..

  4. #4
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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

  1. Initializing variables using constructors
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 08:28 PM
  2. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •