0

I'm running into a problem here. I was told to initialize values through a parameterized constructor. Write a method "getInput" to take in values for these attributes, call the parameterized constructor and then return a Child object.

I think this is what I did...but when I call the method i get something like this: Child@75d9fd51 Is this just the object reference? I'm trying to get it to print the inputted age, name and grade. Thank you in advance!

So far this is what i have:

import java.util.Scanner;

class Child{
    int age;
    String name;
    char grade;

public Child(){

}

public Child(int age, String name, char grade) {
    this.age = age;
    this.name = name;
    this.grade = grade;
}
public int getAge() {
    return age;
}
public void setAgel(int age) {
    this.age = age;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public char getGrade() {
    return grade;
}
public void setGrade(char grade) {
    this.grade = grade;
}
}

public class Data {

public static Child getInput(Child s){
    Scanner input = new Scanner(System.in);
    System.out.println("Input age: ");
    int age = input.nextInt();
    System.out.println("Input name: ");
    String name = input.next();
    System.out.println("Input grade: ");
    char grade = input.next().charAt(0);
    s = new Child(age, name, grade);

    return s;
}

public static void main(String[] args) {
    // TODO Auto-generated method stub


    Child x = new Child(20, "James", 'A');   //I want this to be changed with input

    getInput(x);

    System.out.print(x);
}
user3806226
  • 225
  • 1
  • 6
  • 14

4 Answers4

2

Override your toString methode in your child class

public String toString(){
        return age + " " + name + " " + grade;
    }

when your run this line in your agent class which is Data

public static void main(String...args){
   Child x = new Child(20, "James", 'A');   
   System.out.print(x.toString());
  }

output:

Input age: 
32
Input name: 
izak
Input grade: 
A
32 izak A
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
2

No, 'Child@75d9fd51' is not a object reference. It's the data stored in the instance x of the class Child. In the Java programming language, every class provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer).

If you want to get the input data and print the inputted data(age, name and grade), you should make some changes:

First, change getInput(x); to x = getInput(x);. This statement assigns new class to the class x.

Second, put the method

public String toString() { 
   return "Input age: " + getAge() + "\nInput name: " + getName() + "\nInput grade: " +        getGrade(); 
}

inside child class. When you call System.out.print(x); on main method, System.out.print(x.toString()); is actually called.

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
eseol
  • 46
  • 2
1

A generic Object doesn't have a specific printable representation just because Java can't just guess how you'd like to print it. So what you obtain is the reference in memory.

But the language gives you the public String toString() method from Object class, which can be overridden to provide the specific representation you'd like. This method is automatically called when you pass an object to the println method.

Eg:

class Child {
  public String toString() { 
       return name + "," + grade + "," + age;
  }
}
JulianR
  • 16,213
  • 5
  • 55
  • 85
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Oh! That's so useful! So now all I have to do to get it to implement my new inputs is use my setters to take in the inputs? – user3806226 Jul 07 '14 at 00:32
1

When passed an object, System.out.println() will print out the results of the object's toString method. The default output is in the format you're seeing. If you want to see something more informative, you can either pass the fields individually

System.out.println("Age: " + x.getAge());
System.out.println("Name: " + x.getName());
System.out.println("Grade: " + x.getGrade());

or you can override the toString method on Child:

@Override
public String toString() {
    return "Age: " + this.age + ", Name: " + this.name + ", Grade: " + this.grade;
}

Now you can call System.out.println(x) as you were before.

On a side note, there's no point in creating and passing an instance of Child just to replace it with a new instance in getInput.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • Thank you! Out of curiosity why is there no point in doing that? Also I was trying to use the method it to implement my new inputs. So if I set those inputs to my setters it might work? – user3806226 Jul 07 '14 at 00:44
  • @user3806226 I'm not sure why you would think there's a point. You're creating an instance of a class and ignoring it. – shmosel Jul 07 '14 at 00:53
  • @user3806226 I'm not sure I fully understand your question, but if you want to add fields to your `Child` class, they would also have to be specified in your output. – shmosel Jul 07 '14 at 00:55
  • Sorry, I'm a beginner so I'm not really sure about everything yet. I figured out my problem, though, sorry for the confusion! Thanks for the help! – user3806226 Jul 07 '14 at 02:19