Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8619873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:26:43+00:00 2026-06-12T06:26:43+00:00

OK, I need to create three constructors as part of a project, one default,

  • 0

OK, I need to create three constructors as part of a project, one default, one general and one copy. I’ve managed to create a default constructor, but I can’t create either the general or copy constructors because otherwise my code won’t compile. Here is the code if anybody knows the answer:

package lab02;

import javax.swing.JOptionPane;

/**
 * Stores the personal details of a friend.
 * 
 * @author Keith Francis(11109971)
 * @date 4-10-2012
 */
public class Friend {

    private String firstName;// stores first name
    private String surname;// stores surname
    private String address;// stores address
    private int age;// stores age in years
    private int height;// stores height in cms
    private String hairColourString;// stores hiar colour as a string

    private boolean colourTrue = false;// hair colour value is not valid

    public static final int BLACK = 0;
    public static final int BROWN = 1;
    public static final int BLONDE = 2;
    public static final int RED = 3;
    public static final int GREY = 4;

    /**
     * Default constructor sets everything to 0 or null, depending on type.
     */
    public Friend() {

        firstName = null;
        surname = null;
        address = null;
        age = 0;
        height = 0;
        hairColourString = null;
    }

    /**
     * Allows the first name to be edited
     * 
     * @param first
     *            first name variable
     */
    public void setFirstName(String first) {
        firstName = first;
    }

    /**
     * Retrieves first name
     * 
     * @return first name to String
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * Allows the surname to be edited
     * 
     * @param last
     *            creates last name variable
     */
    public void setSurname(String last) {
        surname = last;
    }

    /**
     * Retrieves the surname
     * 
     * @return last name to string
     */
    public String getSurname() {
        return surname;
    }

    /**
     * Allows the address to be edited
     * 
     * @param place
     *            where the friend lives
     */
    public void setAddress(String place) {
        address = place;
    }

    /**
     * Retrieves the address
     * 
     * @return the address of the friend
     */
    public String getAddress() {
        return address;
    }

    /**
     * Allows the age (in years) to be edited
     * 
     * @param years
     *            the age in years
     */
    public void setAge(int years) {
        age = years;
    }

    /**
     * Retrieves the age in years
     * 
     * @return the age in years
     */
    public int getAge() {
        return age;
    }

    /**
     * Allows the height in centimetres to be edited
     * 
     * @param h
     *            height in centimetres
     */
    public void setHeight(int h) {
        height = h;
    }

    /**
     * Retrieves the height in centimetres
     * 
     * @return height in centimetres
     */
    public int getHeight() {
        return height;
    }

    /**
     * 
     * @return String of the personal details of the friend
     */
    @Override
    public String toString() {
        return ("First name is: " + firstName + "\nSurname is: " + surname
                + "\nAddress is: " + address + "\nAge is :" + age
                + "\nHeight is: " + height + "\nHair colour is: " + hairColourString);
    }

    /**
     * Uses JOptionPanel to edit the friend's personal details
     */
    void inputFriend()
    {
        //welcome message
        JOptionPane.showMessageDialog(null,"Weclome",null,JOptionPane.PLAIN_MESSAGE);
        //prompt to enter first name
        String name1 = JOptionPane.showInputDialog("Enter the friend's first name.");
        //calls setFirstName method
        setFirstName(name1);
        //prompt user to enter second name
        String name2 = JOptionPane.showInputDialog("Enter the friend's surname.");
        setSurname(name2);// calls setSurname method

        //prompt user to enter address
        String thisAddress = JOptionPane.showInputDialog("Enter the friend's address.");
        setAddress(thisAddress);//calls setAddress method
        //prompt user to enter age in years
        String ageString = JOptionPane.showInputDialog("Enter the friend's age in years.");
        int i = Integer.parseInt(ageString);
        setAge(i);
        //prompt user to enter height in centimetres
        String heightString = JOptionPane.showInputDialog("Enter the friend's height in cenimetres.");
        int j = Integer.parseInt(heightString);
        setHeight(j);
        //prompt user to enter hair colour
        String hairColourInput = JOptionPane.showInputDialog("Select the friend's " +
                "hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey");
        while(colourTrue != true)//if hair colour is valid
        {
        if(
            hairColourInput.equals("0"))
        { hairColourString = "Black";//hair is black
            colourTrue = true;}//entry is valid
        else if (hairColourInput.equals("1"))
        { hairColourString = "Brown";//hair is brown
            colourTrue = true;}//entry is valid
        else if (hairColourInput.equals("2"))
        { hairColourString = "Blonde";//hair is blonde
            colourTrue = true;}//entry is valid
        else if (hairColourInput.equals("3"))
        { hairColourString = "Red";//hair is red
            colourTrue = true;}//entry is valid 
        else if (hairColourInput.equals("4"))
        { hairColourString = "Grey";//hair is grey
            colourTrue = true;}//entry is valid
            else {
                JOptionPane.showMessageDialog(null,
                        "The number entered is invalid.", "Error",
                        JOptionPane.WARNING_MESSAGE);// warns user that entry is
                                                        // not valid
                hairColourInput = JOptionPane
                        .showInputDialog("Select the friend's " +
                        "hair colour:\n 0 = Black\n1 = Brown\n2 = Blonde\n3 = Red\n4 = Grey");
            }// user is asked to choose again until they enter a valid number

        }
    }

    /**
     * 
     * @param args
     *            Calls inputFriend method and prints out the final String using
     *            JOptionPane
     */
    public static void main(String[] args) {
        Friend friend = new Friend();
        friend.inputFriend();// calls inputFriend method
        JOptionPane.showMessageDialog(null, friend.toString()); // prints out details
    }
}

Here is my attempt at a copy constructor:

public Friend(Friend aFriend) {
    this(aFriend.getFirstName(), aFriend.getSurname(), aFriend.getAddress, aFriend.getAge, aFriend.getHeight);

and my attempt at the general constructor:

public Friend2(){
    public static final int BLACK = 0;
    public static final int BROWN = 1;
    public static final int BLONDE = 2;
    public static final int RED = 3;
    public static final int GREY = 4;
}

What came up was that a class, interface or enum was expected when I inserted the constructor. Hope that helps.

Right, I’ve tried the copy constructor like this:

public Friend(Friend f) {
this(f.getFirstName(),f.getSurname(),f.getAddress(),f.getAge(),f.getHeight());
}

But I am getting a message saying that I don’t have a suitable constructor.

UPDATE:general and copy constructors are now working. Thanks for your help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:26:44+00:00Added an answer on June 12, 2026 at 6:26 am

    you can overload the constructor like below:

    cons1:

    public Friend()
    {
    
    }
    

    cons2:

        public Friend(int arg)
    {
    
    }
    

    cons3:

        public Friend(String s)
    {
    
    }
    

    copy cons:

        public Friend(Friend f)
    {
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my constructor, I want to create a random color. Therefore, I need three
I need to create a method to generate a unit vector in three dimensions
I have the following three arrays and need to create a new two-dimensional array
I am trying to create chained constructors but well, im no good at it
I am supposed to: create two constructors. a. query for student's names and three
Most layout managers have no-argument constructors (that is, you can create a FlowLayout with
I've created a three step order form and I need help with the third
I need to create a product catalog, in tree type. every tree node presents
We need to create a basic PDF reader running on J2ME. While there are
I need to know if there is a way to create a custom shortcut

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.