Is the constructor missing something? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 17 days ago.
Improve this question
I keep getting compilation error for name constructor, what am I missing/doing wrong here :/
//I am new to coding in general, and as I have recently learnt java, I'm trying to create a name constructor that takes in the first name and last name from a .txt file, using scanner function, which will print the names onto the terminal when code is executed, but I'm facing some compilation error:
d.java:330: error: constructor Name in class Name cannot be applied to given types;
My code:
class Name
{
private String firstName, lastName;
//Default Constructor
public void Name()
{
//Do nothing
}
//Other Constructor
public void Name(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
//Copy Constructor
public Name(Name n)
{
this.firstName = n.firstName;
this.lastName = n.lastName;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public void setName(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
public String toString()
{
return "Name: " + firstName + ", " + lastName + "\n";
}
}
public static void main (String [ ] args) throws IOException
{
//instance variables
String firstName, lastName;
Name name;
//Construct a Scanner
Scanner input = new Scanner (new File ("input.txt"));
//Input variables from input.txt
firstName = input.nextLine();
lastName = input.nextLine();
//Construct the object
Name n1 = new Name (firstName, lastName);
Profile p1 = new Profile (n1, d1, h1, weight, year);
}
Name n1 = new Name (firstName, lastName);

Yes, you have an error in your constructor. The constructor methods should have a return type of "Name", not "void".
Change:
//Default Constructor
public void Name()
{
//Do nothing
}
//Other Constructor
public void Name(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
to:
//Default Constructor
public Name()
{
//Do nothing
}
//Other Constructor
public Name(String firstName, String lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}

Related

AWS Lambda Constructor Overloading Java POJOs

I created a Lambda based on this URL: https://docs.aws.amazon.com/lambda/latest/dg/java-handler-io-type-pojo.html. There are three main classes:
package example;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class HelloPojo implements RequestHandler<RequestClass, ResponseClass>{
public ResponseClass handleRequest(RequestClass request, Context context){
String greetingString = String.format("Hello %s, %s.", request.firstName, request.lastName);
return new ResponseClass(greetingString);
}
}
is the Lambda handler.
package example;
public class RequestClass {
String firstName;
String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public RequestClass(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public RequestClass() {
}
}
is the requestClass.
package example;
public class ResponseClass {
String greetings;
public String getGreetings() {
return greetings;
}
public void setGreetings(String greetings) {
this.greetings = greetings;
}
public ResponseClass(String greetings) {
this.greetings = greetings;
}
public ResponseClass() {
}
}
is the response class.
Input is something along the lines of:
{ "firstName": "John", "lastName": "Doe" }
That being said, I do have some questions regarding the RequestClass. Right now, it is dependent on there being a firstName and lastName provided as part of the input. However, let's say only a firstName is provided, and I want to have another constructor in the RequestClass with just a firstName parameter and initialize the lastName to a default lastName, something like
public RequestClass(String firstName) {
this.firstName = firstName;
this.lastName = "defaultLastName";
}
When I try doing something along these lines and access the variables in the handleRequest, I'm able to get the firstName but lastName is always null (guessing because I do not provide it as part of the input). Any reason as to why this happens and what I can do so that when accessing lastName in the handler class, I am able to get "defaultLastName" instead of null?
Please let me know if there are further clarifications I should add, I don't post to StackOverflow very often and want to make sure my question is appropriate!
you could set some default values in your default constructor.
public RequestClass() {
firstName = "defaultFirstName";
lastName = "defaultLastName;
}
And then call your parameterized constructor with this.
public RequestClass(String firstName) {
this();
this.firstName = firstName;
}
Readings for constructor overloading:
https://beginnersbook.com/2013/05/constructor-overloading/

Creating a method where two objects are considered equal if the first and last names are the same.

So within this class, I need to create a Equals method that will check to determine if the two objects have the same name. I tried creating the two objects within the class and just initialize it with "" for the constructor, but it gave an error on the created objects
Person.Java
public class Person
{
String firstName = "";
String lastName = "";
String age = "";
public Person (String firstName, String lastName, String age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getAge(){
return age;
}
public void setFirstName(String firstName){
this.firstName = firstName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public void setAge(String age){
this.age = age;
}
public String toString(){
return firstName + " " + lastName + ", " + age + " years old";
}
}
Here is my driver, so basically I need a method that sees both have the same name and prints out a message saying that they have the same name. My lab states it has to be in the class NOT the driver, which is why I'm lost considering I could easily make an if/else statement within the driver.
public class PersonDriver
{
public static void main(String[] args)
{
Person p1 = new Person("John","Doe", "42");
Person p2 = new Person("John","Doe", "43");
System.out.println(p1);
System.out.println(p2);
}
}

program calling default constructor? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Beginner here.
I cant get my head around why this code outputs the default halfway through. Can anyone take look?
sorry if the format is wrong, first time posting and will fix if not correct.
public class officemanager {
public static void main(String[] args) {
Staffmember aStaffMember = new Staffmember("Steven", "bob");
System.out.println(aStaffMember.toString());
Programmer appleprg = new Programmer("Marion", "bob", "Java");
appleprg.getLanguage();
System.out.println(appleprg.toString());
Doctor dr = new Doctor();
dr.setWard(5);
dr.setFirstName("ed");
dr.setLastName("fall");
System.out.println(dr.toString());
}
}
OUTPUT
Staffmember firstName=Steven, lastName=bob
Programmer firstName=Marion , lastName=bob language Java
default constructor
Doctor firstName=ed , lastName=fall Ward 5
Sorry guys here the class the default constructor is in. It is the Superclass called Staffmember and the firstname, lastname Strings are passed through it.
package oopinheritance;
public class Staffmember {
private String firstName;
private String lastName;
// default constructor
public Staffmember() {
System.out.println("default constructor");
}
// constructor
public Staffmember(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;}
public void setLastName(String lastName) {
this.lastName = lastName;}
public String toString() {
return "Staffmember firstName="
+firstName+ ", lastName=" + lastName;
}
}
Here is the Doctor class, it is a subclass of Staffmember and it has its own tostring method:
package oopinheritance;
public class Doctor extends Staffmember{
private int ward;
public int getWard() {
return ward;
}
public void setWard(int ward) {
this.ward = ward;
}
public String toString() {
return "Doctor firstName="
+this.getFirstName() + " , lastName=" + this.getLastName() + " \t
ward" + this.ward;
}
}
As you have not shown your whole program, so its hard to tell where is the error, but it might be in the default constructor of the doctor class.
Anyways here is the code that you can refer. It will give the correct output.
Here is the link you can refer to see the execution order
http://javabeginnerstutorial.com/learn-by-example-3/order-of-execution-of-blocks-in-java/
Java Constructors - Order of execution in an inheritance hierarchy
class GfG {
public static void main(String[] args) {
Staffmember aStaffMember = new Staffmember("Steven", "bob");
System.out.println(aStaffMember.toString());
Programmer appleprg = new Programmer("Marion", "bob", "Java");
appleprg.getLanguage();
System.out.println(appleprg.toString());
Doctor dr = new Doctor();
dr.setWard(5);
dr.setFirstName("ed");
dr.setLastName("fall");
System.out.println(dr.toString());
}
}
class Staffmember {
String firstName;
String lastname;
public Staffmember(String firstName, String lastname) {
super();
this.firstName = firstName;
this.lastname = lastname;
}
#Override
public String toString() {
return "Staff Member firstName=" + firstName + ", lastname=" + lastname;
}
}
class Programmer {
String firstName;
String lastName;
String Language;
public String getLanguage() {
return Language;
}
public void setLanguage(String language) {
Language = language;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Programmer(String firstName, String lastname, String Language) {
super();
this.firstName = firstName;
this.lastName = lastname;
this.Language = Language;
}
#Override
public String toString() {
return "Programmer firstName=" + firstName + ", lastName=" + lastName + ", Language=" + Language;
}
}
class Doctor {
int ward;
String firstName;
String lastName;
public void setWard(int ward) {
this.ward = ward;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastname) {
this.lastName = lastname;
}
public Doctor(int ward, String firstName, String lastName) {
super();
this.ward = ward;
this.firstName = firstName;
this.lastName = lastName;
}
#Override
public String toString() {
return "Doctor ward=" + ward + ", firstName=" + firstName + ", lastName=" + lastName;
}
}

Get and set to private variables in java

Good day,
I am new to JAVA'm learning this language and what I have learned it seems a fantastic language. My question is in relation to the following:
Suppose I have a class like this:
public class Person{
private String firstName;
private String lastName;
private int age;
private String entireName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEntireName() {
return entireName;
}
public void setEntireName(String entireName) {
this.entireName = entireName;
}
public static void Main(String args[]){
Person person = new Person();
person.setFirstName("Jhon");
person.setLastName("Adams");
person.setAge(20);
//Atention this line
person.setEntireName(person.getFirstName()+person.getLastName());
}
}
The language allows me to do this: person.setEntireName(person.getFirstName()+person.getLastName());
and it works fine however I would like to know how is best to do this, how it behaves at the object level and how high or low the performance.
Thank you ..
What you do is perfectly valid, but not very logical. Why not just drop the setEntireName() since it just combines two existing fields?
public String getEntireName() {
return firstName + " " + lastname;
}
This is valid. There is no performance difference, becasue JIT compiler optimize this code if needed (simply replace method with fields access).
Typically it is easier to eliminate the entireName property and its setter, and use the getter to perform the concatenation like so:
public String getEntireName() {
return firstName + " " + lastName;
}
This is also easier to maintain than updating entireName every time firstName or lastName is changed.

How to make constructor that takes in 2 if there are 2 parameters, or 3 if there are 3

New to Java...
I have a name class that has:
private String firstName;
private String middleInitial;
private String lastName;
as its instance variables.
If I had certain data that had only firstName and lastName, no middleInitial, how would I make the constructor so that it took only 2 parameters instead of three?
You simply write a constructor with two parameters and a constructor with three
public YourClass(String firstName, String lastName) {
...
}
public YourClass(String firstName, String middleInitial, String lastName) {
...
}
Callers can then choose to use the appropriate constructor based on their needs.
Well, two options:
Just have a constructor with three parameters, and call it using null or the empty string for middleInitial
Overload the constructors, possibly calling one from the other.
As an example for the latter, using an empty string as the default middle initial:
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public Person(String firstName, String lastName)
{
this(firstName, "", lastName);
}
However, the compiler will need to know which one you're calling from the call site. So you can do:
new Person("Jon", "L", "Skeet");
or
new Person("Jon", "Skeet");
... but you can't do:
// Invalid
new Person(firstName, gotMiddleInitial ? middleInitial : ???, lastName);
and expect the compiler to decide to use the "two name" variant instead.
In Java, constructors can't have default arguments. Your only option here is to write two constructors. Fortunately, Java does allow you to call constructors from other constructors. You could do something like:
public class MyClass {
private String firstName;
private String middleInitial;
private String lastName;
public MyClass(String firstName, String middleInitial, String lastName) {
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = lastName;
}
public MyClass(String firstName, String lastName) {
this(firstName, "", lastName);
}
...
}
public Class Name{
private String first;
private String middle;
private String last;
public Name(String first, String middle, String last){
this.first = first;
this.middle = middle;
this.last = last;
}
public Name(String first, String last){
this.first = first;
this.last = last;
}
}
You could use two constructors:
public Person(String firstName, String lastName)
{
this(firstName, null, lastName);
}
public Person(String firstName, String middleInitial, String lastName)
{
this.firstName = firstName;
this.middleInitial = middleInitial;
this.lastName = = lastName;
}
Define 2 constructors, one with 2 parameters and one with 3 parameters.
You can write two constructors.
public Person( String firstName, String lastName, String middleName ) { ... }
public Person( String firstName, String lastName ) { ... }
public void myBike(String name, String color)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
}
public void myBike(String name, String color, float height)
{
System.out.println("My bike's name is " + name + " and is " + color + ".");
System.out.println("My bike is also " + height + " inches tall.");
}
Builder pattern...
class Name {
Builder builder;
public String getSurname() {
return builder.surname;
}
// getter...
public Name(Builder builder) {
this.builder = builder;
}
class Builder {
String surname = "";
String middleName = "";
String name = "";
Builder surname(String surname) {
this.surname = surname;
return this;
}
Builder middleName(String middleName) {
this.middleName = middleName;
return this;
}
Builder name(String name) {
this.name = name;
return this;
}
Name build() {
return new Name(this);
}
}
}

Categories

Resources