Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Grrr......Java CourseWork Problem...Ne1?

  • 16-10-2001 11:57am
    #1
    Posts: 0


    Ok, this is prolly all laughable to you guys coz its so easy but I am having serious trouble with some Coursework thats due at 5pm today!!!!

    Heres the Q:

    Implement a class Car with the following properties:

    A car has fuel efficiency measured in liters/km, and a current amount of fuel in the fuel tank. The efficiency is specified in the constructor, and the initial fuel level is 0. Define a method drive that simulates driving the car for a given distance, reducing the fuel level in the tank, and methods getFuelLevel to return the current fuel level, and refill to refuel.


    Is it just me or is this a really ambiguous Question? I dont know whether im meant to have an input of a distance which then calculates fuel loss and which sends out a message to refuel if the distance you say is more then the fuel will allow, or whether to just list a few methods. It's gettin me really angry.

    We've only had 6 lectures so far and havent covered inputs but I have read ahead and reckon I could give em a stab. Can ne1 tell from this info of that is necessary?


    Heres what Ive Done so far btw:
    public class Car 
    {
    private String name;
    /* The Car's Name. */
    private int efficiency;
    /* The Car's Efficiency (litres per kilometer). */
    private int fuelLevel;
    /* The Car's Fuel Level. */
    /* Create new Car with the given name, efficiency and fuel level.  Fuel Level will be set to 0. */
    public Car(String name)
    { 
    name = Ferarri;
    int efficiency = 29;
    double fuelLevel = 0;
    }
    /* Refills Car Engine */  
    public void refill()
    { fuellevel = 20; }
    /* Drives 100 km */ 
    public void drive() 
    { fuellevel = (fuelLevel) - (100/efficency)}
    /* Returns the FuelLevel. */
    public String getFuelLevel()
    { return fuelLevel; }
    /* Display details on the screen. */
    public void display()
    { 
    System.out.println("Car Name: " + name);
    System.out.println("Litres Used Per km: " + name);
    System.out.println("Initial Fuel Level: " + fuellevel);   
    }
    }
    
    
    


