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

Question about java class and function.

Options
  • 08-05-2008 8:59pm
    #1
    Registered Users Posts: 2,234 ✭✭✭


    Okay,

    I am doing a question and it says to model a composite datatype for start time and finish time of event.(24 hour)
    I'm going with an integer.

    Next part of question is:
    Provide a private static function method isValidTime that takes a time t as its argument
    and returns a boolean indicating whether t is a valid time (on the 24-hour clock);

    -
    will I just have to functions. The first one takes startTime t as a paramater and the second one takes finishTime t as a parameter. Both of these functions will be called isValidTime then java will choose the correct function to execute based on the type of datatype provided..

    Thanks.

    EDIT: Actually, an integer wouldn't do, what if the time was 12 O'clock (24 Hour)? Would a string be the best bet then you could analyse each character and check to see if it's within a certain range.


Comments

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


    no, you won't have two functions.



    what you're lecturer wants from you is to bulid a class that has variables to store the hours minutes and seconds (int should be fine for this) and have one member method that will return true or false if it's passed a time object, in this method you'll have to do some calculation to make sure hours aren't under 0 over 24 and so on for minutes and seconds. of course you should have both default and parameterised constructors for the class as well as isValidTime().


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Hey Cremo,

    Thats only part of the question. I don't think i'm required to build a class for the date am I? Here's the full question:

    An airline company keeps records of all its flights. Each flight has
    • a unique code (e.g. KH117),
    • a starting airport (e.g. Dublin),
    • a finishing airport (e.g. Paris),
    • a starting time (e.g. 1245), and
    • a finishing time (e.g. 1520).
    
    Question 1:
    
    Write down a class definition Flight with the above attributes.
    (3 marks)
    
    Question 2:
    Provide the following methods:
    
    • a constructor method to initialise the attributes with values supplied as arguments;
    (2 marks)
    • a private static function method isValidTime that takes a time t as its argument
    and returns a boolean indicating whether t is a valid time (on the 24-hour clock);
    

    I plan on having:
    private String fCode // flight code
    private String sAirport //start airport
    private String fAirport //finish airport
    
    Should I Still have individual variables for time??
    

    Thanks..


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Pseudo-code:
    public bool isValid(string time)
    {
       //'time' is formatted as a string: "HH:MM:SS"
       string[] split1=split(':',time);
       int hours=(int)split1[0];
       int mins=(int)split1[1];
       int secs=(int)split1[2];
    
       //logic to check if the hours/mins/secs are valid:
       if(hours>=0 && hours<=23 && mins>=0 && mins<=59 && secs>=0 && secs<=59)
       {
          return true;
       }
       else
       {
          return false;
       }
    }
    


  • Closed Accounts Posts: 2,039 ✭✭✭rmacm


    Yes you'll need variables to store the times if you're going to be working with them and checking if they are valid and they'll have to be part of the class definition. int variables to store the hours and minutes should be fine by the looks of it from the question you'll then be able to use these with your isValidTime method.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    yea thats perfect, thanks.. I was getting carried away with the 2 times being a composite datatype but when the function is within the class I can refer to the strings..

    Penny finally drops..


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


    A composite datatype would be a javabean (AFAIR).

    something like.
    class Time { 
      private int hour;
      private int minute;
    
      getHour(...
    
      setHour(...
    
     // etc
    }
    


  • Closed Accounts Posts: 81 ✭✭cowan


    Yep thats right, you'd need a class Time, with constructors, get and set methods for both variable, these would be public, and your global variables, i.e. hour, minute, would have to be private.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Okay I have another query..

    Question:
    Design a getter method, dist, to compute the distance between the Point object and another Point object, the latter being supplied as a parameter. (The distance of a
    point (x, y, z) from another point.

    Am i right in saying that you declare a function that only takes on argument of type point? Or should it take two?

    But how does the first point arrive in the function and where do he values come from?


  • Registered Users Posts: 4,287 ✭✭✭NotMe


    The getter method is part of the Point class I would imagine so it returns the distance between itself and the point you pass in.


  • Registered Users Posts: 2,567 ✭✭✭daveharnett


    techguy wrote: »
    Okay I have another query..

    Question:
    Design a getter method, dist, to compute the distance between the Point object and another Point object, the latter being supplied as a parameter. (The distance of a
    point (x, y, z) from another point.

    Am i right in saying that you declare a function that only takes on argument of type point? Or should it take two?

    But how does the first point arrive in the function and where do he values come from?

    the getDistance function will be a member of the point class. Probably best to call it distanceTo. So a call to it will look like Point1.distanceTo(Point2) .
    Class Point{
    public int x, y, z;

    void Point(int x, y, z){
    this.x = x;
    this.y=y;
    this.z=z;
    }

    float distanceTo(Point Point2){
    return (sqrt((Point2.x-this.x)+(Point2.y-this.y))); //i don't rember how to calculate 3d distances :(
    }
    }


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


    is it just me or is the "getter" part of that question completely off putting?


  • Registered Users Posts: 2,567 ✭✭✭daveharnett


    Cremo wrote: »
    is it just me or is the "getter" part of that question completely off putting?
    Yeah, it's a bit of a misnomer allright.


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


    Cremo wrote: »
    is it just me or is the "getter" part of that question completely off putting?

    Actually it isn't. Javabeans have a strict coding convention which the point() method above does not adhere to.

    So getters and setters are named a certain way.


  • Registered Users Posts: 2,234 ✭✭✭techguy


    Woah! I didn't get notified of all these posts, thanks guys!


  • Registered Users Posts: 93 ✭✭Yarnhall


    Are you using GMT or UTC to represent the time, is the arrival time the destinations local time or the departure locations' local time?

    Keep the time in the simplest form and then convert it when displaying it (Date)


Advertisement