Click here to Skip to main content
16,012,759 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a class person each person has a name age address and job.

The class also has a constructor as well as the following methods:
Display() to display the person data
set Address() and getAddress() to set and get the persons address

write java program to create a person his data was entered by the user then display his data


The code:
Java
import java.util.Scanner;
public class person
{
    String name,address,job;
    byte age;
    person(String name,byte age,String address,String job)
    {
        this.name=name;
        this.age=age;
        this.address=address;
        this.job=job;
    }
    void Display()
    {
        System.out.println("name="+name);
        System.out.println("age="+age);
        System.out.println("address="+address);
        System.out.println("job="+job);
    }
    void setAddress()
    {
        String add=address;
    }
    String getAddress()
    {
        return address;
    }

    public static void main(String[] args)
    {
        Scanner input=new Scanner (System.in);
        person p = new person();
        System.out.print("name is");
        String s=input.next();
        System.out.print("age is");
        byte x=input.nextByte();
        System.out.print("address is");
        String y=input.next();
        System.out.print("job is");
        String z=input.next();
        p.Display();
    }
}
Posted
Updated 23-Jul-12 22:35pm
v2
Comments
Richard MacCutchan 24-Jul-12 4:24am    
First thing you need to do is add proper indentation to your code so it is readable. The second thing you need to do is tell us the exact text of the error, and where it occurs.
OriginalGriff 24-Jul-12 4:25am    
And the error is?
Member 8697827 24-Jul-12 5:24am    
The error occurs at this line
person p = new person();
Richard MacCutchan 24-Jul-12 7:43am    
You really need to take OriginalGriff's advice here.
Sandeep Mewara 24-Jul-12 7:57am    
What error?

There are a number of problems here, most of which are related to the java compiler not being psychic! :laugh:

You need to go back to your lecture notes, and read them again, because this shows that you really haven't understood anything except teh basic syntax of java.

For example, look at your setAddress and getAddress routines: the latter returns a value, but the former does not accept a value to set at all. So you have created a temporary value into which you load the current address instead, presumably because you know you have to do something there, but don't know what.

I'm sorry, but I am not going to fix "the problem" because it would entail writing the whole of your homework, and giving you the code here will teach you nothing! There are too many fundamental concepts that you appear to have completely not grasped yet.
Go back to you notes, read them again, and see what you can work out - if you get a specific problem we will be happy to help.
 
Share this answer
 
Comments
OriginalGriff 24-Jul-12 5:42am    
You haven't fixed the error - you haven't started to fix the error!
All you have done is throw some code together without thinking about what it is supposed to do, or how it is supposed to do it. Sorry to be blunt, but there is no nice way to say this! :)
For example: What exactly is the "error" you have? Because I can see maybe a dozen...:laugh:
You have created a parameterized constructor. You need to pass the parameters to it while creating the object

This line of code in the main()
C#
person(String name,byte age,String address,String job)


will change to

C#
person p =new person("aspnet_regiis",21,"Pune India","Software Developer")
 
Share this answer
 
Comments
Member 8697827 24-Jul-12 8:40am    
thanks alot
Java
import java.util.Scanner;
public class Person
{
    String name,address,job;
    byte age;
    Person(){}
    Person(String name,byte age,String address,String job)
    {
        this.name=name;
        this.age=age;
        this.address=address;
        this.job=job;
    }
    void Display()
    {
        System.out.println("name   = "+name);
        System.out.println("age    = "+age);
        System.out.println("address= "+address);
        System.out.println("job    = "+job);
    }
    void setAddress(String address)
    {
        String add=address;
    }
    String getAddress(String address)
    {
        return address;
    }
 
    public static void main(String[] args)
    {
        Scanner input=new Scanner (System.in);
        Person p = new Person();
        
        System.out.print("name is ");
        String s=input.next();
        System.out.print("age is ");
        byte x=input.nextByte();
        System.out.print("address is ");
        String y=input.next();
        System.out.print("job is ");
        String z=input.next();
        Person P = new Person(s,x,y,z);
        P.Display();
}
}



Its Very simple Well if u define any type of Constructor with arguments in a class then u have to define also a default Constructor with no arguments .java not provide a default Constructor in this your case .Java Can provide default Constructor when no Constructor in the class.
 
Share this answer
 
v2
Comments
Member 8697827 24-Jul-12 8:40am    
Thank you very much for your help
I do it and it works
Member 8697827 24-Jul-12 8:46am    
can you please explain the setAddress() and getAddress()
TorstenH. 25-Jul-12 3:13am    
the getAddress() is simply false! Use your version.

also the method Display() - a method is never starting with capital letter. NEVER!
#Code:
public void display()
{
System.out.println("name = "+getName());
System.out.println("age = "+getAge());
System.out.println("address= "+getAddress());
System.out.println("job = "+getJob());
}

# end CODE

by calling p.getAddress() in your main you will receive the String that was set in that variable - probably the address. You might need to add the rest of the getters.
You can trigger that function by calling p.display() in your main.
Member 8697827 25-Jul-12 3:59am    
I have fixed the code
import java.util.Scanner;
public class person {
String name,address,job;
byte age;
person(String name,byte age,String address,String job)
{this.name=name;
this.age=age;
this.address=address;
this.job=job;
}
public void display()
{System.out.println("name= "+name);
System.out.println("age= "+age);
System.out.println("address= "+getAddress());
System.out.println("job= "+job);
}
void setAddress()
{String add=address;
}
String getAddress()
{return address;
}

public static void main(String[] args) {
Scanner input=new Scanner (System.in);
System.out.print("name is");
String s=input.next();
System.out.print("age is");
Byte x=input.nextByte();
System.out.print("address is");
s=input.next();
System.out.print("job is");
s=input.next();
person p = new person(s,x,s,s);
p.display();

}
}
can you say your comment
TorstenH. 25-Jul-12 7:09am    
false - your setAddress method should look like this:

void setAddress(String aAddress) {
address = aAddress;
}

The rest looks quite good. Why is age a Byte value? make it int.
Do you use Eclipse? right click -> source -> format
That's the "make it pretty" function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900