Comments

  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    tis been a while but
    these have to be in somewhere
    include java.io.*;
    
    public static void main(String [] args) throws IOEXCEPTION{ (?)
    

    maybe completely wrong
    and i dont think u can input numbers with that so
    ive wasted everyones time :)


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    perhaps you should check out a java site online, for a tutorial and such, ive found some good ones in my time but damned if i know the addresses

    use google.com and search for "java programs"


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Righty, I take it you're new to programming. Well good for you, I hope this helps without patronising.

    Tip #1: Don't do a search in google
    It makes it to easy and you won't have google in your exams.

    Tip #2: The results of division should not go into integers (even if your not doing that division)

    Personally I'd reckon don't use ints for your efficiency, litres per mile will contain decimals, use doubles or floats. This bug is repeated in your code somewhere else.

    Tip #3: Avoid literals when ever possible
    Define a method drive that simulates driving the car for a given distance

    I'd consider your code to have a specified (literal) distance instead of a given. Perhaps it should take a parameter in instead of a literal value? i.e. one of your methods can be passed a value representing distance. Even if you've been specifically told to use 100km do this anyway. It'll be more fun.

    Em, what else? Your constructor is being passed the name of the car, remember tip #3. When you've finished with this try adding some other cool stuff like braking distance or something. You'll learn more and it'll earn you a few brownie points even if it doesn't work.

    hope this help
    Prof. Evil Phil ;)


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    I'm too late for 5pm, but if you still need help(handing it in late), post back here. For the mo, your constructor needs an overhaul. If the Q you posted is the question word-for-word, your constructor should something look like this:(correct me if im wrong)
    public Car(String carName, double eff) {
       name = carName;//don't call your parameter name,causes probs
       efficiency = eff;
       fuelLevel = 0;
    }
    

    I'll help you with the whole thing if you want, but they'd kill me!!! Listen to Evil Phil, he knows what he's talking about.


  • Posts: 0 [Deleted User]


    Hey thanx guys,

    There is some really good points in there I didn't really pick up from my lectures. Yeah, this is my 1st object-oriented language and I've only been at it for 3 weeks but its really fun (Oh, a nerd am I).

    1 or 2 Qs (Please Excuse ma stupidity):

    Seamus => If I put 'efficiency = eff' in ther will that set efficiency to a string as opposed to an int/double even if i change it l8r?

    Evil Phil => So, if Im doing math that will have decimal points in it somewhere am i safer to just ignore int and always use double?

    I see what u mean in point #3, I spent the whole day after just finding someway of putting all the java i knew into the code and it was cool. Not overly impressive but It pushed me to check out the java libraries for the 1st time to get commands i needed.


    Thanx again guys, btw, i passed by handing in what i put in the topic with a few syntax errors fixed. Ill get that honors next week :D


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,684 ✭✭✭Kraken


    Not really a buff at this tbh but the double or float is handy to use but you wont need it all the time make sure you can do both.
    Ass for the eff you have to define it in you code some where so that it knows what eff is. You do need double for this or float no biggy there. To get a true result. For a first timer at it you seem prettty good much better than me. but then again im a lazy git.


    Kraken


  • Registered Users, Registered Users 2 Posts: 625 ✭✭✭Vincent


    Hope you dont me adding this problem i have, doesn't seem worth making a new topic about, ok this code comes back with an output whether the confirmation and password are the same or not, But i can only get it to work with numbers, when i use strings it always comes back saying the passwords are not the same whether they are or not, anyoen point me in the right direction? and what classes can i use so i can use both letters and numbers together? this is a worksheet btw so if i cant do it i'll be shown next week anyway but i'm so impatient i cant wait that long :)



    import java.io.*;
    class Password
    {
    public static void main(String args []) throws IOException
    {
    int password;
    int confirmedpassword;

    BufferedReader keyboardInput;

    keyboardInput = new BufferedReader (new InputStreamReader (System.in));

    System.out.print(" Enter your password :");
    password = Integer.parseInt (keyboardInput.readLine());
    System.out.println("\n ************");

    System.out.print(" Confirm your password :");
    confirmedpassword = Integer.parseInt (keyboardInput.readLine());
    System.out.println("\n ************");

    if (password == confirmedpassword)
    {
    System.out.println(" Passwords match: Proceed ");
    }

    else
    {
    System.out.println(" Password do not match");
    }

    }
    }


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    When you use the == comparison for String objects (or objects of any kind for that matter), you're actually comparing references, so you're checking if both objects point to the same memory address rather than comparing the contents of the Strings. What you want is the String.equals(String) method.

    So change the line
    if (password == confirmedpassword)
    to
    if (password.equals(confirmedpassword))

    and it'll work (presuming password and confirmedpassword are Strings of course).


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    If you notice in my code piece that the eff parameter is a double value, so when I write 'efficiency = eff', then the efficiency variable is assigned the double value in eff.

    For example, say I wanted to make this car object, I would use code like this:
    Car myCar = [b]new[/b] Car('Ferrari', 0.22);
    

    And this car is created with the name 'Ferrari' and an efficiency of 0.22 litres/km.

    Just as a hint, a better way to write the above would be:
    String carName = 'Ferrari';
    [b]double[/b] efficiency = 0.22;
    
    ........//more code.....
    
    Car myCar = [b]new[/b] Car(carName, efficiency);
    

    And in answer to question 2, if you are ever going to use the / operator, or involve any doubles somewhere in your calculations, then all values should really be set to float or double, to save hassle.


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    Strings should be contained in double quotes, and characters in single quotes. That should be
    String carName = "Ferrari";
    
    I'm sure this is just a typo on your part seamus, but best not confuse psIRE any more than is necessary!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    YEP, sorry everyone. Doing too much programming in Delphi over the summer, javac keeps catching me out on this too :)


Advertisement