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

Java - Help Needed! - Part 2

Options
  • 09-12-2008 12:54pm
    #1
    Closed Accounts Posts: 3,604 ✭✭✭


    Here is the origional thread -
    http://www.boards.ie/vbulletin/showthread.php?t=2055436620Here is as far as ive got with some help:

    public class UtilityBill
    {
    /* Meter Readings */
    private int numUnits = 0;
    private int cent5 = 0;
    private int cent2 = 0;
    private int cent1 = 0;

    /* Get the Meter Readings from the user */
    public int getMeterReadings() {
    int prevReading = 0;
    int curReading = 0;
    System.out.println("Enter Previous Meter Reading");
    prevReading = Keyboard.readInt();
    System.out.println("Enter Current Meter Reading");
    curReading = Keyboard.readInt();
    numUnits = curReading - prevReading;
    System.out.println("Number of billable Units = " + numUnits);
    return 0;
    }

    public int calculateBill() {
    if (numUnits > 0) {
    /* Get num of units to charge at .1 cent */
    if (numUnits > 10000) {
    cent1 = 10000;
    }
    else {
    cent1 = numUnits;
    }
    numUnits -= cent1; /* Remove the units we're charging for */
    /* get num of units to charge at .2 cent */
    if (numUnits > 10000) {
    cent2 = 10000;
    }
    else {
    cent2 = numUnits;
    }
    numUnits -= cent2; /* Remove the units we're charging for */
    /* get num of units to charge at .2 cent */
    if (numUnits > 0) {
    /* something to charge */
    cent5 = numUnits;
    }
    }
    return 0;
    }

    public int printBill() {
    int cent1euro = 0;
    int cent1cents = 0;
    int cent2euro = 0;
    int cent2cents = 0;
    int cent5euro = 0;
    int cent5cents = 0;
    int totalCents = 0;

    /* get euro at .1 cent */
    cent1euro = (cent1 / 10) / 100;
    cent1cents = (cent1 / 10 ) % 100;
    totalCents = cent1 / 10;

    System.out.println(cent1 + " units @ .1c = " + cent1euro + "." + cent1cents );

    return 0;
    }

    /* Main Function to execute the Example */
    public static void main(String[] args) {
    System.out.println("Utility Bill Reading Application v0.1");
    UtilityBill bill = new UtilityBill();
    bill.getMeterReadings();
    bill.calculateBill();
    bill.printBill();
    System.out.println("Press ANY key to exit");
    }
    }


Comments

  • Registered Users Posts: 610 ✭✭✭nialo


    Try using double instead of int for your cents. Will make your maths possible.

    ie

    private double cent1 = 0.01


  • Registered Users Posts: 4,188 ✭✭✭pH


    Never use double/float (or any real number) for financial calculations. In Java what you're looking for is BigDecimal.

    http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html


  • Closed Accounts Posts: 3,604 ✭✭✭Kev_ps3


    Got this off guy in college, he is fairly good at programming and says its complete, what ye think?
    class water_charges{

    static final double RENTAL = 20;

    public static void main (String[] args){
    double present, previous, total, qused, tmp, value;
    double [] liters = {0.005, 0.002, 0.001};
    double [] charged = {0, 0, 0};
    double [] wather = {0, 0, 0};

    // Get data from keyboard
    Screen.message ("Please Enter previous Meter Readig: ");
    previous = Keyboard.readDouble();

    Screen.message ("Please Enter present Meter Reading: ");
    present = Keyboard.readDouble();

    // Calculate quantity used and chceck present value is grater than previous
    qused = present - previous;

    if ( qused > 0 )
    {
    // Print table with basic data
    Screen.newline(2);
    Screen.message ("================================ Water Charge ================================");
    Screen.newline();

    Screen.message ("\t\t\t\tUnits\t\t\t");
    Screen.message ("Amount Due (EUR)");
    Screen.newline();

    Screen.message ("Meter Rental\t\t\t\t\t\t");
    Screen.writedouble (RENTAL,2);
    total = RENTAL;
    Screen.newline();

    Screen.message ("Present Reading\t\t\t");
    Screen.writedouble (present,0);
    Screen.newline();

    Screen.message ("Previous Reading\t\t");
    Screen.writedouble (previous,0);
    Screen.newline();

    qused = present-previous;
    Screen.message ("Quantity Used\t\t\t");
    Screen.writedouble (qused,0);
    Screen.newline(2);

    // Calculate charges in each range
    // Calculate total amount
    tmp = 20000;
    value = 0;
    for (int x=0; x<3; x=x+1)
    {
    //first column
    Screen.message ("Liters At ");
    Screen.writedouble (liters[x], 3);
    Screen.message (" Cents\t\t");

    if (qused>tmp)
    {
    wather[x] = qused-tmp-value;
    charged[x] = wather[x] * liters[x];
    }
    //second column
    Screen.writedouble (wather[x],0);
    Screen.message ("\t\t\t");

    //third column
    Screen.writedouble (charged[x],2);
    total = total + charged[x];
    Screen.newline();
    tmp = tmp - 10000;
    value = value + wather[x];
    }

    Screen.message ("Less Domestic Waiver\t\t\t\t\t");
    Screen.writedouble (-1*charged[2],2);
    total = total - charged[2];
    Screen.newline();

    Screen.message ("\t\t\t\t\t\t\t=======");
    Screen.newline();
    Screen.message ("Total Amount Due Now\t\t\t\t\t");
    Screen.writedouble (total,2);
    Screen.newline(2);
    }
    else
    Screen.message ("Present Meter Reading must be grater than Previous!");

    Screen.newline(2);
    }
    }


  • Registered Users Posts: 1,119 ✭✭✭Donald-Duck


    You'll fall far behind in programming if you don't do the work yourself. Getting help from someone is one thing, copying their code and renaming the variables is learning nothing.


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


    You could break that guys class down into different methods.

    So instead of just static main you'd also have the following methods:
    printHeader();
    printPresentReading();
    printPreviousReading();
    printQuantityUsed();
    calculateAndPrintCharges();

    Which would separate the logic into manageable blocks for you and that would be better programming practice. Its a good learning exercise too and all the code you need is right there already You've already done most of the work in your original post. You just have to understand the logic of the other guys code to do this. And if you do try it will make the next task you have in college all the easier. Nobody is good at programming when they start, you have to practice and challenge yourself.

    <edit/>
    If it was training a junior programmer I'd be looking for what you did originally over the other code that was posted.


  • Advertisement
  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    his code is an absolute head wreck. Hard to follow, lack standard java code styling and I would be pretty annoyed if someone coded like that was on my team in work.

    Your attempt is much better and will attract better mark in an assignment

    A few pointers though...

    1 you getmeterreadings method has a return of an int, it doesn't return to anything when you call it I suggest changing it to void

    You'll get much further with if you attempt it yourself and not cog off others. I've seen people getting into the habit of just cogging off others due to laziness but when they actually attempt it they get the majority of it working and also lecturers will award more marks for clear concise code rather than a mess

    Also don't forget to comment your code as well


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    another pointer is that some of the lecturers read the forum as well. As people said it is better to go with your own code, even if there are issues with it. You have a better chance of explaining how it works and why you did it that way.


Advertisement