-2

My program is a simple program that involves the use of objects. There are no errors the only problem is that my program is printing out junk. After it asked the user for it name, age , and gender.

Down below are two sets of programs. The first one is the object or the skeleton of the person. The second one is the print that asks for the user name age gender and prints it out.

public class Person 
{
    private String name;
    private int age,personality,appearance;
    private String gender;

    //constructor method. only use it once
    public Person(String nm, int ag,String gend) {
        name=nm;
        age=ag;
        gend=gender;
        personality=1+(int)(Math.random()*10);
        appearance=1+(int)(Math.random()*10);
    }

    //accessor created
    public String getName() {
        return name;
    }

    public String getGend() {
        return gender;
    }     

    public int getInt() {
        return age;
    }


    //mutator method. When using "void" NO RETURN TYPE
    public void setName (String nm) {
        name=nm;
    }

    public void setAge (int ag) {
        age=ag;
    }

    public void setGender (String gend)
    {
        gender=gend;
    }

    //helper method (kind of like print but not really printing
    public String toString () {
        String orange ="";
        orange ="Name "+name+"/n";
        orange +="age"+age+"/n";
        orange +="Gender: "+gender"/n";
        orange +="Personality "+personality+"/n";
        orange +="Apperance "+appearance+"/n";

        return orange;
    }
}

2)

import java .util.Scanner;
public class PersonTester {
    public static void main (String []args){

    // calling person 
    Person person;
    String name="", gender ="";
    int age =0;

    Scanner input =new Scanner(System.in);
    System.out.println ("What is your name");
    name =input.nextLine();

    System.out.println("What your age?");
    age=input.nextInt();

    input.nextLine();
    System.out.println ("What is your gender");
    gender =input.nextLine();

    person=new Person (name,age,gender);

    System.out.println(person);
}

We are learning bout basic objects for example we only learned about private variables,constructor, accessor, mutator, and helper methods.

Raman Shrivastava
  • 2,923
  • 15
  • 26

2 Answers2

2

In your toString() you have two errors. You need to use a + between gender"/n" and you need to use \n if you want a newline.

public String toString () {
    return "Name " + name + "\n" +
           "Age" + age + "\n" +
           "Gender: " + gender + "\n" +
           "Personality " + personality + "\n" +
           "Appearance " + appearance + "\n";
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

If the problem is the gender is not printed out properly, the problem is in your constructor. You are passing in gend, but not saving it. Instead you overwrite the argument with the gender member variable:

public Person(String nm, int ag,String gend)
{
    name=nm;
    age=ag;
    gend=gender;

You wanted:

    gender = gend;
AJNeufeld
  • 8,526
  • 1
  • 25
  • 44