Results 1 to 14 of 14

Thread: java assignment

  1. #1
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default java assignment

    i have a java assingment to do, and its basically to make a flight scheduling/booking system, can anyone check if my code is correct so far?

    Heres the code so far:
    Java Code:
    import java.io.IOException;
    import java.io.PrintStream;
    enum flightStatus { S , B , D , C };
    public class Flight {
    	
    	private String fNo;
    	private String dPoint;
    	private String dest;
    	private double fLength;
    	private String fDate;
    	private String fTime;
    	private int capa;
    	private int book;
    	private char fStatus ;
    
    public Flight (String flightNo, String departurePoint, String destination,
    double flightLength, String flightDate, String flightTime, int capacity, int booked, char flightStatus){
    fNo = flightNo; 
    dPoint = departurePoint;
    dest = destination;
    fLength = flightLength;
    fDate = flightDate;
    fTime = flightTime;
    capa = capacity ;
    book = booked;
    fStatus = flightStatus;
    
    }
    
    
    public String getFlightNo()
    {
    	return fNo ;
    }
    
    public String setFlightNo(String flightNo)
    {
    	return this.fNo = flightNo;
    }
    
    public double getFlightLength()
    {
    	return fLength;
    }
    
    public double setFlightLength(double flightLength)
    {
    	return this.fLength = flightLength;
    }
    
    public String getFlightDate()
    {
    	return fDate;
    }
    
    public String setFlightDate(String flightDate)
    {
    	return this.fDate = flightDate;
    }
    
    public String getFlightTime()
    {
    	return fTime;
    }
    
    public String setFlightTime(String flightTime)
    {
    	return this.fTime = flightTime;
    }
    
    public String getDeparturePoint()
    {
    	return dPoint;
    }
    
    public String setDeparturePoint(String departurePoint)
    {
    	return this.dPoint = departurePoint;
    }
    
    public String getDestination()
    {
    	return dest;
    }
    public String setDestination(String destination)
    {
    	return this.dest = destination;
    }
    public int getCapacity()
    {
    	return capa;
    }
    
    public int setCapacity(int capacity)
    {
    	return this.capa = capacity;
    }
    
    
    
    String flightTime;
    String[] parts = fTime.split(":",2);
    int hours = Integer.valueOf(parts[0]);
    int minutes = Integer.valueOf(parts[1]);
    {
    return (hours*60)+minutes;
    	
    }
    Please excuse my formatting, it is pretty bad.

    So it asks for me to provide instance variables, a constructor and methods to reserve seats.

    Also it wants additional instance variables for status of the flight, which must be set to either 'S' (scheduled), 'B'(boarding), ‘D’ (departed) or 'C'(cancelled).
    Do i create them as char?
    ie; private char status ;


    I think i've done the instance variables and constructors, can anyone confirm if it's correct or if im missing anything?


    Thanks in advance!
    Last edited by omgonoes; 04-23-2011 at 03:09 AM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    1.) Use code tags when posting code
    2.) Use enum for the status so you have type safety, with char you risk having invalid values.
    3.) Use naming conventions for your getters and setters. A field called flightNo should have its getter and setter named getFlightNo and setFlightNo resp.

  3. #3
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    What's enum?
    Sorry , I'm java illiterite ): still learning.

    Is this what you mean by the getter and setter:
    Java Code:
    public String getFlightNo()
    {
    	return flightNo;
    }
    
    public String setFlightNo(String flightNo)
    {
    	this.flightNo = flightNo;
    }
    Last edited by omgonoes; 04-21-2011 at 07:39 AM.

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    You shouldn't need this.flightNo. this. should automatically be added by the compiler.

    However, yes. That is what basic getters and setters look like.

    One last thing, if you're asking us if your code is correct try compiling it. If it works then your code is correct. However if you're compiling it, running it, and not getting your expected output then ask us a question about how to fix your output. Show us your output, your expected output, and any error that may occur.

    If you can't compile, make sure you post the error that occured when trying to compile it.

    If you have a different question, such as "Am I setting up my classes correctly?" then make sure its specific and not like you posted, "Is my code correct?"

    If you follow those tips, you will recieve more answers, faster, and with less mockery responses.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Okay thanks!

  6. #6
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Java Code:
    public class Flight {
    public static  void main(String args[]){
    	private String flightNo;
    	private String departurePoint;
    	private String destination;
    	private double flightLength;
    	private String flightDate;
    	private String flightTime;
    	private int capacity;
    	private int booked;
    	private char status ;
    
    public Flight (String fNo, String dPoint, String dst,
    double fLngth, String fDate, String fTime, int cpcty, int bk, char Stat){
    flightNo = fNo; 
    departurePoint = dPoint;
    destination = dst;
    flightLength = fLngth;
    flightDate = fDate;
    flightTime = fTime;
    capacity = cpcty;
    booked = bk;
    status = Stat;
    
    
    }	
    public char getStat()
    {
    	return status;
    }
    
    
    public String fTime()
    {
    	return flightTime;
    }
    public String getFlightNo()
    {
    	return flightNo;
    }
    
    public String setFlightNo(String flightNo)
    {
    	this.flightNo = flightNo;
    }	
    String fTime;
    String[] parts = fTime.split(":",2);
    int hours = Integer.valueOf(parts[0]);
    int minutes = Integer.valueOf(parts[1]);
    {
    return hours*60+minutes;
    }
    
    
    private double fLength;
    
    }
    
    }
    1.Have I set up my classes correctly?

    2. I'm also having a problem with the instance variables also,
    So far i've been told to produce like this:
    Java Code:
    private String destination;
    	private double flightLength;
    	private String flightDate;
    	private String flightTime;
    	private int capacity;
    But in eclipse it tells me to remove the private as it's an invalid modifier.
    Would it make a difference if i removed the private from each one?

    Sorry I'm really slow at learning. ):
    Thanks for your patience.
    Last edited by omgonoes; 04-21-2011 at 08:33 AM. Reason: Completely wrong

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Dark View Post
    You shouldn't need this.flightNo. this. should automatically be added by the compiler.
    this is required in this case otherwise the instance variable is hidden in the setter and never assigned a value. It's not required when there is no variable hiding (no name clashes).

    @omgonoes, you don't seem to have applied the advice given earlier.

  8. #8
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Oh wait, sorry misread, which bit of advice?
    I did the getter and setter and the code tags.
    Last edited by omgonoes; 04-21-2011 at 08:49 AM.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The flightNo was just an example, apply the same logic to all your fields.
    Then google "java enum" and read.

  10. #10
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Ohh. alright thanks
    Is there something wrong with my constructor?
    Its got lots of red lines in eclipse.
    Java Code:
    public Flight (String fNo, String dPoint, String dst,
    double fLngth, String fDate, String fTime, int cpcty, int bk, char Stat){
    flightNo = fNo; 
    departurePoint = dPoint;
    destination = dst;
    flightLength = fLngth;
    flightDate = fDate;
    flightTime = fTime;
    capacity = cpcty;
    booked = bk;
    status = Stat;
    Last edited by omgonoes; 04-21-2011 at 09:07 AM.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your structure is wrong because you are declaring the fields and constructor inside the main method which is not allowed and doesn't really make sense.
    Forget about writing the main method for now and just write your class with it's constructor(s) and accessors.

    P.S When eclipse gives you red lines, check the error console to find out what the error messages are. The error messages are usually very good clues as to what is wrong and how to fix it.

  12. #12
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi, sorry about this but I'm completely confused, I don't know whats what; so if i show you what i have can you label it ?

    1.
    Java Code:
    public class Flight {
    2.
    Java Code:
    public static  void main(String args[]){
    	String flightNo;
    	String departurePoint;
    	String destination;
    	 double flightLength;
    	 String flightDate;
    	 String flightTime;
    	 int capacity;
    	 int booked;
    	enum status ;
    3.
    Java Code:
     public Flight (String fNo, String dPoint, String dst,
    double fLngth, String fDate, String fTime, int cpcty, int bk, enum Stat){
    4.
    Java Code:
     flightNo = fNo; 
    departurePoint = dPoint;
    destination = dst;
    flightLength = fLngth;
    flightDate = fDate;
    flightTime = fTime;
    capacity = cpcty;
    booked = bk;
    status = Stat;
    So is 1. the class
    2. the instance variables
    3. the constructor
    4. Im not too sure

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

  14. #14
    omgonoes is offline Member
    Join Date
    Apr 2011
    Posts
    11
    Rep Power
    0

    Default

    Hi, have I set up all my class correctly? and are my braces correctly placed?
    Also when i attempt to compile it, it gives me a
    "java.lang.NoSuchMethodError: main", can anyone show me where to put the main method?

    Cheers.

    Java Code:
    import java.io.IOException;
    import java.io.PrintStream;
    enum flightStatus { S , B , D , C };
    public class Flight {
    	
    	
    	private String fNo;
    	private String dPoint;
    	private String dest;
    	private double fLength;
    	private String fDate;
    	private String fTime;
    	private int capa;
    	private int book;
    	private char fStatus ;
    	
    public Flight (String flightNo, String departurePoint, String destination,
    double flightLength, String flightDate, String flightTime, int capacity, int booked, char flightStatus){
    fNo = flightNo; 
    dPoint = departurePoint;
    dest = destination;
    fLength = flightLength;
    fDate = flightDate;
    fTime = flightTime;
    capa = capacity ;
    book = booked;
    fStatus = flightStatus;
    
    
    }
    	
    
    public String getFlightNo()
    {
    	return fNo ;
    }
    
    public String setFlightNo(String flightNo)
    {
    	return this.fNo = flightNo;
    }
    
    public double getFlightLength()
    {
    	return fLength;
    }
    
    public double setFlightLength(double flightLength)
    {
    	return this.fLength = flightLength;
    }
    
    public String getFlightDate()
    {
    	return fDate;
    }
    
    public String setFlightDate(String flightDate)
    {
    	return this.fDate = flightDate;
    }
    
    public String getFlightTime()
    {
    	return fTime;
    }
    
    public String setFlightTime(String flightTime)
    {
    	return this.fTime = flightTime;
    }
    
    public String getDeparturePoint()
    {
    	return dPoint;
    }
    
    public String setDeparturePoint(String departurePoint)
    {
    	return this.dPoint = departurePoint;
    }
    
    public String getDestination()
    {
    	return dest;
    }
    public String setDestination(String destination)
    {
    	return this.dest = destination;
    }
    public int getCapacity()
    {
    	return capa;
    }
    
    public int setCapacity(int capacity)
    {
    	return this.capa = capacity;
    }
    
    
    
    String flightTime;
    String[] parts = fTime.split(":",2);
    int hours = Integer.valueOf(parts[0]);
    int minutes = Integer.valueOf(parts[1]);
    {
    return (hours*60)+minutes;
    	
    }
    	}
    Last edited by omgonoes; 04-23-2011 at 03:16 AM.

Similar Threads

  1. Java assignment.
    By jmsjames in forum New To Java
    Replies: 14
    Last Post: 12-07-2010, 03:18 AM
  2. Help me please (Java assignment)
    By cris_carriaga in forum Java Applets
    Replies: 4
    Last Post: 10-06-2010, 04:11 PM
  3. My java assignment -- please help me !
    By java_beginner1 in forum New To Java
    Replies: 11
    Last Post: 05-20-2010, 04:00 PM
  4. java assignment, need help bad.
    By carlos123 in forum New To Java
    Replies: 1
    Last Post: 11-06-2007, 04:53 PM
  5. Help with my assignment java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •