Question

I have an past exam question that says: "Create a class Element that records the name of the element as a String and has a public method, toString that returns the String name. Define a constructor for the class (that should receive a String to initialise the name)."

I gave it a go and don't where to go from here...

main class is:

public class builder { 
public static void main(String[] args) { 
    element builderObject = new element(elementName);

}
}

and constructor is:

    import java.util.*;
class element {
public int getInt(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the first number");
    String elementName = scan.nextLine();
    System.out.println("%s");
}

public String toString() {
    return elementName;
}
}
Was it helpful?

Solution

Don't get frustrated. Please read java tutorials first and understand the concepts. your exam question is very clear on what you need to do. Atleast for this question, you need to know what is constructor, the purpose of having toString() in a class.

May be the below can help you.

public class Element {

private String elementName;

public Element(String elementName) {
    this.elementName = elementName;
}

@Override
public String toString() {
    return elementName;
}

}

OTHER TIPS

I can't think of a way to explain this without actually giving the answer, so....

public class Element { /// Create class Element

    private final String name; // Record the 'name'

    public Element(String name) { // constructor receives and sets the name
        this.name = name;
    }

    public String toString() { // public method toString() returns the name
        return name;
    }
}

You are missing the constructor itself. The point of constructors is to initialize the object, usually by saving the given parameters to data members. E.g.:

class Element {
    /** A data member to save the Element's name */
    private String elementName;

    /** A constructor from an Element's name*/
    public Element(String elementName) {
        this.elementName = elementName;
    }

    @Override
    public String toString() {
        return elementName;
    }
}
class Element {
    private String name = "";

    /**
    /* Constructor
    /**/
    public void Element(final String name){
        this.name = name;
    }

    @Override
    public String toString(){
        return name;
    }
}

You don't have a constructor in there. A constructor typically looks something like this:

public class MyClass {
     private String name;
     private int age;         

     //This here is the constructor:
     public MyClass(String name, int age) {
         this.name = name;
         this.age = age;
     }

     //here's a toString method just for demonstration
     @Override
     public String toString() {
         return "Hello, my name is " + name + " and I am " + age + " years old!";
     }
}

You should be able to use that as a guideline for making your own constructor.

class Element 
{
  private String name = "UNSET";
  public String getName() { return name; }

  public Element(String name) {
    this.name = name;
  }

  public String toString() { 
   return getName(); 
  }
}

You are missing a constructor you might be looking for something like this

public class Element{
   private String name;

   public Element(String name){ //Constructor is a method, having same name as class
       this.name = name;
   }

   public String toString(){
       return name;
    }
}

A note
I take you are starting with java, In java class names usually start with capital letter, thus element should be Element. Its important that one picks up good habits early..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top