Java program help needed ! public class Person { public static void main(String[
ID: 3755301 • Letter: J
Question
Java program help needed !
public class Person {
public static void main(String[] args) {
// 1. Add some attributes to the Person class that store appropriate data about a person.
// 2. Add some methods that allow the attribute values to be set.
// 3. Add another method called displayDetails() that outputs all the personal details stored about a person.
// 4. Within this main() method, create a new instance of the Person class (called 'p1')
// 5. Call each of the available set methods on the 'p1' instance.
// 6. Call the displayDetails() method to confirm the set methods have worked correctly.
// Extra. Create a Scanner object, and prompt the user for each detail of a person. Use the input data to setup a person object then display the details.
}
}
Explanation / Answer
import java.util.Scanner;
public class Person
{
String firstname;
String middlename;
String lastname;
String address;
String phoneno;
public void setvalue(String fname, String mname,String lname,String add,String pno)
{
firstname=fname;
middlename=mname;
lastname=lname;
address=add;
phoneno=pno;
}
public void displayDetails()
{
System.out.println("firstname"+ " "+ firstname);
System.out.println("middlename"+" "+ middlename);
System.out.println("lastname"+" "+ lastname);
System.out.println("address"+ " "+address);
System.out.println("phoneno"+ " "+phoneno);
}
public static void main(String args[])
{
Scanner scan=new Scanner(System.in);
String f,m,l,a,p;
f=scan.nextLine();
m=scan.nextLine();
l=scan.nextLine();
a=scan.nextLine();
p=scan.nextLine();
Person p1=new Person();
p1.setvalue(f,m,l,a,p);
p1.displayDetails();
}
}
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.