package com.codegym.task.task05.task0516;
/*
You can't buy friends
*/
public class Friend {
String name;
int age;
char sex;
public friend(String name)
{
this.name = name;
System.out.println(name);
}
public friend(String name, int age)
{
this.name = name;
this.age = age;
System.out.println(name + "," + age);
}
public friend(String name, int age, char sex)
{
this.name = name;
this.age = age;
this.sex = sex;
System.out.println(name + "," + age + "," + sex);
}//write your code here
public static void main(String[] args) {
Friend frnd1 = new Friend();
Friend frnd2 = new Friend();
Friend frnd3 = new Friend();
frnd1.name("Arun");
frnd2.name("Aaron",23);
frnd3.name("Arrun",24,'M');
}
}
What is wrong with my code
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Abdul-Hamed Mohammed
19 February 2020, 07:48
1: All 3 constructors should be "public Friend()" not "public friend()" Constructor's names should match the class name.
2: Remove all the System.out..... from the constructor. There are no requirements for that.
3: Remove the creation of objects in the main.
If this help please mark the question as resolved.
0
Gabe
5 June 2019, 06:02
Hello Bryce ;
Try that way:
Friend frnd1 = new Friend("Arun")
It should work.
Good Luck!!
0
hidden #10419982
23 May 2019, 13:18
can you please post all the requirements need for this task to be accomplished? This will be easier for me to find out what the task is supposed to do and which requirements are not met.
In oder to do this: Go to Help ?
click on a new question
ask the question and publish the whole thing
Requirements:
1. The Friend class must have a String variable name.
2. The Friend class must have an int variable age.
3. The Friend class must have a char variable sex.
4. The class must have a constructor that takes a name as an argument and initializes the corresponding instance variable.
5. The class must have a constructor that takes a name and age as arguments, and initializes the corresponding instance variables.
6. The class must have a constructor that takes a name, age, and sex as arguments, and initializes the corresponding instance variables.
First of all there is no requirement to print the instances variables from the constructor. So remove all the System.out..... from the constructor
2. No requirement to create objects, you can just do it for your testing, but by verifying the task just comment them out or remove them.
3. you could call the already defined constructor in other contructor
- for example in the constructor you can use this(name) which will initialize the variable name
public friend(String name, int age)
{
this(name) // call the constructor with name to initialize the variable name
this.age = age;
}
public friend(String name, int age, char sex)
{
// this(name, age) call the constructor with name and age to initialize the variables name and age
this(name, age)
this.sex = sex;
}
Good luck!!
0