CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
arun et
Level 11
Chennai
  • 21.05.2019
  • 1079views
  • 3comments

I Have no idea what is wrong?

Question about the task You can't buy friends
Java Syntax,  Level 5,  Lesson 9
Under discussion


Create a Friend class with three constructors:
- Name
- Name, age
- Name, age, sex

Requirements:
  • The Friend class must have a String variable name.
  • The Friend class must have an int variable age.
  • The Friend class must have a char variable sex.
  • The class must have a constructor that takes a name as an argument and initializes the corresponding instance variable.
  • The class must have a constructor that takes a name and age as arguments, and initializes the corresponding instance variables.
  • The class must have a constructor that takes a name, age, and sex as arguments, and initializes the corresponding instance variables.
package com.codegym.task.task05.task0516; /* You can't buy friends */ public class Friend { String name; int age; char sex; void name(String name) { this.name = name; System.out.println(name); } void name(String name, int age) { this.name = name; this.age = age; System.out.println(name + "," + age); } void name(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'); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Raivo Lapiņš
Level 18 , Riga, Latvia
21 May 2019, 19:48
I think both replies are incorrect. Constructor has no type at all. It should be
public Friend(){
     initialization here
}
. Also I don't see in specification that you should print the fields in the console output.
+2
Denise
Level 14 , Delft, Netherlands
21 May 2019, 08:52
The constructor should have the same name as the class, so void Friend(String name) instead of void name(String name)
0
ThomasLC
Level 16 , Singapore, Singapore
21 May 2019, 08:52
A constructor has to have the same name as its class. In this case all your constructors must be named "void Friend ()" instead of "void name()"
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.