Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default Need help with java program!

    I am very new to java and was wondering if I can get some help with an assignment. Here is the description of what needs to be done.

    1. Create a Java program with proper documentation and indentation.
    2. Give a brief description to the user.
    3. Mary suggests creating a class named ‘CarTracker’ with two String variables (first and last names of the driver), a double variable to track the fares paid to the driver; a double variable to track the gasoline purchased by the driver; a double variable to track the gasoline used.
    4. Display for the user the following and enter the data (use the Scanner class) exactly to test your program:

    (type the name in exactly as shown):
    Please enter the Driver's first name:
    john
    Please enter the Driver's Last name:
    betterDriver
    Please enter the amount paid to the Driver (fares):
    121.45
    Please enter the amount the Driver paid for gas:
    36.50
    Please enter the distance the Driver travelled:
    525
    Please enter the amount of gasoline the Driver used:
    35.5

    5. Create a green panel using a JPanel and JLabels. With the given input (above).

    6. Mary suggests using these formulas to determining the output and the client agreed:

    mpg = miles-travelled / gas-Used
    fares-per-mile = fare / miles-travelled
    gas-paid-per-mile = gas-purchased / miles- travelled
    profit = fare – gas-purchased;

    7. Joe (the IS person) suggests (because of his many years of experience with clients) better looking output by using these Java classes:

    DecimalFormat dfmt = new DecimalFormat("0.##");
    NumberFormat cfmt = NumberFormat.getCurrencyInstance();

    8. Add a driver so you can track two drivers. Use any input for the second driver, however, keep using the same input for the first driver. Create a JPanel for each driver. Have one JPanel be a green color and the other a blue color.

    9. Have the name (first and last) appear using the following format: all letters are lower case except for the first letter of the first and last name: (e.g., John Betterdriver). The input for driver one should be exactly as shown above, for example enter in the name: john ‘betterDRIVER’. The output should have the name appear as ‘John Betterdriver’.

    10. Also create an icon for each driver. The image you use is up to you, however, use a different image for each driver and place the icon inside the JPanels. Make the output look good.

    So those are the guidelines and here is what I have come up with so far. I need help with what i'm mssing and what I still need to have in my program. Any and all help is greatly appreciated.


    // This program will Calculate the mileage statistics for a cab company in Allendale
    //


    import Java.awt.Color;
    import Java.awt.Dimension;
    import Java.text.NumberFormat;
    import Java.text.DecimalFormat;
    import Java.util.Scanner;

    import Javax.swing.JFrame;
    import Javax.swing.JLabel;
    import Javax.swing.JPanel;
    import Javax.swing.SwingConstrants;


    public class CarTracker
    {


    public static void main(String[ ] args )
    {
    Scanner reader;

    double fares1;
    double gas1;
    double distance1;

    reader = new Scanner( System.in );

    System.out.println("Enter the driver's first name");
    firstname1= scan.nextline();

    System.out.println("Enter the driver's last name");
    lastname1= scan.nextline();

    System.out.println("Enter the amount paid to the driver");
    fares1= scan.nextdouble();

    System.out.println("Enter the amount the driver paid for gas");
    gas1= scan.nextdouble();

    System.out.println("Enter the distance the driver traveled");
    distance1= scan.nextdouble();

    // Presents two colored panels nested within a third.

    JFrame frame = new JFrame ("Nested Panels");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPane1 subPanel1 = new JPane1();
    subPanel1.setPreferredSize (new dimension(175, 175));
    subPanel1.setBackground (Color.green);
    JLabel label1 = new JLabel ("name " + firstname1 + " " + lastname1);


    subPanel1.add (label1);

    //Set up primary pane
    JPanel1 primary = new JPanel();
    primary.setBackground (Color.blue);
    primary.add (subPanel1);

    frame.getContentPane().add(primary);
    frame.pack();
    frame.setVisable(true);



    }
    }

  2. #2
    waltersk20 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    I am also a beginner on java and I notice on your program that you created a variale "reader" of the Scanner class, but you are storing your inout under "scan.nextLine"
    Also i think you should store the date buy capitalizyng the first letter of the data type. Such as "reader.nextDouble();" for a double or "reader.nextInt();"

    I hope it helps

  3. #3
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    I havent read your text at all since i am in hurry...i only noticed this
    Java Code:
    frame.getContentPane[B]()[/B].add(primary);

    Does this work? ? ?

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    OP it would be helpful if you told us which steps you have yet to complete (or if you have problems with what you've already done) as going through the code and checking if each step is completed tends to be a bit tedious and time-consuming.

    What Walter said is also correct in that you are attempting to call a method (.nextLine()) on an object that isn't initialized (scan), while it should be called on reader. Additionally, your methods should be nextLine and nextDouble.

    As well with your .setVisable call, it should be .setVisible. This is important, as the compiler isn't a spell checker as well.

    And while I understand your attempt to help, jlmp, that is valid and you should check that kind of thing before posting it as advice.

    Much luck!

    PS: OP, use [code] tags when posting code. ;)

    PPS: Your firstname1 and lastname1 variables are not declared... Also noticed that you use "new dimension" instead of "new Dimension". Have you tried compiling this code to reveal all the locations wherein errors like this are occurring?
    Last edited by Zack; 10-01-2010 at 06:48 AM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

  6. #6
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    profit = fares – gasPurchased;

    I am getting an error on this. Can anyone help
    The error says:
    illegal caracter: \8211

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    The error says:
    illegal caracter: \8211
    Please copy and paste the full text of the error message.
    This message looks like what you typed in, not what was output by the compiler.

  8. #8
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    illegal character: \8211

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    What compiler are you using that only puts out ONE line for an error?
    My compiler puts out this:
    Java Code:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    What source line of the program did the error occur on.

  10. #10
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    I have to use blueJ and it is only giving me one line for an error. I thought that was weird as well. Where it is bolded and in blue is where i am getting an error.



    import java.util.Scanner;
    import java.text.NumberFormat;
    import java.text.DecimalFormat;
    import java.awt.*;
    import javax.swing.*;

    public class CarTracker
    {


    public static void main(String[ ] args )
    {

    Scanner reader;

    double fares1, gas1, miles1, profit1;

    Scanner scan = new Scanner( System.in );

    DecimalFormat dfmt = new DecimalFormat("0.##");
    NumberFormat cfmt = new NumberFormat.getCurrencyInstance();

    System.out.println("Enter the driver's first name");
    firstname1= scan.nextLine();

    System.out.println("Enter the driver's last name");
    lastname1= scan.nextLine();

    System.out.println("Enter the amount paid to the driver");
    fares1= scan.nextDouble();

    System.out.println("Enter the amount the driver paid for gas");
    gas1= scan.nextDouble();

    System.out.println("Enter the distance the driver traveled");
    distance1= scan.nextDouble();

    mpg = milesTraveled / gasUsed;
    faresPerMile = fares / milesTraveled;
    gasPaidPerMile = gasPurchased / milesTraveled;
    profit = fares – gasPurchased;


    // Presents two colored panels nested within a third.

    JFrame frame = new JFrame ("Nested Panels");
    frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPane1 subPanel1 = new JPane1();
    subPanel1.setPreferredSize (new Dimension(175, 175));
    subPanel1.setBackground (Color.green);
    JLabel label1 = new JLabel ("Name " + firstname1 + " " + lastname1);
    JLabel label2 = new JLabel ("MPG " + milesTraveled + " " + gasUsed);
    JLabel label3 = new JLabel ("Fares per mile " + fares + " " + milesTraveled);
    JLabel label4 = new JLabel ("Gas Paid Per Mile " + gasPurchased + " " + milesTraveled);
    JLabel label5 = new JLabel ("Total Profit " + fares + " " + gasPurchased);

    subPanel1.add (label1);
    subPanel2.add (label2);
    subPanel3.add (label3);
    subPanel4.add (label4);
    subPanel5.add (label5);

    //Set up primary pane
    JPanel1 primary = new JPanel();
    primary.setBackground (Color.blue);
    primary.add (subPanel1);

    frame.getContentPane().add(primary);
    frame.pack();
    frame.setVisable(true);



    }
    }

  11. #11
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by university123 View Post
    profit = fares – gasPurchased;

    in java, before you can use a variable you must declare it. in the code you post i didn't see any declarations for profit, fares or gasPurchased. here is a list of errors your code produce in eclipse

    NumberFormat.getCurrencyInstance cannot be resolved to a type
    firstname1 cannot be resolved to a variable
    lastname1 cannot be resolved to a variable
    distance1 cannot be resolved to a variable
    mpg cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    gasUsed cannot be resolved to a variable
    faresPerMile cannot be resolved to a variable
    fares cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    gasPaidPerMile cannot be resolved to a variable
    gasPurchased cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    The primitive type double of fares1 does not have a field gasPurchased
    Syntax error on token "Invalid Character", . expected
    JPane1 cannot be resolved to a type
    JPane1 cannot be resolved to a type
    firstname1 cannot be resolved to a variable
    lastname1 cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    gasUsed cannot be resolved to a variable
    fares cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    gasPurchased cannot be resolved to a variable
    milesTraveled cannot be resolved to a variable
    fares cannot be resolved to a variable
    gasPurchased cannot be resolved to a variable
    subPanel2 cannot be resolved
    subPanel3 cannot be resolved
    subPanel4 cannot be resolved
    subPanel5 cannot be resolved
    JPanel1 cannot be resolved to a type
    The method setVisable(boolean) is undefined for the type JFrame

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    How did you type in the - character? It has an int value of 8211.
    When I enter a - its value is 45.

  13. #13
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    He is using an emdash rather than a minus sign/hyphen.

    Also, OP, can you please use [code] tags around your code when posting it? Unformatted code is a pain to read and can often be overlooked by people who would otherwise want to help.
    Last edited by Zack; 10-02-2010 at 12:08 AM.

  14. #14
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    Ok so how do I get these to do calculations for me and then display it in my panel?

    JLabel label1 = new JLabel ("Name: " + driver.driversName());
    JLabel label2 = new JLabel ("MPG: " + distance + " " + fuel);
    JLabel label3 = new JLabel ("Fares per mile: " + fares + " " + distance);
    JLabel label4 = new JLabel ("Gas Paid Per Mile: " + gasPrice + " " + distance);
    JLabel label5 = new JLabel ("Total Profit: " + fares + " " + gasPrice);

    For instance-
    MPG = distance/fuel
    Fares per mile= fares/distance
    Gas paid per mile= price/distance
    profit= fares/price

  15. #15
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    I know I just posted this before you posted, so I'm just going to reiterate it.
    Quote Originally Posted by Zack View Post
    Also, OP, can you please use [code] tags around your code when posting it? Unformatted code is a pain to read and can often be overlooked by people who would otherwise want to help.

    Doing those calculations is as easy as it can be--simply put your equation into the println or other function, example:
    Java Code:
    int a = 12;
    int b = 2;
    System.out.println("12 over 2 = " + (a/b));
    System.out.println("12 times 2 = " + (a*b));
    In your case, the println function is actually the JLabel constructor. (That is, new JLabel("12 over 2 = " + (a/b));)

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

  17. #17
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    ...what's with all the cross-posters today?

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,590
    Rep Power
    23

    Default

    I guess the OP wants to give everyone a chance to answer his question.

  19. #19
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    How civic-minded of him.

  20. #20
    university123 is offline Member
    Join Date
    Oct 2010
    Posts
    33
    Rep Power
    0

    Default

    I need help with rounding decimal places to my output. I have tried putting this code in a bunch of different places in my code and it's not doing to trick!

    DecimalFormat df = new DecimalFormat("#.##");

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 09-30-2010, 06:32 PM
  2. Replies: 4
    Last Post: 07-30-2010, 12:25 PM
  3. execute java program within java program
    By popey in forum New To Java
    Replies: 2
    Last Post: 10-22-2009, 05:32 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 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
  •