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

Very basic java problem

  • 03-01-2007 11:22pm
    #1
    Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭


    Having some trouble with a Java assignment I've been working on. My level of java is very limited, I haven't been reading out of a published book - just a course book our lecturer gave us, and working off altering tidbits of programs that are in this course book.

    I'm making a program to input a number of lectures I have in one day.
    The first part of my assignment asks me to input however many lectures, and say how many hours free time are left in the day outside of lectures (no sleep, no eating, just how many free hours).
    The first extension asks to check that they end after they begin, and really do fit in 24 hours - all well and good. It also asks to check they do not overlap - I haven't been able to manage this, and I think it's because of how I've been inputting the lectures.

    Onto the next assignment - this requires us to print out a list of periods which we're free of lectures during that day. This definitely means I've been inputting the times wrong.

    I won't post my code - it would be long and unnecessary, and I'm sure there's something in the college regulations against it.
    Instead, here is how I've done things:

    while (anotherlecture is true) {
    count = count + 1
    enter lecture (count)'s starthour from keyboard - store in starthour
    enter lecture (count)'s startminute from keyboard - store in startminute
    enter lecture (count)'s endhour from keyboard - store in endhour
    enter lecture (count)'s endminute from keyboard - store in endminute
    enter if there is another lecture today - store in anotherlecture
    // Perform various checks and calculations
    }

    However, since the start hour, start minute, end hour, and end minute are simply stored in "starthour", "startminute", "endhour", "endminute" the actual periods in which lectures take place are not recorded - starthour, startminute, etc are each overwritten after every run of the while loop.

    What I'm wondering is what is the best way to go about storing every period, without simply making a constructor for "starthour1, starthour2, starthour3" etc etc.

    What I had in mind was straight after taking in the times of the current lecture do this:
    starthour(count) = starthour
    startminute(count) = startminute
    endhour(count) = endhour
    endminute(count) = endminute

    However I'm not sure of how the syntax to implement this goes.
    Count is already in existence as you can see above, however if I just say starthourcount= it will just take it literally...

    So - a long winded question, but hopefully I've made myself clear. I'm not asking anybody to just give me a solution to this since this is a student situation - just lead me in the right direction. Am I thinking the right way, and will my above method work?

    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    You need to declare an array to store a sequence of values, e.g.

    int starthour[] = new int[24];

    gives you space for 24 'starthour' values. You reference them as follows:

    starthour[0] = a;
    starthour[1] = b;
    ...
    ...
    starthour[23] = x;

    So in your loop, you'd have:

    starthour[count] = inputStarthour;

    But notice that the array starts at 0. So 'count' should be 0 the first time around the loop.

    You could either declare four arrays for each of your values (starthour, startminute etc.), or you could declare a new class which contains your four values, and have one array of that class. The second method is preferable although a bit more advanced.

    A potential problem with these types of arrays is you need to know in advance what the maximum size will be. e.g. in the above example, if the user entered more than 24 entries you'd run into trouble. If you don't know in advance what the maximum size will be, Java has a growable array called a Vector:

    http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    I'm struggling to understand your post!
    Just have a few questions here.
    The first part of my assignment asks me to input however many lectures, and say how many hours free time are left in the day outside of lectures (no sleep, no eating, just how many free hours).
    The first extension asks to check that they end after they begin, and really do fit in 24 hours

    From what I see here you are asked "How many Lectures you have" and "How many free hours are outside of these" which I would imagine is just (24*60) - (lecturetime * minutes) = minutes free?

    With this amount of information i can't see how you would detect if the lectures overlap.
    Do you mean that you ask them to enter how many lectures they got and then ask the begin time of each of these lectures?

    If so then things mightn't be so bad. Sorry for my lack of understanding, think i'm a little tired


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Thanks Padraig, that looks like it'l do the job nicely - just a piece of java I didn't know about, I'll give that a go.

    Webmonkney - the lectures can last for any length of time - 50 minutes, 60 minutes, 2 hours...
    I input the starting time and ending time of all the lectures. But how much free time is indeed (24*60) - (total lectures in minutes) = minutes free > which needs to be converted back into hours and minutes, joy of joys.
    This might make it clearer...


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Right, arrays seem to be working quite well - just one other piece which I haven't been able to find anywhere through googling, it's just a syntax error I think.
    For the starthour of the lecture, there is a method to getStarthour() as follows:
    public double getStarthour(int count) {
    return this.starthour[count];
    }
    Now, elsewhere in my program if I want the start hour I simply say :
    timetable.getStarthour();
    However, how do I reference an array through saying this? I've done a load of searching on what the brackets do, but whenever I try putting what I need (count) into the brackets in eclipse it makes an error and doesn't compile.
    What I need to do (although I know this is wrong syntax) is the following:
    timetable.getStarthour()[count-1];

    Any help here, or am I trying the impossible? Thanks!


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Would you not put count-1 into the brackets like

    timetable.getStarthour(count-1); ?

    I don't know anything about java but in C/C++ you would have to supply this argument or it will fail.

    Also if count is 0, you will end up passing in -1 which is an invalid index in the array so just be careful there.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Right I have something working now - nothing to do with syntax, just my stupidity! Putting it in the brackets did work, just the method I was looking at was starttotal while I was getting starthour...! Thanks for the prompt reply, hopefully that'l work - and thanks for pointing out the 0-1 thing.


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Right all is going nicely except for one problem - in trying to return the value of an array other than the current one, I'm just getting zero. Very basically, here's what I'm trying to do:
    class Reader {
    // This declares the starthour bit
    private double[] starthour = new double[24];

    // This is a constructor that corresponds with the bit where the terminal reads in the lecture hour
    public Reader (double starthour) {
    this.starthour[count= = starthour;
    }

    // This gets the starting hour of the lecture
    private double getStarthour(int count) {
    return this.starthour[count];
    }

    }
    class Readerprogram {
    // Below, count will be equal to 0, 1, 2, 3 and so on
    while anotherlecture !=0 {
    timetable = new Reader(terminal.readdouble("Enter the starting hour of today's lecture: "));
    anotherlecture = terminal.readdouble("Have you another lecture today? 1 / 0");
    count = count + 1;
    }

    terminal.println("Lecture 2 = " + getStarthour(2));
    terminal.println("Lecture 1 = " + getStarthour(1));
    terminal.println(Lecture 0 = " + getStarthour(0));

    }
    This code isn't exactly what I've got, I just pasted it in here to keep things simple - but basically the problem is in the last few lines, where I tell the terminal to print lecture 2, 1 and 0. The problem is it will only print lecture 0 - it just acts as if 1 and 2 don't exist. I'm stumped here, it just doesn't seem to be passing any value other than the current lecture time - defeating the purpose of having the array tere.

    Anybody got any ideas? Do I need to post more info? Thanks

    Edit: Edited to include more info regards the constructor used


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Wheres your for loop to enter all the lectures, maybe its where you entering the data that is the problem


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    What year Java is this? Im in second year of an honours degree course and iv never seen code like that so far..


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    lol I must admit, and i don't do java that it looks a bit strange. I think the classes are used for the wrong reasons as these arn't objects, these are just functions that should be on their own acting on a class or being a class function themselves.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    First year. The terminal.printn thing is something from a custom package the college has.

    Webmonkey - I've edited the other post to include more info on exactly how things are calculated.
    I might have to go into the college's programming support centre on Monday, but cutting it short then - it's due Wednesday.


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Webmonkey wrote:
    lol I must admit, and i don't do java that it looks a bit strange. I think the classes are used for the wrong reasons as these arn't objects, these are just functions that should be on their own acting on a class or being a class function themselves.
    Hm yeah you're probably right, I've only really been doing actual java for a month and I'm not entirely sure what I'm doing. Really I just need to get these bloody arrays working, that should be the last step... Thanks for the help


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Edit - Maybe i don't know what i'm talking about actually!

    Anyways goingt o go off looking at some java there and then come back and maybe i'll be of better help :)


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Right, found what I think is the problem - the value isn't making it's way over to the constructor.
    What I need to do: I have two classes, Reader and ReaderProgram - ReaderProgram is the one that starts first, with the public static void main lark.

    I need to pass an int value called count from ReaderProgram to a constructor in Reader, however I'm not sure how to do that.
    I thought it would be just ReaderProgram.this.count - but it doesn't seem to like that. Anybody?


  • Closed Accounts Posts: 636 ✭✭✭NADA


    Hey Cian! I'm oding the same assignment. I have it working but I can't actually help you seeing as I only have one class and don't really know what you are doing. The only problem I have is that it prints out the available times correct but if there is no time between two lectures I still get a time. ie 16:00 and 16:00. I am not that bothered to fix it though! Jonathan Cummins. CSLG by the way!


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    ReaderProgram.this.count
    Guessing this now but 'this' is used within a class to descibe it as a member of that class so outside when you create an instance of this class like ReaderProgram you would use ReaderProgram.count instead of this.count.
    Wish i could help more but dont' know too much about java


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    Thanks for the help - that seems to work, but the constructor doesn't like that it's not a static value, wierd...

    Johnathan - might just go the same route and lob it all into the one class I think!
    Does yours use an array?


  • Registered Users, Registered Users 2 Posts: 3,323 ✭✭✭padraig_f


    Cian, I would recommend using just one class, your ReaderProgram class. The reason your values are getting lost is the following code:

    while anotherlecture !=0 {
    timetable = new Reader(terminal.readdouble("Enter the starting hour of today's lecture: "));
    anotherlecture = terminal.readdouble("Have you another lecture today? 1 / 0");
    count = count + 1;
    }

    Every time around the loop, you're creating a new Reader class, and losing the one from the previous bit of the loop. So you only ever have the latest value entered.

    I would move the following array from Reader to ReaderProgram:

    private double[] starthour = new double[24];

    get rid of the Reader class.

    Then you can just read the value directly into ReaderProgram like this:

    while anotherlecture !=0
    {
    ...
    starthour[count] = terminal.readdouble("Enter the starting hour of today's lecture: ");
    ...
    }


  • Closed Accounts Posts: 636 ✭✭✭NADA


    cianclarke wrote:

    Johnathan - might just go the same route and lob it all into the one class I think!
    Does yours use an array?

    Yeah I used an array. Didn't put it near the class though. It's in the main method. It's the best way to do extension two. One problem I have is that it prints out times with no space between them . Ie you are free between 16:00 and 16:00 . Not sure it thats a problem though. Secondly the other problem is that it actually prints 16:0 and 16:0.


  • Registered Users, Registered Users 2 Posts: 1,389 ✭✭✭cianclarke


    It's better than what most people have so far - I wouldn't worry about it!
    Are you using them as doubles? They tend to print things wierdly...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    For time you would be better off using integers, easy to convert anyways.
    And you can format the output then to make 0, become 00 or 0 become 09.


Advertisement