Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trouble using Constructors and string to string() to print variables to GUI

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble using Constructors and string to string() to print variables to GUI

    Hey guys, I'm not new to programming but I am new to Java. Here is the layout of my program:

    Introduction
    The goal of this assignment is to familiarize you with writing a simple Java Graphical User Interface (GUI) application in Java to reinforce the object-oriented programming concepts discussed during lectures.

    I'm having trouble implementing constructors and string to string to print to the GUI.
    Any suggestions?

    Procedures

    1. *.java - Use an editor or Integrated Development Environment (IDE) to create the .java source code file.
    2. Create a UI application to perform the following functions:
    a. Create a class called Airline.java
    i. Variables
    1. airlineName
    2. airlineHubLocation
    ii. Methods
    1. public getter/setter for all variables
    2. public string toString() to print out the airline information
    b. Create a class called Airport.java
    i. Variables
    1. airportName
    2. airportCity
    3. airportState
    4. airportCode
    ii. Methods
    1. public getter/setter for all variables
    2. constructor that takes four parameters for airportName, airportCity, airportState, airportCode
    3. public string toString() to print out the airport information
    c. Create a main class to run the application
    i. Includes the “public static void main(String[] args) {}” function
    ii. Creates an instance of Airport using the declared constructor; example Airport airport = new Airport(“Orlando International Airport”, “Orlando”, “Florida”, “MCO”);
    iii. Create two instances of Airline; example Airline united = new Airline(); Airline delta = new Airline();
    iv. Set the data attributes of each airline as follows:
    1. united.setAirlineName(“United Airlines”);
    2. united.setAirlineHubLocation(“MCO”);
    3. delta.setAirlineName(“Delta Airlines”);
    4. delta.setAirlineHubLocation(“ATL”);
    v. Display the information about Orlando International Airport and its two airlines using the static method JOptionPane.showMessageDialog(null, string);
    1. Using concatenation for the string parameter display
    a. airport.toString() + “\n” + delta.toString() + “\n” + united.toString()
    2. Output example


    --- Update ---

    Airline.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package airline;
    import javax.swing.JOptionPane;
    /*
     *
     * 
     */
    public class Airline {
        String airlineName;
        String airlineHubLocation;
    //Methods
        public String getAirlineName() {
            return airlineName;
        }
     
        public String getAirlineHublocation() {
            return airlineHubLocation;
        }
     
        public void setAirlineName(String airlineName) {
            this.airlineName = airlineName;
        }
     
        public void setAirlineHubLocation(String airlineHublocation) {
            this.airlineHubLocation = airlineHublocation;
        }
     
        @Override
        public String toString() {
            return "Airline{" + "airlineName=" + airlineName + ", airlineHubLocation=" + airlineHubLocation + '}';
        }
     
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
     
        Airport airport = new Airport(); 
     
     
     
        Airline united = new Airline();
        united.setAirlineName("United Airlines");
        united.setAirlineHubLocation("MCO");
     
        Airline delta = new Airline();
        delta.setAirlineName("Delta Airlines");
        delta.setAirlineHubLocation("ATL");
     
     
        JOptionPane.showMessageDialog(null, "airport.toString() + delta.toString() + united.toString()");
     
     
        }
     
    }

    Airport.java
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package airline;
     
    /**
     *
     * 
     */
     
     
    public class Airport {
        String airportName;
        String airportCity;
        String airportState;
        int airportCode;
     
     
     
        public String getAirportName() {
            return airportName;
        }
     
        public String getAirportCity() {
            return airportCity;
        }
     
        public String getAirportState() {
            return airportState;
        }
     
        public int getAirportCode() {
            return airportCode;
        }
     
        public void setAirportName(String airportName) {
            this.airportName = airportName;
        }
     
        public void setAirportCity(String airportCity) {
            this.airportCity = airportCity;
        }
     
        public void setAirportState(String airportState) {
            this.airportState = airportState;
        }
     
        public void setAirportCode(int airportCode) {
            this.airportCode = airportCode;
        }
     
     
     
     
     
     
     
     
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default Re: Trouble using Constructors and string to string() to print variables to GUI

    JOptionPane.showMessageDialog(null, "airport.toString() + delta.toString() + united.toString()");
    This will only display airport.toString() + delta.toString() + united.toString()
    You want
    JOptionPane.showMessageDialog(null, airport.toString() + delta.toString() + united.toString());
    plus the delimiters as described in the assignment.
    You also forgot to override toString() in Airport.

    In future, please also post the output you get and the output you expect. This way we don't have to wade through code to find oput what might be wrong.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,085
    My Mood
    Mellow
    Thanks
    184
    Thanked 653 Times in 640 Posts

    Default Re: Trouble using Constructors and string to string() to print variables to GUI

    Welcome to the forum, and thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to review other useful info for newcomers.

Similar Threads

  1. Error with String range print -1.
    By JavaA123Chris in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2013, 07:36 PM
  2. [SOLVED] Sorting an object array using string variables from a string array
    By Shadud in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 26th, 2012, 04:50 PM
  3. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  4. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  5. How to define a string pattern using variables?
    By ice in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2011, 09:45 PM