简体   繁体   中英

Method to take in values for parameterized constructor

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);
}

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

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.

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;
  }
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM